# ZigZag based on number of bars

#### thebest07

##### New member
Hello, is there any zigzag that is drawn by number of bars ?, for example define for two periods, every 7 bars and every 21 bars

thanks

What I found so far is that it calculates by %, and not by offset of the highest value for a given number of historical bars.

For example, here is a person who did it for tradingview, he shows his source code so that it is understood more, he uses 2 periods, a short one of 8 bars and a long one of 20 bars and paints them according to the period

Thanks

Last edited by a moderator:
Hi, is there a way to code the following ideas:

if there are higher high consecutively for 5 days, then plot the upswing, vice versa lower lows for the downswing. I checked TOS zigzag version is based on number of points or ATRs, not number of bars.

Any thoughts anyone?

Last edited:
• thebest07
Hi, is there a way to code the following ideas:

if there are higher high consecutively for 5 days, then plot the upswing, vice versa lower lows for the downswing. I checked TOS zigzag version is based on number of points or ATRs, not number of bars.

Any thoughts anyone?

you can have a variable check for a condition.
then have another variable add up those conditions and see if they equal a specific quantity.
then draw an arrow when that happens.

can choose to show only the first arrow in a series, or all of them.
can choose the quantity of bars to look for rising/falling values.

i don't know what you want to see , from these words, plot the upswing. i had it draw arrows,

Code:
``````input show_only_first_arrow = no;
input qty = 5;

def cond1 = high > high;
def cond1sum = Sum(cond1, qty);
def up = if cond1sum == qty then 1 else 0;
def up2 = if !show_only_first_arrow then up else if up then 0 else up;

def cond2 = low < low;
def cond2sum = Sum(cond2, qty);
def dwn = if cond2sum == qty then 1 else 0;
def dwn2 = if !show_only_first_arrow then dwn else if dwn then 0 else dwn;

plot z1 = up2;
z1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
z1.SetDefaultColor(Color.GREEN);
z1.SetLineWeight(2);

plot z2 = dwn2;
z2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_down);
z2.SetDefaultColor(Color.red);
z2.SetLineWeight(2);
#``````

you can have a variable check for a condition.
then have another variable add up those conditions and see if they equal a specific quantity.
then draw an arrow when that happens.

can choose to show only the first arrow in a series, or all of them.
can choose the quantity of bars to look for rising/falling values.

i don't know what you want to see , from these words, plot the upswing. i had it draw arrows,

Code:
``````input show_only_first_arrow = no;
input qty = 5;

def cond1 = high > high;
def cond1sum = Sum(cond1, qty);
def up = if cond1sum == qty then 1 else 0;
def up2 = if !show_only_first_arrow then up else if up then 0 else up;

def cond2 = low < low;
def cond2sum = Sum(cond2, qty);
def dwn = if cond2sum == qty then 1 else 0;
def dwn2 = if !show_only_first_arrow then dwn else if dwn then 0 else dwn;

plot z1 = up2;
z1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
z1.SetDefaultColor(Color.GREEN);
z1.SetLineWeight(2);

plot z2 = dwn2;
z2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_down);
z2.SetDefaultColor(Color.red);
z2.SetLineWeight(2);
#``````
Thank you. I can follow your code. But instead of plotting arrows, i wanna connect the lines from a last swing low to a swing high. Lets say a swing low is identified, then market had 5 consecutive highs, on the 6th day, it failed to make new highs, so i wanna plot a up zigzag line connecting the swing low to the 5th day high, but on the 6th day(which i assume is the current day) NO line is plotted because the condition is not satisfied. If after the 6th, maket continues to make higher high for another 5 days to 12th day, then the upswing line will continue to connect to the high on 12th day. Same logic for downswings. The output is a zigzag line that looks like TOS zigzaghighlow version, but the logic is totally different

Thank you. I can follow your code. But instead of plotting arrows, i wanna connect the lines from a last swing low to a swing high. Lets say a swing low is identified, then market had 5 consecutive highs, on the 6th day, it failed to make new highs, so i wanna plot a up zigzag line connecting the swing low to the 5th day high, but on the 6th day(which i assume is the current day) NO line is plotted because the condition is not satisfied. If after the 6th, maket continues to make higher high for another 5 days to 12th day, then the upswing line will continue to connect to the high on 12th day. Same logic for downswings. The output is a zigzag line that looks like TOS zigzaghighlow version, but the logic is totally different

