EMA crossover strategy fails to close positions

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #104198 quote
    Thally
    Participant
    Average

    Hi,

    I have written a simple EMA crossover strategy which I am now forward testing in demo mode. It is in the market close to 100% of the time and goes long when fast EMA crosses over the slow and vice versa for short positions and long exits. The strategy code is pasted below. The problem I am having is that it seems to be failing to close positions. E.g. Over night last night on the Nasdaq it when short at the appropriate place but failed to close out the position this morning when the fast EMA crossed above the slow (see attached image).

    Does anyone have an idea as to why this is occurring and how it can be resolved?

    Thanks,

    Roj

     

    //-------------------------------------------------------------------------
    // Main code : EMA crossover NASDAQ m3
    //-------------------------------------------------------------------------
    // Definition of code parameters
    DEFPARAM CumulateOrders = False // Cumulating positions deactivated
    
    EMAfast = 9
    EMAslow = 30
    
    //Risk Management
    Capital = 5000
    Risk = 0.01
    StopLoss = abs(close-ExponentialAverage[EMAslow](close))
    
    equity = Capital + StrategyProfit
    maxrisk = round(equity*Risk)
    PositionSize = abs(round((maxrisk/StopLoss)/PointValue)*pipsize)
    
    // Conditions to enter long positions
    indicator1 = ExponentialAverage[EMAfast](close)
    indicator2 = ExponentialAverage[EMAslow](close)
    c1 = (indicator1 CROSSES OVER indicator2)
    
    IF c1 THEN
    BUY PositionSize PERPOINT AT MARKET
    ENDIF
    
    // Conditions to exit long positions
    indicator3 = ExponentialAverage[EMAfast](close)
    indicator4 = ExponentialAverage[EMAslow](close)
    c2 = (indicator3 CROSSES UNDER indicator4)
    
    IF c2 THEN
    SELL AT MARKET
    ENDIF
    
    // Conditions to enter short positions
    indicator5 = ExponentialAverage[EMAfast](close)
    indicator6 = ExponentialAverage[EMAslow](close)
    c3 = (indicator5 CROSSES UNDER indicator6)
    
    IF c3 THEN
    SELLSHORT PositionSize PERPOINT AT MARKET
    ENDIF
    
    // Conditions to exit short positions
    indicator7 = ExponentialAverage[EMAfast](close)
    indicator8 = ExponentialAverage[EMAslow](close)
    c4 = (indicator7 CROSSES OVER indicator8)
    
    IF c4 THEN
    EXITSHORT AT MARKET
    ENDIF
    
    Screenshot-2019-08-05-at-09.11.43.png Screenshot-2019-08-05-at-09.11.43.png
    #104201 quote
    Vonasi
    Moderator
    Master

    Most likely because of the 3 minute time frame that you are using. If the chart that you show was started at a different minute to the minute that the strategy was started at then you will get a different chart. In fact you can get three different charts of the 3 minute time frame depending upon which of the three minutes that you start the chart on!

    #104207 quote
    GraHal
    Participant
    Master

    Have to ask (quicker than thinking as I have to go out) … is the reasoning above the same for, for example, the 5 min Chart??

    #104213 quote
    Vonasi
    Moderator
    Master

    Five minute chart candles are always at 0000 0005 0010 0015 etc but a three minute candle is constructed from whenever the chart or strategy starts. If could be 0000 or 0001 or 0002. The open at 0000 is different to that at 0001 and so we can instantly see that a different three minute candle will be drawn depending on the start time. The close is different too and you are plotting an average of it so it stands to reason that the two different start times will not have the same average returned.

    GraHal thanked this post
    #104215 quote
    Thally
    Participant
    Average

    Thanks very much, I will try m5 or m1 and see how it goes. I chose m3 because it gave the best results in backtesting.

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

EMA crossover strategy fails to close positions


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Thally @rodger_dodger Participant
Summary

This topic contains 4 replies,
has 3 voices, and was last updated by Thally
6 years, 6 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 08/05/2019
Status: Active
Attachments: 1 files
Logo Logo
Loading...