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

OnExecution error

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

    OnExecution error

    Hello,

    I get an error message when I test my strategy on historic data. This error comes in two cases:

    1. When position is closed automatically at the end of session (ExitOnClose=true).
    2. When position is reversed, (enterlong is submitted while short position is open).

    Here is the error message:

    Error on calling "OnExecution" method for strategy ...: Object reference is not set to an instance of an object.

    And here is "OnExecution", as you can see it is completelly empty.

    protected override void OnExecution(IExecution execution)
    {
    if (execution != null && (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled))
    { // do nothing }
    }

    I believe there is a problem with internal synchronization inside NT, how can it be fixed?

    Best regards,
    Sergey.

    #2
    comment

    Just to explain, I use OnExecution only for reporting purposes, normally I have there only one Print() command. But I get an error even when the "OnExecution" has only "if" statement provided in the previous email, need your help, please.

    Comment


      #3
      Sergey, do you work then with any IOrder object in your other strategy parts / conditions?
      BertrandNinjaTrader Customer Service

      Comment


        #4
        Yes I use IOrder objects, mainly I monitor execution of orders and write to a log file. I also have a flag that monitors that order was filled (in case of partial fill). Everytime I use IOrder object I check it (order[0] != null).

        But again, I get this error mesage even if OnExecution contains only one line with if statement (if (execution != null && (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled))), and only in those two circumstances. Here how I place order normally: order[0] = EnterLong(enter_quantity, enter_name);

        Here is a complete OnExecution:

        protected override void OnExecution(IExecution execution)
        {
        if (execution != null && (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled))
        {
        Print(execution.Order.Name + " " + execution.Quantity.ToString() + "@" + execution.Order.AvgFillPrice.ToString());
        if (order[0] != null && execution.Order == order[0] && execution.Order.OrderState == OrderState.Filled)
        {
        enter_price = execution.Order.AvgFillPrice;
        enter_completed = true;
        }
        }
        }

        Comment


          #5
          Sergey, works ok here for me - are your IOrders stored in a list or array? I suspect there might be an coding issue related to them here at play, please try with a regular IOrder object directly in the code you posted.
          BertrandNinjaTrader Customer Service

          Comment


            #6
            found the problem

            Hi Bertrand

            I found a problem, it is more like a bug in NT.

            When OnExecution is called in some cases (in particular OnClose) execution != null but execution.Order == null. So if I specify two conditions such as "if (execution != null && execution.Order != null)" then I do not get error message.

            Possibly this is a synchronization problem in NT when you null order object before OnExecution gets completed. Again this happens for me only in some cases.

            Hope it helps to improve the system.

            Sergey.

            Comment


              #7
              Sergey, thanks for the feedback - you're correct the .order == null check should be done as well here.
              BertrandNinjaTrader Customer Service

              Comment


                #8
                This is a bug right?

                I'm getting execution.order = null every time for exit on close, and I need to refer to it in OnExecution. Why is it null? This is not a synchro problem - you can't be post execution of an order that doesn't exist.

                Comment


                  #9
                  Hi Dave, no this is not a bug - this could be the case and those checks would be needed.
                  BertrandNinjaTrader Customer Service

                  Comment


                    #10
                    Hi Bertrand,

                    Please explain why it is not a bug, in light of what I have said. Checks for null are useless to me - I need the order object in OnExecution.

                    Please also note the Ninja help document




                    If you are relying on the OnExecution() method to trigger actions such as the submission of a stop loss order when your entry order is filled ALWAYS reference the properties on the IOrder object property attached to the IExecution object passed into the OnExecution() method.

                    Comment


                      #11
                      dave, I'll have Bertrand get back to you tomorrow.
                      AustinNinjaTrader Customer Service

                      Comment


                        #12
                        dave1992,

                        I have not read the whole thread, but just jumping in here. An "Exit on Close" order is not an order triggered by your code with an associated IOrder per se. It is an internally generated order and does not necessarily have an order object attached to it. Basically what I am saying is you never set any IOrder object to it and there's no way you could have so I am not sure what you need execution.Order for on "Exit on Close".
                        Josh P.NinjaTrader Customer Service

                        Comment


                          #13
                          Come on Josh, of course it's a proper order. It has an order ID and everything. I need to detect Exit on Close in OnExecution since I run an unmanaged strategy.

                          See order update logging below.


                          31/05/2011 09:49:00 Order Update = Order='NT-00018/Backtest' Name='Exit on close' State=PendingSubmit Instrument='FGBS ##-##' Action=Sell Limit price=0 Stop price=0 Quan
                          31/05/2011 09:49:00 Order Update = Order='NT-00018/Backtest' Name='Exit on close' State=Accepted Instrument='FGBS ##-##' Action=Sell Limit price=0 Stop price=0 Quantity=
                          31/05/2011 09:49:00 Order Update = Order='NT-00018/Backtest' Name='Exit on close' State=Working Instrument='FGBS ##-##' Action=Sell Limit price=0 Stop price=0 Quantity=1
                          31/05/2011 09:49:00 Order Update = Order='NT-00018/Backtest' Name='Exit on close' State=Filled Instrument='FGBS ##-##' Action=Sell Limit price=0 Stop price=0 Quantity=1

                          Comment


                            #14
                            Dave,

                            I never said it wasn't a "proper" order. I said it is an order that was not submitted by you with an IOrder reference. The takeaway from my prior post is if you want an IOrder reference you need to set one yourself in OnOrderUpdate() or OnExecution(). Without such a reference I do not see what you are trying to achieve in checking something like execution.Order.

                            All of the information for the Exit on Close is already available to you. It is a matter of your programming to take advantage of it.
                            Josh P.NinjaTrader Customer Service

                            Comment


                              #15
                              Originally posted by NinjaTrader_Josh View Post
                              you want an IOrder reference you need to set one yourself in [...] OnExecution().
                              Josh,

                              Perhaps you could explain how I can set an IOrder ref in OnExecution to refer to the order associated with the execution, as you mention above.

                              What I am trying to achieve is this. I run everything off OnExecution and that works fine. When an Exit On Close order is generated I need to detect it on OnExecution because I want to do something specific after Exit On Close. It's really not relevant what I want to do. Even if I detected Exit On Close on OnOrderUpdate, how would I know that a subsequent execution is related to that Exit on Close if Execution.order is null?

                              You can't execute an order that doesn't exist, right? And if an order exists, presumably it's an IOrder object? And if it's an IOrder object, wouldn't you expect execution.order to hold that order?

                              thanks
                              Dave

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by jaybedreamin, Today, 05:56 PM
                              0 responses
                              3 views
                              0 likes
                              Last Post jaybedreamin  
                              Started by DJ888, 04-16-2024, 06:09 PM
                              6 responses
                              18 views
                              0 likes
                              Last Post DJ888
                              by DJ888
                               
                              Started by Jon17, Today, 04:33 PM
                              0 responses
                              1 view
                              0 likes
                              Last Post Jon17
                              by Jon17
                               
                              Started by Javierw.ok, Today, 04:12 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post Javierw.ok  
                              Started by timmbbo, Today, 08:59 AM
                              2 responses
                              10 views
                              0 likes
                              Last Post bltdavid  
                              Working...
                              X