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 Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          648 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          369 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          108 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          572 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          573 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X