Code for the ZigZag indicator version V.1 simplified without the use of array.
(see previous: https://www.prorealcode.com/prorealtime-indicators/zigzag-indicator/ )
Similar to the previous one, but draws a fixed number of segments (in the version below, the last 10, between 5 tops and 5 lows – can be increased adding lines in the code as needed).
Easier to use within a strategy, (e.g. Ross’ 1-2-3)
//08/01/2023 - T.F. - V1: ZigZag Version V1: simplified without arrays - draws the last 9 segments (between 5 tops and 5 lows, can be increased as needed)
//Sharing on ProRealCode
DEFPARAM DRAWONLASTBARONLY=TRUE
//Variables:
//Points=20 //Pips
//Perc=0.25 //%
//Type=0 //0=pips; 1=perc
VarY=CustomClose //close,tpicalprice, etc
//initilaize first point as a "TOP"
once LastPoint = 1
once TX0 = barindex
once TY0 = VarY
//Type ZZ in Points o Perc
If Type=0 then //ZZ in pips
DeltaY=Points*pipsize
elsif Type=1 then //ZZ in %
DeltaY=Perc/100*Dclose(1)////Dclose used as ref.
endif
//ZZ in fase 1
if LastPoint=1 then //lastpoint was a top
if VarY>=TY0 then //update last top and stay in LastPoint=1
TY0=VarY
TX0=barindex
endif
if VarY<=TY0-DeltaY then //first point in low
LX4=LX3 //shift memory previous points
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 //lastpoint was a low
if VarY<=LY0 then //update last low and stay in LastPoint=-1
LY0=VarY
LX0=barindex
endif
if VarY>=LY0+DeltaY then //first point in top
TX4=TX3 //shift memory previous points
TY4=TY3
TX3=TX2
TY3=TY2
TX2=TX1
TY2=TY1
TX1=TX0
TY1=TY0
TY0=VarY
TX0=barindex
LastPoint=1
endif
endif
//---GRAPH---
If lastpoint=1 then
drawsegment (barindex,close,TX0,TY0) style (dottedline,2) COLOURED (0,0,200) //segment in progress
drawsegment (TX0,TY0,LX0,LY0) style (line,2) COLOURED (0,0,200) //segments definitive
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) //segment in progress
drawsegment (LX0,LY0,TX0,TY0) style (line,2) COLOURED (0,0,200) //segments definitive
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