Repaints ZigZag High Low with Supply & Demand Zones for ThinkorSwim

Repaints
@chillc15 Per your request, I have modified @RobertPayne code to include SwingHigh points which are now plotted in CYAN with the swing high points painted in PINK. So now you have both swing high and low on your charts.

Code:
# Swing High and Swing Low
# tomsk
# 11.18.2019

# As requested by chillc15 I have modified [USER=1174]@RobertPayne[/USER] code to include SwingHigh
# points which are now plotted in CYAN with the swing high points painted in PINK.
# So now you have both swing high and low on your charts

#  +------------------------------------------------------------+
#  |  Example: How to extend levels to the right of the chart   |
#  |                        Robert Payne                        |
#  |               https://funwiththinkscript.com               |
#  +------------------------------------------------------------+

# SWING LOW

# define swing low points
input length = 10;
def bn = BarNumber();
def lastBar = HighestAll(if IsNaN(close) then 0 else bn);
def offset = Min(length - 1, lastBar - bn);
def swingLow = low < Lowest(low[1], length - 1) and low == GetValue(Lowest(low, length), -offset);

# identify the very last swing low point
def lowPointOneBarNumber = HighestAll(if swingLow then bn else 0);
def lowPointOneValue = if bn == lowPointOneBarNumber then low else lowPointOneValue[1];
plot low1 = if bn < lowPointOneBarNumber then Double.NaN else lowPointOneValue;
low1.SetDefaultColor(Color.RED);

# identify the 2nd to last swing low point
def lowPointTwoBarNumber = HighestAll(if swingLow and bn < lowPointOneBarNumber then bn else 0);
def lowPointTwoValue = if bn == lowPointTwoBarNumber then low else lowPointTwoValue[1];
plot low2 = if bn < lowPointTwoBarNumber then Double.NaN else lowPointTwoValue;
low2.SetDefaultColor(Color.RED);

# just keep doing ths for as many lines as you want to add to the chart
def lowPointThreeBarNumber = HighestAll(if swingLow and bn < lowPointTwoBarNumber then bn else 0);
def lowPointThreeValue = if bn == lowPointThreeBarNumber then low else lowPointThreeValue[1];
plot low3 = if bn < lowPointThreeBarNumber then Double.NaN else lowPointThreeValue;
low3.SetDefaultColor(Color.RED);

# SWING HIGH

# define swing high points
def swingHigh = high > Highest(high[1], length - 1) and high == GetValue(Highest(high, length), -offset);

# identify the very last swing high point
def highPointOneBarNumber = HighestAll(if swingHigh then bn else 0);
def highPointOneValue = if bn == highPointOneBarNumber then high else highPointOneValue[1];
plot high1 = if bn <  highPointOneBarNumber then Double.NaN else highPointOneValue;
high1.SetDefaultColor(Color.CYAN);

# identify the 2nd to last swing high point
def highPointTwoBarNumber = HighestAll(if swingHigh and bn < highPointOneBarNumber then bn else 0);
def highPointTwoValue = if bn == highPointTwoBarNumber then high else highPointTwoValue[1];
plot high2 = if bn < highPointTwoBarNumber then Double.NaN else highPointTwoValue;
high2.SetDefaultColor(Color.CYAN);

# just keep doing ths for as many lines as you want to add to the chart
def highPointThreeBarNumber = HighestAll(if swingHigh and bn < highPointTwoBarNumber then bn else 0);
def highPointThreeValue = if bn == highPointThreeBarNumber then high else highPointThreeValue[1];
plot high3 = if bn < highPointThreeBarNumber then Double.NaN else highPointThreeValue;
high3.SetDefaultColor(Color.CYAN);

# ADJUST CANDLE COLORS

# change candle colors just to make it easier to see what we are working with
AssignPriceColor(if swingLow then Color.LIME else if swingHigh then Color.PINK else Color.DARK_GRAY);
# End Swing High and Swing Low
 
WOW! Thanks @tomsk very much appreciated.