ver2
draw a line between points.
a series is 1 or more bars that has higher highs or lower lows, for x quantity of bars.

can choose first or last or all. if all arrows, no line is drawn.
draw a line between those in series bars , first or last bars

default is draw lines from the last in each series

Ruby:
``````# count_x_higher_highs_01

# ver1
# can choose first or last or all. if all arrows, no line is drawn
# draw a line between those in series bars , first or last bars

def na = double.nan;

input show_series_arrows = { only_first , default only_last , all };

def arr;
switch (show_series_arrows) {
case only_first:
arr = 1;
case only_last:
arr = 2;
case all:
arr = 3;
}

addlabel(arr == 1, "lines from first bar in a series", color.yellow);
addlabel(arr == 2, "lines from last bar in a series", color.yellow);

#input show_only_first_arrow = no;
#input show_only_last_arrow = yes;
input qty = 7;

def cond1 = high > high;
def cond1sum = Sum(cond1, qty);
def up = if cond1sum == qty then 1 else 0;
#def upfirst = if !show_only_first_arrow then up else if up then 0 else up;
def upfirst = if arr == 1 and !up then up else 0;
#def uplast = if !show_only_last_arrow then up else if (up and !up[-1]) then up else 0;
def uplast = if arr == 2 and !up[-1] then up else 0;
def upall = if arr == 3 then up else 0;

def cond2 = low < low;
def cond2sum = Sum(cond2, qty);
def dwn = if cond2sum == qty then 1 else 0;
#def dwnfirst = if !show_only_first_arrow then dwn else if dwn then 0 else dwn;
def dwnfirst = if arr == 1 and !dwn then dwn else 0;
#def dwnlast = if !show_only_last_arrow then dwn else if (dwn and !dwn[-1]) then dwn else 0;
def dwnlast = if arr == 2 and !dwn[-1] then dwn else 0;
def dwnall = if arr == 3 then dwn else 0;

input arrow_size = 3;
plot z1 = upfirst or upall;
z1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
z1.SetDefaultColor(Color.GREEN);
z1.SetLineWeight(arrow_size);

plot z2 = dwnfirst or dwnall;
z2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_down);
z2.SetDefaultColor(Color.red);
z2.SetLineWeight(arrow_size);

plot z3 = uplast;
z3.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
z3.SetDefaultColor(Color.cyan);
z3.SetLineWeight(arrow_size);

plot z4 = dwnlast;
z4.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_down);
z4.SetDefaultColor(Color.yellow);
z4.SetLineWeight(arrow_size);

# -------------------------------

# draw a line between the first or last bars in each series

# save close during a last bar
def price = close;
def linedata = if (arr == 1 and (upfirst or dwnfirst)) or (arr == 2 and (uplast or dwnlast)) then price else na;

def lineprices = if !isnan(linedata) then linedata else lineprices;
def isup = if !isnan(linedata) and close > lineprices then 1 else 0;

plot z5 = linedata;
z5.EnableApproximation();
#z5.SetStyle(Curve.MEDIUM_DASH);
z5.SetDefaultColor(Color.yellow);
z5.setlineweight(2);
z5.hidebubble();
#``````

MDT 1hr 90day
find 5 higher highs or 5 lower lows. a series is 1 or more of them.
draw lines from the last bar in each series. ver2
draw a line between points.
a series is 1 or more bars that has higher highs or lower lows, for x quantity of bars.

can choose first or last or all. if all arrows, no line is drawn.
draw a line between those in series bars , first or last bars

default is draw lines from the last in each series

Ruby:
``````# count_x_higher_highs_01

# ver1
# can choose first or last or all. if all arrows, no line is drawn
# draw a line between those in series bars , first or last bars

def na = double.nan;

input show_series_arrows = { only_first , default only_last , all };

def arr;
switch (show_series_arrows) {
case only_first:
arr = 1;
case only_last:
arr = 2;
case all:
arr = 3;
}

