Request for a simple double time frame strategy to be coded for spread betting

Viewing 15 posts - 1 through 15 (of 20 total)
  • Author
    Posts
  • #141184 quote
    AugustusKing
    Participant
    Junior

    Request for a simple double-time-frame strategy to be coded for spread betting

    I’d love to ask you clever folk on this forum for some help in creating a ProRealCode system that uses 2 timeframes, 200 EMAs and a MACD on the shorter time frame chart.

    Here’s the proposed system for testing on EUR/USD (and other major FX pairs):

    LONG TRADE

    If price is > 200EMA on DAILY chart then execute LONG only trades

    [and]

    If price is > 200EMA on 30 min chart

    [and]

    If there’s a bullish cross on the MACD on the 30 min chart (where the 12ma moves up through the 26ma regardless of where it is).

    [If ALL of the above, then]

    BUY 1 / pip on next candle open

    STOP LOSS: 50 pips (Spread betting, so not using ‘LOTS’)

    TAKE PROFIT: 100 pips (Spread betting, so not using ‘LOTS’)

     


     

    SHORT TRADE

    If price is < 200EMA on DAILY chart then execute SHORT only trades

    [and]

    If price is < 200EMA on 30 min chart

    [and]

    If there’s a bearish cross on the MACD on the 30 min chart (where the 12ma moves down through the 26ma regardless of where it is).

    [If ALL of the above, then]

    SELL 1 / pip on next candle open

    STOP LOSS: 50 pips (Spread betting, so not using ‘LOTS’)

    TAKE PROFIT: 100 pips (Spread betting, so not using ‘LOTS’)

     


     

    BONUS POINTS

    I’d also love to see how this model would work if used with a TRAILING STOP LOSS rather than a fixed risk/reward. If you felt inclined to also include a version of the code with a trailing stop loss then, I’d probably want to test it trailing by say 25 pips.

     


    QUESTIONS?

    I’m open to them 🙂

     

    Thank you so much for taking the time to read and consider this, I’d be very grateful for any help or comments on this from the community.

    All the best!

    Matt

    #141185 quote
    AugustusKing
    Participant
    Junior

    *I meant to say in parenthesis next to the MACD specification that “when the MACD line moves through the 9ma Signal line”

    #141186 quote
    AugustusKing
    Participant
    Junior

    This is what I have so far from trying to do it myself. But I can’t add the second time frame on the Daily in for another level of confirmation of the trend (above the 200ema for bullish and visa versa).

    // Definition of code parameters
    
    DEFPARAM CumulateOrders = False // Cumulating positions deactivated
    
    // Conditions to enter long positions
    
    indicator1 = ExponentialAverage[200](close)
    
    c1 = (close > indicator1)
    
    indicator2 = MACDline[12,26,9](close)
    
    indicator3 = MACDSignal[12,26,9](close)
    
    c2 = (indicator2 CROSSES OVER indicator3)
    
    IF c1 AND c2 THEN
    
    BUY 1 SHARES AT MARKET
    
    ENDIF
    
    // Conditions to enter short positions
    
    indicator4 = ExponentialAverage[200](close)
    
    c3 = (close < indicator4)
    
    indicator5 = MACDline[12,26,9](close)
    
    indicator6 = MACDSignal[12,26,9](close)
    
    c4 = (indicator5 CROSSES UNDER indicator6)
    
    IF c3 AND c4 THEN
    
    SELLSHORT 1 SHARES AT MARKET
    
    ENDIF
    
    // Stops and targets
    
    SET STOP pLOSS 50
    
    SET TARGET pPROFIT 100
    #141187 quote
    robertogozzi
    Moderator
    Master

    Always use the ‘Insert PRT Code’ button when putting code in your posts to make it easier for others to read.

    I tidied it up for you.

    Thank you 🙂

    AugustusKing thanked this post
    #141191 quote
    GraHal
    Participant
    Master

    Try below, not tested, but you get the general idea?

    Timeframe (Daily, updateonclose)
    
    indDaily = ExponentialAverage[200](close)
    cDaily =  (close > indDaily)
    
    Timeframe (30 mn, updateonclose)
    
    ind30m = ExponentialAverage[200](close)
    c30m = (close > ind30m)
    
    AugustusKing thanked this post
    #141203 quote
    AugustusKing
    Participant
    Junior

    Ah excellent, many thanks for looking at this for me, it means a lot that you’ve taken the time.

    So, excuse my ignorance, that’s why I’m here haha, but do I just insert this into the pre-existing code that I’ve posted above? Will this take into account the MACD trigger crossovers that I use for entry if the price is above the 2 time-frame EMAs?

     

    Cheers

     

    Matt

    #141234 quote
    GraHal
    Participant
    Master

    Below is the Buy side, you do the SellShort and post the full code please.

    DEFPARAM CumulateOrders = False // Cumulating positions deactivated
     
    // Conditions to enter long positions
    
    Timeframe (Daily, updateonclose)
     
    indDaily = ExponentialAverage[200](close)
    cDaily =  (close > indDaily)
     
    Timeframe (30 mn, updateonclose)
     
    ind30m = ExponentialAverage[200](close)
    c30m = (close crosses over ind30m)
    
    //indicator1 = ExponentialAverage[200](close)
     
    //c1 = (close > indicator1)
     
    //indicator2 = MACDline[12,26,9](close)
     
    //indicator3 = ExponentialAverage[9](MACDline[12,26,9](close))
     
    //c2 = (indicator2 CROSSES OVER indicator3)
     
    IF cDaily AND c30m THEN
     
    BUY 1 SHARES AT MARKET
     
    ENDIF
    #141235 quote
    GraHal
    Participant
    Master

    Above is not quite what you want as I have rem’d out C2 (I’ve had a few wines and beers! 🙂 ).

    But, in your original code, you had the the MACD Signal line incorrectly coded, it should be …

    indicator3 = ExponentialAverage[9](MACDline[12,26,9](close))
    AugustusKing thanked this post
    #141245 quote
    AugustusKing
    Participant
    Junior

    Thanks for that, I’ve really battled with it this evening, but like you, I’m a couple of G&Ts and some craft ales deep, and now I’m not seeing the wood for the trees. But I didn’t want to end the day without something to show for our efforts.

    I ended up running this, and it’s miles away from the expected results I had from manual backtesting, so I think I’m back to the drawing board.

    Here’s the code I ended up with:

    DEFPARAM CumulateOrders = False // Cumulating positions deactivated
     
    // Conditions to enter long positions
     
    Timeframe (Daily, updateonclose)
     
    indDaily = ExponentialAverage[200](close)
    cDaily =  (close > indDaily)
     
    Timeframe (30 mn, updateonclose)
     
    ind30m = ExponentialAverage[200](close)
    c30m = (close crosses over ind30m)
     
    indicator1 = ExponentialAverage[200](close)
     
    c1 = (close > indicator1)
     
    indicator2 = MACDline[12,26,9](close)
     
    indicator3 = ExponentialAverage[9](MACDline[12,26,9](close))
     
    c2 = (indicator2 CROSSES OVER indicator3)
     
    IF cDaily AND c30m AND c1 AND c2 THEN
     
    BUY 1 SHARES AT MARKET
     
    ENDIF
    
    // Conditions to enter short positions
     
    Timeframe (Daily, updateonclose)
     
    indDaily = ExponentialAverage[200](close)
    cDailySell =  (close < indDaily)
     
    Timeframe (30 mn, updateonclose)
     
    ind30m = ExponentialAverage[200](close)
    c30mSell = (close crosses under ind30m)
     
    indicator1 = ExponentialAverage[200](close)
     
    c3 = (close < indicator1)
     
    indicator2 = MACDline[12,26,9](close)
     
    indicator3 = ExponentialAverage[9](MACDline[12,26,9](close))
     
    c4 = (indicator3 CROSSES UNDER indicator2)
     
    IF cDaily AND c30mSell AND c3 AND c4 THEN
     
    SELL 1 SHARES AT MARKET
     
    ENDIF
    
    // Stops and targets
     
    SET STOP pLOSS 50
     
    SET TARGET pPROFIT 100

    and the corresponding results are attached.

    However, the code I had above in the thread yielded much better results over the year, and this wasn’t using multiple time frames (also see results attached)

    I think something’s amiss, to be honest, but I’m too confused and don’t know how to code myself, I just use the ‘simplified creation’ bit normally, or I take programmed code and tinker using as much common sense/logic as I’m able to muster haha.

    Anyway, back to the drawing board. I really appreciate the help on trying to code this, my expectations of how it should perform (based on manual backtesting on GBP/USD over 7 months in 2020 are:

    • Profit = 11%
    • Trades = 37
    • Win Rate = 76.19%
    • Max Consecutive Wins = 3
    • Max Consecutive Losses = 4
    • Max Drawdown = 7%
    • Max Run up = 12%

    Hope you’ve had a great weekend GraHal. I’m sure as I get more involved in the community I’ll be able to better solve errors and contribute. But I have to find the time to start formerly learning how first!

    Cheers

     

    Matt

    Screenshot-2020-08-09-at-20.16.21.png Screenshot-2020-08-09-at-20.16.21.png Screenshot-2020-08-09-at-20.03.33.png Screenshot-2020-08-09-at-20.03.33.png
    #141248 quote
    GraHal
    Participant
    Master

    Hahah … what a pair we are!

    In my (drunken) opinion 🙂 No I’ve sobered up a bit now!

    You don’t need C1 and C2  now on the Buy side nor C3 and C4 as you have replaced with CDaily and C30m.

    Opening a Short is made using SellShort (not Sell as you have above) so I’m not surprised your results  are all over the place ! 🙂

    Come back Roberto … all is forgiven! 🙂

    #141249 quote
    AugustusKing
    Participant
    Junior

    Hmm, that’s really odd, I couldn’t get it to even show results unless I did it like that. It just kept showing ‘No Data’. Do you happen to get any results show up on a backtest of the code snippet you gave me above for the buy-side trades? I’m not getting it to show any results.

    #141254 quote
    GraHal
    Participant
    Master

    In below, why didn’t you use CDailySell ??

    There are so many things not correct that it is best to post your latest code as you correct it.

     

    IF cDaily AND c30mSell AND c3 AND c4 THEN
     
    SELL 1 SHARES AT MARKET
     
    ENDIF
    #141255 quote
    AugustusKing
    Participant
    Junior

    Principally because I don’t know how to programme trading systems, and was asking for help haha. So I’m just guessing how to do it, and using trial and error. I originally did use cDailySell (which I made up in the first place), but then I figured that it would have been a constant/the same on both the buy and sell side. So, only changed the bits that I thought were different for a sell trade, as opposed to what you gave me for the buy trade.

    I don’t really have a latest code version at present other than what had been shared, because none of it came close sadly.

    #141258 quote
    GraHal
    Participant
    Master

    Ha … best way to learn is to try try and try again.

    I am not the best at coding and I am watching a film with one eye and trying to help you at same time.

    Why don’t you just get the Buy side working first as that means that there is only half the code to keep changing etc?

    #141259 quote
    robertogozzi
    Moderator
    Master

    I also had a pizza and a beer, but I think this should do:

    Defparam CumulateOrders = false
    //
    Timeframe(Daily,UpdateOnClose)
    EmaD         = average[200,1](close)
    //
    TimeFrame(30 minute,UpdateOnClose)
    Ema30        = average[200,1](close)
    MyMACD       = ExponentialAverage[12](close) - ExponentialAverage[26](close)
    MySignalLine = ExponentialAverage[9](MyMACD)
    //MyHisto    = MyMACD - MySignalLine
    //
    TimeFrame(Default)
    L1           = close > EmaD
    L2           = close > Ema30
    L3           = MyMACD CROSSES OVER  MySignalLine
    Lcond        = L1 AND L2 AND L3 AND Not OnMarket
    //
    S1           = close < EmaD
    S2           = close < Ema30
    S3           = MyMACD CROSSES UNDER MySignalLine
    Scond        = S1 AND S2 AND S3 AND Not OnMarket
    //
    IF Lcond THEN
       BUY 1 Contract at Market        //Contracts/Lots/Perpont make no difference
    ELSIF Scond THEN
       SELLSHORT 1 Contract at Market
    ENDIF
    SET STOP   pLOSS   50
    SET TARGET pPROFIT 100
    GraHal and AugustusKing thanked this post
Viewing 15 posts - 1 through 15 (of 20 total)
  • You must be logged in to reply to this topic.

Request for a simple double time frame strategy to be coded for spread betting


ProOrder: Automated Strategies & Backtesting

New Reply
Author
Summary

This topic contains 19 replies,
has 3 voices, and was last updated by GraHal
5 years, 6 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 08/09/2020
Status: Active
Attachments: 2 files
Logo Logo
Loading...