# lolo_then_hihi_0
#https://usethinkscript.com/threads/intraday-lower-low-followed-by-a-higher-high-indicator.12563/
# display when an intraday lower low, followed by a higher high is created
#---------------------------------
# robert_peaks_updated_01
#https://usethinkscript.com/threads/zigzag-high-low-with-supply-demand-zones-for-thinkorswim.172/#post-7048
# define swing low points
#------------------------
def bn = BarNumber();
def na = double.nan;
input length = 7;
def lastbn = HighestAll(if IsNaN(close) then 0 else bn);
def offset = Min(length - 1, lastbn - bn);
def peak = high > highest(high[1], length - 1) and high == GetValue(highest(high, length), -offset);
def valley = low < Lowest(low[1], length - 1) and low == GetValue(Lowest(low, length), -offset);
# determine if peaks are higher or lower
def peak_bn;
def peak_pr;
def peak_dir;
if bn == 1 then {
peak_bn = 0;
peak_pr = 0;
peak_dir = 0;
} else if peak then {
peak_bn = bn;
peak_pr = high;
peak_dir = if (peak_pr < peak_pr[1]) then -1 else if (peak_pr > peak_pr[1]) then 1 else 0;
} else {
peak_bn = peak_bn[1];
peak_pr = peak_pr[1];
peak_dir = peak_dir[1];
}
# determine if valleys are higher or lower
def valley_bn;
def valley_pr;
def valley_dir;
if bn == 1 then {
valley_bn = 0;
valley_pr = 0;
valley_dir = 0;
} else if valley then {
valley_bn = bn;
valley_pr = low;
valley_dir = if (valley_pr < valley_pr[1]) then -1 else if (valley_pr > valley_pr[1]) then 1 else 0;
} else {
valley_bn = valley_bn[1];
valley_pr = valley_pr[1];
valley_dir = valley_dir[1];
}
# is this a valley and a lower valley ?
def vlo = (valley and valley_dir < 0);
# if a lolo, look ahead for a hihi
def next_off;
def hihibn;
def hihipr;
def lolobn;
def lolopr;
if bn == 1 or peak[1] then {
# reset vars after a peak
next_off = 0;
hihibn = 0;
hihipr = 0;
lolobn = 0;
lolopr = 0;
} else if vlo then {
# find next peak
next_off = fold i = 0 to (lastbn - bn + 1)
with p
while !isnan(getvalue(close, -i)) and !getvalue(peak, -i)
do p + 1;
# read next peak, is it a hihi ? then get bn of it
hihibn = if getvalue(peak_dir, -next_off) > 0 then (bn + next_off) else 0;
# if peak is a hihi then get high of peak
hihipr = if hihibn > 0 then getvalue(high, -next_off) else 0;
# if next peak is a hihi , then get bn of current low
lolobn = if hihibn > 0 then bn else 0;
# if next peak is a hihi , then get current low
lolopr = if hihibn > 0 then low else 0;
} else {
hihibn = hihibn[1];
next_off = next_off[1];
hihipr = hihipr[1];
lolobn = lolobn[1];
lolopr = lolopr[1];
}
#----------------
# up arrow on hihi peak
plot hihipeak = if bn == hihibn then low*0.998 else na;
hihipeak.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
hihipeak.SetDefaultColor(Color.cyan);
hihipeak.setlineweight(3);
hihipeak.hidebubble();
plot hitop = if hihibn > 0 then hihipr else na;
plot hibot = if hihibn > 0 then lolopr else na;
hitop.SetDefaultColor(Color.light_gray);
hibot.SetDefaultColor(Color.light_gray);
input show_cloud = yes;
def ztop = if show_cloud then hitop else na;
addcloud( ztop, hibot, color.light_gray);
#-------------------------------------
# draw dots above peaks and below valleys
# colored , green if a higher, red if a lower
input peak_valley_dots = yes;
def vert = 0.002;
plot z1 = if peak_valley_dots and peak then high*(1+vert) else na;
z1.SetPaintingStrategy(PaintingStrategy.POINTS);
#z1.AssignValueColor(if peak_dir < 0 then color.blue else if peak_dir > 0 then color.cyan else color.gray);
z1.AssignValueColor(if peak_dir < 0 then color.red else if peak_dir > 0 then color.green else color.gray);
#z1.SetDefaultColor(Color.magenta);
z1.setlineweight(3);
z1.hidebubble();
plot z2 = if peak_valley_dots and valley then low*(1-vert) else na;
z2.SetPaintingStrategy(PaintingStrategy.POINTS);
#z2.AssignValueColor(if valley_dir < 0 then color.red else if valley_dir > 0 then color.yellow else color.gray);
z2.AssignValueColor(if valley_dir < 0 then color.red else if valley_dir > 0 then color.green else color.gray);
z2.SetDefaultColor(Color.magenta);
z2.setlineweight(3);
z2.hidebubble();
#---------------------------
# test stuff
input test1_bn = no;
addchartbubble(test1_bn and vlo, low*0.99,
bn + "\n" +
next_off + "\n" +
hihibn + "\n" +
lolobn
, color.yellow, no);
# x.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
# x.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
#