Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

The Exit order methods do not return an instance of the Order class

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

    The Exit order methods do not return an instance of the Order class

    Hi,

    In my strategy, I am using unmanaged orders for entry and exits. Everything works fine when I backtest my strategy, however, when I run the strategy on live chart I get an error when I call the ExitLongStopMarket and ExistLongLimit methods for my SL and TP inside OnOrderUpdate.

    The error that I get when for example ExitLongStopMarket is called is:
    • Object reference not set to an instance of an object

    Following is the code within OnOrderUpdate:

    Code:
    protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string comment)
    {
      if (entryOrder != null && order == entryOrder && entryOrder1.OrderState == OrderState.Filled)
        {
          if (order.IsLong) {
            stopOrder1 = ExitLongStopMarket(0, true, 1, averageFillPrice - 10 * TickSize, "SL", "Entry");
            targetOrder1 = ExitLongLimit(0, true, 1, averageFillPrice + 10 * TickSize, "TP", "Entry");
          } else {
            stopOrder1 = ExitShortStopMarket(0, true, 1, averageFillPrice + 10 * TickSize, "SL", "Entry");
            targetOrder1 = ExitShortLimit(0, true, 1, averageFillPrice - 10 * TickSize, "TP", "Entry");
          }
       }
    }​
    What am I missing here that it works in the backtests but not on the live chart?

    Thanks.
    Pouya

    #2
    Hello pouya-p,

    The error you are seeing means some object you used was null. You may have an error in this code, I see you have two different variables for entry order:

    if (entryOrder != null && order == entryOrder && entryOrder1.OrderState == OrderState.Filled)

    entryOrder is a different variable from entryOrder1​, one of these variables may be null. Because your condition is checking that entryOrder is not null its likely entryOrder1

    F​urther, order objects should only be assigned to variables from OnOrderUpdate().
    "OnOrderUpdate() will run inside of order methods such as EnterLong() or SubmitOrderUnmanaged(), therefore attempting to assign an order object outside of OnOrderUpdate() may not return as soon as expected. If your strategy is dependent on tracking the order object from the very first update, you should try to match your order objects by the order.Name (signal name) from during the OnOrderUpdate() as the order is first updated."

    Comment


      #3
      Sorry about the confusion, I tried cleaning up my real code to before posting it as an example here. Those are one variable in the real code as follows:

      Code:
      protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string comment)
              {
                    if (entryOrder != null && order == entryOrder && entryOrder.OrderState == OrderState.Filled)
                  {
                      if (order.IsLong) {
                          Order stopOrder = ExitLongStopMarket(0, true, 1, averageFillPrice - 10 * TickSize, "SL", "Entry");
                          Order targetOrder = ExitLongLimit(0, true, 1, averageFillPrice + 10 * TickSize, "TP", "Entry");
                      } else {
                          Order stopOrder = ExitShortStopMarket(0, true, 1, averageFillPrice + 10 * TickSize, "SL", "Entry");
                          Order targetOrder = ExitShortLimit(0, true, 1, averageFillPrice - 10 * TickSize, "TP", "Entry");
                      }
                  }
              }​

      Comment


        #4


        Why did you add Order?

        Order stopOrder = ...
        Order targetOrder = ...


        Your variables stopOrder and targetOrder should
        not have that type keyword Order in front.

        Although the C# compiler allows you do this, it is almost
        guaranteed a human error to define local Order variables
        inside those small blocks {} of code.

        My point is, you added Order (it wasn't there before) and
        now you've introduced potential new errors by doing so.

        Do you understand why?

        Hint: It makes them too local, and effectively pointless.

        Last edited by bltdavid; 07-09-2024, 02:47 PM.

        Comment


          #5
          Again for the sake of example, I added the type Order in front of my variables to show that those variables are of type Order. However, the returned value of the ExitLongStopMarketdoes not seem to be of type Order when I run the code on the live chart.

          Comment


            #6
            Originally posted by pouya-p View Post
            In my strategy, I am using unmanaged orders for entry and exits. Everything works fine when I backtest my strategy, however, when I run the strategy on live chart I get an error when I call the ExitLongStopMarket and ExistLongLimit methods for my SL and TP inside OnOrderUpdate.
            Are you really using the Unmanaged approach?

            Both ExitLongStopMarket and ExitLongLimit are for the Managed approach.

            I don't really think you should mix and match these two approaches together.

            NT Support can comment further.

            Just my 2˘.


            Comment


              #7
              Originally posted by pouya-p View Post
              Again for the sake of example, I added the type Order in front of my variables to show that those variables are of type Order. However, the returned value of the ExitLongStopMarketdoes not seem to be of type Order when I run the code on the live chart.
              Beg pardon, but the example code you provided
              along with the error message you received are
              insufficient to reproduce your issue.

              Can you provide a scaled down minimal script
              that shows an example of the error you are
              seeing?

              Sorry, your comment 'does not seem to be of
              type Order' is, thus far, simply not believable.

              [For ex, if this were actually true, you should
              get a compiler error.]

              I'm able to help, but more proof is needed.

              Do you have an example script?


              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by NullPointStrategies, Today, 05:17 AM
              0 responses
              41 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