#zigzag_multi_trend_lines_0
#https://usethinkscript.com/threads/drawing-zig-zag-lines-but-based-on-highs-lows-not-percentage.16290/
#multiple diag lines from a low, or a high
up case,
if high > previous high and the low > previous low
draw a line from the low of the previous candle to the high of the new candle
def na = double.nan;
def bn = barnumber();
input n = 100;
def up = high[0] > high[1] and low[0] > low[1];
def dwn = high[0] < high[1] and low[0] < low[1];
def firstup = up and !up[1];
def firstdwn = dwn and !dwn[1];
# find qty of up/down bars in a row
def upqty;
def dwnqty;
def revcnt;
def cnt;
if bn == 1 then {
upqty = 0;
dwnqty = 0;
revcnt = 0;
cnt = -1;
} else if !up and !dwn then {
#if not up or dwn then nothing
upqty = 0;
dwnqty = 0;
revcnt = 0;
cnt = -1;
} else if firstup then {
# on 1st up, loop to find qty of higher bars
upqty = fold k1 = 1 to n
with p1
while getvalue(up, -k1)
do p1 + 1;
dwnqty = 0;
revcnt = upqty;
cnt = 0;
} else if firstdwn then {
# on 1st dwn, loop to find qty of lower bars
upqty = 0;
dwnqty = fold k2 = 1 to n
with p2
while getvalue(dwn, -k2)
do p2 + 1;
revcnt = dwnqty;
cnt = 0;
} else {
upqty = upqty[1];
dwnqty = dwnqty[1];
revcnt = revcnt[1] - 1;
cnt = cnt[1] + 1;
}
#calc slopes
input min_qty = 2;
def s1;
def s2;
def s3;
def s4;
def s5;
def s6;
def s7;
def s8;
def s9;
def s10;
if bn == 1 then {
s1 = 0;
s2 = 0;
s3 = 0;
s4 = 0;
s5 = 0;
s6 = 0;
s7 = 0;
s8 = 0;
s9 = 0;
s10 = 0;
} else if firstup then {
s1 = if upqty >= min_qty and upqty >= 1 then (getvalue(high, -1) - low) / 1 else 0;
s2 = if upqty >= min_qty and upqty >= 2 then (GetValue(high, -2) - low) / 2 else 0;
s3 = if upqty >= min_qty and upqty >= 3 then (GetValue(high, -3) - low) / 3 else 0;
s4 = if upqty >= min_qty and upqty >= 4 then (GetValue(high, -4) - low) / 4 else 0;
s5 = if upqty >= min_qty and upqty >= 5 then (GetValue(high, -5) - low) / 5 else 0;
s6 = if upqty >= min_qty and upqty >= 6 then (GetValue(high, -6) - low) / 6 else 0;
s7 = if upqty >= min_qty and upqty >= 7 then (GetValue(high, -7) - low) / 7 else 0;
s8 = if upqty >= min_qty and upqty >= 8 then (GetValue(high, -8) - low) / 8 else 0;
s9 = if upqty >= min_qty and upqty >= 9 then (GetValue(high, -9) - low) / 9 else 0;
s10 = if upqty >= min_qty and upqty >= 10 then (GetValue(high, -10) - low) / 10 else 0;
} else if firstdwn then {
s1 = if dwnqty >= min_qty and dwnqty >= 1 then (getvalue(low, -1) - high) / 1 else 0;
s2 = if dwnqty >= min_qty and dwnqty >= 2 then (getvalue(low, -2) - high) / 2 else 0;
s3 = if dwnqty >= min_qty and dwnqty >= 3 then (getvalue(low, -3) - high) / 3 else 0;
s4 = if dwnqty >= min_qty and dwnqty >= 4 then (getvalue(low, -4) - high) / 4 else 0;
s5 = if dwnqty >= min_qty and dwnqty >= 5 then (getvalue(low, -5) - high) / 5 else 0;
s6 = if dwnqty >= min_qty and dwnqty >= 6 then (getvalue(low, -6) - high) / 6 else 0;
s7 = if dwnqty >= min_qty and dwnqty >= 7 then (getvalue(low, -7) - high) / 7 else 0;
s8 = if dwnqty >= min_qty and dwnqty >= 8 then (getvalue(low, -8) - high) / 8 else 0;
s9 = if dwnqty >= min_qty and dwnqty >= 9 then (getvalue(low, -9) - high) / 9 else 0;
s10 = if dwnqty >= min_qty and dwnqty >= 10 then (getvalue(low, -10) - high) / 10 else 0;
} else {
s1 = s1[1];
s2 = s2[1];
s3 = s3[1];
s4 = s4[1];
s5 = s5[1];
s6 = s6[1];
s7 = s7[1];
s8 = s8[1];
s9 = s9[1];
s10 = s10[1];
}
# calc line data
def line1 = if upqty == 0 and dwnqty == 0 then na
else if max(upqty, dwnqty) < 1 then na
else if cnt < 0 or cnt > 1 then na
else if firstup then low
else if firstdwn then high
else line1[1] + s1;
def line2 = if upqty == 0 and dwnqty == 0 then na
else if max(upqty, dwnqty) < 2 then na
else if cnt < 0 or cnt > 2 then na
else if firstup then low
else if firstdwn then high
else line2[1] + s2;
def line3 = if upqty == 0 and dwnqty == 0 then na
else if max(upqty, dwnqty) < 3 then na
else if cnt < 0 or cnt > 3 then na
else if firstup then low
else if firstdwn then high
else line3[1] + s3;
def line4 = if upqty == 0 and dwnqty == 0 then na
else if max(upqty, dwnqty) < 4 then na
else if cnt < 0 or cnt > 4 then na
else if firstup then low
else if firstdwn then high
else line4[1] + s4;
def line5 = if upqty == 0 and dwnqty == 0 then na
else if max(upqty, dwnqty) < 5 then na
else if cnt < 0 or cnt > 5 then na
else if firstup then low
else if firstdwn then high
else line5[1] + s5;
def line6 = if upqty == 0 and dwnqty == 0 then na
else if max(upqty, dwnqty) < 6 then na
else if cnt < 0 or cnt > 6 then na
else if firstup then low
else if firstdwn then high
else line6[1] + s6;
def line7 = if upqty == 0 and dwnqty == 0 then na
else if max(upqty, dwnqty) < 7 then na
else if cnt < 0 or cnt > 7 then na
else if firstup then low
else if firstdwn then high
else line7[1] + s7;
def line8 = if upqty == 0 and dwnqty == 0 then na
else if max(upqty, dwnqty) < 8 then na
else if cnt < 0 or cnt > 8 then na
else if firstup then low
else if firstdwn then high
else line8[1] + s8;
def line9 = if upqty == 0 and dwnqty == 0 then na
else if max(upqty, dwnqty) < 9 then na
else if cnt < 0 or cnt > 9 then na
else if firstup then low
else if firstdwn then high
else line9[1] + s9;
def line10 = if upqty == 0 and dwnqty == 0 then na
else if max(upqty, dwnqty) < 10 then na
else if cnt < 0 or cnt > 10 then na
else if firstup then low
else if firstdwn then high
else line10[1] + s10;
#draw diag lines
plot z1 = if max(upqty,dwnqty) >= min_qty then line1 else na;
plot z2 = if max(upqty,dwnqty) >= min_qty then line2 else na;
plot z3 = if max(upqty,dwnqty) >= min_qty then line3 else na;
plot z4 = if max(upqty,dwnqty) >= min_qty then line4 else na;
plot z5 = if max(upqty,dwnqty) >= min_qty then line5 else na;
plot z6 = if max(upqty,dwnqty) >= min_qty then line6 else na;
plot z7 = if max(upqty,dwnqty) >= min_qty then line7 else na;
plot z8 = if max(upqty,dwnqty) >= min_qty then line8 else na;
plot z9 = if max(upqty,dwnqty) >= min_qty then line9 else na;
plot z10 = if max(upqty,dwnqty) >= min_qty then line10 else na;
z1.SetStyle(Curve.SHORT_DASH);
z2.SetStyle(Curve.SHORT_DASH);
z3.SetStyle(Curve.SHORT_DASH);
z4.SetStyle(Curve.SHORT_DASH);
z5.SetStyle(Curve.SHORT_DASH);
z6.SetStyle(Curve.SHORT_DASH);
z7.SetStyle(Curve.SHORT_DASH);
z8.SetStyle(Curve.SHORT_DASH);
z9.SetStyle(Curve.SHORT_DASH);
z10.SetStyle(Curve.SHORT_DASH);
z1.SetDefaultColor(Color.yellow);
z2.SetDefaultColor(Color.yellow);
z3.SetDefaultColor(Color.yellow);
z4.SetDefaultColor(Color.yellow);
z5.SetDefaultColor(Color.yellow);
z6.SetDefaultColor(Color.yellow);
z7.SetDefaultColor(Color.yellow);
z8.SetDefaultColor(Color.yellow);
z9.SetDefaultColor(Color.yellow);
z10.SetDefaultColor(Color.yellow);
#------------------
# test stuff
input show_bar_quantities = yes;
addchartbubble(show_bar_quantities and firstup and upqty >= min_qty, low,
upqty + "\n"
, color.yellow, no);
addchartbubble(show_bar_quantities and firstdwn and dwnqty >= min_qty, high,
dwnqty + "\n"
, color.yellow, yes);
addchartbubble(0 and firstup and upqty >= min_qty, low,
upqty + "\n" +
low + "\n" +
s1 + "\n" +
s2 + "\n" +
s3 + "\n" +
s4 + "\n" +
s5 + "\n"
, color.yellow, no);
addchartbubble(0 and firstdwn and dwnqty >= min_qty, high,
dwnqty + "\n" +
high + "\n" +
s1 + "\n" +
s2 + "\n" +
s3 + "\n" +
s4 + "\n" +
s5 + "\n"
, color.yellow, yes);
addchartbubble(0, low,
upqty + "\n" +
dwnqty + "\n" +
high + "\n" +
low + "\n" +
cnt + "\n" +
s1 + " s1\n" +
s2 + "\n" +
s3 + "\n" +
s4 + "\n" +
s5 + "\n"
, color.yellow, no);
addchartbubble(0, low,
z1 + "\n" +
z2 + "\n" +
z3 + "\n" +
z4 + "\n" +
z5 + "\n"
, color.yellow, no);
#