If you don’t mind I have a few questions that will help me learn and maybe others.

Obviously this swing-high-swing- low is similar to the Trend Reversal indicator as discussed on here and also the Zig-Zag.

As I watched Roberts swing-low this is a repainter as it waits to determine its bottom. With the addition of the swing-high I assume it will repaint as well until the top is determined as well.

The burning question is what condition in the code eventually keeps the bar locked in either high or low and it will no longer repaint.

Is it a certain amount of bars looking back?

For a swing-high does there have to be a certain amount of down bars before it locks itself in? Vice-versa at the low.

I know there are some zig-zag indicators using an ATR reversal factor and other conditions. Is it possible to implement this swing-high-swing-low into a zig-zag as well?

If you are open I can send you a zig-zag I have been working with to see if they could be implemented together.

Thanks again

For those that may be visual like me you can adjust the colors of the candles if you like. Here is another version.

Code:
# ADJUST CANDLE COLORS
# change candle colors just to make it easier to see what we are working with
AssignPriceColor(if swingLow then Color.YELLOW else if swingHigh then Color.BLUE else Color.CURRENT);
 
@chillc15 This study was originally designed by @RobertPayne so if you have any specific queries regarding the design of the code you might like to check with him. I essentially added the associated swing high logic based on Robert's underlying design

My understanding is that whenever a new swing low is detected, the code notes the price together with the bar number at that point
If the bar number that is being processed matches the bar number that had earlier been noted, then a line is plotted. Ditto for the swing high code
 
Can anyone explain to me how to extend the previous valley past each new valley as a dashed line like in @RobertPayne original version here using the code he posted above instead of the original?

Code:
# define swing low points
input length = 10;
def bn = BarNumber();
def lastBar = HighestAll(if IsNaN(close) then 0 else bn);
def offset = Min(length - 1, lastBar - bn);
def swingLow = low < Lowest(low[1], length - 1) and low == GetValue(Lowest(low, length), -offset);
# change candle colors just to make it easier to see what we are working with
AssignPriceColor(if swingLow then Color.LIME else Color.DARK_GRAY);

# using a single plot to show the swing low points
def l1 = if swingLow then low else l1[1];
plot low1 = l1;
low1.SetDefaultColor(Color.RED);
low1.SetPaintingStrategy(12);
 
@tenacity11 Sure thing, here is your customized scan code to scan for a swing low.
I have just tested this scanning against the S&P 500 with the following results:

Daily - 19 hits
15m - 15 hits
60m - 6 hits

Code:
# Swing Low Scan
# tomsk
# 12.1.2019

# I have modified @RobertPayne code to scan for the most recent swing low
#
#  +------------------------------------------------------------+
#  |  Example: How to extend levels to the right of the chart   |
#  |                        Robert Payne                        |
#  |               https://funwiththinkscript.com               |
#  +------------------------------------------------------------+

input length = 10;

def bn = BarNumber();
def lastBar = HighestAll(if IsNaN(close) then 0 else bn);
def offset = Min(length - 1, lastBar - bn);
def swingLow = low < Lowest(low[1], length - 1) and low == GetValue(Lowest(low, length), -offset);
def lowPointOneBarNumber = HighestAll(if swingLow then bn else 0);
plot scan = bn == lowPointOneBarNumber;
# End Swing Low Scan
 
Last edited:
Can you provide the correct code to show ONLY the supply and demand zones without any fib indications, as shown in your initial top pic? Thanks.
 
@lazeelink I had actually worked with this code several years ago when ZZZ/Lar first posted it to the ThinkScript lounge. It looked like a solid piece of code with lots of bells and whistles. Being intrigued I sectionalized the study, and rearranged the flow of some of the ZZ Logic in order to digest it better.

Since you do not wish to see any of the fib indications, the following version had all the fib related code taken out. Not sure what your intention is but you could very easily have set the user input showFibExtLines to "no" and the fib lines will not be plotted. At any rate, here's the study I worked on last night following your request complete with change log. Have fun!

