ZigZag Scan

babayaga_310

New member
VIP
Hello,
Hoping someone here could help me develop a scanner based off an existing indicator native to ToS, ZigZagHighLow.

I would like to develop a stock scanner to identify when the support to the high is lost or when the resistance to the low is gained.
  • If the market is trending down you would have a swing low in price (point A), usually followed by a lower high and a small trend up (point B, or resistance to the low), then what is made is another swing low which creates a lower low (point C). When the market begins a reversal, I would want the scanner to detect when the price manages to close back above point B (resistance to the low).
  • Same goes for the opposite: if the market is trending up you would have a swing high in price (point A), usually followed by a higher low and a small trend (point B, or support to the high), then what is made is another swing high which creates a higher high (point C). when the market begins a reversal, I would want the scanner to detect when the price manages to close back below point B (support to the high).

The settings I'm using right now for the ZigZagHighLow indicator are as follows:
  • price h = HIGH
  • price l = LOW
  • percentage reversal = 0.0
  • absolute reversal = 0.0
  • atr length = 1
  • atr reversal = 1.0
  • tick reversal = 0
 
Solution
Here's where I'm at currently.

Can you play around with it and let me know what else you'd like to see with this ZZHL pattern identification study?

Code:
## ZZHL Pattern Identification ###
## by whoDAT

## Standard ZZHL ###

input priceH = high;
input priceL = low;
input percentageReversal = 0.0;
input absoluteReversal = 0.0;
input atrLength = 1;
input atrReversal = 1.0;
input tickReversal = 0;

Assert(percentageReversal >= 0, "'percentage reversal' must not be negative: " + percentageReversal);
Assert(absoluteReversal >= 0, "'absolute reversal' must not be negative: " + absoluteReversal);
Assert(atrReversal >= 0, "'atr reversal' must not be negative: " + atrReversal);
Assert(tickReversal >= 0, "'ticks' must not be negative: " +...
Hello,
Hoping someone here could help me develop a scanner based off an existing indicator native to ToS, ZigZagHighLow.


Sadly no.
Repainters are not used in scans, watchlists, strategies, or conditional orders because they repaint.
Scanners work in real time
Those perfect highs and lows may have been painted on many many bars back.
The ones painted on the current candle will be erased and repainted multiple times before it decides on a final repaint.
read more:
https://usethinkscript.com/threads/answers-to-commonly-asked-questions.6006/#post-57833
 
Last edited:
Hello,
Hoping someone here could help me develop a scanner based off an existing indicator native to ToS, ZigZagHighLow.

I would like to develop a stock scanner to identify when the support to the high is lost or when the resistance to the low is gained.
  • If the market is trending down you would have a swing low in price (point A), usually followed by a lower high and a small trend up (point B, or resistance to the low), then what is made is another swing low which creates a lower low (point C). When the market begins a reversal, I would want the scanner to detect when the price manages to close back above point B (resistance to the low).
  • Same goes for the opposite: if the market is trending up you would have a swing high in price (point A), usually followed by a higher low and a small trend (point B, or support to the high), then what is made is another swing high which creates a higher high (point C). when the market begins a reversal, I would want the scanner to detect when the price manages to close back below point B (support to the high).

The settings I'm using right now for the ZigZagHighLow indicator are as follows:
  • price h = HIGH
  • price l = LOW
  • percentage reversal = 0.0
  • absolute reversal = 0.0
  • atr length = 1
  • atr reversal = 1.0
  • tick reversal = 0
Babayaga.

I'm working on a non-repainting zig-zag that follows ZZHL pretty well. I am willing to test this indicator on your project. I'll release everything here as public info.

Are you sure on those ZZHL settings? Settings like that will produce a signal nearly every other tick. What timeframe are you using? Which stocks are you following?
 
Hello,
Hoping someone here could help me develop a scanner based off an existing indicator native to ToS, ZigZagHighLow.

I would like to develop a stock scanner to identify when the support to the high is lost or when the resistance to the low is gained.
  • If the market is trending down you would have a swing low in price (point A), usually followed by a lower high and a small trend up (point B, or resistance to the low), then what is made is another swing low which creates a lower low (point C). When the market begins a reversal, I would want the scanner to detect when the price manages to close back above point B (resistance to the low).
  • Same goes for the opposite: if the market is trending up you would have a swing high in price (point A), usually followed by a higher low and a small trend (point B, or support to the high), then what is made is another swing high which creates a higher high (point C). when the market begins a reversal, I would want the scanner to detect when the price manages to close back below point B (support to the high).

The settings I'm using right now for the ZigZagHighLow indicator are as follows:
  • price h = HIGH
  • price l = LOW
  • percentage reversal = 0.0
  • absolute reversal = 0.0
  • atr length = 1
  • atr reversal = 1.0
  • tick reversal = 0
you rules are too vague.
what defines a trend up , down?
no idea where your points are , a,b,c,
tell us where the highs and lows are in relation to other candles over the 3 or 4 candles of this pattern.

what if you just used a fractal study instead?
 
Babayaga.

I'm working on a non-repainting zig-zag that follows ZZHL pretty well. I am willing to test this indicator on your project. I'll release everything here as public info.

Are you sure on those ZZHL settings? Settings like that will produce a signal nearly every other tick. What timeframe are you using? Which stocks are you following?
Thanks @whoDAT , that's awesome to hear! Those are the settings I'm using, the reason being is it's seems be the most similar to an indicator that I have in Tradingview/Ninjatrader. I would say it's about 60% accurate, seems to draw more trends than expected but identifies WHEN a trend start is forming.

Primary timeframe I'm using for stocks is 4H/D (with visual validation from W/M timeframes), primary timeframe I use for futures/forex trading is 4H/15min (with visual validation from 4H/D/W timeframes).But I would validate this on stocks due to higher sample size, not biased towards any stocks specifically (exclude OTC's) as I focus more on finding that setup.
 
