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 Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        603 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        349 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        104 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        560 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        560 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X