Code:
# ZigZag High Low Supply Demand
# ZigZag High Low modified in part by Linus' and Lar's code
# ZZZ, with modifications by tomsk
# 1.12.2020

# https://usethinkscript.com/threads/zigzag-high-low-with-supply-demand-zones-for-thinkorswim.172/page-2#post-13790

# V1.0 - 10.09.2016 - ZZZ   - Initial release of ZigZag High Low Supply Demand
# V1.1 - 06.28.2017 - tomsk - Sectionalized code, and rearranged flow of some ZZ Logic
# V1.2 - 01.12.2020 - tomsk - Removed Fibonacci related code per user request

input showBubblesChange = yes;  # Price Change between Zigzags
input showBubblesPrice = no;    # Price at Zigzag High/Low
input showBubblesBarCount = no; # Bar Count between Zigzags
input showBubblesVolume = no;   # Volume at Zigzag Reversals

input showArrows = no;
input useAlerts = no;
input numberSuppDemandToShow = 2;
input showSupplyDemand = {default Pivot, Arrow, None};
input showSupplyDemandCloud = yes;

input BubbleOffset = .0005;
input PercentAmount = .01;
input RevAmount = .15;
input ATRreversal = 1.0;
input ATRlength = 5;

def zz = ZigZagHighLow("price h" = high, "price l" = low, "percentage reversal" = PercentAmount, 
"absolute reversal" = RevAmount, "atr length" = ATRlength, "atr reversal" = ATRreversal);

def ReversalAmount = if (close * PercentAmount / 100) > Max(RevAmount < ATRreversal * ATRlength, RevAmount) 
                     then (close * PercentAmount / 100) 
                     else if RevAmount < ATRreversal * ATRlength 
                          then ATRreversal * ATRlength 
                          else RevAmount;
# Zig Zag Specific Data

def zzSave = if !IsNaN(zz) then zz else GetValue(zzSave, 1);
def chg = (if zzSave == high then high else low) - GetValue(zzSave, 1);
def isUp = chg >= 0;
def isConf = AbsValue(chg) >= ReversalAmount or (IsNaN(GetValue(zz, 1)) and GetValue(isConf, 1));

# Price Change Between ZigZags

def xxHigh = if zzSave == high then high else xxHigh[1];
def chgHigh = high - xxHigh[1];
def xxLow = if zzSave == low then low else xxLow[1];
def chgLow = low - xxLow[1];

# Bar Count Between ZigZags

def zzCount = if zzSave[1] != zzSave then 1 else if zzSave[1] == zzSave then zzCount[1] + 1 else 0;
def zzCountHiLo = if zzCountHiLo[1] == 0 and (zzSave == high or zzSave == low) then 1 
                  else if zzSave == high or zzSave == low then zzCountHiLo[1] + 1 
                  else zzCountHiLo[1];
def zzHiLo = if zzSave == high or zzSave == low then zzCountHiLo else zzCountHiLo + 1;
def zzCountHigh = if zzSave == high then zzCount[1] else Double.NaN;
def zzCountLow  = if zzSave == low then zzCount[1] else Double.NaN;

# Volume at Reversals

def vol = if BarNumber() == 0 then 0 else volume + vol[1];
def vol1 = if BarNumber() == 1 then volume else vol1[1];
def xxVol = if zzSave == high or zzSave == low then TotalSum(volume) else xxVol[1];
def chgVol =  if xxVol - xxVol[1] + vol1 == vol then vol else xxVol - xxVol[1];

# Label for Confirmed/Unconfirmed Status of Current Zigzag

AddLabel(BarNumber() != 1, (if isConf then "Confirmed " else "Unconfirmed ") + "ZigZag: " + chg, 
    if !isConf then Color.Dark_Orange else if isUp then Color.Green else Color.Red);

# Zig Zag Plot

plot zzp = if isUp <= 1 then zz else Double.NaN;
zzp.AssignValueColor(if isUp then Color.Green else if !isUp then Color.Red else Color.Dark_Orange);
zzp.SetStyle(Curve.FIRM);
zzp.EnableApproximation();