you rules are too vague.
what defines a trend up , down?
no idea where your points are , a,b,c,
tell us where the highs and lows are in relation to other candles over the 3 or 4 candles of this pattern.

what if you just used a fractal study instead?
Here would be the breakdown of when the setup would be valid for either direction:

  • If the market is trending up you would have a swing high in price (point A), usually followed by a higher low and a small trend (point B, or support to the high), then what is made is another swing high which creates a higher high (point C). when the market begins a reversal, I would want the scanner to detect when the price manages to close back below point B (support to the high).
  • Same goes for the opposite: If the market is trending down you would have a swing low in price (point A), usually followed by a lower high and a small trend (point B, or support to the low), then what is made is another swing low which creates a lower low (point C). When the market begins a reversal, I would want the scanner to detect when the price manages to close back above point B (support to the low).
 

Attachments

  • Screenshot 2025-06-09 121424.png
    Screenshot 2025-06-09 121424.png
    49.9 KB · Views: 40
Thanks @whoDAT , that's awesome to hear! Those are the settings I'm using, the reason being is it's seems be the most similar to an indicator that I have in Tradingview/Ninjatrader. I would say it's about 60% accurate, seems to draw more trends than expected but identifies WHEN a trend start is forming.

Primary timeframe I'm using for stocks is 4H/D (with visual validation from W/M timeframes), primary timeframe I use for futures/forex trading is 4H/15min (with visual validation from 4H/D/W timeframes).But I would validate this on stocks due to higher sample size, not biased towards any stocks specifically (exclude OTC's) as I focus more on finding that setup.
For example. Here is an image of the non-repainting zig-zag (yellow) compared to ZigZagHighLow (blue). This is a 15 minute period for forex pair AUD/USD.

The ZZHL settings here are:
ATR_Reversal = 2.0
ZZ_Rev_Percent = 0.01
ZZ_ATR_Length = 5

ZZ3.jpg



These ZZHL settings yield about 4 trades (reversals) per day. These settings for ZZHL produced 19 trades in the past 5 days. ZZHL (after repainting) gave 100% success on the signals. Producing about $420 profit on $200 trading in the 5 days (with leverage).

