number of bars passed since last zigzag
Forums › ProRealTime English forum › ProBuilder support › number of bars passed since last zigzag
- This topic has 3 replies, 2 voices, and was last updated 10 months ago by Swisspatrick.
-
-
02/05/2024 at 9:09 PM #227445
hi! using the standard built-in zigzag function, how can I find out:
- the number of bars that have passed since the last zigzag (peak or bottom) occurred and now?
- the closing price of that last zigzag (peak or bottom)?
02/06/2024 at 11:38 AM #227459Hi there 😊!
To retrieve information using the built-in ZigZag function in ProRealTime for:
- The number of bars that have passed since the last ZigZag (peak) occurred
- The closing price of that last ZigZag (peak): Once you find the bar where the ZigZag indicator shows a peak, you can record the closing price of that bar.
Here’s a simplified example of how you might write this in ProRealTime’s coding language:
12345678910111213141516171819// ZigZag parameter setupZigZagDepth = 5 // This sets the depth parameter for the ZigZag indicator. The depth affects sensitivity.// Applying the ZigZag functionzz = ZigZag[ZigZagDepth](close) // This line calculates the ZigZag indicator values based on the close prices of bars.// Find Peakpeak = zz<zz[1] and zz[1]>zz[2] // A peak is identified when the current ZigZag value is less than the previous one, and the previous value is greater than the one before it. This indicates a high point in the ZigZag line.// Store values when foundif peak thenPeakBar = barindex // If a peak is found, store the current bar index as the PeakBar.LastZigZagClosePrice = close // Also, store the close price of the current bar as the last ZigZag peak's close price.endifBarsSinceLastZigZag = barindex - PeakBar // Calculate the number of bars since the last ZigZag peak by subtracting the PeakBar index from the current bar index.// OutputRETURN BarsSinceLastZigZag AS "Bars Since Last ZigZag", LastZigZagClosePrice AS "Last ZigZag Close Price"2 users thanked author for this post.
02/06/2024 at 7:03 PM #227485thank you Nicolas, that worked 🙂
02/06/2024 at 8:53 PM #227491I enhanced the code for both peaks and bottoms which works fine (medium pane)
zigzagbars1234567891011121314151617181920ZigZagDepth = 0.08zz = ZigZag[ZigZagDepth](close)peak = zz<zz[1] and zz[1]>zz[2]zbottom = zz > zz[1] and zz[1]<zz[2]if peak thenPeakBar = barindexLastZigZagClosePrice = closeendifif zbottom thenBottomBar = barindexLastZigZagClosePrice = closeendifBarsSinceLastZigZagPeak = barindex - PeakBarBarsSinceLastZigZagBottom = barindex - BottomBarBSZZ = 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 zigzag123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263// manual calculation for the zigzag indicatorDEFPARAM DRAWONLASTBARONLY=TRUE//Variables:Points=20 //PipsPerc=0.08 //%Type=1 //0=pips; 1=percVarY=CustomClose //close,typicalprice, etc//initilaize first point as a "TOP"once LastPoint = 1once TX0 = barindexonce TY0 = VarY//Type ZZ in Points o PercIf Type=0 then //ZZ in pipsDeltaY=Points*pipsizeelsif Type=1 then //ZZ in %DeltaY=Perc/100*Dclose(1)////Dclose used as ref.endif//ZZ in fase 1if LastPoint=1 then //lastpoint was a topif VarY>=TY0 then //update last top and stay in LastPoint=1TY0=VarYTX0=barindexendifif VarY<=TY0-DeltaY then //first point in lowLY0=VarYLX0=barindexLastPoint=-1endifendif//ZZ in fase -1if LastPoint=-1 then //lastpoint was a lowif VarY<=LY0 then //update last low and stay in LastPoint=-1LY0=VarYLX0=barindexendifif VarY>=LY0+DeltaY then //first point in topTY0=VarYTX0=barindexLastPoint=1endifendifBarsSinceLastZigZagPeak = barindex - TX0BarsSinceLastZigZagBottom = barindex - LX0BSZZ = min(BarsSinceLastZigZagPeak, BarsSinceLastZigZagBottom)//---GRAPH---If lastpoint=1 thendrawsegment (barindex,close,TX0,TY0) style (dottedline,2) COLOURED (0,0,200) //segment in progressdrawsegment (TX0,TY0,LX0,LY0) style (line,2) COLOURED (0,0,200) //segments definitiveendifIf lastpoint=-1 thendrawsegment (barindex,close,LX0,LY0) style (dottedline,2) COLOURED (0,200,0) //segment in progressdrawsegment (LX0,LY0,TX0,TY0) style (line,2) COLOURED (0,0,200) //segments definitiveendifreturnBut upon trying to calculate the bars that have passed since last (either peak or bottom), my code messed up (lower pane):
erroneous code1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859// manual calculation for the zigzag indicatorDEFPARAM DRAWONLASTBARONLY=TRUE//Variables:Points=20 //PipsPerc=0.08 //%Type=1 //0=pips; 1=percVarY=CustomClose //close,tpicalprice, etc//initialize first point as a "TOP"once LastPoint = 1once TX0 = barindexonce TY0 = VarY//Type ZZ in Points o PercIf Type=0 then //ZZ in pipsDeltaY=Points*pipsizeelsif Type=1 then //ZZ in %DeltaY=Perc/100*Dclose(1)////Dclose used as ref.endif//ZZ in fase 1if LastPoint=1 then //lastpoint was a topif VarY>=TY0 then //update last top and stay in LastPoint=1TY0=VarYTX0=barindexendifif VarY<=TY0-DeltaY then //first point in lowLY0=VarYLX0=barindexLastPoint=-1endifendif//ZZ in fase -1if LastPoint=-1 then //lastpoint was a lowif VarY<=LY0 then //update last low and stay in LastPoint=-1LY0=VarYLX0=barindexendifif VarY>=LY0+DeltaY then //first point in topTY0=VarYTX0=barindexLastPoint=1endifendif// BarsSinceLastZigZagPeak = barindex - TX0// BarsSinceLastZigZagBottom = barindex - LX0BarsSinceLastZigZagPeak = barindex - TX0BarsSinceLastZigZagBottom = barindex - LX0BSZZ = 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?
-
AuthorPosts
Find exclusive trading pro-tools on