Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

SetStopLoss in Strategy not submitted in Market Replay Modus

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

    #16
    Hello RolfTheus,

    The error message:

    **NT** Error on calling 'OnExecution' method for strategy 'MySampleMACrossOver/6dc5b53172494f85a53dd1b43d1e3d5d': Object reference not set to an object instance.
    Means that you are trying to access an object, that has not been defined yet or is null. This maybe due to accessing entryOrder inside of OnExecution() without checking it for null first.

    For example:

    Code:
    protected override void OnExecution(IExecution execution)
    {
            if (entryOrder != null)
    	{
                     Print("OnExecution entryOrder: " + entryOrder +" execution.Order: " + execution.Order);
            }
            // Rest of Code
    }
    JCNinjaTrader Customer Service

    Comment


      #17
      Hallo,

      yes i know, but if you see the code the entryOrder cannot be null because when onExcecution is called it is initialized. Or is onExecution random called ?

      i have found that execution.Order ==null and i don't understand why.

      Please test the simple code in Replaymode.

      Thank you
      Rolf Theus

      The modified code:

      public class MySampleMACrossOver : Strategy
      {
      #region Variables
      private int fast = 10;
      private int slow = 25;
      private IOrder entryOrder = null;
      private int internPosSize=1;
      #endregion

      /// <summary>
      /// This method is used to configure the strategy and is called once before any strategy method is called.
      /// </summary>
      protected override void Initialize()
      {
      SMA(Fast).Plots[0].Pen.Color = Color.Orange;
      SMA(Slow).Plots[0].Pen.Color = Color.Green;

      Add(SMA(Fast));
      Add(SMA(Slow));

      CalculateOnBarClose = true;
      }

      protected override void OnExecution(IExecution execution)
      {
      if( execution != null && entryOrder!=null) Print("OnExecution entryOrder: " + entryOrder +" execution.Order: " + execution.Order);

      if (execution != null && execution.Order !=null && (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0)))
      {
      Print("OnExecution 2 execution.Order.Filled:" + execution.Order.Filled +
      " execution.Order.OrderState : " + execution.Order.OrderState +
      " OrderState.Filled: " + OrderState.Filled +
      " OrderState.PartFilled: " + OrderState.PartFilled );
      Print("OnExecution 2 execution.Order.Filled:" + execution.Order.Filled +
      " execution.Order.OrderState : " + execution.Order.OrderState +
      " OrderState.Filled: " + OrderState.Filled +
      " OrderState.PartFilled: " + OrderState.PartFilled );
      }
      }
      /// <summary>
      /// Called on each bar update event (incoming tick).
      /// </summary>
      protected override void OnBarUpdate()
      {
      if (CrossAbove(SMA(Fast), SMA(Slow), 1))
      entryOrder=EnterLong(internPosSize,"Long");
      else if (CrossBelow(SMA(Fast), SMA(Slow), 1))
      entryOrder=EnterShort(internPosSize,"Short");
      }

      #region Properties
      /// <summary>
      /// </summary>
      [Description("Period for fast MA")]
      [GridCategory("Parameters")]
      public int Fast
      {
      get { return fast; }
      set { fast = Math.Max(1, value); }
      }

      /// <summary>
      /// </summary>
      [Description("Period for slow MA")]
      [GridCategory("Parameters")]
      public int Slow
      {
      get { return slow; }
      set { slow = Math.Max(1, value); }
      }
      #endregion
      }
      }
      Last edited by RolfTheus; 02-17-2014, 12:02 PM.

      Comment


        #18
        Hello RolfTheus,

        OnExecution is not called at random, but you want to make sure that the variable you are trying to access has a value.

        In looking at your logs OnExecution() is going to be called for you ExitOnClose order since you have it set to true.
        JCNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by charlesugo_1, 05-26-2026, 05:03 PM
        0 responses
        57 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
        161 views
        0 likes
        Last Post CarlTrading  
        Started by CarlTrading, 05-10-2026, 08:12 PM
        0 responses
        97 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