addlabel(arr == 1, "lines from first bar in a series", color.yellow);
addlabel(arr == 2, "lines from last bar in a series", color.yellow);

#input show_only_first_arrow = no;
#input show_only_last_arrow = yes;
input qty = 7;

def cond1 = high > high;
def cond1sum = Sum(cond1, qty);
def up = if cond1sum == qty then 1 else 0;
#def upfirst = if !show_only_first_arrow then up else if up then 0 else up;
def upfirst = if arr == 1 and !up then up else 0;
#def uplast = if !show_only_last_arrow then up else if (up and !up[-1]) then up else 0;
def uplast = if arr == 2 and !up[-1] then up else 0;
def upall = if arr == 3 then up else 0;

def cond2 = low < low;
def cond2sum = Sum(cond2, qty);
def dwn = if cond2sum == qty then 1 else 0;
#def dwnfirst = if !show_only_first_arrow then dwn else if dwn then 0 else dwn;
def dwnfirst = if arr == 1 and !dwn then dwn else 0;
#def dwnlast = if !show_only_last_arrow then dwn else if (dwn and !dwn[-1]) then dwn else 0;
def dwnlast = if arr == 2 and !dwn[-1] then dwn else 0;
def dwnall = if arr == 3 then dwn else 0;

input arrow_size = 3;
plot z1 = upfirst or upall;
z1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
z1.SetDefaultColor(Color.GREEN);
z1.SetLineWeight(arrow_size);

plot z2 = dwnfirst or dwnall;
z2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_down);
z2.SetDefaultColor(Color.red);
z2.SetLineWeight(arrow_size);

plot z3 = uplast;
z3.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
z3.SetDefaultColor(Color.cyan);
z3.SetLineWeight(arrow_size);

plot z4 = dwnlast;
z4.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_down);
z4.SetDefaultColor(Color.yellow);
z4.SetLineWeight(arrow_size);

# -------------------------------

# draw a line between the first or last bars in each series

# save close during a last bar
def price = close;
def linedata = if (arr == 1 and (upfirst or dwnfirst)) or (arr == 2 and (uplast or dwnlast)) then price else na;

def lineprices = if !isnan(linedata) then linedata else lineprices;
def isup = if !isnan(linedata) and close > lineprices then 1 else 0;

plot z5 = linedata;
z5.EnableApproximation();
#z5.SetStyle(Curve.MEDIUM_DASH);
z5.SetDefaultColor(Color.yellow);
z5.setlineweight(2);
z5.hidebubble();
#``````

MDT 1hr 90day
find 5 higher highs or 5 lower lows. a series is 1 or more of them.
draw lines from the last bar in each series. This doesn't seem right to me, but thank your for your great effort, let me try refer to the zigzag highlow script and see if i can come up with something more close. Basically i wanna plot something like zigzag highlow as built in TOS, but based on numbers of bars

ver2
draw a line between points.
a series is 1 or more bars that has higher highs or lower lows, for x quantity of bars.

can choose first or last or all. if all arrows, no line is drawn.
draw a line between those in series bars , first or last bars

default is draw lines from the last in each series

Ruby:
``````# count_x_higher_highs_01

# ver1
# can choose first or last or all. if all arrows, no line is drawn
# draw a line between those in series bars , first or last bars

def na = double.nan;

input show_series_arrows = { only_first , default only_last , all };

def arr;
switch (show_series_arrows) {
case only_first:
arr = 1;
case only_last:
arr = 2;
case all:
arr = 3;
}

addlabel(arr == 1, "lines from first bar in a series", color.yellow);
addlabel(arr == 2, "lines from last bar in a series", color.yellow);

#input show_only_first_arrow = no;
#input show_only_last_arrow = yes;
input qty = 7;

def cond1 = high > high;
def cond1sum = Sum(cond1, qty);
def up = if cond1sum == qty then 1 else 0;
#def upfirst = if !show_only_first_arrow then up else if up then 0 else up;
def upfirst = if arr == 1 and !up then up else 0;
#def uplast = if !show_only_last_arrow then up else if (up and !up[-1]) then up else 0;
def uplast = if arr == 2 and !up[-1] then up else 0;
def upall = if arr == 3 then up else 0;

