Mean Reverting Strategy on AUDCAD on h1 timeframe

Viewing 15 posts - 1 through 15 (of 37 total)
  • Author
    Posts
  • #64053 quote
    Francesco78
    Participant
    Master

    This is a mean reverting strategy that I currently run live on AUDCAD.

    Enjoy, comment and improve 🙂

    //-------------------------------------------------------------------------
    // Main code : audcad_meanrevert_v2
    //-------------------------------------------------------------------------
    defparam cumulateorders = false
    
    n = 1
    m = 3
    l = 90
    
    
    cs= summation[n](close>open) = n
    cs = cs  and close>bollingerup[20](close)  and close>dlow(2)
    cl= summation[m](close<open) = m
    cl = cl   and close<bollingerdown[20](close) and close<Dhigh(2)
    
    
    size = 2
    //entry conditions
    if cs   then
    sellshort size contract at market
    endif
    
    if cl  then
    buy size contract at market
    endif
    
    //exit conditions
    if shortonmarket and close<average[l](close) then
    exitshort at market
    endif
    
    
    if longonmarket and close>average[l](close) then
    sell at market
    endif
    audcadmrh-1519740014c4pl8.png audcadmrh-1519740014c4pl8.png audcad_mr_h1_stat.png audcad_mr_h1_stat.png
    #64416 quote
    Nicolas
    Keymaster
    Master

    Thank you Francesco, going back to mid 2010, the strategy is still quite good in tick by tick backtesting. Did you try some WalkForward analysis since you seem to have optimized the variables? I know there are only 3 of them, but should be interesting to know how it performs with WF. I launched one just now, I’ll get back with results.

    #64424 quote
    juanj
    Participant
    Master

    I threw my Heuristics Algorithm in there to optimize the average[l] variable;

    Now results are looking good on other AUD pairs like AUDUSD as well.

    //-------------------------------------------------------------------------
    //-------------------------------------------------------------------------
    // Main code : audcad_meanrevert_v2
    //-------------------------------------------------------------------------
    defparam cumulateorders = false
    
    // Heuristics Algorithm Start
    
    once OPT = 1
    
    If OPT < 2 and onmarket then
    OPT = 0
    ElsIf countofposition = 0 Then
    OPT = 1
    EndIF
    
    If OPT = 0 Then
    optimize = optimize + 1
    OPT = 2
    EndIf
    
    ////////////////////////////////////////
    
    once periods = 100
    Increment = 14
    MaxIncrement = 4
    Reps = 20 //Number of bars to use for analysis
    once PIncPos = 1 //Positive Increment Position
    once NIncPos = 1 //Neative Increment Position
    once Optimize = 0 ////Initialize Heuristicks Engine Counter (Must be Incremented at Position Start or Exit)
    once Mode = 1 //Switches between negative and positive increments
    once WinCountB = 0 //Initialize Best Win Count
    once StratAvgB = 0 //Initialize Best Avg Strategy Profit
    
    If Optimize = Reps Then
    WinCountA = 0 //Initialize current Win Count
    StratAvgA = 0 //Initialize current Avg Strategy Profit
    
    For i = 1 to Reps Do
    If positionperf(i) > 0 Then
    WinCountA = WinCountA + 1 //Increment Current WinCount
    EndIf
    StratAvgA = StratAvgA + positionperf(i)
    Next
    StratAvgA = StratAvgA/Reps //Calculate Current Avg Strategy Profit
    
    If StratAvgA >= StratAvgB Then
    StratAvgB = StratAvgA //Update Best Strategy Profit
    BestA = Periods
    EndIf
    
    If WinCountA >= WinCountB Then
    WinCountB = WinCountA  //Update Best Win Count
    BestB = Periods
    EndIf
    
    If WinCountA > WinCountB and StratAvgA > StratAvgB Then
    Mode = 0
    ElsIf WinCountA < WinCountB and StratAvgA < StratAvgB and Mode = 1 Then
    Periods = Periods - (Increment*NIncPos)
    NIncPos = NIncPos + 1
    Mode = 2
    ElsIf WinCountA >= WinCountB or StratAvgA >= StratAvgB and Mode = 1 Then
    Periods = Periods + (Increment*PIncPos)
    PIncPos = PIncPos + 1
    Mode = 1
    ElsIf WinCountA < WinCountB and StratAvgA < StratAvgB and Mode = 2 Then
    Periods = Periods + (Increment*PIncPos)
    PIncPos = PIncPos + 1
    Mode = 1
    ElsIf WinCountA > WinCountB or StratAvgA > StratAvgB and Mode = 2 Then
    Periods = Periods - (Increment*NIncPos)
    NIncPos = NIncPos + 1
    Mode = 2
    EndIf
    
    If NIncPos > MaxIncrement or PIncPos > MaxIncrement Then
    If BestA = BestB Then
    Periods = BestA
    Else
    Periods = (BestA+BestB)/2
    EndIf
    NIncPos = 1
    PIncPos = 1
    EndIF
    
    Optimize = 0
    EndIf
    
    GRAPH Periods AS "Periods"
    
    // Heuristics Algorithm End
    
    n = 1
    m = 3
    l = periods//90
     
    cs= summation[n](close>open) = n
    cs = cs  and close>bollingerup[20](close)  and close>dlow(2)
    cl= summation[m](close<open) = m
    cl = cl   and close<bollingerdown[20](close) and close<Dhigh(2)
     
     
    size = 2
    //entry conditions
    if cs   then
    sellshort size contract at market
    endif
     
    if cl  then
    buy size contract at market
    endif
     
    //exit conditions
    if shortonmarket and close<average[l](close) then
    exitshort at market
    endif
    
    if longonmarket and close>average[l](close) then
    sell at market
    endif
    Francesco78, Nicolas, manel and GraHal thanked this post
    #64425 quote
    Francesco78
    Participant
    Master

    Hi Nicolas, yes I did WF test of course.

    Ill be curious to see the results since 2010. Many thanks

    Francesco

    WFtest.png WFtest.png
    #64435 quote
    Nicolas
    Keymaster
    Master

    Sorry, I forgot to attach the result in my first post.

    You’ll find attached 3 different pictures, first with WF non anchored, second one with WF anchored and the last one with the variables you are using but with 200k bars backtest.

    As you can see all the OOS periods make huge gain while the IS period is not performing so well, it is a sign of overfit. It doesn’t mean that the strategy is bad, it means that it should need refining with some extra filters or more OOS periods to optimize more often.


    @juanj
    Thank you, will study this version.

    Francesco78 thanked this post
    WF-non-anchored.png WF-non-anchored.png WF-anchored.png WF-anchored.png normal-backtest.png normal-backtest.png
    #64503 quote
    Francesco78
    Participant
    Master

    Thank you Nicolas, can you claryfy for my own benefit why you think that adding filter in this case could be effecting on reducing overfitting?

    Thanks!

    #64559 quote
    Nicolas
    Keymaster
    Master

    The question is, why does the strategy is not performing as well in first IS as in OOS? 2012 crisis? I meant to study entries depending of the market behavior in this case, of course, more variables mean more overfit… But as I said, maybe it could be solved by adding more IS+OOS repetitions.

    #65330 quote
    Nicolas
    Keymaster
    Master

    Well, I think it could be now added to the library, with the WFA attached, what do you think @Francesco78 ?

    #65358 quote
    Francesco78
    Participant
    Master

    @Nicolas

    Thank you, I am fine with it, let me know if you want me to make some amendments on my original post.

    #65490 quote
    Francesco78
    Participant
    Master

    @Nicolas

    What shoud I do to put it on the library?

    Thank you

    #65528 quote
    Nicolas
    Keymaster
    Master

    Hey Francesco, I’ll do it for you on Monday 😉

    Francesco78 thanked this post
    #69022 quote
    Dymjohn
    Participant
    Senior

    For what it is worth I tried tackling the large value of drawdowns compared to winning trades. I noticed the losing trades tend to have a larger number of bars so I optimized bars to < 80 bars which on 100000 bars reduced profit slightly 11384.4 down to 11015.20.  It did reduce the maximum loss to 415.2o from 761.00.

    The code for this change is attached

    defparam cumulateorders = false
    
    n = 1
    m = 3
    l = 90
    once Trade = 1
    
    cs= summation[n](close>open) = n
    cs = cs  and close>bollingerup[20](close)  and close>dlow(2)
    cl= summation[m](close<open) = m
    cl = cl   and close<bollingerdown[20](close) and close<Dhigh(2)
    
    
    size = 2
    //entry conditions
    if cs   then
    sellshort size contract at market
    endif
    
    if cl  then
    buy size contract at market
    endif
    if Barindex - Tradeindex < 80 then
    Trade =1
    else
    Trade = 0
    endif
    
    //exit conditions
    if shortonmarket and close<average[l](close) or Trade = 0 then
    exitshort at market
    endif
    
    
    if longonmarket and close>average[l](close)or Trade = 0 then
    sell at market
    endif
    
    Francesco78 thanked this post
    #69796 quote
    Jan
    Participant
    Veteran

    Thank you very much,  Francesco, for the strategy, which seems solid and robust !

    I tried this on AUD CAD 30 minutes and it works also quite well.

    Extended variables :   S for short average close

    With 15.000 bars from 7 February till 4 May 2018 it gives a robust profit of 2.945 CAD in 3 months (I did no IS/OOS testing) with M=3, N=2, L = 50 and S = 40, in 398 trades, spread 1,7 pips calculated

    I also tried a maximum of one trade per day, with adding the condition ” OneTradePerDay = IntradayBarIndex < (Barindex – TradeIndex(1)) ”

    This reduces the amount of trades with more then half, 180 trades, giving a robust profit of 2.354 CAD, with  M=3, N=5, L = 50 and S = 90  (also I did no IS/OOS testing so far)

    ========================================================

    For other currencies and indices I am still testing

    So far for the FTSE with 2 contracts, exposure more than 30.000 GBP (!), it gives interesting profit of 3.900 GBP on the 15 minutes bar chart with 15.000 bars, from an a maximum of one trade per day, from 6 september 2017 till 4 May 2018,  with  M=1, N=3, L = 50 and S = 40  (also I did no IS/OOS testing so far !!)

    The risk of overfitting for this kind of short periods is very big !! When testing for 100.000 bars the profit especially in the beginning, completely deminish !!!!!!!, but the endresult is quiet the same !

    When testing the FTSE for 100.000 15 minutes-bars with not restricted trades per day, the NON- robust profit is  a bit lower as when restricted to one trade per day, but not robust and a bit lower.

    Kind regards,

    Jan

    Francesco78 thanked this post
    #69798 quote
    Francesco78
    Participant
    Master

    Thank you Jan!

    #70240 quote
    M. Pettersson
    Participant
    Average

    Really nice strategy !

    I’m running it in my demo account to try it out.

    Thanks Francesco.

    Francesco78 thanked this post
Viewing 15 posts - 1 through 15 (of 37 total)
  • You must be logged in to reply to this topic.

Mean Reverting Strategy on AUDCAD on h1 timeframe


ProOrder: Automated Strategies & Backtesting

New Reply
Author
Summary

This topic contains 36 replies,
has 13 voices, and was last updated by Yngve
7 years, 7 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 03/05/2018
Status: Active
Attachments: 9 files
Logo Logo
Loading...