Hi all,
I am looking for some help with coding this for PRT charts as I need to backtest something. It is a fairly simple analysis, just want to see the % of time the price reacts to pivot levels in Forex and by how much.
Chart set-up:
- Forex pairs major and minor
- Daily, Weekly and Monthly Pivots (Main pivot, S1,S2,S3, R1,R2,R3) Just these levels for all three.
I want to check the following on selected pairs:
- Within a given period, how many times did the price bounce off the daily, weekly or monthly pivot (within 2-3 pips of the pivot considering the spread)
- Stop loss would be between 7-20 pips (I need to able to test different variables here)
- Take profit R:R 1:2, 1:3 etc
I do not know coding, I am assuming more specifics may be required. Just wanted to give a brief info, not sure if this kind of backtesting is even possible on PRT.
Thanks in advance to anyone who responds.
Happy New Year to all!
Mike
Happy New Year Mike, do you need a screener or a strategy?
Hi
Thank you. I want to backtest this, so I am guessing that would be a strategy. Since pivots have an exact calculation, I wanted to test several pairs to find out which one reacts to them the most etc. Does that make sense? And since I have no idea how to code, this is like rocket science to me.
I am sure it would be possible to do this?
Hi kadziak, I already started coding it, but I need some more time to finish it 🙂
Thank you so much, very kind of you
I just finished the MONTHLY test. It took quite a long time to code it in my spare time!
Adding the DAILY and WEEKLY tests will not be as time consuming as the MONTHLY-only version, as it will be a simple copy & paste with some minor editing, but I first want to know your feedback before going ahead, just to avoid wasting time in case the code does not meet your criteria.
I am posting this very first code, which is almost 300 lines, but next times, after finishing it, I will only attach the ITF file, as it will grow well over 500 hundreds lines, making the code and the post very tough to read:
// https://www.prorealcode.com/topic/need-help-with-coding-this/
Timeframe(default)
ONCE FromDate = 20200101 //00000000 to start testing from the first bar loaded
ONCE ToDate = 20231231 //99999999 to finish testing on the current date
ONCE PipBuffer = 3 * PipSize
//
ONCE TradeType = 1 //1=Monthly, 2=Weekly, 3=Daily, 9=all
ONCE LONGenabled = 1 //1=enable LONG trades, 0=disable LONG trades
ONCE SHORTenabled = 1 //1=enable SHORT trades, 0=disable SHORT trades
//
ONCE WeeklySL = 20 * PipSize
ONCE WeeklyTP = WeeklySL * 3
//
ONCE DailySL = 20 * PipSize
ONCE DailyTP = DailySL * 3
//
TradeON = 0
IF (OpenDate >= FromDate) AND (OpenDate <= ToDate) THEN
TradeON = 1
ENDIF
//
Timeframe(Monthly,UpdateOnClose)
ONCE MonthlySL = 20 * PipSize
ONCE MonthlyTP = MonthlySL * 3
//
ONCE MonthlyWinLp = 0
ONCE MonthlyWinSp = 0
ONCE MonthlyLossLp = 0
ONCE MonthlyLossSp = 0
//
ONCE MonthlyWinSr1 = 0
ONCE MonthlyLossSr1 = 0
ONCE MonthlyWinSr2 = 0
ONCE MonthlyLossSr2 = 0
ONCE MonthlyWinSr3 = 0
ONCE MonthlyLossSr3 = 0
//
ONCE MonthlyWinLs1 = 0
ONCE MonthlyLossLs1 = 0
ONCE MonthlyWinLs2 = 0
ONCE MonthlyLossLs2 = 0
ONCE MonthlyWinLs3 = 0
ONCE MonthlyLossLs3 = 0
//
ONCE BouncePvtM = 0
ONCE BounceRes1M = 0
ONCE BounceRes2M = 0
ONCE BounceRes3M = 0
ONCE BounceSus1M = 0
ONCE BounceSup2M = 0
ONCE BounceSup3M = 0
//
ONCE MonthlyPvtT = 0
ONCE MonthlyRes1T = 0
ONCE MonthlyRse2T = 0
ONCE MonthlyRes3T = 0
ONCE MonthlySup1T = 0
ONCE MonthlySup2T = 0
ONCE MonthlySup3T = 0
//
PivotM = (High[1] + Low[1] + Close[1]) / 3
Res1M = PivotM + (PivotM - Low)
Res2M = PivotM + (High - Low)
Res3M = High + (2 * (PivotM - Low))
Sup1M = PivotM - (High - PivotM)
Sup2M = PivotM - (High - Low)
Sup3M = Low - (2 * (High - PivotM))
IF TradeON AND ((TradeType = 1) OR (TradeType = 9)) THEN
//---------------------------------------------------------------------------
// LONG exit
//---------------------------------------------------------------------------
IF MonthlyPvtT = 1 THEN
IF high >= MonthlyTPPvt THEN //TP
MonthlyWinLp = MonthlyWinLp + 1
MonthlyPvtT = 0
ELSIF low <= MonthlyTPPvt THEN //SL
MonthlyLossLp = MonthlyLossLp + 1
MonthlyPvtT = 0
ENDIF
ENDIF
//
IF MonthlySup1T THEN
IF high >= MonthlyTPSup1 THEN //TP
MonthlyWinLs1 = MonthlyWinLs1 + 1
MonthlySup1T = 0
ELSIF low <= MonthlyTPSup1 THEN //SL
MonthlyLossLs1 = MonthlyLossLs1 + 1
MonthlySup1T = 0
ENDIF
ENDIF
//
IF MonthlySup2T THEN
IF high >= MonthlyTPSup2 THEN //TP
MonthlyWinLs2 = MonthlyWinLs2 + 1
MonthlySup2T = 0
ELSIF low <= MonthlyTPSup2 THEN //SL
MonthlyLossLs2 = MonthlyLossLs2 + 1
MonthlySup2T = 0
ENDIF
ENDIF
//
IF MonthlySup3T THEN
IF high >= MonthlyTPSup3 THEN //TP
MonthlyWinLs3 = MonthlyWinLs3 + 1
MonthlySup3T = 0
ELSIF low <= MonthlyTPSup3 THEN //SL
MonthlyLossLs3 = MonthlyLossLs3 + 1
MonthlySup3T = 0
ENDIF
ENDIF
//---------------------------------------------------------------------------
// SHORT exit
//---------------------------------------------------------------------------
IF MonthlyPvtT = 2 THEN
IF low <= MonthlyTPPvt THEN //TP
MonthlyWinSp = MonthlyWinSp + 1
MonthlyPvtT = 0
ELSIF high >= MonthlyTPPvt THEN //SL
MonthlyLossSp = MonthlyLossSp + 1
MonthlyPvtT = 0
ENDIF
ENDIF
//
IF MonthlyRes1T THEN
IF low <= MonthlyTPRes1 THEN //TP
MonthlyWinLs1 = MonthlyWinLs1 + 1
MonthlyRes1T = 0
ELSIF high >= MonthlyTPRes1 THEN //SL
MonthlyLossLs1 = MonthlyLossLs1 + 1
MonthlyRes1T = 0
ENDIF
ENDIF
//
IF MonthlyRes2T THEN
IF low <= MonthlyTPRes2 THEN //TP
MonthlyWinLs2 = MonthlyWinLs2 + 1
MonthlyRes2T = 0
ELSIF high >= MonthlyTPRes2 THEN //SL
MonthlyLossLs2 = MonthlyLossLs2 + 1
MonthlyRes2T = 0
ENDIF
ENDIF
//
IF MonthlyRes3T THEN
IF low <= MonthlyTPRes3 THEN //TP
MonthlyWinLs3 = MonthlyWinLs3 + 1
MonthlyRes3T = 0
ELSIF high >= MonthlyTPRes3 THEN //SL
MonthlyLossLs3 = MonthlyLossLs3 + 1
MonthlyRes3T = 0
ENDIF
ENDIF
//---------------------------------------------------------------------------
// LONG entry
//---------------------------------------------------------------------------
IF LONGenabled THEN
xPivotM = PivotM + PipBuffer
yPivotM = PivotM - PipBuffer
IF (close > yPivotM) AND (low <= xPivotM) AND (MonthlyPvtT = 0) THEN
MonthlyPvtT = 1
BouncePvtM = BouncePvtM + 1
MonthlyEntryPvt = close
MonthlyTPPvt = MonthlyEntryPvt + MonthlyTP*PipSize
MonthlySLPvt = MonthlyEntryPvt - MonthlySL*PipSize
ENDIF
//
xSup1M = Sup1M + PipBuffer
ySup1M = Sup1M - PipBuffer
IF (close > ySup1M) AND (low <= xSup1M) AND (MonthlySup1T = 0) THEN
MonthlySup1T = 1
BounceSup1M = BounceSup1M + 1
MonthlyEntrySup1 = close
MonthlyTPSup1 = MonthlyEntrySup1 + MonthlyTP*PipSize
MonthlySLSup1 = MonthlyEntrySup1 - MonthlySL*PipSize
ENDIF
//
xSup2M = Sup2M + PipBuffer
ySup2M = Sup2M - PipBuffer
IF (close > ySup2M) AND (low <= xSup2M) AND (MonthlySup2T = 0) THEN
MonthlySup2T = 1
BounceSup2M = BounceSup2M + 1
MonthlyEntrySup2 = close
MonthlyTPSup2 = MonthlyEntrySup2 + MonthlyTP*PipSize
MonthlySLSup2 = MonthlyEntrySup2 - MonthlySL*PipSize
ENDIF
xSup3M = Sup3M + PipBuffer
ySup3M = Sup3M - PipBuffer
IF (close > ySup3M) AND (low <= xSup3M) AND (MonthlySup3T = 0) THEN
MonthlySup3T = 1
BounceSup3M = BounceSup3M + 1
MonthlyEntrySup3 = close
MonthlyTPSup3 = MonthlyEntrySup3 + MonthlyTP*PipSize
MonthlySLSup3 = MonthlyEntrySup3 - MonthlySL*PipSize
ENDIF
ENDIF
//---------------------------------------------------------------------------
// SHORT entry
//---------------------------------------------------------------------------
IF SHORTenabled THEN
xPivotM = PivotM + PipBuffer
yPivotM = PivotM - PipBuffer
IF (close < xPivotM) AND (high >= yPivotM) AND (MonthlyPvtT = 0) THEN
MonthlyPvtT = 2
BouncePvtM = BouncePvtM + 1
MonthlyEntryPvt = close
MonthlyTPPvt = MonthlyEntryPvt - MonthlyTP*PipSize
MonthlySLPvt = MonthlyEntryPvt + MonthlySL*PipSize
ENDIF
//
xRes1M = Res1M + PipBuffer
yRes1M = Res1M - PipBuffer
IF (close < xRes1M) AND (high >= yRes1M) AND (MonthlyRes1T = 0) THEN
MonthlyRes1T = 1
BounceRes1M = BounceRes1M + 1
MonthlyEntryRes1 = close
MonthlyTPRes1 = MonthlyEntryRes1 - MonthlyTP*PipSize
MonthlySLRes1 = MonthlyEntryRes1 + MonthlySL*PipSize
ENDIF
//
xRes2M = Res2M + PipBuffer
yRes2M = Res2M - PipBuffer
IF (close < xRes2M) AND (high <= yRes2M) AND (MonthlyRes2T = 0) THEN
MonthlyRes2T = 1
BounceRes2M = BounceRes2M + 1
MonthlyEntryRes2 = close
MonthlyTPRes2 = MonthlyEntryRes2 - MonthlyTP*PipSize
MonthlySLRes2 = MonthlyEntryRes2 + MonthlySL*PipSize
ENDIF
xRes3M = Res3M + PipBuffer
yRes3M = Res3M - PipBuffer
IF (close < xRes3M) AND (high >= yRes3M) AND (MonthlyRes3T = 0) THEN
MonthlyRes3T = 1
BounceRes3M = BounceRes3M + 1
MonthlyEntryRes3 = close
MonthlyTPRes3 = MonthlyEntryRes3 - MonthlyTP*PipSize
MonthlySLRes3 = MonthlyEntryRes3 + MonthlySL*PipSize
ENDIF
ENDIF
ENDIF
//===========================================================================
//===========================================================================
Timeframe(Weekly,UpdateOnClose)
//
PivotW = (High[1] + Low[1] + Close[1]) / 3
Res1W = PivotW + (PivotW - Low)
Res2W = PivotW + (High - Low)
Res3W = High + (2 * (PivotW - Low))
Sup1W = PivotW - (High - PivotW)
Sup2W = PivotW - (High - Low)
Sup3W = Low - (2 * (High - PivotW))
//
//===========================================================================
//===========================================================================
Timeframe(Daily,UpdateOnClose)
//
//
PivotD = (High[1] + Low[1] + Close[1]) / 3
Res1D = PivotD + (PivotD - Low)
Res2D = PivotD + (High - Low)
Res3D = High + (2 * (PivotD - Low))
Sup1D = PivotD - (High - PivotD)
Sup2D = PivotD - (High - Low)
Sup3D = Low - (2 * (High - PivotD))
//
Timeframe(default)
buy at -close limit
//
GRAPH MonthlyWinLp AS "Monthly Wins - LONG pvt"
GRAPH MonthlyLossLp AS "Monthly Losses - LONG pvt"
GRAPH MonthlyWinLs1 AS "Monthly Wins - LONG sup1"
GRAPH MonthlyLossLs1 AS "Monthly Losses - LONG sup1"
GRAPH MonthlyWinLs2 AS "Monthly Wins - LONG sup2"
GRAPH MonthlyLossLs2 AS "Monthly Losses - LONG sup2"
GRAPH MonthlyWinLs3 AS "Monthly Wins - LONG sup3"
GRAPH MonthlyLossLs3 AS "Monthly Losses - LONG sup3"
//
GRAPH MonthlyWinSp AS "Monthly Wins - SHORT pvt"
GRAPH MonthlyLossSp AS "Monthly Losses - SHORT pvt"
GRAPH MonthlyWinSr1 AS "Monthly Wins - SHORT res1"
GRAPH MonthlyLossSr1 AS "Monthly Losses - SHORT res1"
GRAPH MonthlyWinSr2 AS "Monthly Wins - SHORT res2"
GRAPH MonthlyLossSr2 AS "Monthly Losses - SHORT res2"
GRAPH MonthlyWinSr3 AS "Monthly Wins - SHORT res3"
GRAPH MonthlyLossSr3 AS "Monthly Losses - SHORT res3"
the attached pic shows how data will be plotted in the variable panel of the backtest.
Lines 2-16 are self-explanatory.
Line 273 (the BUY line) serves no purpose other than abiding by the requirement of ProBackTest.
As to GRAPH, I fear the dubugging lines exceed the limit of ProBackTest, so I suggest to first monitor the LONG performance only by commenting out the SHORT lines, then reverting the comments to test the SHORT performance.
Hi Roberto,
Thank you so much for this, I really appreciate it. I see the date you sent this message was in Feb, for whatever reason I did not receive a message from the portal notifying me of your response. So, apologies for my late reply. I just got a message yesterday with the ITF file.
Once again, thank you very much for the work you put into this. I hope this will work.
Best regards
Mike