I enhanced the code for both peaks and bottoms which works fine (medium pane)
ZigZagDepth = 0.08
zz = ZigZag[ZigZagDepth](close)
peak = zz<zz[1] and zz[1]>zz[2]
zbottom = zz > zz[1] and zz[1]<zz[2]
if peak then
PeakBar = barindex
LastZigZagClosePrice = close
endif
if zbottom then
BottomBar = barindex
LastZigZagClosePrice = close
endif
BarsSinceLastZigZagPeak = barindex - PeakBar
BarsSinceLastZigZagBottom = barindex - BottomBar
BSZZ = min(BarsSinceLastZigZagPeak, BarsSinceLastZigZagBottom)
return BSZZ coloured(0,0,255) style(line,1) as "bars since ZigZag"
Then I wanted to write a trading system and stumbled accross an error “your trading system uses obsolete instructions which are not supported by the backtesting engine (ZigZag)”
Sadly read that zigzag is not supported for trading systems 🙁 So manually coded it which works fine too:
// manual calculation for the zigzag indicator
DEFPARAM DRAWONLASTBARONLY=TRUE
//Variables:
Points=20 //Pips
Perc=0.08 //%
Type=1 //0=pips; 1=perc
VarY=CustomClose //close,typicalprice, 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
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
TY0=VarY
TX0=barindex
LastPoint=1
endif
endif
BarsSinceLastZigZagPeak = barindex - TX0
BarsSinceLastZigZagBottom = barindex - LX0
BSZZ = min(BarsSinceLastZigZagPeak, BarsSinceLastZigZagBottom)
//---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
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
endif
return
But upon trying to calculate the bars that have passed since last (either peak or bottom), my code messed up (lower pane):
// manual calculation for the zigzag indicator
DEFPARAM DRAWONLASTBARONLY=TRUE
//Variables:
Points=20 //Pips
Perc=0.08 //%
Type=1 //0=pips; 1=perc
VarY=CustomClose //close,tpicalprice, etc
//initialize 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
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
TY0=VarY
TX0=barindex
LastPoint=1
endif
endif
// BarsSinceLastZigZagPeak = barindex - TX0
// BarsSinceLastZigZagBottom = barindex - LX0
BarsSinceLastZigZagPeak = barindex - TX0
BarsSinceLastZigZagBottom = barindex - LX0
BSZZ = min(BarsSinceLastZigZagPeak, BarsSinceLastZigZagBottom)
// return BarsSinceLastZigZagPeak coloured (255,0,0) style (line) as "peak", BarsSinceLastZigZagBottom coloured (0,0,255) style (line) as "bottom"
return BSZZ coloured (255,0,0) style (line) as "peak"
any idea where the calculation should be corrected?