The non-repainting zig-zag I'm describing also produced 19 trades in the past 5 days. This indicator gave 74% success rate. It produced almost $200 profit on $200 traded in the past 5 days (with leverage).

ZZ1.jpg
 
Last edited:
For example. Here is an image of the non-repainting zig-zag (yellow) compared to ZigZagHighLow (blue). This is a 15 minute period for forex pair AUD/USD.

The ZZHL settings here are:
ATR_Reversal = 2.0
ZZ_Rev_Percent = 0.01
ZZ_ATR_Length = 5

View attachment 24928


These ZZHL settings yield about 4 trades (reversals) per day. These settings for ZZHL produced 19 trades in the past 5 days. ZZHL (after repainting) gave 100% success on the signals. Producing about $420 profit on $200 trading in the 5 days (with leverage).

The non-repainting zig-zag I'm describing also produced 19 trades in the past 5 days. This indicator gave 74% success rate. It produced almost $200 profit on $200 traded in the past 5 days (with leverage).

View attachment 24930
That's both a great win rate as well as R:R, congratulations! Blue signals definitely seem to be cleaner, less trades = less risk.

To clarify below, which one produced 4 trades per day?
"These ZZHL settings yield about 4 trades (reversals) per day."

Have you backtested on futures yet?
 
Thanks @whoDAT , that's awesome to hear! Those are the settings I'm using, the reason being is it's seems be the most similar to an indicator that I have in Tradingview/Ninjatrader. I would say it's about 60% accurate, seems to draw more trends than expected but identifies WHEN a trend start is forming.

Primary timeframe I'm using for stocks is 4H/D (with visual validation from W/M timeframes), primary timeframe I use for futures/forex trading is 4H/15min (with visual validation from 4H/D/W timeframes).But I would validate this on stocks due to higher sample size, not biased towards any stocks specifically (exclude OTC's) as I focus more on finding that setup.
When I use your ZZHL settings on 15 min, AUD/USD, I get the following chart. This seems like its going to produce way too many signals. I believe this is too many signals, as it causes you to chase every order, all day.

zz4.jpg


Zooming in we can finally see the zigs and zags.

zz5.jpg
 
When I use your ZZHL settings on 15 min, AUD/USD, I get the following chart. This seems like its going to produce way too many signals. I believe this is too many signals, as it causes you to chase every order, all day.

View attachment 24931

Zooming in we can finally see the zigs and zags.

View attachment 24932
These screenshots tracks more to the strategy I use, focusing less so on the indicator showing when to trade but more of visualization of immediate price action trends, gaining and losing levels, identifying pivot points, etc.

The crossover of the blue line is the trigger to trade, is that correct?

Below is indicator in action in TV just for reference (15min of AUDUSD):
1749526166199.png
 
That's both a great win rate as well as R:R, congratulations! Blue signals definitely seem to be cleaner, less trades = less risk.

To clarify below, which one produced 4 trades per day?
"These ZZHL settings yield about 4 trades (reversals) per day."

Have you backtested on futures yet?
Both indicators had 19 trades in the last 5 days. ZZHL should produce about 100% success, as it repaints. It will show you a reversal signal, and then 3 hours later say, "Sorry, not sorry, but I've made a mistake." ZZHL will take away the previous signal (repainting it away). It does this quite often.

Which future would you like backtested? Last 5 days. 15 m. periods.
Gold /GC - 16 trades, 75% success.
Copper /HG - 15 trades, 86% success.
SP /MES - 17 trades, 71%.
Russell /RTY - 23, 83%.
Crude /CL - 26 trades, 61%.
Corn /ZC - 19, 58%.
Wheat /ZW = 15, 67%.
Bitcoin /MBT - 20, 65%.
 
Both indicators had 19 trades in the last 5 days. ZZHL should produce about 100% success, as it repaints. It will show you a reversal signal, and then 3 hours later say, "Sorry, not sorry, but I've made a mistake." ZZHL will take away the previous signal (repainting it away). It does this quite often.

