number of bars passed since last zigzag

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #227445 quote
    Swisspatrick
    Participant
    New

    hi! using the standard built-in zigzag function, how can I find out:

    1. the number of bars that have passed since the last zigzag (peak or bottom) occurred and now?
    2. the closing price of that last zigzag (peak or bottom)?
    #227459 quote
    Nicolas
    Keymaster
    Master

    Hi there 😊!

    To retrieve information using the built-in ZigZag function in ProRealTime for:

    1. The number of bars that have passed since the last ZigZag (peak) occurred
    2. 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:

    // ZigZag parameter setup
    ZigZagDepth = 5 // This sets the depth parameter for the ZigZag indicator. The depth affects sensitivity.
    
    // Applying the ZigZag function
    zz = ZigZag[ZigZagDepth](close) // This line calculates the ZigZag indicator values based on the close prices of bars.
    
    // Find Peak
    peak = 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 found
    if peak then 
        PeakBar = 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.
    endif 
    
    BarsSinceLastZigZag = barindex - PeakBar // Calculate the number of bars since the last ZigZag peak by subtracting the PeakBar index from the current bar index.
    
    // Output
    RETURN BarsSinceLastZigZag AS "Bars Since Last ZigZag", LastZigZagClosePrice AS "Last ZigZag Close Price"
    
    Swisspatrick and Carburo thanked this post
    #227485 quote
    Swisspatrick
    Participant
    New

    thank you Nicolas, that worked 🙂

    #227491 quote
    Swisspatrick
    Participant
    New

    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?

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.

number of bars passed since last zigzag


ProBuilder: Indicators & Custom Tools

New Reply
Author
Summary

This topic contains 3 replies,
has 2 voices, and was last updated by Swisspatrick
2 years ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 02/05/2024
Status: Active
Attachments: 1 files
Logo Logo
Loading...