#clouds_10bar_frames_peak
#https://usethinkscript.com/threads/2-consecutive-10-bar-frames-rectangles.21032/
#2 consecutive 10-bar frames (rectangles)
#-------------------------
def na = Double.NaN;
def bn = BarNumber();
#def lastBar = HighestAll(if IsNaN(close) then 0 else bn);
def lastbn = HighestAll(if IsNaN(close) then 0 else bn);
def lastbar = bn == lastbn;
#def lastbar = (!isnan(close) and isnan(close[-1]));
def big = 99999;
#https://usethinkscript.com/threads/zigzag-high-low-with-supply-demand-zones-for-thinkorswim.172/#post-7048
# Robert Payne
# define peaks / valleys
def highx = high;
def lowx = low;
input peak_length = 7;
def offset = Min(peak_length - 1, lastbn - bn);
def peak = highx > Highest(highx[1], peak_length - 1) and highx == GetValue(Highest(highx, peak_length), -offset);
def valley = lowx < Lowest(lowx[1], peak_length - 1) and lowx == GetValue(Lowest(lowx, peak_length), -offset);
input show_peak_arrows = yes;
plot zhi = if show_peak_arrows and peak then high * 1.001 else na;
plot zlo = if show_peak_arrows and valley then low * 0.999 else na;
zlo.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
zlo.SetDefaultColor(Color.RED);
zlo.SetLineWeight(1);
zlo.HideBubble();
zhi.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
zhi.SetDefaultColor(Color.GREEN);
zhi.SetLineWeight(1);
zhi.HideBubble();
def peakbn = if peak then bn else 0;
def valleybn = if valley then bn else 0;
def peaklastbn = highestall(peakbn);
#def valleylastbn = highestall(valleybn);
input rectangle_bars = 10;
#def rect1_first = (rectangle_bars + bn - 1) == peaklast;
#def rect1_firstbn = if bn == 1 then 0
# else if (rectangle_bars + bn - 1) == peaklastbn then bn
# else rect1_firstbn[1];
# rect before peak
def rect1_firstbn = peaklastbn - rectangle_bars + 1;
def rect1_lastbn = peaklastbn;
# rect after peak
def rect2_firstbn = rect1_lastbn + 1;
def rect2_lastbn = rect2_firstbn + rectangle_bars - 1;
def rect1 = (bn >= rect1_firstbn and bn <= rect1_lastbn);
def rect2 = (bn >= rect2_firstbn and bn <= rect2_lastbn);
addchartbubble(0, low*0.97,
bn + " BN\n" +
rect1_firstbn + "-" + rect1_lastbn + "\n" +
rect2_firstbn + "-" + rect2_lastbn + "\n"
, color.yellow, no);
#addverticalline(rect1_firstbn == bn, "-");
#--------------------
# rect1
def rect1_hi;
def rect1_lo;
if rect1_firstbn == bn then {
rect1_hi = GetValue(Highest(high, rectangle_bars), -(rectangle_bars-1));
rect1_lo = GetValue(Lowest(low, rectangle_bars), -(rectangle_bars-1));
} else if rect1 then {
rect1_hi = rect1_hi[1];
rect1_lo = rect1_lo[1];
} else {
rect1_hi = 0;
rect1_lo = 0;
}
plot z1 = if rect1_hi > 0 then rect1_hi else na;
plot z2 = if rect1_lo > 0 then rect1_lo else na;
z1.SetDefaultColor(Color.cyan);
z1.setlineweight(1);
z1.hidebubble();
z2.SetDefaultColor(Color.cyan);
z2.setlineweight(1);
z2.hidebubble();
def r1top = if rect1_hi > 0 then rect1_hi else na;
def r1bot = if rect1_lo > 0 then rect1_lo else na;
addcloud(r1top,r1bot,color.gray);
#--------------------
# rect2
def rect2_hi;
def rect2_lo;
if rect2_firstbn == bn then {
# doesnt work if peak to last bar is < rectangle_bars
# rect2_hi = GetValue(Highest(high, rectangle_bars), -(rectangle_bars-1));
# rect2_lo = GetValue(Lowest(low, rectangle_bars), -(rectangle_bars-1));
rect2_hi = fold a = 0 to (rectangle_bars-1)
with b
while !isnan(getvalue(close,-a))
do max(b, getvalue(high, -a));
rect2_lo = fold c = 0 to (rectangle_bars-1)
with d = big
while !isnan(getvalue(close,-c))
do min(d, getvalue(low,-c));
} else if rect2 then {
rect2_hi = rect2_hi[1];
rect2_lo = rect2_lo[1];
} else {
rect2_hi = 0;
rect2_lo = 0;
}
plot z3 = if rect2_hi > 0 then rect2_hi else na;
plot z4 = if rect2_lo > 0 then rect2_lo else na;
z3.SetDefaultColor(Color.magenta);
z3.setlineweight(1);
z3.hidebubble();
z4.SetDefaultColor(Color.magenta);
z4.setlineweight(1);
z4.hidebubble();
def r2top = if rect2_hi > 0 then rect2_hi else na;
def r2bot = if rect2_lo > 0 then rect2_lo else na;
addcloud(r2top,r2bot,color.gray);
#