Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error order management

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

    Error order management

    hello,

    I am trying to use the managed order of the nt8 but I am running into issue

    short condition and long condition here my example entry condition - the code in red is generating me the following error I am trying different way to get the order name - I am trying to cancel the previous order before entering a new one:
    Click image for larger version

Name:	code error.JPG
Views:	251
Size:	27.3 KB
ID:	1090669

    Code:
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica][SIZE=13px]public class unirenkoextreme : Strategy
     {
      private double Signal_high;
      private double Signal_high1;
      private double Signal_high2;
      private double Signal_Low;
      private double Signal_Low1;
      private double Signal_Low2;
      private Order myEntryOrder = null;
      private Order myOrder;
      private int barNumberOfOrder = 0;
    
      private double up_down;
      private double entry_signal;
      private double Millestone; // highest point achieved of realized + unreliazed gain
      private EMA EMA1;
      private EMA EMA2;
      private entrybar eb1;
      private Account myAccount;
      private Order entryOrder = null;
    
      protected override void OnStateChange()
      {
       if (State == State.SetDefaults)
       {
        Description         = @"Enter the description for your new custom Strategy here.";
        Name          = "unirenkoextreme";
        Calculate         = Calculate.OnBarClose;
        EntriesPerDirection       = 1;
        EntryHandling        = EntryHandling.AllEntries;
        IsExitOnSessionCloseStrategy    = true;
        ExitOnSessionCloseSeconds     = 30;
        IsFillLimitOnTouch       = false;
        MaximumBarsLookBack       = MaximumBarsLookBack.TwoHundredFiftySix;
        OrderFillResolution       = OrderFillResolution.Standard;
        Slippage         = 0;
        StartBehavior        = StartBehavior.WaitUntilFlat;
        TimeInForce         = TimeInForce.Gtc;
        TraceOrders         = false;
        RealtimeErrorHandling      = RealtimeErrorHandling.StopCancelClose;
        StopTargetHandling       = StopTargetHandling.PerEntryExecution;
        BarsRequiredToTrade       = 20;
        // Disable this property for performance gains in Strategy Analyzer optimizations
        // See the Help Guide for additional information
        IsInstantiatedOnEachOptimizationIteration = true;
        DailyLoss       = -2500;
        Entry_threshold     = 3;
        EntrySignalcandlesize    = 50;
        Dailygain_enable    = true;
        DailyGain       = 2500;
        Contract_Quantity     = 1;
        StopLoss_enable     = true;
        Stop_Loss       = 25;
        TakeProfit_enable      = false;
        Take_profit      = 50;
        TrailStop_enable      = false;
        Trail_Stop      = 12;
        EnableMA      = false;
        EMAFast       = 55;
        EMASlow       = 89;
        StartSession   =  DateTime.Parse("15:00", System.Globalization.CultureInfo.InvariantCulture);
        EndSession    =  DateTime.Parse("13:05", System.Globalization.CultureInfo.InvariantCulture);
    
       }
    
       if (State == State.SetDefaults)
           {
              Name    = "Example Strategy";
            }
            // one time only, as we transition from historical to real-time
          else if (State == State.Realtime)
           {
              // convert any old historical order object references
              // to the new live order submitted to the real-time account
              if (entryOrder != null)
               entryOrder = GetRealtimeOrder(entryOrder);
           }[/SIZE][/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica][/FONT][/COLOR][/LEFT]

    Code:
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica]  if (up_down ==1 && Position.MarketPosition == MarketPosition.Flat && Close[0] > Open[0] )[/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica]   {[/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica]   [/FONT][/COLOR][/LEFT][LEFT][COLOR=#FF0000][FONT=Helvetica][SIZE=13px] if (Order.Name == "ShortEntry")
        {
         CancelOrder(myEntryOrder);
        }[/SIZE][/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica]    [/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica]    if(EnableMA &&  Close[0] > EMA1[0]&& EMA1[0]> EMA2[0] && myEntryOrder ==null )[/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica]    {[/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica]    EnterLongLimit(0,true, Contract_Quantity, entry_signal, "LongEntry");[/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica]    return;[/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica]    }[/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica]    else if(!EnableMA && myEntryOrder ==null )[/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica]    {[/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica]    EnterLongLimit(0,true, Contract_Quantity, entry_signal, "LongEntry");[/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica]    return; [/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica]    }[/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica]   }[/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica]   [/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica]   if (up_down ==-1 && Position.MarketPosition == MarketPosition.Flat && Close[0] < Open[0] )[/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica]   {[/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica]   [/FONT][/COLOR][/LEFT][LEFT][COLOR=#FF0000][FONT=Helvetica][SIZE=13px] if (myEntryOrder == "LongEntry")
        {
         CancelOrder(myEntryOrder);
        }[/SIZE][/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica]    if (EnableMA  &&  Close[0] < EMA1[0] && EMA1[0]< EMA2[0] && myEntryOrder == null )[/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica]    {[/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica]    EnterShortLimit(0,true, Contract_Quantity, entry_signal, "ShortEntry");[/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica]    return;[/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica]    }[/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica]    else if(!EnableMA && myEntryOrder == null )[/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica]    {[/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica]    EnterShortLimit(0,true, Contract_Quantity, entry_signal, "ShortEntry");[/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica]    return;[/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica]    }[/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica]     [/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica]    [/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica]   }[/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica]   [/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica]   [/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica]  }[/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica] [/FONT][/COLOR][/LEFT]
    
    
    
    
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica][SIZE=13px]protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled,  double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
    {
      // Assign entryOrder in OnOrderUpdate() to ensure the assignment occurs when expected.
      // This is more reliable than assigning Order objects in OnBarUpdate, as the assignment is not gauranteed to be complete if it is referenced immediately after submitting
      if (order.Name == "LongEntry" || order.Name == "ShortEntry")
          myEntryOrder = order;
    
      // Evaluates for all updates to myEntryOrder.
      if (myEntryOrder != null && myEntryOrder == order)
      {
       if (order.OrderState == OrderState.Filled)
                  entryOrder = null;
    
          // Check if myEntryOrder is cancelled.
          if (myEntryOrder.OrderState == OrderState.Cancelled)
          {
              // Reset myEntryOrder back to null
              myEntryOrder = null;
          }
      }
    }  
    
    private void OnExecutionUpdate(object sender, ExecutionEventArgs e)
    {
      // Cancel all orders if an execution is triggered after 9pm
       if (Bars.IsFirstBarOfSession)
         {
               myAccount.CancelAllOrders(e.Execution.Instrument);
         }
    } [/SIZE][/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica][SIZE=13px][/SIZE][/FONT][/COLOR][/LEFT]


    if I am removing the code in red
    here the error I am getting

    Attached Files

    #2
    Hi madams212121, thanks for your note.

    It would be better for me to see where the error is coming from if you could export your code and send it to me. You may either post it here or send it to platformsupport at ninjatrader.com and reference "Attn ChrisL 2459366"

    I look forward to hearing from you.

    Comment


      #3
      Chris,

      Thank you. I just forwarded you the code.

      awaiting for feedback

      Comment


        #4
        Hi madams212121, thanks for sending in your code.

        I did not look into how the strategy is working with this order cancellation, but the compile error is coming from comparing the actual Order object to a string. To get the Name property of your Order object access it like so:

        Code:
        if (myEntryOrder.Name == "LongEntry") { CancelOrder(myEntryOrder); }
        See all of the Order object properties documented here:
        https://ninjatrader.com/support/help...nt8/?order.htm

        Kind regards,

        -ChrisL

        Comment


          #5
          thank for that. but now I have another issue Error on calling OnBarUpdate method on bar 21: Object reference not set to an instance of an object. from what I could read , this is a reference to object with a != null not being initialized properly but I cannot find it in the code. do I need to initialize - So I replace all the !=null by the actual value of the order long or short entry. I have still have the same issue.

          suggestion ?

          Comment


            #6
            Hi madams212121, thanks for your reply.

            When I'm getting object reference errors like this and the code is very large I like to use Visual Studio debugging to find whatever object is not initialized.

            https://ninjatrader.com/support/help..._debugging.htm - Visual Studio Debugging

            Run the code while you have this setup and Visual Studio will break at the line that caused the exception.

            Visual Studio Community is free to use:
            Try our free, fully-featured, and extensible IDE for creating modern developer apps for Windows, Android, & iOS. Download Community for free today!


            Please let me know if I can assist any further.

            Comment


              #7
              Click image for larger version

Name:	issue visual studio.JPG
Views:	371
Size:	260.9 KB
ID:	1090787

              Now I have this issue in Visual studio. So far the order management has been the most painful part of the coding.

              Attached Files

              Comment


                #8
                Hi, madams212121, thanks for the follow up.

                It looks like you tried to start the script from Visual Studio. Please carefully review the debugging page I linked previously. To get the Visual Studio active on your code you must attach the NinjaTrader process to Visual Studio (Debug>Attach to process>Select "NinjaTrader.exe") then run your code in NinjaTrader until the exception is hit.

                Comment


                  #9
                  I think figure out the debug mode in Visual studio but nothing is showing as error.

                  Comment


                    #10
                    Hi madams212121,

                    The script must re-produce the null object reference error. If that error does not trigger then no break points will be triggered for it.

                    Comment


                      #11
                      After a long battle with the software and recoding also everything. SOLVED !!!!!!


                      for the curious here the code.

                      Code:
                       
                      // Call for the short position 
                      EnterShortLimit(0,true, Contract_Quantity, entry_signal, "ShortEntry"); 
                      
                      
                      protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
                              {
                                  // Handle entry orders here. The entryOrder object allows us to identify that the order that is calling the OnOrderUpdate() method is the entry order.
                                  // Assign entryOrder in OnOrderUpdate() to ensure the assignment occurs when expected.
                                  // This is more reliable than assigning Order objects in OnBarUpdate, as the assignment is not gauranteed to be complete if it is referenced immediately after submitting
                                  if (order.Name == "ShortEntry" || order.Name == "LongEntry" )
                                  {
                                      entryOrder = order;
                      
                                      // Reset the entryOrder object to null if order was cancelled without any fill
                                      if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
                                      {
                                          entryOrder = null;
                                          sumFilled = 0;
                                      }
                                  }
                              }
                      
                       protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
                              {
                                  /* 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 == execution.Order)
                                  {
                                      if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                                      {
                      
                                          // Resets the entryOrder object and the sumFilled counter to null / 0 after the order has been filled
                                          if (execution.Order.OrderState != OrderState.PartFilled && sumFilled == execution.Order.Filled)
                                          {
                                              entryOrder = null;
                                              sumFilled = 0;
                                          }
                                      }
                                  }
                      
                                  // Reset our stop order and target orders' Order objects after our position is closed. (1st Entry)
                                  if ((stopOrder != null && stopOrder == execution.Order) || (targetOrder != null && targetOrder == execution.Order))
                                  {
                                      if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
                                      {
                                          stopOrder = null;
                                          targetOrder = null;
                                      }
                                  }
                              }
                      I have added also at the beginning of on bar update()

                      Code:
                      if (CurrentBar < BarsRequiredToTrade)
                          return;

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by NullPointStrategies, Today, 05:17 AM
                      0 responses
                      37 views
                      0 likes
                      Last Post NullPointStrategies  
                      Started by argusthome, 03-08-2026, 10:06 AM
                      0 responses
                      124 views
                      0 likes
                      Last Post argusthome  
                      Started by NabilKhattabi, 03-06-2026, 11:18 AM
                      0 responses
                      64 views
                      0 likes
                      Last Post NabilKhattabi  
                      Started by Deep42, 03-06-2026, 12:28 AM
                      0 responses
                      41 views
                      0 likes
                      Last Post Deep42
                      by Deep42
                       
                      Started by TheRealMorford, 03-05-2026, 06:15 PM
                      0 responses
                      46 views
                      0 likes
                      Last Post TheRealMorford  
                      Working...
                      X