# Bubbles

# Price Change between Zigzags

AddChartBubble(showBubblesChange and !IsNaN(zz) and BarNumber() != 1, if isUp then high * (1 + BubbleOffset) else low * (1 - BubbleOffset), "$" + chg, if isUp and chgHigh > 0 then Color.Green else if isUp and chgHigh < 0 then Color.Red else if isUp then Color.Yellow else if !isUp and chgLow > 0 then Color.Green else if !isUp and chgLow < 0 then Color.Red else Color.Yellow, isUp);

# Price at Zigzag High/Low

AddChartBubble(showBubblesPrice and !IsNaN(zz) and BarNumber() != 1, if isUp then high * (1 + BubbleOffset) else low * (1 - BubbleOffset), if isUp then "$" + high else "$" + low , if isUp and chgHigh > 0 then Color.Green else if isUp and chgHigh < 0 then Color.Red else if isUp then Color.Yellow else if !isUp and chgLow > 0 then Color.Green else if !isUp and chgLow < 0 then Color.Red else Color.Yellow, isUp);

# Bar Count between Zigzags

AddChartBubble(showBubblesBarCount and !IsNaN(zz) and BarNumber() != 1, if isUp then high * (1 + BubbleOffset) else low * (1 - BubbleOffset), if zzSave == high then zzCountHigh else zzCountLow, if isUp and chgHigh > 0 then Color.Green else if isUp and chgHigh < 0 then Color.Red else if isUp then Color.Yellow else if !isUp and chgLow > 0 then Color.Green else if !isUp and chgLow < 0 then Color.Red else Color.Yellow, if isUp then yes else no );

# Volume at Zigzag Reversals

AddChartBubble(showBubblesVolume and !IsNaN(zz) and BarNumber() != 1, if isUp then high * (1 + bubbleoffset) else low * (1 - bubbleoffset), chgVol,if isUp and chghigh > 0 then Color.Green else if isUp and chghigh < 0 then Color.Red else if isUp then Color.Yellow else if !isUp and chglow > 0 then Color.Green else if !isUp and chglow < 0 then Color.Red else Color.Yellow, if isUp then yes else no );

# Arrows

def zzL = if !IsNaN(zz) and !isUp then low else GetValue(zzL, 1);
def zzH = if !IsNaN(zz) and isUp then high else GetValue(zzH, 1);

def dir = CompoundValue(1, if zzL != zzL[1] or low == zzL[1] and low == zzSave then 1 
                           else if zzH != zzH[1] or high == zzH[1] and high == zzSave then -1 
                           else dir[1], 0);

def signal = CompoundValue(1, if dir > 0 and low > zzL 
                              then if signal[1] <= 0 then 1 else signal[1] 
                              else if dir < 0 and high < zzH 
                                   then if signal[1] >= 0 then -1 else signal[1] 
                                   else signal[1], 0);

plot U1 = showArrows and signal > 0 and signal[1] <= 0;
U1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
U1.SetDefaultColor(Color.Green);
U1.SetLineWeight(4);

plot D1 = showArrows and signal < 0 and signal[1] >= 0;
D1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
D1.SetDefaultColor(Color.Red);
D1.SetLineWeight(4);

# Alerts

Alert(useAlerts and U1, "ZIG-UP", Alert.BAR, Sound.Bell);
Alert(useAlerts and D1, "ZAG-DOWN", Alert.BAR, Sound.Chimes);

# Supply Demand Areas

def data1 = CompoundValue(1, if (zzSave == high or zzSave == low) then data1[1] + 1 else data1[1], 0);
def dataCount1 = (HighestAll(data1) - data1[1]);
def idx = showSupplyDemand == showSupplyDemand.Pivot;

def rLow;
def rHigh;
if signal crosses 0 {
    rLow = low[idx];
    rHigh = high[idx];
} else {
    rLow = rLow[1];
    rHigh = rHigh[1];
}

