# test_highestbn_05e
# @halcyonguy's code
# https://usethinkscript.com/threads/plot-only-the-high-from-the-last-65-most-recent-days.7483/
# find the highest high, in the most rect x bars
# Sleepyz HighestH_LowestL_using_fold_for_range_of_bars
# modified to find HH/LL betweeen bars look_back_bar max/min; added vericals and labels
input verticals = yes;
input labels = yes;
def na = Double.NaN;
def bn = BarNumber();
def lastbar = HighestAll(if !IsNaN(close) then bn else na);
input look_back_bars_max = 78;
input look_back_bars_min = 26;
# is the current bar within the lookback quantity of bars? true/false
def rng = (lastbar - bn <= look_back_bars_max) and (lastbar - bn >= look_back_bars_min);
def hi = fold i = 0 to look_back_bars_max
with p
do if !rng then 0 else if GetValue(high, (bn - (lastbar - i))) > p then GetValue(high, (bn - (lastbar - i))) else p;
plot hix = HighestAll(hi);
hix.SetDefaultColor(Color.YELLOW);
hix.SetStyle(Curve.MEDIUM_DASH);
AddLabel(labels, "HIghestHigh: " + hix, hix.TakeValueColor());
# test code -------------------------
# identify the first bar in the lookback group
AddVerticalLine(verticals and bn == HighestAll(lastbar - look_back_bars_max ), look_back_bars_max , Color.WHITE);
AddVerticalLine(verticals and bn == HighestAll(lastbar - look_back_bars_min), look_back_bars_min, Color.WHITE);
def firstbar = (!IsNaN(close[-look_back_bars_max]) and IsNaN(close[-(look_back_bars_max + 1)]));
plot f = firstbar;
f.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
f.SetDefaultColor(Color.YELLOW);
f.SetLineWeight(4);
AddVerticalLine(firstbar, "xx" , Color.CYAN );
input show_barnumber_bubbles = no;
AddChartBubble(show_barnumber_bubbles, low * 0.995, bn + "\n" + "x", Color.CYAN, no);
#addchartbubble(1, high * 1.01, hi, color.cyan, yes);
#
#--------------------------------
# lowest from prev x bars
def lolo = LowestAll(if rng then low else na);
plot lox = lolo;
lox.SetDefaultColor(Color.MAGENTA);
lox.SetStyle(Curve.MEDIUM_DASH);
AddLabel(labels, "LowestLow: " + lox, lox.TakeValueColor());
#