Which future would you like backtested? Last 5 days. 15 m. periods.
Gold /GC - 16 trades, 75% success.
Copper /HG - 15 trades, 86% success.
SP /MES - 17 trades, 71%.
Russell /RTY - 23, 83%.
Crude /CL - 26 trades, 61%.
Corn /ZC - 19, 58%.
Wheat /ZW = 15, 67%.
Bitcoin /MBT - 20, 65%.
Ahh got it, so repaint doesn't accurately depict what happened previously, guess that's why it was given the name a 'repaint' (duh haha).

I primarily trade ES/MES and NQ/MNQ, CL/MCL but not as much. What were the R:R on the MES trades if I might ask?
71% is still very successful in my mind, I've watched some who day trade as a profession with 30-40% win rates but their average R:R is 3-4, completely changed the way I thought about "winning".
 
These screenshots tracks more to the strategy I use, focusing less so on the indicator showing when to trade but more of visualization of immediate price action trends, gaining and losing levels, identifying pivot points, etc.

The crossover of the blue line is the trigger to trade, is that correct?

Below is indicator in action in TV just for reference (15min of AUDUSD):
View attachment 24933
This shows the ZZHL as the red line. It has something like 16 highs and 16 lows, indicating 32 potential trades. Your candles are blue and white, being repainted by the ZZHL (blue = up signal) (white = down signal).

Trading with every change of color will 1) force you to be in front of the screen all day, and 2) drain your pocketbook due to the cost of spreads entering and leaving trades. Schwab will take about $1 for every $100 risked in Forex for every one of those trades.
 
This shows the ZZHL as the red line. It has something like 16 highs and 16 lows, indicating 32 potential trades. Your candles are blue and white, being repainted by the ZZHL (blue = up signal) (white = down signal).

Trading with every change of color will 1) force you to be in front of the screen all day, and 2) drain your pocketbook due to the cost of spreads entering and leaving trades. Schwab will take about $1 for every $100 risked in Forex for every one of those trades.
To clarify though, I'm not entering trades on every switch of the trend, I'm using the indicator to identify key levels of support/resistance, and mark potential retest entries.

For example, the two trades I saw on the previously shown chart:
1749528367492.png

1749528485598.png


Brings us to live chart:
1749528585336.png
 
Ahh got it, so repaint doesn't accurately depict what happened previously, guess that's why it was given the name a 'repaint' (duh haha).

I primarily trade ES/MES and NQ/MNQ, CL/MCL but not as much. What were the R:R on the MES trades if I might ask?
71% is still very successful in my mind, I've watched some who day trade as a profession with 30-40% win rates but their average R:R is 3-4, completely changed the way I thought about "winning".

Last 5 days / 15 min periods for those futures. Sorry, the profit levels ($) shown here are going to be incorrect. The winning % will be accurate, as well as the total win $ will give you an indication that there are more wins than losses. I don't do risk/reward. Either I win or lose.

MES - 71%
MNQ - 73%
MCL - 50% - But should be $ positive, as it catches the moves, but loses on the flat areas.

zz7.jpg
zz8.jpg
zz9.jpg
 
Last 5 days / 15 min periods for those futures. Sorry, the profit levels ($) shown here are going to be incorrect. The winning % will be accurate, as well as the total win $ will give you an indication that there are more wins than losses. I don't do risk/reward. Either I win or lose.

MES - 71%
MNQ - 73%
MCL - 50% - But should be $ positive, as it catches the moves, but loses on the flat areas.

View attachment 24935View attachment 24936View attachment 24937
This would probably do very well when paired with an ADX indicator then. Only take trades when ADX is up to signal that your stock is trending, whether that be up or down.

Have you put this ZZHL indicator to the test yet with real $ yet? Have you watched it in real time to verify it's not repainting these stats after the fact? Just curious.
 
To clarify though, I'm not entering trades on every switch of the trend, I'm using the indicator to identify key levels of support/resistance, and mark potential retest entries.

For example, the two trades I saw on the previously shown chart:
View attachment 24934
View attachment 24938

Brings us to live chart:
View attachment 24939

I believe your methods require a brain. I've given up trying to think through trades. Every time I convince myself I'm smart enough to trade, my account gets cut by 50% or more... My world is now either up or down. The arrows tell me what to do.

