Pivot strategy backtest

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #119133 quote
    Stocks008
    Participant
    Average

    Good day,

    I would like to backtest a strategy with pivots but have no idea how to code an instruction that lets me enter on pivot after a break of pivot.

    My strategy :(long scenario)

    When price breaks through the 4H pivot and closes above, enter if price pulls back and touches pivot with stoploss 10  points below pivot, target is R3.

    Can you please help?

    thank you,

    kind regards,

    juanj thanked this post
    #119144 quote
    robertogozzi
    Moderator
    Master

    I tried this on DAX, 4h:

    DEFPARAM CumulateOrders = false
    DEFPARAM PreLoadBars    = 10000
    //
    TIMEFRAME(4 hour,UpdateOnClose)
    //
    // Pivot & Resistance & Support definitions
    //
    Pivt  = (High[1] + Low[1] + Close[1])/3         // - Pivot
    Res3  = High[1]+(2*(Pivt-Low[1]))               //Res3
    //Res2  = Pivt+(High[1]-Low[1])                 //Res2
    //Res1  = (2*Pivt) - Low[1]                     //Res1
    //Sup1  = (2*Pivt) - High[1]                    //Sup1
    //Sup2  = Pivt-(High[1]-Low[1])                 //Sup2
    //Sup3  = High[1]+(2*(Pivt-Low[1]))             //Sup3
    //
    TIMEFRAME(default)
    //
    ONCE LookBackBars = 5
    c1 = low <= Pivt AND close > Pivt               //Price has touched PIVOT, but closes above it
    c2 = summation[LookBackBars](close > Pivt) > 0  //Make sure within the last LOOKBACK bars price had broken PIVOT
    IF c1 AND c2 AND Not OnMarket THEN
       BUY 1 CONTRACT AT MARKET
       TP = abs(Res3 - close)                       //Target Profit at R3
       SL = abs(close - (Pivt - (10 * pipsize)))    //Stop Loss 10 pips below PIVOT
       SET TARGET PROFIT TP
       SET STOP   LOSS   SL
    ENDIF

    Lines with unused variables are commented out, but I left them in case you need them in the future.

    I set a LOOKBACK number of bars to 5, you can change it to whatever number you prefer.

    I used the MTF support to make sure pivots are ALWAYS calculated on a 4 hour-TF, but you can run it on any lower TF provided 4H is a multiple of it  (2 hours, 1 hour, 30-20-15-10-5-3-2-1 minute, but not 3 hours). I used 4h TF as default, which means I did not take advantage of the MTF support, which could be useful to trail stop loss.

    Stocks008 thanked this post
    #119334 quote
    juanj
    Participant
    Master

    Here is also some code to save you some time going forward. I also use pivots in most of my strategies.

    Timeframe(Monthly)
    
    MOpen = open[1]
    MHigh = high[1]
    MClose = close[1]
    MLow = low[1]
    
    MPivot = (MHigh + MLow + MClose)/3 //Monthly Pivot Calculation
    
    MR1 = MPivot*2-MLow
    MS1 = MPivot*2-MHigh
    MR2 = MPivot+(MHigh-MLow)
    MS2 = MPivot-(MHigh-MLow)
    MR3 = MHigh+(2*(MPivot-MLow))
    MS3 = MLow-(2*(MHigh-MPivot))
    
    Timeframe(Weekly)
    
    WOpen = open[1]
    WHigh = high[1]
    WClose = close[1]
    WLow = low[1]
    
    WPivot = (WHigh + WLow + WClose)/3 //Weekly Pivot Calculation
    
    WR1 = WPivot*2-WLow
    WS1 = WPivot*2-WHigh
    WR2 = WPivot+(WHigh-WLow)
    WS2 = WPivot-(WHigh-WLow)
    WR3 = WHigh+(2*(WPivot-WLow))
    WS3 = WLow-(2*(WHigh-WPivot))
    
    TImeframe(Default) //lower than daily
    
    //Daily pivots can be calculated natively on intraday timeframes
    
    If dayofweek = 1 Then //neccesary to avoid Sunday bars
    
    DPivot = (DHigh(2) + DLow(2) + DClose(2))/3 //Daily Pivot Calculation
    
    DR1 = DPivot*2-Dlow(2)
    DS1 = DPivot*2-DHigh(2)
    DR2 = DPivot+(DHigh(2)-DLow(2))
    DS2 = DPivot-(DHigh(2)-DLow(2))
    DR3 = DHigh(2)+(2*(DPivot-DLow(2)))
    DS3 = DLow(2)-(2*(DHigh(2)-DPivot))
    
    Else
    
    DPivot = (DHigh(1) + DLow(1) + DClose(1))/3 //Daily Pivot Calculation
    
    DR1 = DPivot*2-Dlow(1)
    DS1 = DPivot*2-DHigh(1)
    DR2 = DPivot+(DHigh(1)-DLow(1))
    DS2 = DPivot-(DHigh(1)-DLow(1))
    DR3 = DHigh(1)+(2*(DPivot-DLow(1)))
    DS3 = DLow(1)-(2*(DHigh(1)-DPivot))
    EndIf
    Stocks008 thanked this post
Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.

Pivot strategy backtest


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Stocks008 @jd008 Participant
Summary

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

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 02/10/2020
Status: Active
Attachments: No files
Logo Logo
Loading...