ZigZagHighLow with look back and look forward bars?

SubZer0

New member
Hello,
I'm currently using the inbuilt ZigZagHighLow indicator in ToS. It is designed to detect swing points using price change %. It works for some stocks but doesn't work for others. I am looking for any similar indicators that connect the swing high/low with trend lines and also support looking back n number of bars forward and backward. The closest i came across were these threads

https://usethinkscript.com/threads/...i-retracement-indicator-for-thinkorswim.8091/
https://usethinkscript.com/threads/improving-swing-high-low-indicator.10983/

I got it working (don't need the fib code) but am stuck at the point where i am trying to have the code automatically draw trend lines to connect the swing highs and swing lows to generate the market structure plot instead of the chart bubbles.

Appreciate any advise and pointers to achieve an indicator that is very close to the ZigZagHighLow indicator, draws the market structure plot and accepts lookback and lookforward of n number of bars.

Thanks!!
 
Last edited:
Solution
OK. Took me a bit of digging but i found the root cause. If ShowHorizontals is turned off, the %change value shows as N/A. Works fine when turning it back on. I can reproduce this on the charts and flexible grid you shared.

Looking at the code, RLo and RHi are set to NaN when horizontals are turned off. These values feed into downstream calculations for showing labels and also for the bubbles.

This fixes that problem by using sethiding(). Thank you.

Ruby:
def h = high;
def l = low;
def c = close;
def o = open;
def v = volume;

# Simple Fractal Recon
input length = 5;


def HH1 = h >= Highest(h, length) and h >= Highest(h, length)[-length];
def  High1 = if HH1 then h else Double.NaN;
def LL1 = l <= Lowest(l, length) and l <=...

SleepyZ

Well-known member
VIP
Lifetime
Hello,
I'm currently using the inbuilt ZigZagHighLow indicator in ToS. It is designed to detect swing points using price change %. It works for some stocks but doesn't work for others. I am looking for any similar indicators that connect the swing high/low with trend lines and also support looking back n number of bars forward and backward. The closest i came across were these threads

https://usethinkscript.com/threads/...i-retracement-indicator-for-thinkorswim.8091/
https://usethinkscript.com/threads/improving-swing-high-low-indicator.10983/

I got it working (don't need the fib code) but am stuck at the point where i am trying to have the code automatically draw trend lines to connect the swing highs and swing lows to generate the market structure plot instead of the chart bubbles.

Appreciate any advise and pointers to achieve an indicator that is very close to the ZigZagHighLow indicator, draws the market structure plot and accepts lookback and lookforward of n number of bars.

Thanks!!

This is part of a code a did awhile ago that should help. It trys to avoid multiple highs or lows between pivots to create a zigzag of high to low and vice versa.

Capture.jpg
Ruby:
def h = high;
def l = low;
def c = close;
def o = open;
def v = volume;

# Simple Fractal Recon
input length = 5;

def HH1 = h >= Highest(h, length) and h >= Highest(h, length)[-length];
def High1 = if HH1 then h else Double.NaN;
def LL1 = l <= Lowest(l, length) and l <= Lowest(l, length)[-length];
def Low1 = if LL1 then l else Double.NaN;
def PointCount = if BarNumber() == 1 then 0 else
                 if IsNaN(c) then PointCount[1] else
                 if !IsNaN(High1) then Max(1, PointCount[1] + 1) else
                 if !IsNaN(Low1) then Min(-1, PointCount[1] - 1)
                 else PointCount[1];

input showhorizontals = yes;
def RangeHI = if !IsNaN(High1) and PointCount == 1
then h else RangeHI[1];
plot Rhi = if !showhorizontals then Double.NaN else RangeHI;
Rhi.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Rhi.SetLineWeight(1);
Rhi.SetDefaultColor(Color.RED);
def RangeLO = if !IsNaN(Low1) and PointCount == -1
then l else RangeLO[1];
plot Rlo = if !showhorizontals then Double.NaN else RangeLO;
Rlo.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Rlo.SetLineWeight(1);
Rlo.SetDefaultColor(Color.GREEN);

#Harndog
input HHLLlabels = yes;

def rh1 = if !IsNaN(Rhi) then Rhi else rh1[1];
rec rh2 = if rh1 != rh1[1] then rh1[1] else rh2[1];

AddLabel(HHLLlabels, if rh1 > rh2
              then "Higher Highs"
              else "Lower Highs",
              if rh1 > rh2
              then Color.GREEN
              else Color.RED);

def rl1 = if !IsNaN(Rlo) then Rlo else rl1[1];
rec rl2 = if rl1 != rl1[1] then rl1[1] else rl2[1];
AddLabel(HHLLlabels, if  rl1 > rl2
              then "Higher Lows"
              else "Lower Lows",
              if rl1 > rl2
              then Color.GREEN
              else Color.RED);

input HHLLBubbles = yes;
input HHLLbubblemover = 6;
def  hb  = HHLLbubblemover;
def hb1  = hb + 1;

AddChartBubble(!IsNaN(c[hb1]) and IsNaN(c[hb]) and HHLLBubbles , c[hb1], if rl1[hb1] > rl2[hb1] then "HL" else "LL" , if rl1[hb1] > rl2[hb1] then Color.GREEN else Color.RED, yes);

AddChartBubble(!IsNaN(c[hb1]) and IsNaN(c[hb]) and HHLLBubbles , c[hb1], if rh1[hb1] > rh2[hb1] then "HH" else "LH" , if rh1[hb1] > rh2[hb1] then Color.GREEN else Color.RED, yes);


#ZigZags
def zHI;
def zLO;
zHI = if !IsNaN(High1) and PointCount == 1 then h else Double.NaN;
zLO = if !IsNaN(Low1) and PointCount == -1 then l else Double.NaN;
plot hilowline = if !IsNaN(zHI) then zHI else zLO;
hilowline.EnableApproximation();

AddChartBubble(!IsNaN(High1) and PointCount == 1, high, "H", Color.GREEN);
AddChartBubble(!IsNaN(Low1) and PointCount == -1, low, "L", Color.RED, no);
 
Last edited:

SubZer0

New member
This is part of a code a did awhile ago that should help. It trys to avoid multiple highs or lows between pivots to create a zigzag of high to low and vice versa.
Thank you very much for the script! This is exactly what i've been trying to achieve. I was totally not expecting such a fast response, you have my deep appreciation and gratitude for that.

If i could get help with two requests, that would be mighty appreciated.
1. At each swing point, is there a way to show % price change between previous and current swing point?
2. How do i connect the last swing leg to the latest bar (even if it hasn't formed a new swing point)?

Thanks again!

Also PS: I tweaked the last two lines of the script to hide the bubbles when toggled off from the config dialog

Code:
AddChartBubble(HHLLBubbles and !IsNaN(High1) and PointCount == 1, high, "H", Color.GREEN);
AddChartBubble(HHLLBubbles and !IsNaN(Low1) and PointCount == -1, low, "L", Color.RED, no);
 
Last edited:

SleepyZ

Well-known member
VIP
Lifetime
Thank you very much for the script! This is exactly what i've been trying to achieve. I was totally not expecting such a fast response, you have my deep appreciation and gratitude for that.

If i could get help with two requests, that would be mighty appreciated.
1. At each swing point, is there a way to show % price change between previous and current swing point?
2. How do i connect the last swing leg to the latest bar (even if it hasn't formed a new swing point)?

Thanks again!

Also PS: I tweaked the last two lines of the script to hide the bubbles when toggled off from the config dialog

Code:
AddChartBubble(HHLLBubbles and !IsNaN(High1) and PointCount == 1, high, "H", Color.GREEN);
AddChartBubble(HHLLBubbles and !IsNaN(Low1) and PointCount == -1, low, "L", Color.RED, no);

This will provide bubbles with price, %change and color coded higher high or lower low than the previous high/low. Also an extension of the zz pattern was added to the current close.

Capture.jpg
Ruby:
def h = high;
def l = low;
def c = close;
def o = open;
def v = volume;

# Simple Fractal Recon
input length = 5;


def HH1 = h >= Highest(h, length) and h >= Highest(h, length)[-length];
def  High1 = if HH1 then h else Double.NaN;
def LL1 = l <= Lowest(l, length) and l <= Lowest(l, length)[-length];
def Low1 = if LL1 then l else Double.NaN;
def PointCount = if BarNumber() == 1 then 0 else
                 if IsNaN(c) then PointCount[1] else
                 if !IsNaN(High1) then Max(1, PointCount[1] + 1) else
                 if !IsNaN(Low1) then Min(-1, PointCount[1] - 1)
                 else PointCount[1];

input showhorizontals = yes;
def RangeHI = if !IsNaN(High1) and PointCount == 1
then h else RangeHI[1];
plot Rhi = if !showhorizontals then Double.NaN else RangeHI;
Rhi.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Rhi.SetLineWeight(1);
Rhi.SetDefaultColor(Color.RED);
def RangeLO = if !IsNaN(Low1) and PointCount == -1
then l else RangeLO[1];
plot Rlo = if !showhorizontals then Double.NaN else RangeLO;
Rlo.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Rlo.SetLineWeight(1);
Rlo.SetDefaultColor(Color.GREEN);

#Harndog
input HHLLlabels = yes;

def rh1 = if !IsNaN(Rhi) then Rhi else rh1[1];
def rh2 = if rh1 != rh1[1] then rh1[1] else rh2[1];

AddLabel(HHLLlabels, if rh1 > rh2
              then "Higher Highs"
              else "Lower Highs",
              if rh1 > rh2
              then Color.GREEN
              else Color.RED);

def rl1 = if !IsNaN(Rlo) then Rlo else rl1[1];
def rl2 = if rl1 != rl1[1] then rl1[1] else rl2[1];
AddLabel(HHLLlabels, if  rl1 > rl2
              then "Higher Lows"
              else "Lower Lows",
              if rl1 > rl2
              then Color.GREEN
              else Color.RED);

#ZigZags
def zHI;
def zLO;

zHI = if !IsNaN(High1) and PointCount == 1 then h else Double.NaN;
zLO = if !IsNaN(Low1) and PointCount == -1 then l else Double.NaN;

def bn = BarNumber();
def lastzhi = if !IsNaN(zHI) then bn else lastzhi[1];
def lastzlo = if !IsNaN(zLO) then bn else lastzlo[1];


plot hilowline = if !IsNaN(zHI) then zHI else zLO;
hilowline.EnableApproximation();
hilowline.SetLineWeight(3);
hilowline.SetDefaultColor(Color.WHITE);

def last = if IsNaN(close[-1]) and !IsNaN(close) then BarNumber() else Double.NaN;
def lastbar   = if BarNumber() == HighestAll(last) then 1 else 0;
def lastvalue = if lastbar then close else Double.NaN;

plot hilowline1 = if bn < Max(lastzhi, lastzlo)
                  then Double.NaN
                  else if lastzlo > lastzhi and !IsNaN(zLO)
                  then zLO
                  else if lastzlo < lastzhi and !IsNaN(zHI)
                  then zHI else lastvalue;
hilowline1.EnableApproximation();
hilowline1.SetLineWeight(3);
hilowline1.SetDefaultColor(Color.WHITE);

def xxhigh  = if rh1 == high then high else xxhigh[1];
def xxlow   = if rl1 == low then low else xxlow[1];
def chghigh = (rh1 - xxlow[1]) / xxlow[1] * 100;
def chglow  = (rl1 - xxhigh[1]) / xxhigh[1] * 100;

AddChartBubble(!IsNaN(High1) and PointCount == 1, high, "H\n" + AsText(high) + " \n" +  Round(chghigh) + "%", if xxhigh > xxhigh[1] then Color.GREEN else Color.RED);
AddChartBubble(!IsNaN(Low1) and PointCount == -1, low, "L\n" + AsText(low) + " \n" +  Round(chglow) + "%", if xxlow > xxlow[1] then Color.GREEN else Color.RED, no);
 

SubZer0

New member
Thank you for the help. I tested latest code. The swing legs are working, they connect to the latest bar.

However, %change appears as N/A% and all bubbles are in red. Did i miss any configuation settings?

I can't seem to figure out how to upload images to this post, screenshot is available at link below.
http://www.dreaminginpixels.com/download/ToS/2022-09-22_14-11-05.jpg

Please advise. I will test some more and reach out if i have more questions
 

SleepyZ

Well-known member
VIP
Lifetime
Thank you for the help. I tested latest code. The swing legs are working, they connect to the latest bar.

However, %change appears as N/A% and all bubbles are in red. Did i miss any configuation settings?

I can't seem to figure out how to upload images to this post, screenshot is available at link below.
http://www.dreaminginpixels.com/download/ToS/2022-09-22_14-11-05.jpg

Please advise. I will test some more and reach out if i have more questions
Try this share link to see the % for lows ttps://tos.mx/DyREtVz
 

SubZer0

New member
This is very strange. Your link works perfectly fine when i import it. I can see the %change in the bubbles on your chart while mine still shows N/A%. After quite a bit of fiddling around, i am seeing that if the chart is inside a flexible grid (which is all charts in my case), i can see this problem. If it is a standalone single chart, your script works. That is so weird. I tried to see what's different and the settings all appear to be the same. Tried creating a new flexible grid, clean chart with just your script but i still get N/A%. Any ideas on how a flexible grid would impact studies? I'm at a loss.
 

SleepyZ

Well-known member
VIP
Lifetime
This is very strange. Your link works perfectly fine when i import it. I can see the %change in the bubbles on your chart while mine still shows N/A%. After quite a bit of fiddling around, i am seeing that if the chart is inside a flexible grid (which is all charts in my case), i can see this problem. If it is a standalone single chart, your script works. That is so weird. I tried to see what's different and the settings all appear to be the same. Tried creating a new flexible grid, clean chart with just your script but i still get N/A%. Any ideas on how a flexible grid would impact studies? I'm at a loss.

Here is a flexible grid with % showing for both https://tos.mx/LUxgZqk
 

SubZer0

New member
Ok. This is super weird and thanks for being patient. I removed all my other studies and added just your study in my flexible grid and it seems to work. I am guessing there is a conflict with other studies on my chart. I will peek under the hood and try to get to the bottom of this tonight. Thanks so much for bearing with me while i sort this out! Have a good evening and appreciate all the help!
 

SubZer0

New member
Here is a flexible grid with % showing for both https://tos.mx/LUxgZqk
OK. Took me a bit of digging but i found the root cause. If ShowHorizontals is turned off, the %change value shows as N/A. Works fine when turning it back on. I can reproduce this on the charts and flexible grid you shared.

Looking at the code, RLo and RHi are set to NaN when horizontals are turned off. These values feed into downstream calculations for showing labels and also for the bubbles.
 

SleepyZ

Well-known member
VIP
Lifetime
OK. Took me a bit of digging but i found the root cause. If ShowHorizontals is turned off, the %change value shows as N/A. Works fine when turning it back on. I can reproduce this on the charts and flexible grid you shared.

Looking at the code, RLo and RHi are set to NaN when horizontals are turned off. These values feed into downstream calculations for showing labels and also for the bubbles.

This fixes that problem by using sethiding(). Thank you.

Ruby:
def h = high;
def l = low;
def c = close;
def o = open;
def v = volume;

# Simple Fractal Recon
input length = 5;


def HH1 = h >= Highest(h, length) and h >= Highest(h, length)[-length];
def  High1 = if HH1 then h else Double.NaN;
def LL1 = l <= Lowest(l, length) and l <= Lowest(l, length)[-length];
def Low1 = if LL1 then l else Double.NaN;
def PointCount = if BarNumber() == 1 then 0 else
                 if IsNaN(c) then PointCount[1] else
                 if !IsNaN(High1) then Max(1, PointCount[1] + 1) else
                 if !IsNaN(Low1) then Min(-1, PointCount[1] - 1)
                 else PointCount[1];

input showhorizontals = no;
def RangeHI = if !IsNaN(High1) and PointCount == 1
then h else RangeHI[1];
plot Rhi = RangeHI;
Rhi.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Rhi.SetLineWeight(1);
Rhi.SetDefaultColor(Color.RED);
rhi.sethiding(!showhorizontals);

def RangeLO = if !IsNaN(Low1) and PointCount == -1
then l else RangeLO[1];
plot Rlo =  RangeLO;
Rlo.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Rlo.SetLineWeight(1);
Rlo.SetDefaultColor(Color.GREEN);
Rlo.sethiding(!showhorizontals);

#Harndog
input HHLLlabels = yes;

def rh1 = if !IsNaN(Rhi) then Rhi else rh1[1];
def rh2 = if rh1 != rh1[1] then rh1[1] else rh2[1];

AddLabel(HHLLlabels, if rh1 > rh2
              then "Higher Highs"
              else "Lower Highs",
              if rh1 > rh2
              then Color.GREEN
              else Color.RED);

def rl1 = if !IsNaN(Rlo) then Rlo else rl1[1];
def rl2 = if rl1 != rl1[1] then rl1[1] else rl2[1];
AddLabel(HHLLlabels, if  rl1 > rl2
              then "Higher Lows"
              else "Lower Lows",
              if rl1 > rl2
              then Color.GREEN
              else Color.RED);

#ZigZags
def zHI;
def zLO;

zHI = if !IsNaN(High1) and PointCount == 1 then h else Double.NaN;
zLO = if !IsNaN(Low1) and PointCount == -1 then l else Double.NaN;

def bn = BarNumber();
def lastzhi = if !IsNaN(zHI) then bn else lastzhi[1];
def lastzlo = if !IsNaN(zLO) then bn else lastzlo[1];


plot hilowline = if !IsNaN(zHI) then zHI else zLO;
hilowline.EnableApproximation();
hilowline.SetLineWeight(3);
hilowline.SetDefaultColor(Color.WHITE);

def last = if IsNaN(close[-1]) and !IsNaN(close) then BarNumber() else Double.NaN;
def lastbar   = if BarNumber() == HighestAll(last) then 1 else 0;
def lastvalue = if lastbar then close else Double.NaN;

plot hilowline1 = if bn < Max(lastzhi, lastzlo)
                  then Double.NaN
                  else if lastzlo > lastzhi and !IsNaN(zLO)
                  then zLO
                  else if lastzlo < lastzhi and !IsNaN(zHI)
                  then zHI else lastvalue;
hilowline1.EnableApproximation();
hilowline1.SetLineWeight(3);
hilowline1.SetDefaultColor(Color.WHITE);

def xxhigh  = if rh1 == high then high else xxhigh[1];
def xxlow   = if rl1 == low then low else xxlow[1];
def chghigh = (rh1 - xxlow[1]) / xxlow[1] * 100;
def chglow  = (rl1 - xxhigh[1]) / xxhigh[1] * 100;

AddChartBubble(!IsNaN(High1) and PointCount == 1, high, "H\n" + AsText(high) + " \n" +  Round(chghigh) + "%", if xxhigh > xxhigh[1] then Color.GREEN else Color.RED);
AddChartBubble(!IsNaN(Low1) and PointCount == -1, low, "L\n" + AsText(low) + " \n" +  Round(chglow) + "%", if xxlow > xxlow[1] then Color.GREEN else Color.RED, no);
 
Solution

SubZer0

New member
Hi, I've been using your latest indicator. It works really well. I have few observations and wanted your opinion.

Attached links are of SPY M5 from today

1. In an ideal (and theoretical) situation, you will have a swing high followed by a swing low and this repeats. There are situations where you will have two consecutive swing highs without having a swing low (and vice versa). In this case, should the latest swing high point be considered and the previous one be ignored? See circles in the screenshot where i think the swing points should have been
http://www.dreaminginpixels.com/download/ToS/2022-09-23_14-32-23.jpg

2. The last swing leg that connects the latest bar always seems to connect to the top of the latest bar. If the previous swing point was higher than current price (it was a swing high), should the last leg connect to the bottom of the latest bar? Likewise, if the previous swing point was lower than the current price (it was a swing low), should the last leg connect to the top of the latest bar, like it is currently working?
http://www.dreaminginpixels.com/download/ToS/2022-09-23_14-37-33.jpg

3. Sorry, i know i requested earlier for the last swing leg to connect to the latest bar, but i am seeing how this might be misleading. Should the last swing leg connect to the lowest (or highest) that was formed between the previous swing point and latest bar?. See the circles in this screenshot where i think it should've been
http://www.dreaminginpixels.com/download/ToS/2022-09-23_15-03-33.jpg

Please let me know what you think. Your indicator is the best i found anywhere on the internet, wanted to see if my observations are valid or if this is technically not possible to achieve this.

Thank you for all the great help so far. You the best!
 

SleepyZ

Well-known member
VIP
Lifetime
Hi, I've been using your latest indicator. It works really well. I have few observations and wanted your opinion.

Attached links are of SPY M5 from today

1. In an ideal (and theoretical) situation, you will have a swing high followed by a swing low and this repeats. There are situations where you will have two consecutive swing highs without having a swing low (and vice versa). In this case, should the latest swing high point be considered and the previous one be ignored? See circles in the screenshot where i think the swing points should have been
http://www.dreaminginpixels.com/download/ToS/2022-09-23_14-32-23.jpg

2. The last swing leg that connects the latest bar always seems to connect to the top of the latest bar. If the previous swing point was higher than current price (it was a swing high), should the last leg connect to the bottom of the latest bar? Likewise, if the previous swing point was lower than the current price (it was a swing low), should the last leg connect to the top of the latest bar, like it is currently working?
http://www.dreaminginpixels.com/download/ToS/2022-09-23_14-37-33.jpg

3. Sorry, i know i requested earlier for the last swing leg to connect to the latest bar, but i am seeing how this might be misleading. Should the last swing leg connect to the lowest (or highest) that was formed between the previous swing point and latest bar?. See the circles in this screenshot where i think it should've been
http://www.dreaminginpixels.com/download/ToS/2022-09-23_15-03-33.jpg

Please let me know what you think. Your indicator is the best i found anywhere on the internet, wanted to see if my observations are valid or if this is technically not possible to achieve this.

Thank you for all the great help so far. You the best!

As to #1, I think it is best to stick with the ideal model as that is why I used the pointcount convention. If I do code one shifting the swings to for example pointcount==2 or higher I will update this thread.
As to #2 and #3, Iike your idea of using instead of close for the final connection point, that moves as close shifts, but to instead use the midpoint of the midpoint of the last swing. So the following code uses that.

Ruby:
def h = high;
def l = low;
def c = close;
def o = open;
def v = volume;

# Simple Fractal Recon
input length = 5;

def HH1 = h >= Highest(h, length) and h >= Highest(h, length)[-length];
def  High1 = if HH1 then h else Double.NaN;
def LL1 = l <= Lowest(l, length) and l <= Lowest(l, length)[-length];
def Low1 = if LL1 then l else Double.NaN;
def PointCount = if BarNumber() == 1 then 0 else
                 if IsNaN(c) then PointCount[1] else
                 if !IsNaN(High1) then Max(1, PointCount[1] + 1) else
                 if !IsNaN(Low1) then Min(-1, PointCount[1] - 1)
                 else PointCount[1];

input showhorizontals = no;

def RangeHI = if !IsNaN(High1) and PointCount == 1
then h else RangeHI[1];
plot Rhi = RangeHI;
Rhi.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Rhi.SetLineWeight(1);
Rhi.SetDefaultColor(Color.RED);
Rhi.SetHiding(!showhorizontals);

def RangeLO = if !IsNaN(Low1) and PointCount == -1
then l else RangeLO[1];
plot Rlo =  RangeLO;
Rlo.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Rlo.SetLineWeight(1);
Rlo.SetDefaultColor(Color.GREEN);
Rlo.SetHiding(!showhorizontals);

#Harndog
input HHLLlabels = yes;

def rh1 = if !IsNaN(Rhi) then Rhi else rh1[1];
def rh2 = if rh1 != rh1[1] then rh1[1] else rh2[1];

AddLabel(HHLLlabels, if rh1 > rh2
              then "Higher Highs"
              else "Lower Highs",
              if rh1 > rh2
              then Color.GREEN
              else Color.RED);

def rl1 = if !IsNaN(Rlo) then Rlo else rl1[1];
def rl2 = if rl1 != rl1[1] then rl1[1] else rl2[1];
AddLabel(HHLLlabels, if  rl1 > rl2
              then "Higher Lows"
              else "Lower Lows",
              if rl1 > rl2
              then Color.GREEN
              else Color.RED);

#ZigZags
def zHI;
def zLO;

zHI = if !IsNaN(High1) and PointCount == 1 then h else Double.NaN;
zLO = if !IsNaN(Low1) and PointCount == -1 then l else Double.NaN;

def bn = BarNumber();
def lastzhibar = if !IsNaN(zHI) then bn else lastzhibar[1];
def lastzlobar = if !IsNaN(zLO) then bn else lastzlobar[1];
def lastzhi    = if bn==highestall(lastzhibar) then h else lastzhi[1];
def lastzlo    = if bn==highestall(lastzlobar) then l else lastzlo[1];

plot hilowline = if !IsNaN(zHI) then zHI else zLO;
hilowline.EnableApproximation();
hilowline.SetLineWeight(3);
hilowline.SetDefaultColor(Color.WHITE);

def last = if IsNaN(close[-1]) and !IsNaN(close) then BarNumber() else Double.NaN;
def lastbar   = if BarNumber() == HighestAll(last) then 1 else 0;
def lastvalue = if lastbar then (lastzhi + lastzlo) / 2 else Double.NaN;

plot hilowline1 = if bn < Max(lastzhibar, lastzlobar)
                  then Double.NaN
                  else if lastzlobar > lastzhibar and !IsNaN(zLO)
                  then zLO
                  else if lastzlobar < lastzhibar and !IsNaN(zHI)
                  then zHI else lastvalue;
hilowline1.EnableApproximation();
hilowline1.SetLineWeight(3);
hilowline1.SetDefaultColor(Color.WHITE);

def xxhigh  = if rh1 == high then high else xxhigh[1];
def xxlow   = if rl1 == low then low else xxlow[1];
def chghigh = (rh1 - xxlow[1]) / xxlow[1] * 100;
def chglow  = (rl1 - xxhigh[1]) / xxhigh[1] * 100;


AddChartBubble(!IsNaN(High1) and PointCount == 1, high, "H\n" + AsText(high) + " \n" +  Round(chghigh) + "%", if xxhigh > xxhigh[1] then Color.GREEN else Color.RED);
AddChartBubble(!IsNaN(Low1) and PointCount == -1, low, "L\n" + AsText(low) + " \n" +  Round(chglow) + "%", if xxlow > xxlow[1] then Color.GREEN else Color.RED, no);
#
 

SubZer0

New member
Hello SleepyZ, thank you for taking the time to make changes!

I tested your latest changes for #2 and #3. While it is not as noticeable on lower timeframes (such as M5), the last leg looks way off on the D1 timeframe. Below are some screenshots for SPY and AES (i see this on all tickers)
http://www.dreaminginpixels.com/download/ToS/2022-09-26_14-27-24.jpg
http://www.dreaminginpixels.com/download/ToS/2022-09-26_14-26-38.jpg


As for #1, this is what prompted me to reach out earlier. If there are two consecutive swing highs followed by a swing low and the first swing high is chosen, the trend look like this. I can see this on various timeframes. Circles are where the second swing point would be.
http://www.dreaminginpixels.com/download/ToS/2022-09-26_14-17-51.jpg
http://www.dreaminginpixels.com/download/ToS/2022-09-26_14-16-35.jpg
http://www.dreaminginpixels.com/download/ToS/2022-09-26_14-12-52.jpg

For now, i will revert back to your previous version. Thanks again, very much!
 

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