You are using an out of date browser. It may not display this or other websites correctly.

You should upgrade or use an alternative browser.

You should upgrade or use an alternative browser.

- Thread starter thebest07
- Start date

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

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:

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?

@thebest07 @lishuimu88888888

If you search the forum for pivot points most of them allow you to specify the look back length. HTH

If you search the forum for pivot points most of them allow you to specify the look back length. HTH

Last edited:

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);
#
```

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 differentyou 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

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.

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 barsver2

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.

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: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.

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.

Thread starter | Similar threads | Forum | Replies | Date |
---|---|---|---|---|

M | ZIGZAG High Low length | Questions | 0 | |

R | ZigZag & Future Bars Are Repainting! | Questions | 1 | |

W | ZigZag Percent: Add labels displaying averages | Questions | 1 | |

B | Help Needed with Upper and Lower zigzag band | Questions | 32 | |

ZigZag during certain hours | Questions | 1 |

Start a new thread and receive assistance from our community.

87k+
Posts

268
Online

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

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.

- The most viewed thread:

https://usethinkscript.com/threads/repainting-trend-reversal-for-thinkorswim.183/ - Our most popular indicator:

https://usethinkscript.com/threads/buy-the-dip-indicator-for-thinkorswim.3553/ - Answers to frequently asked questions:

https://usethinkscript.com/threads/answers-to-commonly-asked-questions.6006/

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.