#hi_lo_diff_cls_2
#https://usethinkscript.com/threads/script-that-calculated-distance-from-previous-high-low-of-past-5-candles.19219/
#Script that Calculated distance from previous High/Low of past 5 candles
def na = double.nan;
def bn = barnumber();
#def lastbn = HighestAll(If(IsNaN(close), 0, bn));
#def lastbar = if (bn == lastbn) then 1 else 0;
def lastbar = !isnan(close[0]) and isnan(close[-1]);
def afterlastbar = !isnan(close[1]) and isnan(close[0]);
#def last_close = highestall(if lastbar then close else 0);
input show_lines_on_last_bar = no;
def lastlines = (show_lines_on_last_bar and afterlastbar);
input past_bars = 5;
def hix = Highest(high[1], past_bars);
def lox = lowest(low[1], past_bars);
def rngx = hix - lox;
def above_cls = round(hix - close,2);
def below_cls = round(close - lox,2);
plot z1 = if lastlines then hix[1] else na;
z1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z1.SetDefaultColor(Color.cyan);
z1.setlineweight(2);
z1.hidebubble();
plot z2 = if lastlines then lox[1] else na;
z2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z2.SetDefaultColor(Color.cyan);
z2.setlineweight(2);
z2.hidebubble();
# close
plot z3 = if lastlines then close[1] else na;
z3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z3.SetDefaultColor(Color.yellow);
z3.setlineweight(2);
z3.hidebubble();
addchartbubble(lastlines,hix[1],
above_cls[1]
, color.yellow, yes);
addchartbubble(lastlines,lox[1],
below_cls[1]
, color.yellow, no);
input diff_on_each_bar = yes;
plot z4 = if diff_on_each_bar then above_cls else na;
z4.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
z4.SetDefaultColor(Color.white);
z4.setlineweight(2);
z4.hidebubble();
plot z5 = if diff_on_each_bar then below_cls else na;
z5.SetPaintingStrategy(PaintingStrategy.VALUES_below);
z5.SetDefaultColor(Color.white);
z5.setlineweight(2);
z5.hidebubble();
#