Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

execution.Order returned null in OnExecution() in backtest?

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

    execution.Order returned null in OnExecution() in backtest?

    Hello,

    I am running a backtest of my strategy in Strategy Analyzer and run into troubles.

    In this example, I have a SellShort position of 156 shares of NFLX. After conditions are met, I take a reverse position by using EnterLong(). NT triggered PlaceOrder() to close the current SellShort position, cancel the protective Stop Order for the associated SellShort position, and attempts to enter into a Long position.

    TraceOrder output the follows:

    19-Jan-19 3:45:00 AM Entered internal PlaceOrder() method at 19-Jan-19 3:45:00 AM: BarsInProgress=0 Action=Buy OrderType=Market Quantity=200 LimitPrice=0 StopPrice=0 SignalName='5M_Lg0' FromEntrySignal=''

    19-Jan-19 3:45:00 AM Cancelled pending exit order, since associated position is closed: Order='NT-00003/Backtest' Name='Stop loss' State=Working Instrument='NFLX' Action=BuyToCover Limit price=0 Stop price=340.65 Quantity=156 Strategy='SimpleSARStrategy' Type=Stop Tif=Gtc Oco='NT-00002-140' Filled=0 Fill price=0 Token='3cbcf12bddc3442fbbb8771e6312c1a5' Gtd='01-Dec-99 12:00:00 AM'

    Then I expect NT to trigger the OnExecution() method. I override OnExecution with the following lines of code:

    protected override void OnExecution(IExecution execution)
    {
    Print(header+" ###OnExecution### Execution: "+ execution.ToString());
    Print(header+" ###OnExecution### ExecutionOrder: "+execution.Order);
    ......
    }

    NT entered OnExecution as I expected, and completed the first line of Print, but when it comes to the 2nd line of Print, it prompted "Error on calling 'OnExecution' method, Object reference not set to an instance of an object."

    Output is as follow:-

    (NFLX 5Min) ###OnExecution### Execution: Execution='NT-00003' Instrument='NFLX' Account='Backtest' Name='Close position' Exchange=Default Price=339.77 Quantity=156 Market position=Long Commission=0 Order='NT-00004' Time='19-Jan-19 3:50:00 AM'

    (NFLX 5Min) ###OnExecution### ExecutionOrder:
    **NT** Error on calling 'OnExecution' method for strategy 'SimpleSARStrategy/158d54539ad541e588d20e569e7f2ffb': Object reference not set to an instance of an object.

    I wonder if execution.Order pointed to null so this error occurred?

    I have no problem running this strategy in Market Replay or realtime, but somehow it comes to error when I do a backtest.

    I will be very grateful if someone can shed some lights on what is going on here.

    Thank you very much!

    Cheers,
    Hiromichi

    #2
    Originally posted by Hiromichi View Post
    I wonder if execution.Order pointed to null so this error occurred?
    Is this Managed or Unmanaged mode?

    Correct. A careful study of their sample code shows that execution.Order being null will
    be properly ignored.

    Now, why it could be null is because (I think) of internal generated orders, such as when
    closing all orders in a strategy due to ExitOnClose.

    NinjaTrader support, can you confirm this and any additional scenarios where
    execution.Order is null?

    Comment


      #3
      Hello Hiromichi,

      Thank you for the post.

      All of the following links are publicly available.

      You should always check that the execution.Order object is not null before accessing it.
      The second example on the help guide page for OnExecution() shows a check for execution.Order != null

      Code:
      // Example #2
      
      // Generic execution logic not specific to a particular IOrder object
      
      protected override void OnExecution(IExecution execution)
      {
      
          // Remember to check the underlying IOrder object for null before trying to access its properties
          if (execution.Order != null && execution.Order.OrderState == OrderState.Filled)
               Print(execution.ToString());
      }
      I am not sure why that Order object can be null at that time, there is internal logic going on that is non-viewable. If you do need to access information on the Order object itself, override OnOrderUpdate and you will get update events for orders the strategy takes.

      Below is a relevant reference sample where OnExecutionUpdate() and OnOrderUpdate() are used to place protective stops for filled orders.

      https://ninjatrader.com/support/help...and_onexec.htm

      Please let me know if I can assist further.
      Last edited by NinjaTrader_ChrisL; 01-22-2019, 11:20 AM.
      Chris L.NinjaTrader Customer Service

      Comment


        #4
        Originally posted by NinjaTrader_ChrisL View Post
        I am not sure why that Order object can be null at that time, there is internal logic going on that is non-viewable.
        Two things.

        Chris, your statement is a cop-out, the internal logic IS viewable to YOU, or SOMEONE else who works there.
        In what scenarios will execution.Order be null is a very valid question. I don't recall Bertrand being this dismissive.
        I gave you a partial hint (internally generated orders) but you chose to dismiss my input and can't be bothered to expand
        upon it or refute it -- not even a little -- that's very dismissive, don't you think?

        Second thing.

        Have you noticed this is the NT7 forum?
        Why is everything in your answer, all examples, all links, all for NT8?
        Is this the new policy now, to answer NT7 questions using NT8 examples?
        Why this inability of NT Support to stay on topic of NT7 in the NT7 forums?
        This happens all the time ...

        Comment


          #5
          Hello bltdavid,

          Thank you for the reply.

          I apologize if I came off as dismissive. I do not have access to the NT source code, but I could ask if I could get any more info on this. All the information on the post itself is still relevant for NinjaTrader 7. I will change my links/syntax shortly.

          Thanks in advance for your patience.
          Chris L.NinjaTrader Customer Service

          Comment


            #6
            Hello Bltdavid,


            Is this Managed or Unmanaged mode?
            I used managed approach to code, and I override OnPositionUpdate and OnExecution for order handling.

            Yes I also think it would be very helpful if we understand better on the internal logic of how null would be thrown.
            NT Support: Please do give us more info on this and keep us posted.


            Thank you all guys!


            Cheers,
            Hiromichi

            Comment


              #7
              Hello, all.

              I have more information on the execution.Order object.

              There is no guarantee of the sequence of events of OnOrderUpdate/OnExecutionUpdate/OnPositionUpdate. They can be considered there own independent channels and in the case the OnExecutionUpdate processes. In a scenario where the one submits an order using a third party application (order not originating from NinjaTrader) OnExecution gets processed first then we would not have an Order object created yet for it (Just an order ID for the executing order).

              Please let me know if you have any questions on this.
              Chris L.NinjaTrader Customer Service

              Comment


                #8
                Originally posted by NinjaTrader_ChrisL View Post
                There is no guarantee of the sequence of events of OnOrderUpdate/OnExecutionUpdate/OnPositionUpdate.
                You sure about that?

                In NT7, the sequence is well-documented in that OnExecution is always after OnOrderUpdate.

                See here,
                https://ninjatrader.com/support/help...nexecution.htm

                In fact, I think the order is,
                OnOrderUpdate --> OnExecution --> OnPositionUpdate

                Good discussion here.

                Also, a good discussion of OrderState transitions (with a great state diagram) is here.
                Attached Files

                Comment


                  #9
                  Originally posted by NinjaTrader_ChrisL View Post
                  In a scenario where the one submits an order using a third party application (order not originating from NinjaTrader) OnExecution gets processed first then we would not have an Order object created yet for it (Just an order ID for the executing order).
                  (red emphasis mine)

                  Ok, that makes sense.

                  But that scenario doesn't apply to the OP's situation, does it?

                  I mean, OP said he was using Strategy Analyzer -- so we're back to the underlying
                  question of how does the executon.Order become null when OnExecution is called?
                  So, my plea for more information on the scenarios explaining how this happens in
                  NT7 Strategy Analyzer
                  is still open (because the scenario you provided doesn't
                  apply here, because Strategy Analyzer is not a third party application.)

                  Also:
                  OP says he's trying to SellShort 156 shares of NFLX stock, and OP then shows his
                  first TraceOrder output message showing Quantity=200.

                  Huh? 200? Why would it say 200 there?
                  Last edited by bltdavid; 01-30-2019, 10:41 PM.

                  Comment


                    #10
                    Hello bltdavid,

                    Thanks for the reply.

                    It turns out that in NinjaTrader 8 there is actually no guarantee of call order for OnExecutionUpdate/OnOrderUpdate. The help guide page for that will be revised soon.

                    For NinjaTrader 7, you must assign your order object to an IOrder interface because you do not actually have direct access to the Order object itself. In NinjaTrader 8, we gave programmers direct access to the Order object, so you don't need to assign an Order object in OnOrderUpdate before OnExecutionUpdate is called. Please see the attached example. Commenting out the OnOrderUpdate method results in a null object reference in OnExecution.

                    For the quantity, it was likely a partial fill of 156 shares. We would need to print out the Order state to know this.

                    Please let me know if I can assist further.
                    Attached Files
                    Chris L.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by jclose, Today, 09:37 PM
                    0 responses
                    4 views
                    0 likes
                    Last Post jclose
                    by jclose
                     
                    Started by WeyldFalcon, 08-07-2020, 06:13 AM
                    10 responses
                    1,413 views
                    0 likes
                    Last Post Traderontheroad  
                    Started by firefoxforum12, Today, 08:53 PM
                    0 responses
                    10 views
                    0 likes
                    Last Post firefoxforum12  
                    Started by stafe, Today, 08:34 PM
                    0 responses
                    10 views
                    0 likes
                    Last Post stafe
                    by stafe
                     
                    Started by sastrades, 01-31-2024, 10:19 PM
                    11 responses
                    169 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Working...
                    X