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 CarlTrading, 03-31-2026, 09:41 PM
        1 response
        81 views
        1 like
        Last Post NinjaTrader_ChelseaB  
        Started by CarlTrading, 04-01-2026, 02:41 AM
        0 responses
        42 views
        0 likes
        Last Post CarlTrading  
        Started by CaptainJack, 03-31-2026, 11:44 PM
        0 responses
        64 views
        2 likes
        Last Post CaptainJack  
        Started by CarlTrading, 03-30-2026, 11:51 AM
        0 responses
        66 views
        0 likes
        Last Post CarlTrading  
        Started by CarlTrading, 03-30-2026, 11:48 AM
        0 responses
        54 views
        0 likes
        Last Post CarlTrading  
        Working...
        X