plot HighLine = if dataCount1 <= numberSuppDemandToShow and 
                   showSupplyDemand != showSupplyDemand.None and 
                   !isNaN(close) and 
                   rHigh != 0 
                then rHigh 
                else Double.NaN;
HighLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
HighLine.AssignValueColor(if signal > 0 then Color.Green else Color.Red);

plot LowLine = if dataCount1 <= numberSuppDemandToShow and 
                  showSupplyDemand != showSupplyDemand.None and 
                  !isNaN(close) and 
                  rLow != 0 
               then rLow 
               else Double.NaN;
LowLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LowLine.AssignValueColor(if signal > 0 then Color.Green else Color.Red);

def hlUp = if signal > 0 then HighLine else Double.NaN;
def hlDn = if signal < 0 then HighLine else Double.NaN;

AddCloud(if showSupplyDemandCloud then hlUp else Double.NaN, LowLine, Color.Light_Green, Color.Light_Green);
AddCloud(if showSupplyDemandCloud then hlDn else Double.NaN, LowLine, Color.Light_Red, Color.Light_Red);

# End ZigZag High Low Supply Demand
 
Last edited:
Not being familiar with scripting I wasn't sure what to do but your explanation clarified quite a bit. Your modified script along with a few other changes following your instructions has given me exactly what I want. Thank you for taking the time to respond.
 
Anyone have a scan for the following indicator:

Code:
# Swing High and Swing Low
# tomsk
# 11.18.2019
# As requested by chillc15 I have modified [USER=1174]@RobertPayne[/USER] code to include SwingHigh
# points which are now plotted in CYAN with the swing high points painted in PINK.
# So now you have both swing high and low on your charts
#  +------------------------------------------------------------+
# | Example: How to extend levels to the right of the chart |
# | Robert Payne |
# | https://funwiththinkscript.com |
#  +------------------------------------------------------------+
# SWING LOW
# define swing low points
input length = 10;
def bn = BarNumber();
def lastBar = HighestAll(if IsNaN(close) then 0 else bn);
def offset = Min(length - 1, lastBar - bn);
def swingLow = low < Lowest(low[1], length - 1) and low == GetValue(Lowest(low, length), -offset);
# identify the very last swing low point
def lowPointOneBarNumber = HighestAll(if swingLow then bn else 0);
def lowPointOneValue = if bn == lowPointOneBarNumber then low else lowPointOneValue[1];
plot low1 = if bn < lowPointOneBarNumber then Double.NaN else lowPointOneValue;
low1.SetDefaultColor(Color.LIGHT_RED);
# identify the 2nd to last swing low point
def lowPointTwoBarNumber = HighestAll(if swingLow and bn < lowPointOneBarNumber then bn else 0);
def lowPointTwoValue = if bn == lowPointTwoBarNumber then low else lowPointTwoValue[1];
plot low2 = if bn < lowPointTwoBarNumber then Double.NaN else lowPointTwoValue;
low2.SetDefaultColor(Color.Light_RED);
# just keep doing ths for as many lines as you want to add to the chart
# identify then 3rd to last swingHigh point low
def lowPointThreeBarNumber = HighestAll(if swingLow and bn < lowPointTwoBarNumber then bn else 0);
def lowPointThreeValue = if bn == lowPointThreeBarNumber then low else lowPointThreeValue[1];
plot low3 = if bn < lowPointThreeBarNumber then Double.NaN else lowPointThreeValue;
low3.SetDefaultColor(Color.Light_RED);
# identify then 4th to last swingHigh point low
def lowPointFourBarNumber = HighestAll(if swingLow and bn < lowPointThreeBarNumber then bn else 0);
def lowPointFourValue = if bn == lowPointFourBarNumber then low else lowPointFourValue[1];
plot low4 = if bn < lowPointFourBarNumber then Double.NaN else lowPointFourValue;
low4.SetDefaultColor(Color.Light_RED);
# identify then 5th to last swingHigh point low
def lowPointFiveBarNumber = HighestAll(if swingLow and bn < lowPointFourBarNumber then bn else 0);
def lowPointFiveValue = if bn == lowPointFiveBarNumber then low else lowPointFiveValue[1];
plot low5 = if bn < lowPointFiveBarNumber then Double.NaN else lowPointFiveValue;
low5.SetDefaultColor(Color.Light_RED);
# identify then 6th to last swingHigh point low
def lowPointSixBarNumber = HighestAll(if swingLow and bn < lowPointFiveBarNumber then bn else 0);
def lowPointSixValue = if bn == lowPointSixBarNumber then low else lowPointSixValue[1];
plot low6 = if bn < lowPointSixBarNumber then Double.NaN else lowPointSixValue;
low6.SetDefaultColor(Color.Light_RED);
# identify then 7th to last swingHigh point low
def lowPointSevenBarNumber = HighestAll(if swingLow and bn < lowPointSixBarNumber then bn else 0);
def lowPointSevenValue = if bn == lowPointSevenBarNumber then low else lowPointSevenValue[1];
plot low7 = if bn < lowPointSevenBarNumber then Double.NaN else lowPointSevenValue;
low7.SetDefaultColor(Color.Light_RED);
# identify then 8th to last swingHigh point low
def lowPointEightBarNumber = HighestAll(if swingLow and bn < lowPointSevenBarNumber then bn else 0);
def lowPointEightValue = if bn == lowPointEightBarNumber then low else lowPointEightValue[1];
plot low8 = if bn < lowPointEightBarNumber then Double.NaN else lowPointEightValue;
low8.SetDefaultColor(Color.Light_RED);
# identify then 9th to last swingHigh point low
def lowPointNineBarNumber = HighestAll(if swingLow and bn < lowPointEightBarNumber then bn else 0);
def lowPointNineValue = if bn == lowPointNineBarNumber then low else lowPointNineValue[1];
plot low9 = if bn < lowPointNineBarNumber then Double.NaN else lowPointNineValue;
low9.SetDefaultColor(Color.Light_RED);
# identify then 10th to last swingHigh point low
def lowPointTenBarNumber = HighestAll(if swingLow and bn < lowPointNineBarNumber then bn else 0);
def lowPointTenValue = if bn == lowPointTenBarNumber then low else lowPointTenValue[1];
plot low10 = if bn < lowPointTenBarNumber then Double.NaN else lowPointTenValue;
low10.SetDefaultColor(Color.Light_RED);


