#fourth_hilo
#https://usethinkscript.com/threads/how-t-o-find-4th-highest-close-in-last-100-bars.19755/
#How to find 4th highest close in last 100 bars?
#rewardiaz
input length = 100;
input hi = close;
input lo = close;
def big = 99999;
def na = double.nan;
def bn = barnumber();
def lastbn = HighestAll(If(IsNaN(close), 0, bn));
def lastbar = if (bn == lastbn) then 1 else 0;
# first bar in period , x bars before last bar
def first = (!IsNaN(close[-(length-1)]) and IsNaN(close[-length]));
addverticalline(first,"==",color.cyan);
def prhi1;
def prhi2;
def prhi3;
def prhi4;
def prlo1;
def prlo2;
def prlo3;
def prlo4;
if bn == 1 or isnan(close) then {
prhi1 = na;
prhi2 = na;
prhi3 = na;
prhi4 = na;
prlo1 = na;
prlo2 = na;
prlo3 = na;
prlo4 = na;
} else if first then {
prhi1 = fold i1 = 0 to length
with t1
do Max(getvalue(hi,-i1), t1);
prhi2 = fold i2 = 0 to length
with t2
do if prhi1 > getvalue(hi,-i2) then Max(getvalue(hi,-i2), t2) else t2;
prhi3 = fold i3 = 0 to length
with t3
do if prhi2 > getvalue(hi,-i3) then Max(getvalue(hi,-i3), t3) else t3;
prhi4 = fold i4 = 0 to length
with t4
do if prhi3 > getvalue(hi,-i4) then Max(getvalue(hi,-i4), t4) else t4;
#-------------------------------
prlo1 = fold j1 = 0 to length
with u1 = big
do Min(getvalue(lo,-j1), u1);
prlo2 = fold j2 = 0 to length
with u2 = big
do if prlo1 < getvalue(lo,-j2) then Min(getvalue(lo,-j2), u2) else u2;
prlo3 = fold j3 = 0 to length
with u3 = big
do if prlo2 < getvalue(lo,-j3) then Min(getvalue(lo,-j3), u3) else u3;
prlo4 = fold j4 = 0 to length
with u4 = big
do if prlo3 < getvalue(lo,-j4) then Min(getvalue(lo,-j4), u4) else u4;
} else {
prhi1 = prhi1[1];
prhi2 = prhi2[1];
prhi3 = prhi3[1];
prhi4 = prhi4[1];
prlo1 = prlo1[1];
prlo2 = prlo2[1];
prlo3 = prlo3[1];
prlo4 = prlo4[1];
}
#-------------------------------
input show_price_levels = yes;
plot zh1 = if show_price_levels then prhi1 else na;
plot zh2 = if show_price_levels then prhi2 else na;
plot zh3 = if show_price_levels then prhi3 else na;
plot zh4 = if show_price_levels then prhi4 else na;
plot zl1 = if show_price_levels then prlo1 else na;
plot zl2 = if show_price_levels then prlo2 else na;
plot zl3 = if show_price_levels then prlo3 else na;
plot zl4 = if show_price_levels then prlo4 else na;
input show_hilo_dots = yes;
plot zhdot = if show_hilo_dots and (prhi1 == hi or prhi2 == hi or prhi3 == hi or prhi4 == hi) then hi else na;
zhdot.SetPaintingStrategy(PaintingStrategy.POINTS);
zhdot.SetDefaultColor(Color.cyan);
zhdot.setlineweight(3);
zhdot.hidebubble();
plot zldot = if show_hilo_dots and (prlo1 == lo or prlo2 == lo or prlo3 == lo or prlo4 == lo) then lo else na;
zldot.SetPaintingStrategy(PaintingStrategy.POINTS);
zldot.SetDefaultColor(Color.cyan);
zldot.setlineweight(3);
zldot.hidebubble();
#AddLabel(yes, "len: " + length, Color.GREEN);
AddLabel(yes, "4th Highest price: " + prhi4, Color.GREEN);
#AddLabel(yes, "len: " + length, Color.GREEN);
AddLabel(yes, "4th Lowest price: " + prlo4, Color.magenta);
#-------------------------------
input test1 = no;
addchartbubble(test1, low*0.998,
prhi1 + " h1\n" +
prhi2 + " h2\n" +
prhi3 + " h3\n" +
prhi4 + " h4\n" +
prlo4 + " l4\n" +
prlo3 + " l3\n" +
prlo2 + " l2\n" +
prlo1 + " l1\n"
, color.yellow, no);
#