It seems that your strategy is solidly built on fundamentals. And ZZHL (even though it repaints) is OK to use as the basis for your charting. It will indicate prior patterns that may help you determine resistance levels.

Let me see if I can build what you described at the top of the conversation. But not tonight. My brainstem needs rest.
 
This would probably do very well when paired with an ADX indicator then. Only take trades when ADX is up to signal that your stock is trending, whether that be up or down.

Have you put this ZZHL indicator to the test yet with real $ yet? Have you watched it in real time to verify it's not repainting these stats after the fact? Just curious.
I'm throwing money at it now. I'm reluctant to release it in the wild, as want to get a better feel to it's impact. I only have a few days trading on the indicator.
 
Hello,
Hoping someone here could help me develop a scanner based off an existing indicator native to ToS, ZigZagHighLow.

I would like to develop a stock scanner to identify when the support to the high is lost or when the resistance to the low is gained.
  • If the market is trending down you would have a swing low in price (point A), usually followed by a lower high and a small trend up (point B, or resistance to the low), then what is made is another swing low which creates a lower low (point C). When the market begins a reversal, I would want the scanner to detect when the price manages to close back above point B (resistance to the low).
  • Same goes for the opposite: if the market is trending up you would have a swing high in price (point A), usually followed by a higher low and a small trend (point B, or support to the high), then what is made is another swing high which creates a higher high (point C). when the market begins a reversal, I would want the scanner to detect when the price manages to close back below point B (support to the high).

The settings I'm using right now for the ZigZagHighLow indicator are as follows:
  • price h = HIGH
  • price l = LOW
  • percentage reversal = 0.0
  • absolute reversal = 0.0
  • atr length = 1
  • atr reversal = 1.0
  • tick reversal = 0
Here's where I'm at currently.

Can you play around with it and let me know what else you'd like to see with this ZZHL pattern identification study?

Code:
## ZZHL Pattern Identification ###
## by whoDAT

## Standard ZZHL ###

input priceH = high;
input priceL = low;
input percentageReversal = 0.0;
input absoluteReversal = 0.0;
input atrLength = 1;
input atrReversal = 1.0;
input tickReversal = 0;

Assert(percentageReversal >= 0, "'percentage reversal' must not be negative: " + percentageReversal);
Assert(absoluteReversal >= 0, "'absolute reversal' must not be negative: " + absoluteReversal);
Assert(atrReversal >= 0, "'atr reversal' must not be negative: " + atrReversal);
Assert(tickReversal >= 0, "'ticks' must not be negative: " + tickReversal);
Assert(percentageReversal != 0 or absoluteReversal != 0 or atrReversal != 0 or tickReversal != 0, "Either 'percentage reversal' or 'absolute reversal' or 'atr reversal' or 'tick reversal' must not be zero");

def absReversal;
if (absoluteReversal != 0) {
    absReversal = absoluteReversal;
} else {
    absReversal =  tickReversal * TickSize();
}

def hlPivot;
if (atrReversal != 0) {
    hlPivot = percentageReversal / 100 + WildersAverage(TrueRange(high, close, low), atrLength) / close * atrReversal;
} else {
    hlPivot = percentageReversal / 100;
}
def state = {default init, undefined, uptrend, downtrend};
def maxPriceH;
def minPriceL;
def newMax;
def newMin;
def prevMaxH = GetValue(maxPriceH, 1);
def prevMinL = GetValue(minPriceL, 1);