# SWING HIGH
# define swing high points
def swingHigh = high > Highest(high[1], length - 1) and high == GetValue(Highest(high, length), -offset);
# identify the very last swing high point
def highPointOneBarNumber = HighestAll(if swingHigh then bn else 0);
def highPointOneValue = if bn == highPointOneBarNumber then high else highPointOneValue[1];
plot high1 = if bn < highPointOneBarNumber then Double.NaN else highPointOneValue;
high1.SetDefaultColor(Color.CYAN);
# identify the 2nd to last swing high point
def highPointTwoBarNumber = HighestAll(if swingHigh and bn < highPointOneBarNumber then bn else 0);
def highPointTwoValue = if bn == highPointTwoBarNumber then high else highPointTwoValue[1];
plot high2 = if bn < highPointTwoBarNumber then Double.NaN else highPointTwoValue;
high2.SetDefaultColor(Color.CYAN);
# just keep doing ths for as many lines as you want to add to the chart
def highPointThreeBarNumber = HighestAll(if swingHigh and bn < highPointTwoBarNumber then bn else 0);
def highPointThreeValue = if bn == highPointThreeBarNumber then high else highPointThreeValue[1];
plot high3 = if bn < highPointThreeBarNumber then Double.NaN else highPointThreeValue;
high3.SetDefaultColor(Color.CYAN);
def highPointFourBarNumber = HighestAll(if swingHigh and bn < highPointThreeBarNumber then bn else 0);
def highPointFourValue = if bn == highPointFourBarNumber then high else highPointFourValue[1];
plot high4 = if bn < highPointFourBarNumber then Double.NaN else highPointFourValue;
high4.SetDefaultColor(Color.CYAN);
def highPointFiveBarNumber = HighestAll(if swingHigh and bn < highPointFourBarNumber then bn else 0);
def highPointFiveValue = if bn == highPointFiveBarNumber then high else highPointFiveValue[1];
plot high5 = if bn < highPointFiveBarNumber then Double.NaN else highPointFiveValue;
high5.SetDefaultColor(Color.CYAN);
def highPointSixBarNumber = HighestAll(if swingHigh and bn < highPointFiveBarNumber then bn else 0);
def highPointSixValue = if bn == highPointSixBarNumber then high else highPointSixValue[1];
plot high6 = if bn < highPointsixBarNumber then Double.NaN else highPointsixValue;
high6.SetDefaultColor(Color.CYAN);
def highPointSevenBarNumber = HighestAll(if swingHigh and bn < highPointSixBarNumber then bn else 0);
def highPointSevenValue = if bn == highPointSevenBarNumber then high else highPointSevenValue[1];
plot high7 = if bn < highPointSevenBarNumber then Double.NaN else highPointSevenValue;
high7.SetDefaultColor(Color.CYAN);
def highPointEightBarNumber = HighestAll(if swingHigh and bn < highPointSevenBarNumber then bn else 0);
def highPointEightValue = if bn == highPointEightBarNumber then high else highPointEightValue[1];
plot high8 = if bn < highPointEightBarNumber then Double.NaN else highPointEightValue;
high4.SetDefaultColor(Color.CYAN);
def highPointNineBarNumber = HighestAll(if swingHigh and bn < highPointEightBarNumber then bn else 0);
def highPointNineValue = if bn == highPointNineBarNumber then high else highPointNineValue[1];
plot high9 = if bn < highPointNineBarNumber then Double.NaN else highPointNineValue;
high4.SetDefaultColor(Color.CYAN);
def highPointTenBarNumber = HighestAll(if swingHigh and bn < highPointNineBarNumber then bn else 0);
def highPointTenValue = if bn == highPointTenBarNumber then high else highPointTenValue[1];
plot high10 = if bn < highPointTenBarNumber then Double.NaN else highPointTenValue;
high4.SetDefaultColor(Color.CYAN);

