Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Best Approach For Interactive Brokers - OnOrder or OnExecution update?

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

    Best Approach For Interactive Brokers - OnOrder or OnExecution update?

    Hello,

    I'm curious to know what is the best approach to code to when using Interactive Brokers. While not 100% the same scenario, in this post: https://forum.ninjatrader.com/forum/...18#post1114218

    Jim provides an approach to manage positions using only OnOrder updates and not OnExecution since the order / execution event order is not guaranteed with IB.

    With this issue still around today, is the approach in the above link still the way to go?

    I'm asking because in this newer post: https://forum.ninjatrader.com/forum/...72#post1206372
    from Jim, both OnOrder and OnExecution are being used.

    I'm looking for accurate position information and the best approach to take. Please let me know.

    Thank you!

    ** After finding additional posts, I think I understand.
    Basically write your code so OnOrder / OnExecution / OnPosition support being called out of order and if it happens, support not duplicating, missing, or breaking anything.
    Last edited by neo302; 02-26-2023, 11:50 PM.

    #2
    Hello neo302,

    Thank you for your post.

    Basically write your code so OnOrder / OnExecution / OnPosition support being called out of order and if it happens, support not duplicating, missing, or breaking anything.
    Yes, this is the main takeaway when it comes to developing a strategy for Interactive Brokers or Rithmic. With these two brokers, the events from OnOrderUpdate(), OnPositionUpdate(), and OnExecutionUpdate() are not guaranteed to arrive in a specific order. The key is to have your strategy set up so that it does not depend on Order, Position, or Execution information to be obtained in any order. You may manage your order objects via OnOrderUpdate() based on the OrderState rather than the approach in SampleOnOrderUpdate that uses OnExecutionUpdate information to null order objects. SampleOnOrderUpdate relies on the order of events and having OnExecutionUpdate() called after OnOrderUpdate() and the IB/Rithmic friendly examples involve modifying the logic so it does not rely on event updates in that specific order.

    I hope this information helps to clarify. Please don't hesitate to reach out with any additional questions.

    Comment


      #3
      The Help Documentation for OnOrderUpdate and OnExecutionUpdate both contain the following statements:

      Rithmic and Interactive Brokers Users: When using a NinjaScript strategy it is best practice to only work with passed by value data from OnExecution. Instances of multiple fills at the same time for the same instrument might result in an incorrect OnPositionUpdate, as sequence of events are not guaranteed due to provider API design.
      The Help Documentation for OnOrderUpdate follows this with the additional statement:

      For an example on protecting positions with this approach, see OnExecutionUpdate()
      The OnOrderUpdate Help then continues with these additional statements:

      Critical: If you want to drive your strategy logic based on order fills you must use OnExecutionUpdate() instead of OnOrderUpdate(). OnExecutionUpdate() is always triggered after OnOrderUpdate(). There is internal strategy logic that is triggered after OnOrderUpdate() is called but before OnExecutionUpdate() that can adversely affect your strategy if you are relying on tracking fills within OnOrderUpdate().
      Comments/questions:
      1. Presumably, reference to "OnExecution" in the first statement actually means "OnExecutionUpdate"?
      2. The Critical statement in the OnOrderUpdate documentation seems unambiguous: "OnExecutionUpdate() is always triggered after OnOrderUpdate()".
      3. Does the Critical statement above apply to all brokers and all connections?
      4. Are Rithmic and Interactive Brokers exceptions to this Critical statement?
      5. What is the definitive situation for these two event handlers (three if including Position) for all brokers and connections, including Rithmic and Interactive Brokers?
      Thanks.​
      Multi-Dimensional Managed Trading
      jeronymite
      NinjaTrader Ecosystem Vendor - Mizpah Software

      Comment


        #4
        Hello jeronymite,

        Thank you for your note.

        Yes, the use of "OnExecution" in those statements is referring to OnExecutionUpdate(). The critical statement does not apply to Rithmic and Interactive Brokers, though for all other brokers it is expected that OnExecutionUpdate() is triggered after OnOrderUpdate(). That critical statement is listed right after the bullet point explaining that Rithmic and Interactive Brokers do not guarantee the sequence of events due to API design:
        World's leading screen capture + recorder from Snagit + Screencast by Techsmith. Capture, edit and share professional-quality content seamlessly.


        My colleague has offered a helpful explanation that should answer Uncategorized Groups in the following post:
        Is OnPositionUpdate called in a separate thread from OnBarUpdate? In other words, is it possible for OnPositionUpdate to be called in the middle of execution of OnBarUpdate? Or are these synchronized so that this never happens?


        Most API's order the events in the following sequence:
        1. OnOrderUpdate()
        2. OnExecutionUpdate()
        3. OnPositionUpdate()
        The Position objects for a strategy are updated based on OnExecutionUpdate() information, and when using the Managed Approach order handling logic is tied internally to the strategy position. Account position objects are updated from OnPositionUpdate(). When it comes to Rithmic and Interactive Brokers, the events are not guaranteed to come through in the listed order. This means that OnOrderUpdate() could come before or after OnExecutionUpdate(), for example. If you work with information from OnExecutionUpdate() alone then the strategy position is also going to be up to date. If you work with OnOrderUpdate() alone, then the position object may not be updated if OnExecutionUpdate() has not been called yet, which means you would have to manually manage a position object in your logic based on whether orders are filled/partially filled rather than relying on the strategy's calculated position object.

        Please let us know if we may be of further assistance.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        115 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        61 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        40 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        43 views
        0 likes
        Last Post TheRealMorford  
        Started by Mindset, 02-28-2026, 06:16 AM
        0 responses
        82 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Working...
        X