input Lookback = 2;
input agg = AggregationPeriod.FIFTEEN_MIN;
input StartTime = 0930;
input EndTime = 0945;
def startCounter5 = SecondsFromTime(StartTime);
def endCounter5 = SecondsTillTime(EndTime);
def range = if SecondsFromTime(StartTime) >= 0 and SecondsTillTime(EndTime) > 0 then 1 else 0;
def hper = high(period = agg);
def lper = low(period = agg);
def hrange = if range and !range[1]
then hper
else if range
then if hper > hrange[1]
then hper
else hrange[1]
else hrange[1];
def lrange = if range and !range[1]
then lper
else if range
then if lper < lrange[1]
then lper
else lrange[1]
else lrange[1];
def ct = if hrange[1] != hrange and lrange[1] != lrange then ct[1] + 1 else ct[1];
def cct = HighestAll(ct) - ct + 1;
def xH = if cct == Lookback then hrange else if cct <= Lookback - 1 and SecondsFromTime(1600) < 0 then xH[1] else Double.NaN;
plot H = if cct > Lookback + 1 then Double.NaN else xH;
H.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
def xL = if cct == Lookback then lrange else if cct <= Lookback - 1 and SecondsFromTime(1600) < 0 then xL[1] else Double.NaN;
plot L = xL;
L.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
plot M = (H + L) / 2;
M.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
input cloud = yes;
AddCloud(H, M, Color.GREEN, Color.GREEN);
AddCloud(M, L, Color.RED, Color.RED);