# ADJUST CANDLE COLORS
# change candle colors just to make it easier to see what we are working with
AssignPriceColor(if swingLow then Color.cyan else if swingHigh then Color.mageNTA else Color.current);
# End Swing High and Swing Low
The scan would be when it is a swing low.
 
Last edited by a moderator:
I need help please!!
I'd like to make a zigzag indicator that indetify high and low of the last 5 bar. Every time the market makes a new high or new low, the ZigZag deletes its last drawing and take last value..........How I do that?

Code:
def swinghigh = if high > high[1] and high > high[2] and high > high[3] and high > high[4] and high > high[5] then 1 else 0;
def swinglow = if low < low[1] and low < low[2] and low < low[3] and low < low[4] and low < low[5] then 1 else 0;
plot sh = if swinghigh then high else Double.NaN;
sh.SetStyle(Curve.POINTS);
plot sl = if swinglow then low else Double.NaN;
sl.SetStyle(Curve.POINTS);

gcnLUGr.jpg
 
Last edited:
Hi, Could someone help me convert this PineScript to TOS please? It is a version of ZigZags High Low is the only one I found with the reversion I need.

Code:
study("Zig Zag High Low", overlay = true)
length = input(4, title = "High/Low length")
h = highest(high, length * 2 + 1)
l = lowest(low, length * 2 + 1)
f_isMin(len) =>
    l == low[len]
f_isMax(len) =>
    h == high[len]

