Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Adding a print statement gives different results.

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

    Adding a print statement gives different results.

    Hi,

    When I add a print statement to my strategy I get different backtest results. The log shows the error "Error on calling the 'OnExecution' method for strategy 'BBSqueezeV4ShortFriday': Object reference not set to an instance of an object."
    Without the print statement I don't get this error and I don't understand why.
    The print statement in question is at line 137 and prints the "stopOrder" (Print("stopOrder = " + stopOrder.ToString()).
    Can you help?

    The parameters I'm using are a 60min chart of EURUSD currency from 01/01/2009 to present day. All other settings are default. I'm using version 6.5.1000.14

    Thanks,

    Mark
    Attached Files

    #2
    Hello,

    I have not looked at your code yet, but is there a chance that the stoporder object does not exist at some point and that is the error? What is in stoporder that you are printing?

    Please post code snippets so we can help easier. Thanks!
    DenNinjaTrader Customer Service

    Comment


      #3
      Hi Ben,
      I've pasted the code that's giving me problems below. The print statement is in bold. I don't understand why the print statement should affect the stoporder object?

      Thanks

      protected override void OnExecution(IExecution execution)
      {
      /* We advise monitoring OnExecution to trigger submission of stop/target orders instead of OnOrderUpdate() since OnExecution() is called after OnOrderUpdate()
      which ensures your strategy has received the execution which is used for internal signal tracking. */
      if (entryOrder != null && entryOrder.Token == execution.Order.Token)
      {
      if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0)&& BarsInProgress == 1)
      {
      Print("execution.Order.OrderState = " + execution.Order.OrderState.ToString());

      // Stop-Loss order
      stopOrder = ExitShortStop(0, true, execution.Order.Filled, execution.Order.AvgFillPrice + (target+1*TickSize), "MyStop", "BracketEntry");

      Print("stopOrder = " + stopOrder.ToString());


      // Target order
      targetOrder = ExitShortLimit(0, true, execution.Order.Filled, execution.Order.AvgFillPrice - target, "MyTarget", "BracketEntry");

      Print("targetOrder = " + targetOrder.ToString());


      // Resets the entryOrder object to null after the order has been filled or partially filled
      if (execution.Order.OrderState != OrderState.PartFilled)
      {
      entryOrder = null;

      }
      }

      }

      Comment


        #4
        Hello,

        Try doing a check before hand and see if that helps:
        if (stopOrder != null && stopOrder.Token == order.Token)
        { ...etc.

        DenNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        646 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        367 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
        569 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