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

Problem with onExecutionUpdate flow using IB TWS on stocks

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

    Problem with onExecutionUpdate flow using IB TWS on stocks

    Good afternoon.
    We are testing our new strategy in conjunction with IB TWS. The documentation says that it is better to use the OnExecutionUpdate method to correctly process orders flow coming from IB.
    We didn't have any problems while working with small amount of stocks but we noticed a problem while working with big amount of stocks.
    For example, we have set StopLoss. When it starts to be executed in parts, the method should be called in the order in which the order is filled. As a result, the last method call does not correspond to reality. We have made the log for onExecutionUpdate and onOrderUpdate to understand where the problem is and found out that very often the closing order in the flow doesn't call OnExectuionUpdate.

    Here is a screenshot with the order of calls and parameter passing.


    It shows that at iteration 13 ex.Postion=0, i.e. the order is filled, while the status of PartFilled and ex.Order.Filled is equal to line 12. This is not true. No calls of the OnExecutionUpdate method were made after 13 iteration. If at this moment we look at the data of the order itself, there is the same information, but if we look at Position.Quantity, there is already 0.

    This problem doesn't always occur, but if the filling is done in parts, in 90% of cases it occurs.
    Please advise how to fix it?

    #2
    Hello tanas.eduard,

    Thank you for your post.

    There are some challenges when it comes to the order of events with both Interactive Brokers and Rithmic accounts, because the OnOrderUpdate(), OnExecutionUpdate(), and OnPositionUpdate() are not guaranteed to be called in a specific sequence. For that reason, in your case with an Interactive Brokers account, you will want to be sure that you do not rely on Order information inside of OnExecutionUpdate(). We have some examples available that show how you could handle your stop and target orders using both the managed and unmanaged approaches. Those samples may be found here:
    Hello, I see in the documentation you have fixed: 15048 Added Adapter, Rithmic Updated Rithmic API to version 11.3.0.0 Can you elaborate on that please? Will OnOrderUpdate() OnExecutionUpdate() and OnPositionUpdate() be called in the expected order?


    Please review those sample strategies and feel free to reach out with any additional questions or concerns.
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      Thanks. This example very help us. Now our strategy work fine

      Comment


        #4
        Good afternoon Emily.

        We have read the example you provided and we have some questions.

        I will briefly describe how our strategy works:
        1. The strategy places an order to enter the position
        2. In the OnExecutionUpdate method we recalculate our Take Profits and protective StopLoss based on Position.Quantity and Position.MarketPosition
        3. When Take Profits are triggered, we change the amount for StopLoss based on the current Position.Quantity
        4. When the Stop Loss triggers, we cancel our Take Profit orders.


        Based on the examples that you have sent, we conclude that we can not use Position.Quantity, as it does not carry the correct information at any time due to the specifics of order execution triggering by IB. Then we have to fully account for all inputs and outputs in the strategy using the OnExecutionUpdate method and its parameters price and quantity. In this case we should fully calculate the average entry price and all exits. Also it turns out that inside the strategy we can not rely anywhere on data from the Position variable (such as quantity, average price and MarketPosition).

        We also use Trail for positioning, but for it to work correctly it needs to always know the exact state of the position at the current moment.


        Can you tell us how to build a strategy so that we can confidently use the data from Position?

        Comment


          #5
          Hello tanas.eduard,

          Thank you for your reply.

          The strategy position that you are trying to access is driven by OnExecutionUpdate() events, as noted in the 4th bullet point on the help guide page here:


          You could utilize the unmanaged approach to accurately track your strategy's position from OnOrderUpdate() as described and demonstrated in the sample found in the following post:
          Hello, I connected to Interactive brokers. My strategy uses OnOrderUpdate, OnExecution, and OnPositionUpdate and my strategy uses orderName as a key for example to identify Enter signalName or exit signalName and other uses. But According to help manual : The problem is that the order name does not passed by value data


          Please let us know if we may be of further assistance.
          Emily C.NinjaTrader Customer Service

          Comment


            #6
            Questions:

            1 - Is there any way to use variables from Position when working with IB?
            2 - If I can't use Position to get guaranteed position information, do I have to fully develop my own position logging methods anyway (quantity, average price, current position state)?
            3 - If I need to make my own methods, should I rely on which event - onOrderUpdate or onExecutionUpdate and take values from these functions as parameters? Some examples say that I need onExuctionUpdate, others say that I need onOrderUpdate, it's a bit confusing.

            Comment


              #7
              Hello tanas.eduard,

              Thank you for your reply.

              Whether you rely on OnOrderUpdate() or OnExecutionUpdate() is up to you; I understand that it can be confusing to read seemingly conflicting information. As I mentioned while explaining this concept to another user, the main takeaway is that for Interactive Brokers and Rithmic, the sequence of updates is not guaranteed so you should rely solely on either OnOrderUpdate() or OnExecutionUpdate(). For tracking your position, you would only want to use one method or the other and not have any logic in both methods that would require a guaranteed sequence where one piece of logic in one method is required to trigger before/after another portion of logic in the other method. Please see my explanation at the following link and let me know if there is anything else you would like me to clarify:
              Hi! I have some questions on protective orders with Interactive Brokers: 1. Should I place protective orders from OnOrderUpdate or from OnExecutionUpdate? I am a bit confused if I should make my protective Orders from OnOrderUpdate or from OnExecutionUpdate with IB. In the Help Guide it says: "When using a NinjaScript


              I sincerely appreciate your patience. Please don't hesitate to let me know if I may be of further assistance.
              Emily C.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by dustydbayer, Today, 01:59 AM
              0 responses
              1 view
              0 likes
              Last Post dustydbayer  
              Started by inanazsocial, Today, 01:15 AM
              0 responses
              3 views
              0 likes
              Last Post inanazsocial  
              Started by trilliantrader, 04-18-2024, 08:16 AM
              5 responses
              22 views
              0 likes
              Last Post trilliantrader  
              Started by Davidtowleii, Today, 12:15 AM
              0 responses
              3 views
              0 likes
              Last Post Davidtowleii  
              Started by guillembm, Yesterday, 11:25 AM
              2 responses
              10 views
              0 likes
              Last Post guillembm  
              Working...
              X