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.
//17/12/2022 - T.F. - ZigZag code with Array
//Sharing ProRealCode
DEFPARAM DRAWONLASTBARONLY=TRUE
//Variables:
//Points=20 //Pips
//Perc=0.25 //%
//Type=0 //0=pips; 1=perc
//Segments=10 //n. segments up/dw to show
PI=max(0,(Segments-1)) //points top/low back to store
VarY=CustomClose //close,tpicalprice, etc
//inizializzo primo punto come TOP
once LastPoint = 1
once $TX[0] = barindex
once $TY[0] = VarY
//Tipo ZZ in Punti o Percentuale
If Type=0 then //ZZ in pips
DeltaY=max(2*pipsize,Points*pipsize)
elsif Type=1 then //ZZ in %
DeltaY=max(0.001,Perc/100*Dclose(1)*pipsize) //Dclose used as ref.
endif
//ZZ in fase 1
if LastPoint=1 then //lastpoint was a top
if VarY>$TY[0] then //update last top and stay in LastPoint=1
$TY[0]=VarY
$TX[0]=barindex
elsif VarY<=($TY[0]-DeltaY) then //first point in low
for i=PI downto 0 do //shift memory previous points
$LX[i+1]=$LX[i]
$LY[i+1]=$LY[i]
next
$LY[0]=VarY //store new low and change lastpoint
$LX[0]=barindex
LastPoint=-1
endif
endif
//ZZ in fase -1
if LastPoint=-1 then //lastpoint was a low
if VarY<$LY[0] then //update last low and stay in LastPoint=-1
$LY[0]=VarY
$LX[0]=barindex
elsif VarY>=($LY[0]+DeltaY) then //first point in top
for i=PI downto 0 do //shift memory previous points
$TX[i+1]=$TX[i]
$TY[i+1]=$TY[i]
next
$TY[0]=VarY //store new top and change lastpoint
$TX[0]=barindex
LastPoint=1
endif
endif
//---GRAPH---
If LastPoint=1 then
drawsegment (barindex,VarY,$TX[0],$TY[0]) style (dottedline,2) COLOURED (0,0,200) //segment in progress
for i=0 to PI do //final segments
drawsegment ($TX[i],$TY[i],$LX[i],$LY[i]) style (line,2) COLOURED (0,0,200)
drawsegment ($LX[i],$LY[i],$TX[i+1],$TY[i+1]) style (line,2) COLOURED (0,0,200)
next
endif
If LastPoint=-1 then
drawsegment (barindex,VarY,$LX[0],$LY[0]) style (dottedline,2) COLOURED (0,0,200) //segment in progress
for i=0 to PI do //final segments
drawsegment ($LX[i],$LY[i],$TX[i],$TY[i]) style (line,2) COLOURED (0,0,200)
drawsegment ($TX[i],$TY[i],$LX[i+1],$LY[i+1]) style (line,2) COLOURED (0,0,200)
next
endif
return