ZigZag Indicator

ZigZag Indicator

Code for the zigzag indicator with the use of array.
I compared it with the platform indicator, and the results are almost identical in both pips and percentage modes.

With the “Type” variable it is possible to switch the functioning between “Points” and “Percentage” (Note: in the case of ZZ in %, I used the Day Close of the previous day as a basis for calculation to give stability to the reference; using the ” close” with very low % values, sometimes the recalculation gives rise to graphical artifacts)
The “Segments” variable decides how many pairs of up/down segments to display on the graph.

Share this

Risk disclosure:

No information on this site is investment advice or a solicitation to buy or sell any financial instrument. Past performance is not indicative of future results. Trading may expose you to risk of loss greater than your deposits and is only suitable for experienced investors who have sufficient financial means to bear such risk.

ProRealTime ITF files and other attachments : How to import ITF files into ProRealTime platform?

PRC is also on YouTube, subscribe to our channel for exclusive content and tutorials

  1. JvdG • 01/04/2023 #

    Hi, thanks for this code.
    I implemented it successfully, however, I don’t see an indicator.
    I mean: the zigzag line is drawn, but I cannot translate this line into an indicator or variable that can be used in a strategy.
    Suppose we call this zigzagline “zigzag” then we can test the following pattern:
    if zigzag[2]<zigzag[1] and zigzag[0]<zigzag[1] then
    there is a (local) top that delivers a short signal,
    and so on

    Regards, Jerke vd Geest
    The Netherlands

  2. effegi • 01/04/2023 #

    The coordinates of the zigzag points are stored in the arrays $TX,$TY (for tops) and $LX,$LY (for lows). The last point is stored in index 0, the previous point in index 1 and so on…
    But the version I posted above had some problems which were fixed by Roberto (many thanks!), see link to discussion for the modification needed:

    https://www.prorealcode.com/topic/zigzag-test-code-indicator/

    For use in something like a “1-2-3 J.Ross” strategy, I suggest you to use version 1 that I posted later in the discussion, which draws the zigazag the traditional way, without using arrays , only for the last segments:
    the coordinates of the last point are in the variables TX0, TY0 (top) LX0/LY0 (low), and the previous ones in TX1/TY1, LX1/LY1, TX2/TY2, LX2/LY2, etc.

    With this version, a simple 1-2-3 strategy can be, for example, entering short when:
    lastpoint=-1 (=price in descending phase), TY1>TY0 (=point 3<point 1 by Ross) and Close crosses under LY1 (=breakout point 2 by Ross) – [mirroring for long entering]
    I'm also working on it 😉

    //20/12/2022 – T.F. – V1: Codice ZigZag Versione v1: eliminati gli array e gestito con variabili fisse
    //Sharing on ProRealCode

    DEFPARAM DRAWONLASTBARONLY=TRUE
    //Variabili:
    //Punti=20 //Pips
    //Percentuale=0.25 //%
    //Tipo=0 //0=pips; 1=perc

    VarY=CustomClose //close,tpicalprice, etc

    //inizializzo primo punto come TOP
    once LastPoint = 1
    once TX0 = barindex
    once TY0 = VarY

    //Tipo ZZ in Punti o Percentuale
    If Tipo=0 then //ZZ in punti
    DeltaY=Punti*pipsize
    elsif tipo=1 then //ZZ in %
    DeltaY=Percentuale/100*Dclose(1)//usato Dclose come rif. per stabilità del valore
    endif

    //ZZ in fase 1
    if LastPoint=1 then //ultimo punto era un massimo
    if VarY>=TY0 then // aggiorno il punto di max e rimango in LastPoint=1
    TY0=VarY
    TX0=barindex
    endif
    if VarY<=TY0-DeltaY then //primo punto definitivo low
    rem for i=PI downto 0 do //shift memoria punti precedenti
    LX4=LX3
    LY4=LY3
    LX3=LX2
    LY3=LY2
    LX2=LX1
    LY2=LY1
    LX1=LX0
    LY1=LY0

    LY0=VarY
    LX0=barindex
    LastPoint=-1
    endif
    endif

    //ZZ in fase -1
    if LastPoint=-1 then //ultimo punto era un minimo
    if VarY<=LY0 then // aggiorno il punto di min e rimango in LastPoint=-1
    LY0=VarY
    LX0=barindex
    endif
    if VarY>=LY0+DeltaY then //primo punto definitivo top
    TX4=TX3
    TY4=TY3
    TX3=TX2
    TY3=TY2
    TX2=TX1
    TY2=TY1
    TX1=TX0
    TY1=TY0

    TY0=VarY
    TX0=barindex
    LastPoint=1
    endif
    endif

    //—GRAFICA—
    If lastpoint=1 then
    drawsegment (barindex,close,TX0,TY0) style (dottedline,2) COLOURED (0,0,200) //segmento in progress
    drawsegment (TX0,TY0,LX0,LY0) style (line,2) COLOURED (0,0,200) //segmenti definitivi
    drawsegment (LX0,LY0,TX1,TY1) style (line,2) COLOURED (0,0,200)
    drawsegment (TX1,TY1,LX1,LY1) style (line,2) COLOURED (0,0,200)
    drawsegment (LX1,LY1,TX2,TY2) style (line,2) COLOURED (0,0,200)
    drawsegment (TX2,TY2,LX2,LY2) style (line,2) COLOURED (0,0,200)
    drawsegment (LX2,LY2,TX3,TY3) style (line,2) COLOURED (0,0,200)
    drawsegment (TX3,TY3,LX3,LY3) style (line,2) COLOURED (0,0,200)
    drawsegment (LX3,LY3,TX4,TY4) style (line,2) COLOURED (0,0,200)
    drawsegment (TX4,TY4,LX4,LY4) style (line,2) COLOURED (0,0,200)
    endif
    If lastpoint=-1 then
    drawsegment (barindex,close,LX0,LY0) style (dottedline,2) COLOURED (0,200,0) //segmento in progress
    drawsegment (LX0,LY0,TX0,TY0) style (line,2) COLOURED (0,0,200) //segmenti definitivi
    drawsegment (TX0,TY0,LX1,LY1) style (line,2) COLOURED (0,0,200)
    drawsegment (LX1,LY1,TX1,TY1) style (line,2) COLOURED (0,0,200)
    drawsegment (TX1,TY1,LX2,LY2) style (line,2) COLOURED (0,0,200)
    drawsegment (LX2,LY2,TX2,TY2) style (line,2) COLOURED (0,0,200)
    drawsegment (TX2,TY2,LX3,LY3) style (line,2) COLOURED (0,0,200)
    drawsegment (LX3,LY3,TX3,TY3) style (line,2) COLOURED (0,0,200)
    drawsegment (TX3,TY3,LX4,LY4) style (line,2) COLOURED (0,0,200)
    drawsegment (LX4,LY4,TX4,TY4) style (line,2) COLOURED (0,0,200)
    endif

    return

  3. Seb • 351 days ago #

    Thanks for this!
    My version uses highs and lows and uses 3 * ATR as the minimum difference between points:

    DEFPARAM DRAWONLASTBARONLY=TRUE
    //Variabili:
    Punti=3 * averagetruerange[200] //Pips
    Percentuale=0.25 //%
    Tipo=0 //0=pips; 1=perc

    //VarY=CustomClose //close,tpicalprice, etc

    //inizializzo primo punto come TOP
    once LastPoint = 1
    once TX0 = barindex
    once TY0 = close

    //Tipo ZZ in Punti o Percentuale
    If Tipo=0 then //ZZ in punti
    DeltaY=Punti//*pipsize
    elsif tipo=1 then //ZZ in %
    DeltaY=Percentuale/100*Dclose(1)//usato Dclose come rif. per stabilità del valore
    endif

    //ZZ in fase 1
    if LastPoint=1 then //ultimo punto era un massimo
    if high>=TY0 then // aggiorno il punto di max e rimango in LastPoint=1
    TY0=high
    TX0=barindex
    elsif low<=TY0-DeltaY then //primo punto definitivo low
    rem for i=PI downto 0 do //shift memoria punti precedenti
    LX4=LX3
    LY4=LY3
    LX3=LX2
    LY3=LY2
    LX2=LX1
    LY2=LY1
    LX1=LX0
    LY1=LY0

    LY0=low
    LX0=barindex
    LastPoint=-1
    endif
    endif

    //ZZ in fase -1
    if LastPoint=-1 then //ultimo punto era un minimo
    if low<=LY0 then // aggiorno il punto di min e rimango in LastPoint=-1
    LY0=low
    LX0=barindex
    elsif high>=LY0+DeltaY then //primo punto definitivo top
    TX4=TX3
    TY4=TY3
    TX3=TX2
    TY3=TY2
    TX2=TX1
    TY2=TY1
    TX1=TX0
    TY1=TY0

    TY0=high
    TX0=barindex
    LastPoint=1
    endif
    endif

    //—GRAFICA—
    If lastpoint=1 then
    drawsegment (barindex,high,TX0,TY0) style (dottedline,2) COLOURED (0,0,200) //segmento in progress
    drawsegment (TX0,TY0,LX0,LY0) style (line,2) COLOURED (0,0,200) //segmenti definitivi
    drawsegment (LX0,LY0,TX1,TY1) style (line,2) COLOURED (0,0,200)
    drawsegment (TX1,TY1,LX1,LY1) style (line,2) COLOURED (0,0,200)
    drawsegment (LX1,LY1,TX2,TY2) style (line,2) COLOURED (0,0,200)
    drawsegment (TX2,TY2,LX2,LY2) style (line,2) COLOURED (0,0,200)
    drawsegment (LX2,LY2,TX3,TY3) style (line,2) COLOURED (0,0,200)
    drawsegment (TX3,TY3,LX3,LY3) style (line,2) COLOURED (0,0,200)
    drawsegment (LX3,LY3,TX4,TY4) style (line,2) COLOURED (0,0,200)
    drawsegment (TX4,TY4,LX4,LY4) style (line,2) COLOURED (0,0,200)
    endif
    If lastpoint=-1 then
    drawsegment (barindex,low,LX0,LY0) style (dottedline,2) COLOURED (0,200,0) //segmento in progress
    drawsegment (LX0,LY0,TX0,TY0) style (line,2) COLOURED (0,0,200) //segmenti definitivi
    drawsegment (TX0,TY0,LX1,LY1) style (line,2) COLOURED (0,0,200)
    drawsegment (LX1,LY1,TX1,TY1) style (line,2) COLOURED (0,0,200)
    drawsegment (TX1,TY1,LX2,LY2) style (line,2) COLOURED (0,0,200)
    drawsegment (LX2,LY2,TX2,TY2) style (line,2) COLOURED (0,0,200)
    drawsegment (TX2,TY2,LX3,LY3) style (line,2) COLOURED (0,0,200)
    drawsegment (LX3,LY3,TX3,TY3) style (line,2) COLOURED (0,0,200)
    drawsegment (TX3,TY3,LX4,LY4) style (line,2) COLOURED (0,0,200)
    drawsegment (LX4,LY4,TX4,TY4) style (line,2) COLOURED (0,0,200)
    endif

    return

    • effegi • 351 days ago #

      A good alternative solution, even I had thought of the variant that uses high and low (probabily better than “Close”)
      Another solution that I’m testing (and that seems to work very well when used on timeframe < day) is to use a % of the daily ATR as a parameter for the change of direction: 30% of the daily ATR value ( on 5 days, excluding today) seems to me the best one to follow the market swings on 15M. I use this setup for 1-2-3 strategy
      Add at the The code:

      time frame (1 day)
      Atrd=AverageTrueRange[5](close)[1] //Average Daily max variations, today excluded to avoid recalculations on the ATRD value

      time frame (default)

      VarY=CustomClose //close,tpicalprice, etc

      //initialize first point as TOP
      once LastPoint = 1
      ounces TX0 = barindex
      ounces TY0 = VarY

      //Type ZZ in Points or Percentage
      If Type=0 then //ZZ in points
      DeltaY=Points*pipsize
      elsif type=1 then //ZZ in %
      DeltaY=Percentage/100*Dclose(1) //used Dclose as ref. for value stability

      //newtype=2
      elsif type=2 then //ZZ in % on ATR Daily
      DeltaY=PercOnATRD/100*Atrd //value in points calculated as % on atrd
      endif

  4. Seb • 350 days ago #

    did you automate that 1-2-3 strategy? how is that working for you?

  5. effegi • 350 days ago #

    Not yet, I’m busy developing other strategies/indicators at the moment, but it’s on my to-do list 🙂
    I think it’s not that complicated to make the basic 1-2-3 strategy using this ZigZag code.
    As I said, with this version, a simple 1-2-3 strategy can be, for example, entering short when:
    lastpoint=-1 (=price in descending phase), TY1>TY0 (=point 3<point 1 by Ross) and Close crosses under LY1 (=breakout point 2 by Ross).

    Surely it is more difficult to make it efficient and filter it well to avoid consecutive losses in the lateral phases. Somehow we should take into account the basic trend, and block the strategy in moments of low volatility.
    Even the type of money management (such as setting the target and the stop) can completely change the performance.

