Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

The Various OrderStates OnExecution() Method tracks?

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

    #16
    Can we use try catch to debug OnOrderUpdate() ?

    Comment


      #17
      Hi piyushc,

      A try and catch can be really helpful sometimes.

      I personally prefer to add prints before whatever condition or loop i'm about to run.

      To debug your code I started by adding prints and returns to test small bits of the code at a time.

      I think I see the issue and this issue would not be prevented with a try and a catch because the issue does not cause an error but instead causes NinjaTrader to crash.

      The issue is that you are running ChangeOrder inside of OnOrderUpdate and you are changing the order that triggered OnOrderUpdate.

      Changing an order will cause OnOrderUpdate to run on that order. You are once again causing an endless loop.

      This endless loop will not throw an error because technically sometimes its needed to do this. But thats the same with a while loop. Sometimes its needed to use a while loop. But if your loop never ends, it will crash the program it is running in no matter what programming language you are using.

      You call OnOrderUpdate and then call ChangeOrder which calls OnOrderUpdate which calls ChangeOrder which calls OnOrderUpdate.. etc, forever until NinjaTrader crashes.

      While using ChangeOrder inside of OnOrderUpdate is necessary sometimes, like to change a stop loss or profit target, you will need to make sure that you are not changing the order that triggered OnOrderUpdate. OR you can use a bool to make sure that is it run once.

      Changing an order the very second it is placed does not sound like good code in the first place, however. If you are wanting to trail an order, OnOrderUpdate does not sound like the place for it. You may want to look at the logic you have and double check that this is what you want to be doing.
      Last edited by NinjaTrader_ChelseaB; 03-19-2014, 10:30 AM.
      Chelsea B.NinjaTrader Customer Service

      Comment


        #18
        Thank you for your exhaustive effort & insight.
        Let me think it through & will come back after making necessary changes to the code.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by charlesugo_1, 05-26-2026, 05:03 PM
        0 responses
        60 views
        0 likes
        Last Post charlesugo_1  
        Started by DannyP96, 05-18-2026, 02:38 PM
        1 response
        148 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by CarlTrading, 05-11-2026, 05:56 AM
        0 responses
        162 views
        0 likes
        Last Post CarlTrading  
        Started by CarlTrading, 05-10-2026, 08:12 PM
        0 responses
        98 views
        0 likes
        Last Post CarlTrading  
        Started by Hwop38, 05-04-2026, 07:02 PM
        0 responses
        286 views
        0 likes
        Last Post Hwop38
        by Hwop38
         
        Working...
        X