# days5_hilo_vert
def na = Double.NaN;
def bn = BarNumber();
def lastbar = (!isnan(close) and isnan(close[-1]));
def lastbar2 = (!isnan(close[1]) and isnan(close[0]));
input within_percent = 2;
input days = 5;
def diffday = if getday() != getday()[1] then 1 else 0;
def agg = AggregationPeriod.day;
def daycls = close(period = agg);
# find a day , x days before the last bar
def xdaysbefore = if ( !isnan(daycls[-(days-1)]) and isnan(daycls[-days])) then 1 else 0;
def dayxbar1 = if (diffday and xdaysbefore) then 1 else 0;
# this works , outside of if-then
def hiz = highest( high(period = agg)[-(days-1)], days);
def loz = lowest( low(period = agg)[-(days-1)], days);
#addchartbubble(1, high*1.02, hiz, color.yellow,yes);
def hi;
def lo;
if bn == 1 or lastbar2 then {
hi = na;
lo = na;
} else if dayxbar1 then {
hi = hiz;
lo = loz;
# these don't work in if-then
#def hiz = highest( high(period = agg)[-(days-1)], days);
#def loz = lowest( low(period = agg)[-(days-1)], days);
} else {
hi = hi[1];
lo = lo[1];
}
input show_horz_limit_lines = yes;
plot z1 = if show_horz_limit_lines then hi else na;
plot z2 = if show_horz_limit_lines then lo else na;
z1.setdefaultcolor(color.gray);
z2.setdefaultcolor(color.gray);
# percent levels , % of the range , highest - lowest
def rng = hi-lo;
def hi_per = round(hi - (rng * (within_percent/100)) ,2);
def lo_per = round(lo + (rng * (within_percent/100)) ,2);
input show_horz_percent_lines = yes;
plot z3 = if show_horz_limit_lines then hi_per else na;
plot z4 = if show_horz_limit_lines then lo_per else na;
z3.setdefaultcolor(color.gray);
z4.setdefaultcolor(color.gray);
# check if price crosses a percent level
def hix = if (high crosses hi_per) then 1 else 0;
def lox = if (low crosses lo_per) then 1 else 0;
addverticalline(hix, "CROSS HI", color.red, Curve.SHORT_DASH);
addverticalline(lox, "CROSS LO", color.green, Curve.SHORT_DASH);
# -----------------------------------------
input test5_hi = yes;
addchartbubble(test5_hi and (high == hi), high*1.001, hi, color.green,yes);
input test6_lo = yes;
addchartbubble(test6_lo and (low == lo), low*0.999, lo, color.red,no);
# -----------------------------------------
input test1 = no;
addchartbubble(test1 and xdaysbefore, low, "xx", color.magenta,no);
input test2 = no;
addchartbubble(test2 and diffday, high, diffday, color.magenta,yes);
input test3_dayxbar1 = no;
#addchartbubble(test3 and diffday and xdaysbefore, low*0.99, "X", color.cyan,no);
addchartbubble(test3_dayxbar1 and dayxbar1, low*0.99, "X", color.cyan,no);
# show the highest value
input test4 = no;
addchartbubble(test4 , high, hi + "\n" + lo, color.magenta,yes);
#