var dirUp = false
var lastLow = high * 100
var lastHigh = 0.0
var timeLow = bar_index
var timeHigh = bar_index
var line li = na
f_drawLine() =>
    _li_color = dirUp ? color.teal : color.orange
    line.new(
         timeHigh - length, lastHigh,
         timeLow - length, lastLow,
         xloc.bar_index, color=_li_color, width=2
         )

if dirUp
    if (f_isMin(length) and low[length] < lastLow)
        lastLow := low[length]
        timeLow := bar_index
        line.delete(li)
        li := f_drawLine()

    if (f_isMax(length) and high[length] > lastLow)
        lastHigh := high[length]
        timeHigh := bar_index
        dirUp := false
        li := f_drawLine()

if not dirUp
    if (f_isMax(length) and high[length] > lastHigh)
        lastHigh := high[length]
        timeHigh := bar_index
        line.delete(li)
        li := f_drawLine()
    if f_isMin(length) and low[length] < lastHigh
        lastLow := low[length]
        timeLow := bar_index
        dirUp := true
        li := f_drawLine()
        if (f_isMax(length) and high[length] > lastLow)
            lastHigh := high[length]
            timeHigh := bar_index
            dirUp := false
            li := f_drawLine()
 
Here's one from legendary,.....zzz, blt,...we miss your creations....https://tos.mx/kyCs9yK
 
@BenTen This is great work! How can this be modified to work with FOREX pairs? It's only working on USD/JPY. On any other currency, the zigzags seem off and the supply and demand is not showing.
 
Last edited:
@BenTen
I think it could help me to solve my problem if you explained to me how this code works to plot zigzag (which is the one that I take as a base to draw the one I did)

Code:
def barNumber = BarNumber();
def barCount = HighestAll(If(IsNaN(priceH), 0, barNumber));
def newState = GetValue(state, 0) != GetValue(state, 1);
def offset = barCount - barNumber + 1;
def highPoint = state == state.uptrend and priceH == maxPriceH;
def lowPoint = state == state.downtrend and priceL == minPriceL;

def lastH;
if highPoint and offset > 1 {
    lastH = fold iH = 1 to offset with tH = priceH while !IsNaN(tH) and !GetValue(newState, -iH) do if GetValue(newMax, -iH) or iH == offset - 1 and GetValue(priceH, -iH) == tH then Double.NaN else tH;
} else {
    lastH = Double.NaN;
}

def lastL;
if lowPoint and offset > 1 {
    lastL = fold iL = 1 to offset with tL = priceL while !IsNaN(tL) and !GetValue(newState, -iL) do if GetValue(newMin, -iL) or iL == offset - 1 and GetValue(priceL, -iL) == tL then Double.NaN else tL;
} else {
    lastL = Double.NaN;
}

plot ZZ;
if barNumber == 1 {
    ZZ = fold iF = 1 to offset with tP = Double.NaN while IsNaN(tP) do if GetValue(state, -iF) == GetValue(state.uptrend, 0) then priceL else if GetValue(state, -iF) == GetValue(state.downtrend, 0) then priceH else Double.NaN;
} else if barNumber == barCount {
    ZZ = if highPoint or state == state.downtrend and priceL > minPriceL then priceH else if lowPoint or state == state.uptrend and priceH < maxPriceH then priceL else Double.NaN;
} else {
    ZZ = if !IsNaN(lastH) then lastH else if !IsNaN(lastL) then lastL else Double.NaN;
}
ZZ.SetDefaultColor(GetColor(1));
ZZ.EnableApproximation();


specifically what this part calculates:

Code:
def barNumber = BarNumber();

def barCount = HighestAll(If(IsNaN(priceH), 0, barNumber));
 
Hello all I am requesting a new indicator, or rather a feature to be added to an old indicator. As we all know the Zig Zag High Low indicator repaints. What I would like to see added, if possible, is to show where the indicator repaints. Just something showing where the zig zag has changed. If not possible are their other indicators that do something similar to what I have described. Thank you in advance.
 
@Tintin I'm not even sure if that is possible. I would love to see a non-repainting version of the ZigZag high & low as well.
 

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

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
308 Online
Create Post

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