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 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