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

OnExecutionUpdate throws error I can't trap

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

    OnExecutionUpdate throws error I can't trap

    Hello Forum,

    I'm trying to use OnExecutionUpdate to add target and stop orders to my strategy once the entry is filled.

    However, when I execute the event below it throws an Object not found error I can only find by looking at the trace file.

    protected void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
    {
    // set up the target and stop orders associated with the order being filled that triggered this execution
    try
    {
    if (execution.Order != null && execution.Order.OrderState == OrderState.Filled)
    {
    Print(execution.ToString());

    // Print(Time[0] + " Order filled with name: " + execution.Order.Name);
    // if(execution.Order.IsLong)
    // {
    // TargetOrder = ExitLongLimit(0, true, 1, GetCurrentBid() + TargetTicks * TickSize, "LTarget", execution.Order.Name);
    // StopOrder = ExitLongStopMarket(0, true, 1, GetCurrentBid() - StopTicks * TickSize, "LStop", execution.Order.Name);
    // }
    // if(execution.Order.IsShort)
    // {
    // TargetOrder = ExitLongLimit(0, true, 1, GetCurrentBid() + TargetTicks * TickSize, "LTarget", execution.Order.Name);
    // StopOrder = ExitLongStopMarket(0, true, 1, GetCurrentBid() - StopTicks * TickSize, "LStop", execution.Order.Name);
    // }
    }
    }
    catch (Exception ex) {Print("OnExecutionUpdate: " + ex.Message.ToString());}
    }

    Any help on this is greatly appreciated.
    daqu40
    NinjaTrader Ecosystem Vendor - QTradez

    #2
    Originally posted by daqu40 View Post
    Hello Forum,

    I'm trying to use OnExecutionUpdate to add target and stop orders to my strategy once the entry is filled.

    However, when I execute the event below it throws an Object not found error I can only find by looking at the trace file.

    Any help on this is greatly appreciated.
    I don't think you can do it there. I can't say I've seen an example that does that in there.

    Do that in OnBarUpdate.

    Comment


      #3
      Originally posted by sledge View Post
      I don't think you can do it there. I can't say I've seen an example that does that in there.

      Do that in OnBarUpdate.
      Huh? What about this example?

      The OnOrderUpdate() and OnExecution() methods are reserved for experienced programmers. Instead of using Set() methods to submit stop-loss and profit target orders, you can submit and update them manually through the use of IOrder and IExecution objects in the OnOrderUpdate() and OnExecution() methods. The OnOrderUpdate()

      Comment


        #4
        Originally posted by daqu40 View Post
        However, when I execute the event below it throws an Object not found error I can only find by looking at the trace file.
        That's cause (presumably) what you're doing is wrong.

        Look at the attached screenshot, which shows the code form the NinjaTrader example I provided in the above post.
        Attached Files

        Comment


          #5
          Originally posted by bltdavid View Post
          Thanks Never done that.

          The documentation is lagging.

          Comment


            #6
            Originally posted by sledge View Post
            Thanks Never done that.

            The documentation is lagging.

            https://ninjatrader.com/support/help...er_methods.htm
            Maybe, but NT7 docs are about the same, I think.

            OnBarUpdate() is for entry and (perhaps some) exit orders, but protective stoploss and profit target orders are of a different kind of "exit" order -- and are always best submitted from OnExecution.

            Study the NT examples very carefully, they are well worth your time.

            Comment


              #7
              What next

              Thanks for all the input Folks.

              This says I'm on the right track using OnExecutionUpdate to put in my stop and target orders.

              However, there is still the fact that my code; which only checks the if the execution != null and then the Order.State of the Order attached to that Execution, is creating an object not found error that only shows in the trace file despite my try catch trap.
              daqu40
              NinjaTrader Ecosystem Vendor - QTradez

              Comment


                #8
                Originally posted by daqu40 View Post
                Thanks for all the input Folks.

                This says I'm on the right track using OnExecutionUpdate to put in my stop and target orders.

                However, there is still the fact that my code; which only checks the if the execution != null and then the Order.State of the Order attached to that Execution, is creating an object not found error that only shows in the trace file despite my try catch trap.
                Well, if you're not going to follow the example, which automatically filters out cases where the Execution reference is null, perhaps you should explicitly check for that?

                Despite what you imply above, your original post does not check execution != null.
                Last edited by bltdavid; 01-06-2017, 12:14 AM.

                Comment


                  #9
                  Actually, it does check for null condition first. I also just tested it with the only condition being execution.Order != null. Still throws the following error: "2017-01-06 07:49:22:851 Gui.ChartBars.OnExecutionUpdateEvent: Object reference not set to an instance of an object." which I find in the trace file. Even though this error trapped nothing shows in my output window.
                  daqu40
                  NinjaTrader Ecosystem Vendor - QTradez

                  Comment


                    #10
                    Originally posted by daqu40 View Post
                    Actually, it does check for null condition first. I also just tested it with the only condition being execution.Order != null.
                    No, it doesn't.

                    Why do you think these two are the same thing?

                    execution.Order != null

                    execution != null <-- Add this one to your code

                    Comment


                      #11
                      Thank you for your report daqu40. Could you run the below modifications to your code sample? If any FAIL messages show up in your output, could you then attach a complete code sample (a C# file with the .cs extension from either your (My) Documents\NinjaTrader 8\bin\Custom\Indicators or (My) Documents\NinjaTrader 8\bin\Custom\Strategies folder) to a reply so that we may test on our end?

                      Code:
                      [FONT=Courier New]protected void OnExecutionUpdate (
                          Execution execution, string executionId, double price, int quantity,
                          MarketPosition marketPosition, string orderId, DateTime time
                          )
                      {
                        Print("TEST1: " + (null == execution ? "FAIL" : "PASS"));
                        Print("TEST2: " + (null == execution.Order ? "FAIL" : "PASS"));
                        Print("TEST3: " + (OrderState.Filled == execution.Order.OrderState ? "FAIL" : "PASS"));
                      
                        if (execution != null && && execution.Order != null && execution.Order.OrderState == OrderState.Filled)
                        {
                          Print(execution.ToString());
                        }
                      }[/FONT]
                      Jessica P.NinjaTrader Customer Service

                      Comment


                        #12
                        Ah, Thanks bltdavid!

                        With proper the condition, it's not throwing the error, but I'm still not getting the even to do anything. It's as if the execution always == null.

                        I'm seeing the same thing with the OnOrderUpdate event.

                        I'll try a few more things and report back what I find.
                        daqu40
                        NinjaTrader Ecosystem Vendor - QTradez

                        Comment


                          #13
                          Hi Jessica,

                          I tried this with your code and it doesn't produce anything in the output window. No errors in the trace file either.

                          I'll submit the whole thing via email with a reference to this thread.

                          Thanks
                          daqu40
                          NinjaTrader Ecosystem Vendor - QTradez

                          Comment


                            #14
                            Thank you for sending us that code, daqu40. As I mentioned in my direct reply, there are many things that can cause a null value to be passed in to this method. I believe the sample author's intent was to intentionally trigger an error in this circumstance, as a null execution object is a signal that the way trades are being placed should be reviewed. However, in order to improve the experience of yourself and other customers, I have requested that the code sample in the help guide be updated to include the null check for the passed-in execution object. Please let us know if there are any other ways we can help.
                            Jessica P.NinjaTrader Customer Service

                            Comment


                              #15
                              Thank you for the additional code demonstrating OrderState.Filled events triggering inside of OnOrderUpdate with OnExecutionUpdate never being called. I will be investigating this on our end, and will return to this thread with my results.
                              Jessica P.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by mmckinnm, Today, 01:34 PM
                              0 responses
                              2 views
                              0 likes
                              Last Post mmckinnm  
                              Started by f.saeidi, Today, 01:32 PM
                              0 responses
                              1 view
                              0 likes
                              Last Post f.saeidi  
                              Started by traderqz, Today, 12:06 AM
                              9 responses
                              16 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by kevinenergy, 02-17-2023, 12:42 PM
                              117 responses
                              2,766 views
                              1 like
                              Last Post jculp
                              by jculp
                               
                              Started by Mongo, Today, 11:05 AM
                              5 responses
                              15 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Working...
                              X