declare lower;
declare zerobase;
def trade_time_s = SecondsFromTime(0000);
def delta_t_s = trade_time_s - trade_time_s[1];
def delta_t_m = delta_t_s / 60;
input minutes = 10;
def trades = tick_count();
# def trades = Volume;
def trades_per_minute = trades / delta_t_m;
def average = SimpleMovingAvg(trades_per_minute, 5);
# def five_min = if (trade_time_s % 300) == 0 then 1 else 0;
def min_blocks = trade_time_s % (60 * minutes);
def time_boundary = if min_blocks[1] > min_blocks then 1 else 0;
# addVerticalLine(time_boundary == 1, text="5m", color.black);
def time_open = if time_boundary == 1 then open else time_open[1];
def time_volume = if time_boundary == 1 then VOLUME else time_volume[1] + VOLUME;
# plot o = time_open;
# o.SetPaintingStrategy(paintingStrategy.HORIZONTAL);
def candle_width = if min_blocks[0] > min_blocks[-1] then 1
else if min_blocks[-1] > min_blocks[-2] then 2
else if min_blocks[-2] > min_blocks[-3] then 3
else if min_blocks[-3] > min_blocks[-4] then 4
else if min_blocks[-4] > min_blocks[-5] then 5
else if min_blocks[-5] > min_blocks[-6] then 6
else if min_blocks[-6] > min_blocks[-7] then 7
else if min_blocks[-7] > min_blocks[-8] then 8
else if min_blocks[-8] > min_blocks[-9] then 9
else if min_blocks[-9] > min_blocks[-10] then 10
else if min_blocks[-10] > min_blocks[-11] then 11
else if min_blocks[-11] > min_blocks[-12] then 12
else if min_blocks[-12] > min_blocks[-13] then 13
else if min_blocks[-13] > min_blocks[-14] then 14
else if min_blocks[-14] > min_blocks[-15] then 15
else if min_blocks[-15] > min_blocks[-16] then 16
else if min_blocks[-16] > min_blocks[-17] then 17
else if min_blocks[-17] > min_blocks[-18] then 18
else if min_blocks[-18] > min_blocks[-19] then 19
else if min_blocks[-19] > min_blocks[-20] then 20
else if min_blocks[-20] > min_blocks[-21] then 21
else if min_blocks[-21] > min_blocks[-22] then 22
else if min_blocks[-22] > min_blocks[-23] then 23
else if min_blocks[-23] > min_blocks[-24] then 24
else if min_blocks[-24] > min_blocks[-25] then 25
else if min_blocks[-25] > min_blocks[-26] then 26
else if min_blocks[-26] > min_blocks[-27] then 27
else if min_blocks[-27] > min_blocks[-28] then 28
else if min_blocks[-28] > min_blocks[-29] then 29
else if min_blocks[-29] > min_blocks[-30] then 30
else 41;
def final_volume = getValue(time_volume, -(candle_width) + 1);
plot v = if time_boundary[0] != 1 then final_volume else double.nan;
v.SetPaintingStrategy(paintingStrategy.HORIZONTAL);
AddCloud(0, v, color.gray, color.gray);