Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

EMA Cross

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    EMA Cross

    Hello,

    I have developed a simple EMA cross strategy. When a cross occurs, it should buy at the beginning of the next period. What I am finding in backtesting is that it buys 15 minutes too late almost consistently.

    Can someone tell me what I might be doing wrong? Thank you.

    Code:
        public class SimpleEMA : Strategy
        {
            private int fast = 1; // Default setting for Fast
            private int slow = 1; // Default setting for Slow
            private int shares = 100; // Default setting for Shares
          
            protected override void Initialize()
            {
                EMA(Fast).Plots[0].Pen.Color = Color.Orange;
                EMA(Slow).Plots[0].Pen.Color = Color.Green;
                
                Add(EMA(Fast));
                Add(EMA(Slow));
    
                CalculateOnBarClose = true;
            }
          
            protected override void OnBarUpdate()
            {
                if (CrossAbove(EMA(Fast), EMA(Slow), 1))
                {
                    ExitShort();
                    EnterLong(this.Shares, "");
                }
                else if (CrossBelow(EMA(Fast), EMA(Slow), 1))
                {
                    ExitLong();
                    EnterShort(this.Shares, "");
                 }
            }                
        
            public int Fast
            {
                get { return fast; }
                set { fast = Math.Max(1, value); }
            }
    
            public int Slow
            {
                get { return slow; }
                set { slow = Math.Max(1, value); }
            }
    
            public int Shares
            {
                get { return shares; }
                set { shares = Math.Max(100, value); }
            }
        }
    }

    #2
    Hi Jeff, are you running this strategy on 15 minute data by chance?
    AustinNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Austin View Post
      Hi Jeff, are you running this strategy on 15 minute data by chance?
      Hi Austin, yes, sorry, should have mentioned that. Running it on 15 minute data. Thanks.

      Comment


        #4
        Jeff, in general, if you're doing a simple backtest without any added detail, orders will execute one bar 'late'.

        CalculateOnBarClose always is true during a backtest, so that means all calculations are done only once at the end of the bar (which is also the beginning of the next bar--the open of a bar isn't known until the bar before it closes). Then, once the conditions are true, an order is submitted (at the close). This order is then executed at the beginning of the next bar. In your case, this would be 15 minutes later.

        If you'd like to add more detail to your backtest, please see the Backtesting NinjaScript Strategies with an intrabar granularity reference page.
        AustinNinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Austin View Post
          Jeff, in general, if you're doing a simple backtest without any added detail, orders will execute one bar 'late'.
          Great, thanks very much.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by CarlTrading, 03-31-2026, 09:41 PM
          1 response
          63 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by CarlTrading, 04-01-2026, 02:41 AM
          0 responses
          35 views
          0 likes
          Last Post CarlTrading  
          Started by CaptainJack, 03-31-2026, 11:44 PM
          0 responses
          54 views
          1 like
          Last Post CaptainJack  
          Started by CarlTrading, 03-30-2026, 11:51 AM
          0 responses
          61 views
          0 likes
          Last Post CarlTrading  
          Started by CarlTrading, 03-30-2026, 11:48 AM
          0 responses
          48 views
          0 likes
          Last Post CarlTrading  
          Working...
          X