declare upper;
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 = 5;
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);
# AddVerticalLine(min_blocks < min_blocks[1], "5m", Color.BLACK);
def upper = average + ( 4 * StandardDeviation(average, length = 5) );
def change = if close - close[1] > 0 then 1 else -1;
def v = (average * change) + v[1];
# plot velocity = v;
def c5 = if min_blocks[0] < min_blocks[1] then close()[-2] else c5[1];
def o5 = if min_blocks[0] < min_blocks[1] then open()[0] else o5[1];
def c = c5;
def o = o5;
#close5m.EnableApproximation();
#open5m.EnableApproximation();
# AddCloud(o, c, Color.RED, Color.GREEN);
#plot MA5m = Average(c, 100);
#MA5m.EnableApproximation();
#o.SetPaintingStrategy(paintingStrategy.HORIZONTAL);
#close5m.SetPaintingStrategy(paintingStrategy.HORIZONTAL);
def barStart = if min_blocks[0] < min_blocks[1] then 1 else 0;
def barEnd = if min_blocks[-1] < min_blocks[0] then 1 else 0;
#Find the middle bar in the lower timeframe
# def midBar = if modTime == halfAgg then 1 else 0;
#If start or end of bar - used to draw side border of candle body
def openbar = if barStart or barEnd then 1 else 0;
def na = double.nan;
#------------------------------------
# ADD CLOUDS
#------------------------------------
input addcloud = yes;
def side;
if min_blocks < min_blocks[1] then{
if side[1] == 1 then{
side = 2;
}
else{
side = 1;
}
}else{
side = side[1];
}
DEF sider = side;
def doji = C==O;
AddCloud( if (addcloud and sider == 2 and !doji) then o else Double.NaN, if (addcloud and sider == 2 and !doji) then C else Double.NaN, Color.LIGHT_RED, Color.LIGHT_GREEN, yes);
AddCloud( if (addcloud and sider == 1 and !doji) then O else Double.NaN, if (addcloud and sider == 1 and !doji) then C else Double.NaN, Color.LIGHT_RED, Color.LIGHT_GREEN, yes);
AddCloud( if (addcloud and sider == 1 and doji) then O else Double.NaN, if (addcloud and sider == 1 and doji) then C else Double.NaN, Color.WHITE, Color.WHITE, yes);
AddCloud( if (addcloud and sider == 2 and doji) then O else Double.NaN, if (addcloud and sider == 2 and doji) then C else Double.NaN, Color.WHITE, Color.WHITE, yes);
#plot barType = midBar;
#plot barType = Sync;
#barType.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
#barType.AssignValueColor(color.WHITE);
### making stuff up here drawing the wick through the middle candle.
def nan = double.nan;
def up = c > o;
def ubh = nan;
def ubc = nan;
def ubo = nan;
def ubl = nan;
def dbh = nan;
def dbc = nan;
def dbo = nan;
def dbl = nan;
def djbh = nan;
def djbc = nan;
def djbo = nan;
def djbl = nan;
def sideuh = if up and openbar then C else nan;
def sideul = if up and openbar then O else nan;
def sidedh = if !up and !doji and openbar then O else nan;
def sidedl = if !up and !doji and openbar then C else nan;
def sidedjh = if doji and openbar then O else nan;
def sidedjl = if doji and openbar then C else nan;
#Draw Upper Wick Green
# AddChart(high = ubh, low = ubc, open = na, close = na, type = ChartType.bar , growcolor = if 1 then color.LIGHT_GREEN else color.light_green);
#Draw Upper Wick Red
# AddChart(high = dbh, low = dbo, open = na, close = na, type = ChartType.bar, growcolor = color.LIGHT_RED);
#Draw Upper Wick White - Doji
# AddChart(high = djbh, low = djbo, open = na, close = na, type = ChartType.bar, growcolor = color.WHITE);
#Draw Lower Wick Green
# AddChart(high = ubo, low = ubl, open = na, close = na, type = ChartType.bar, growcolor = color.LIGHT_GREEN);
#Draw Lower Wick Red
# AddChart(high = dbc, low = dbl, open = na, close = na, type = ChartType.bar, growcolor = color.LIGHT_RED);
#Draw Lower Wick White - Doji
# AddChart(high = djbc, low = djbl, open = na, close = na, type = ChartType.bar, growcolor = color.WHITE);
#Draw Side of Body Green
AddChart(high = sideuh, low = sideul, open = na, close = na, type = ChartType.bar, growcolor = color.LIGHT_GREEN);
#Draw Side of Body Red
AddChart(high = sidedh, low = sidedl, open = na, close = na, type = ChartType.bar, growcolor = color.LIGHT_RED);
#Draw Side of Body White
AddChart(high = sidedjh, low = sidedjl, open = na, close = na, type = ChartType.bar, growcolor = color.WHITE);
plot highs = ubh;
highs.setpaintingStrategy(PaintingStrategy.POINTS);
highs.setDefaultColor(Color.Green);
plot lhighs = dbh;
lhighs.setpaintingStrategy(paintingstrategy.points);
lhighs.setdefaultColor(Color.Red);
plot lows = ubl;
lows.setpaintingStrategy(PaintingStrategy.Points);
lows.setdefaultColor(Color.Green);
plot llows = dbl;
llows.setpaintingStrategy(PaintingStrategy.POINTS);
llows.SetDefaultColor(Color.Red);
# end