avatar
Register or

Likes

avatar avatar avatar avatar
Related users ' posts
philippe59139 Bravo super travail
gidien
3 months ago
gidien Thanks for the hint. I think i know now, why this happen. The "settings" block was added by ...
LucasBest Thank you for sharing your work, both original and very disconcerting. When I went through t...
gidien Hello LucasBest, thanks for your comment. Point 1: Yes your are right. The Zigzag ve...
Nicolas Il doit y avoir impérativement plus d'unités affichées que le paramètre "BarsLimit". Ceci ét...
steffen_burat Hello Nicolas, I have a cumulative histogram update problem, the indicator often updates ...
taklause Hello Nicolas, if I try to use your indicator in a trading system, the error on prc pops up ...
supertiti Quand on parle d'AFR on parle de quoi ? merci
Sever AFR means Average Filter Regression
supertiti Thanks you
jonpt88 hI . THIS LOOKS GOOD. Thanks. Just one doubt: is this kind of impulse indicator - does it sh...
StephFor //ind1 = ZigZagPoint[2*averagetruerange[200]](close) avr = 3 //3% ind1 = ZigZag[avr](close...
Manu L. @Steph un grand merci pour ton aide mais :-( J'ai copier le code tel quel mais cependant j...
AlphaMauss Je n'ai pas réussi à faire fonctionner l'indicateur avec le code fourni en commentaire sur P...
Meta Signals Pro Pleasure ! Please let us know if you make good trades with it and if you see improvements we...
francis59 Bonjour, Bonjour, comment puis-je créer un screener basé sur cet indicateur, qui affiche le...
Trader Sab I am curious to try it how, however I get an error message for line 47 and 48 - drawsegment,...
datageek How do I change the thickness of the line or change it too dashed lines? Kind regards
lkiklkik NE fonctionne pas ( ou plus ... )
Nicolas il faut modifier toutes les références à top par itop et bottom par ibottom. Ces mots étant ...
teddy58 Ich habe es mit N= 1,5 und N=2,5 getestet mit 200000 Einheiten. Es funktioniert leider erst ...
Matriciel I use this indicator to help me make a decision when there is a divergence. The divergences ...
Jan Wind Hi, I personnaly am not a big fan of ZIGZAG indicator, as it repaints itself during time. ...
Nicolas Sure, I suggest you open a new topic in the forum to discuss about your ideas.
hdgm Bonjour Nicolas, est ce que l'on peut en faire une stratégie de trading automatisée ?
Nicolas Non, pas en utilisant l'indicateur zigzag traditionnel de la plateforme.
TACBOLSA
6 years ago
Tradingrob Hi Tac Bolsa, thanks for the information. But after carefully ready all the material i still...
TAC Bolsa Hello, the indicator creates the support depending on the volume and is dynamic.
carlvan Not working on PRT 11.1. And the site https://www.tiburonesdealetacorta.com does not exist....
rama I want to use this indicator in trading system, it says zig zag is obsolete and not supported
Nicolas Right, due to its repainting behavior, the zigzag is not allowed in automated trading with r...
rama I noticed it changes as time progress, I am 7 as the average, how many period it wont chang...
Nicolas
8 years ago
Nicolas add it on price chart, change the cp parameter according to the period you want to observe f...
Robert22 hola buenos días: yo estoy buscando una cosa parecida para realizar con ello un indicador de...
Nicolas https://www.prorealcode.com/prorealtime-indicators/rsi-classical-hidden-divergences-indicator/

Top