This code snippet demonstrates how to implement a ZigZag indicator in ProBuilder, focusing on using high and low prices instead of closing prices. The ZigZag indicator is useful for identifying price trends by filtering out minor fluctuations and focusing on significant changes.
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] = High
//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 High>$TY[0] then //update last top and stay in
LastPoint=1
$TY[0]=High
$TX[0]=barindex
elsif Low<=($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]=Low //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 Low<$LY[0] then //update last low and stay in
LastPoint=-1
$LY[0]=Low
$LX[0]=barindex
elsif High>=($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]=High //store new top and change lastpoint
$TX[0]=barindex
LastPoint=1
endif
endif
//---GRAPH---
If LastPoint=1 then
drawsegment (barindex,Low,$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,High,$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
The ZigZag indicator is constructed by alternating between tracking highs and lows based on a defined deviation amount, which can be set in pips or percentage. The indicator updates its points only when a significant reversal is detected.
This example helps in understanding how to manipulate arrays and conditional logic in ProBuilder to track and visualize significant price movements effectively.
Check out this related content for more information:
https://www.prorealcode.com/topic/zigzag-indicator-using-highs-and-lows/#post-220573
Visit Link