if GetValue(state, 1) == GetValue(state.init, 0) {
    maxPriceH = priceH;
    minPriceL = priceL;
    newMax = yes;
    newMin = yes;
    state = state.undefined;
} else if GetValue(state, 1) == GetValue(state.undefined, 0) {
    if priceH >= prevMaxH {
        state = state.uptrend;
        maxPriceH = priceH;
        minPriceL = prevMinL;
        newMax = yes;
        newMin = no;
    } else if priceL <= prevMinL {
        state = state.downtrend;
        maxPriceH = prevMaxH;
        minPriceL = priceL;
        newMax = no;
        newMin = yes;
    } else {
        state = state.undefined;
        maxPriceH = prevMaxH;
        minPriceL = prevMinL;
        newMax = no;
        newMin = no;
    }
} else if GetValue(state, 1) == GetValue(state.uptrend, 0) {
    if priceL <= prevMaxH - prevMaxH * hlPivot - absReversal {
        state = state.downtrend;
        maxPriceH = prevMaxH;
        minPriceL = priceL;
        newMax = no;
        newMin = yes;
    } else {
        state = state.uptrend;
        if (priceH >= prevMaxH) {
            maxPriceH = priceH;
            newMax = yes;
        } else {
            maxPriceH = prevMaxH;
            newMax = no;
        }
        minPriceL = prevMinL;
        newMin = no;
    }
} else {
    if priceH >= prevMinL + prevMinL * hlPivot + absReversal {
        state = state.uptrend;
        maxPriceH = priceH;
        minPriceL = prevMinL;
        newMax = yes;
        newMin = no;
    } else {
        state = state.downtrend;
        maxPriceH = prevMaxH;
        newMax = no;
        if (priceL <= prevMinL) {
            minPriceL = priceL;
            newMin = yes;
        } else {
            minPriceL = prevMinL;
            newMin = no;
        }
    }
}

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();

### Bar Coloring ###

input BarColoring = yes;
AssignPriceColor(if !BarColoring then Color.CURRENT else
                 if !IsNaN(lastL) OR GetValue(state, 0) == GetValue(state.downtrend, 1) then color.WHITE else
                 if !IsNaN(lastH) OR GetValue(state, 0) == GetValue(state.uptrend, 1) then color.CYAN else
                 Color.GRAY);


##### Color Last X ZZHL Line for Pattern Detection #####

input NoOfHighLows = 3;

def ZZHBar = if ZZ == priceH then 1 else double.NaN;
def ZZLBar = if ZZ == priceL then 1 else double.NaN;

def ZZHcnt = if !isNaN(ZZHBar) then ZZHcnt[1]+1 else ZZHcnt[1];
def ZZLcnt = if !isNaN(ZZLBar) then ZZLcnt[1]+1 else ZZLcnt[1];

def cntZZHHi = highestAll(ZZHcnt);
def cntZZLHi = highestAll(ZZLcnt);

def cntZZHCond = if (ZZHcnt) > (cntZZHHi) - NoOfHighLows AND ZZHBar then priceH else double.NaN;
def cntZZLCond = if (ZZLcnt) > (cntZZLHi) - NoOfHighLows AND ZZLBar then priceL else double.NaN;

def LastZZ = if !isNaN(cntZZHCond)then cntZZHCond else if !isNaN(cntZZLCond) then cntZZLCond else double.NaN;

plot LastXZZ = LastZZ;
LastXZZ.EnableApproximation();

zzhl1.jpg
 
Here's where I'm at currently.

Can you play around with it and let me know what else you'd like to see with this ZZHL pattern identification study?

Code:
## ZZHL Pattern Identification ###
## by whoDAT

## Standard ZZHL ###

input priceH = high;
input priceL = low;
input percentageReversal = 0.0;
input absoluteReversal = 0.0;
input atrLength = 1;
input atrReversal = 1.0;
input tickReversal = 0;

Assert(percentageReversal >= 0, "'percentage reversal' must not be negative: " + percentageReversal);
Assert(absoluteReversal >= 0, "'absolute reversal' must not be negative: " + absoluteReversal);
Assert(atrReversal >= 0, "'atr reversal' must not be negative: " + atrReversal);
Assert(tickReversal >= 0, "'ticks' must not be negative: " + tickReversal);
Assert(percentageReversal != 0 or absoluteReversal != 0 or atrReversal != 0 or tickReversal != 0, "Either 'percentage reversal' or 'absolute reversal' or 'atr reversal' or 'tick reversal' must not be zero");

def absReversal;
if (absoluteReversal != 0) {
    absReversal = absoluteReversal;
} else {
    absReversal =  tickReversal * TickSize();
}