def cond2 = low < low;
def cond2sum = Sum(cond2, qty);
def dwn = if cond2sum == qty then 1 else 0;
#def dwnfirst = if !show_only_first_arrow then dwn else if dwn then 0 else dwn;
def dwnfirst = if arr == 1 and !dwn then dwn else 0;
#def dwnlast = if !show_only_last_arrow then dwn else if (dwn and !dwn[-1]) then dwn else 0;
def dwnlast = if arr == 2 and !dwn[-1] then dwn else 0;
def dwnall = if arr == 3 then dwn else 0;

input arrow_size = 3;
plot z1 = upfirst or upall;
z1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
z1.SetDefaultColor(Color.GREEN);
z1.SetLineWeight(arrow_size);

plot z2 = dwnfirst or dwnall;
z2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_down);
z2.SetDefaultColor(Color.red);
z2.SetLineWeight(arrow_size);

plot z3 = uplast;
z3.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
z3.SetDefaultColor(Color.cyan);
z3.SetLineWeight(arrow_size);

plot z4 = dwnlast;
z4.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_down);
z4.SetDefaultColor(Color.yellow);
z4.SetLineWeight(arrow_size);

# -------------------------------

# draw a line between the first or last bars in each series

# save close during a last bar
def price = close;
def linedata = if (arr == 1 and (upfirst or dwnfirst)) or (arr == 2 and (uplast or dwnlast)) then price else na;

def lineprices = if !isnan(linedata) then linedata else lineprices;
def isup = if !isnan(linedata) and close > lineprices then 1 else 0;

plot z5 = linedata;
z5.EnableApproximation();
#z5.SetStyle(Curve.MEDIUM_DASH);
z5.SetDefaultColor(Color.yellow);
z5.setlineweight(2);
z5.hidebubble();
#``````

MDT 1hr 90day
find 5 higher highs or 5 lower lows. a series is 1 or more of them.
draw lines from the last bar in each series. I have tried but this script is too difficult for me. Is there a way to make your script to show highs and lows alternatively? your code sometimes connected high to another high. I have some pieces of code that i see elsewhere and this is similar to my idea:

def swinghigh = if high > high and high > high and high > high[-1] and high > high[-2] then 1 else 0;

def swinglow = if low < low and low < low and low < low[-1] and low < low[-2] then 1 else 0;

plot sh = if swinghigh then high else Double.NaN;
plot sl = if swinglow then low else Double.NaN;
sh.SetStyle(Curve.POINTS);
sl.SetStyle(Curve.POINTS);

The problem is what if I wanna find 5 consecutive higher highs or more? Instead of high>high and high>high... high>high, is there an easy way to do it so that i can input whatever numbers i want.

Next problem is how to draw the lines between each high-low dot in the above script? Appreciated if anyone can shed some light

I have tried but this script is too difficult for me. Is there a way to make your script to show highs and lows alternatively? your code sometimes connected high to another high. I have some pieces of code that i see elsewhere and this is similar to my idea:

def swinghigh = if high > high and high > high and high > high[-1] and high > high[-2] then 1 else 0;

def swinglow = if low < low and low < low and low < low[-1] and low < low[-2] then 1 else 0;

plot sh = if swinghigh then high else Double.NaN;
plot sl = if swinglow then low else Double.NaN;
sh.SetStyle(Curve.POINTS);
sl.SetStyle(Curve.POINTS);

The problem is what if I wanna find 5 consecutive higher highs or more? Instead of high>high and high>high... high>high, is there an easy way to do it so that i can input whatever numbers i want.

Next problem is how to draw the lines between each high-low dot in the above script? Appreciated if anyone can shed some light

my code does look for any quantity of higher highs and lower lows
input qty = 7;
def cond1 = high > high;
def cond1sum = Sum(cond1, qty);
def up = if cond1sum == qty then 1 else 0;

lines are plotted from the last bar ( or the first bar) in the series, which is why it looks 'strange' .
there isn't going to be alternating groups of highs and lows. sometimes there will be 2+ groups of highs in a row.

if you want a different plot, come up with different rules.

### Not the exact question you're looking for?

87k+ Posts
268 Online ## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?