#second_highest_nbars_lower
#https://usethinkscript.com/threads/second-highest-high.20594/
#Second highest high
#DisciplinedTrader 2/23
#Can anyone help me with a script for,
# scanning for stocks whose,
# current 4-hr bar's high,
# is the second highest high,
# over 5 bars?
declare lower;
def na = double.nan;
def bn = barnumber();
input n = 5;
addlabel(1, "range of bars " + n, color.yellow);
# find highest high in past n bars
def hi1 = highest(high,n);
# find second highest high in past n bars
def hi2 = fold a = 0 to n
with b
do (if getvalue(high,a)<hi1 then max(b,getvalue(high,a)) else b);
# draw a small arrow on bars that are the 2nd highest in past n bars
plot zh1 = high == hi2;
#---------------------
input test2_bubbles = no;
addchartbubble(test2_bubbles, 1,
high + "\n" +
bn + "\n" +
hi1 + "\n" +
hi2 + "\n"
, color.yellow, yes);
#