def hlPivot;
if (atrReversal != 0) {
    hlPivot = percentageReversal / 100 + WildersAverage(TrueRange(high, close, low), atrLength) / close * atrReversal;
} else {
    hlPivot = percentageReversal / 100;
}
def state = {default init, undefined, uptrend, downtrend};
def maxPriceH;
def minPriceL;
def newMax;
def newMin;
def prevMaxH = GetValue(maxPriceH, 1);
def prevMinL = GetValue(minPriceL, 1);

if GetValue(state, 1) == GetValue(state.init, 0) {
    maxPriceH = priceH;
    minPriceL = priceL;
    newMax = yes;
    newMin = yes;
    state = state.undefined;
} else if GetValue(state, 1) == GetValue(state.undefined, 0) {
    if priceH >= prevMaxH {
        state = state.uptrend;
        maxPriceH = priceH;
        minPriceL = prevMinL;
        newMax = yes;
        newMin = no;
    } else if priceL <= prevMinL {
        state = state.downtrend;
        maxPriceH = prevMaxH;
        minPriceL = priceL;
        newMax = no;
        newMin = yes;
    } else {
        state = state.undefined;
        maxPriceH = prevMaxH;
        minPriceL = prevMinL;
        newMax = no;
        newMin = no;
    }
} else if GetValue(state, 1) == GetValue(state.uptrend, 0) {
    if priceL <= prevMaxH - prevMaxH * hlPivot - absReversal {
        state = state.downtrend;
        maxPriceH = prevMaxH;
        minPriceL = priceL;
        newMax = no;
        newMin = yes;
    } else {
        state = state.uptrend;
        if (priceH >= prevMaxH) {
            maxPriceH = priceH;
            newMax = yes;
        } else {
            maxPriceH = prevMaxH;
            newMax = no;
        }
        minPriceL = prevMinL;
        newMin = no;
    }
} else {
    if priceH >= prevMinL + prevMinL * hlPivot + absReversal {
        state = state.uptrend;
        maxPriceH = priceH;
        minPriceL = prevMinL;
        newMax = yes;
        newMin = no;
    } else {
        state = state.downtrend;
        maxPriceH = prevMaxH;
        newMax = no;
        if (priceL <= prevMinL) {
            minPriceL = priceL;
            newMin = yes;
        } else {
            minPriceL = prevMinL;
            newMin = no;
        }
    }
}

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();

### Bar Coloring ###

input BarColoring = yes;
AssignPriceColor(if !BarColoring then Color.CURRENT else
                 if !IsNaN(lastL) OR GetValue(state, 0) == GetValue(state.downtrend, 1) then color.WHITE else
                 if !IsNaN(lastH) OR GetValue(state, 0) == GetValue(state.uptrend, 1) then color.CYAN else
                 Color.GRAY);


##### Color Last X ZZHL Line for Pattern Detection #####

input NoOfHighLows = 3;

def ZZHBar = if ZZ == priceH then 1 else double.NaN;
def ZZLBar = if ZZ == priceL then 1 else double.NaN;

def ZZHcnt = if !isNaN(ZZHBar) then ZZHcnt[1]+1 else ZZHcnt[1];
def ZZLcnt = if !isNaN(ZZLBar) then ZZLcnt[1]+1 else ZZLcnt[1];

def cntZZHHi = highestAll(ZZHcnt);
def cntZZLHi = highestAll(ZZLcnt);

def cntZZHCond = if (ZZHcnt) > (cntZZHHi) - NoOfHighLows AND ZZHBar then priceH else double.NaN;
def cntZZLCond = if (ZZLcnt) > (cntZZLHi) - NoOfHighLows AND ZZLBar then priceL else double.NaN;

def LastZZ = if !isNaN(cntZZHCond)then cntZZHCond else if !isNaN(cntZZLCond) then cntZZLCond else double.NaN;

plot LastXZZ = LastZZ;
LastXZZ.EnableApproximation();

View attachment 24944

This script repaints. Whenever maxhigh is greater then prevhigh & minlow is less then prevlow


@babayaga_310 @whoDAT
 
Solution

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