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 charlesugo_1, 05-26-2026, 05:03 PM
          0 responses
          56 views
          0 likes
          Last Post charlesugo_1  
          Started by DannyP96, 05-18-2026, 02:38 PM
          1 response
          143 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by CarlTrading, 05-11-2026, 05:56 AM
          0 responses
          160 views
          0 likes
          Last Post CarlTrading  
          Started by CarlTrading, 05-10-2026, 08:12 PM
          0 responses
          96 views
          0 likes
          Last Post CarlTrading  
          Started by Hwop38, 05-04-2026, 07:02 PM
          0 responses
          276 views
          0 likes
          Last Post Hwop38
          by Hwop38
           
          Working...
          X