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

https://www.tradingview.com/script/mRbjBGdL-Double-Zig-Zag-with-HHLL/

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?
 
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[1];
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[1] then 0 else up;

def cond2 = low < low[1];
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[1] 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[1];
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[1] then 0 else up;

def cond2 = low < low[1];
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[1] 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

# https://usethinkscript.com/threads/zigzag-based-on-number-of-bars.6941/#post-96433

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[1];
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[1] then 0 else up;
def upfirst = if arr == 1 and !up[1] 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[1];
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[1] then 0 else dwn;
def dwnfirst = if arr == 1 and !dwn[1] 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[1];
def isup = if !isnan(linedata) and close > lineprices[1] 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.
4xjJaVK.jpg
 
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

# https://usethinkscript.com/threads/zigzag-based-on-number-of-bars.6941/#post-96433

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[1];
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[1] then 0 else up;
def upfirst = if arr == 1 and !up[1] 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[1];
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[1] then 0 else dwn;
def dwnfirst = if arr == 1 and !dwn[1] 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[1];
def isup = if !isnan(linedata) and close > lineprices[1] 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.
4xjJaVK.jpg
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

# https://usethinkscript.com/threads/zigzag-based-on-number-of-bars.6941/#post-96433

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[1];
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[1] then 0 else up;
def upfirst = if arr == 1 and !up[1] 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[1];
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[1] then 0 else dwn;
def dwnfirst = if arr == 1 and !dwn[1] 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[1];
def isup = if !isnan(linedata) and close > lineprices[1] 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.
4xjJaVK.jpg
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[1] and high > high[2] and high > high[-1] and high > high[-2] then 1 else 0;

def swinglow = if low < low[1] and low < low[2] 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[1] and high[1]>high[2]... high[4]>high[5], 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[1] and high > high[2] and high > high[-1] and high > high[-2] then 1 else 0;

def swinglow = if low < low[1] and low < low[2] 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[1] and high[1]>high[2]... high[4]>high[5], 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[1];
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.
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
343 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

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?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top