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

OnOrderUpdate using Interactive Brokers API

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

    OnOrderUpdate using Interactive Brokers API

    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 :
    Rithmic and Interactive Brokers Users: Due to provider API design these adapters do not guarantee the sequence of events of OnOrderUpdate, OnExecution, and OnPositionUpdate. Therefore when working on a strategy that will run on these connections it is best practice to only work with passed by value data from that callback to eliminate the dependency on the sequence of events.
    The problem is that the order name does not passed by value data from that callback.
    the same problem is also refers to OnOrderUpdate, OnExecution, and OnPositionUpdate.

    How I can solve this situation?

    Sincerely, Kobi






    #2
    Hello Kobi,

    Thanks for your question.

    You could consider checking the order name from Execution.Order.Name.

    When designing a robust strategy that will work with Rithmic and Interactive Brokers, you can consider designing a strategy that uses OnOrderUpdate only, and this will work around the obscure event ordering for Rithmic and IB.

    Please note that strategy positions (which the Managed Approach uses internally) are updated on Execution events (OnExecutionUpdate.) If you would like to track your own strategy position that is updated on OrderUpdates, this would be possible, but requires we use the Unmanaged Approach.

    I have attached an example Unmanage strategy that uses OnOrderUpdate only, and creates its own Position object to track the strategy position. The strategy is built to be similar to our SampleOnOrderUpdate strategy.

    Ref.

    Execution - https://ninjatrader.com/support/help.../execution.htm

    SampleOnOrderUpdate - https://ninjatrader.com/support/help...and_onexec.htm

    Unmanaged Approach - https://ninjatrader.com/support/help...d_approach.htm

    We look forward to assisting.
    Attached Files
    JimNinjaTrader Customer Service

    Comment


      #3
      Hello Jim,

      Thank you for your response.

      I am a little bit confused.

      1.
      Code:
      You could consider checking the order name from Execution.Order.Name.
      Does order.name in OnOrderUpdate or Execution.Order.Name in OnExecutionUpdate are synchronize with
      with passed by value data from that callback?

      2.
      Code:
      When designing a robust strategy that will work with Rithmic and Interactive Brokers, you can consider designing a strategy that uses OnOrderUpdate only, and this will work around the obscure event ordering for Rithmic and IB.
      What you mean by
      Code:
      this will work around the obscure event ordering
      ?
      Do you mean that it possible to get OnExecutionUpdate event before OnOrderUpdate?

      3.
      Code:
      Please note that strategy positions (which the Managed Approach uses internally) are updated on Execution events (OnExecutionUpdate.) If you would like to track your own strategy position that is updated on OrderUpdates, this would be possible, but requires we use the Unmanaged Approach.
      Follow your example Unmanage strategy that uses OnOrderUpdate only ,
      why I need to use Unmanaged Approach ?
      I think that I will not use or implement the real OnExecutionUpdate() or real OnPositionUpdate.

      I only need from OnOrderUpdate to call my private function InlineExecutionUpdate() as the attached example.

      Sincerely,Kobi
      Last edited by levikNT; 11-03-2020, 05:32 AM.

      Comment


        #4
        Hello Kobi,

        Re 1: I was just referring to using Execution.Order.Name in OnExecutionUpdate to be able to get the order name of the executing order. Since this is from the Execution event, we can expect it to be correct. I still recommend taking an OnOrderUpdate only approach to avoid issues seen with the obscure event ordering.

        Re 2: Yes, an ExecutionUpdate can be seen before the order update. ExecutionUpdates will drive the strategy position and are updated as early as OnExecutionUpdate and PositionUpdates drive the account position and are updated as early as OnPositionUpdate. Strategy positions can be referenced with Position/Positions and account positions can be referenced with PositionAccount/PositionsAccount.

        Since the Managed Approach is tied to the Strategy Position and that is updated in OnExecutionUpdate, we need to decouple ourselves from the internal strategy position and make our own using OrderUpdates instead. Making our own Position object based on OrderUpdates gives us a Position object to use, and using the Unmanaged Approach decouples us from the built in Strategy Position object.

        My example uses an "InlineExecutionUpdate" method so the work flow is similar to what we are used to working with following SampleOnOrderUpdate style strategies.

        We look forward to assisting.

        JimNinjaTrader Customer Service

        Comment


          #5
          Hello Jim,

          Thank you for your replay.

          My strategy contains a private position object that I developed. it different than the NT position object.
          My strategy manages its own private position which is not the NT strategy position.

          My strategy manages this object from the opening until the position is closed and does not use NT strategy position object .
          I agree with your idea of taking an OnOrderUpdate only approach to avoid issues seen with the obscure event ordering.

          My questions are:
          1. If I do not use NT strategy position object and not use OnExecution, OnPositionUpdate and continue with your idea,
          Will I still be able to continue working with manage approach instead of Unmanaged Approach?

          2. will I be able to work with account position?

          Sincerely, Kobi

          Comment


            #6
            Hello Kobi,

            If you use the Managed Approach, your order submissions are going to be limited to what is currently seen for the strategy's built in Position object. You need to use Unmanaged to break away from that. Since the strategy Position object is based on ExecutionUpdates and your logic is going to focus on OnOrderUpdate, then you can still hit some of the same issues involving order/execution/position ordering and what the strategy sees as its current position. Using the Unmanaged Approach would be needed.

            Account position updates will come from PositionUpdate events. These happen more reliably after OrderUpdate events with Interactive Brokers, but if there is a change to the way they submit these events, the strategy logic may break. My recommendation is to track a Position object based off of Filled and Part Filled order updates in OnOrderUpdate. The code I have given can help you you started there.

            JimNinjaTrader Customer Service

            Comment


              #7
              Hello Jim,

              Sorry, Its still not clear to me

              From your first response I understood that only if I want to track the strategy position the Unmanaged Approach would be needed

              If you would like to track your own strategy position that is updated on OrderUpdates, this would be possible, but requires we use the Unmanaged Approach.
              As I mentioned I am not intending to use NT strategy position object. so I do not understand why my order submissions are going to be limited?

              What limitations of order submissions will be if I use the Manage approach?

              Sincerely, Kobi

              Comment


                #8
                If you call ExitLong, it is tied to the strategy's Position object, not your own Position object. If the strategy does not see there is a position with its built in Position object, ExitLong won't execute.
                JimNinjaTrader Customer Service

                Comment


                  #9
                  Hi _Jimhave a few questions about the "UnmanagedOnOrderUpdate.zip":

                  1. How can I correctly query the Position.AveragePrice without the "OnExecutionUpdate" method? The "OnExecutionUpdate" is called after the "OnOrderUpdate". i need the "Position.AveragePrice" value but already in the "OnOrderUpdate" method. If several trades are executed consecutively, the "OnOrderUpdate" method does not display the value correctly. This is probably due to rounding differences and would be displayed correctly with the direct query via the "OnExecutionUpdate" method (see picture).
                  Code:
                  private void InlineExecutionUpdate(Order order, double AvgFillPrice, int quantity, MarketPosition marketPosition, string orderId)
                  {
                     ...
                     positionAveragePrice = Instrument.MasterInstrument.RoundToTickSize(Positi on.AveragePrice);
                     Print("positionAveragePrice: " + positionAveragePrice);
                     ...
                  }
                  Click image for larger version

Name:	Screenshot_3.jpg
Views:	712
Size:	184.3 KB
ID:	1126365

                  2. Wouldn't it be better to monitor the orders in the "OnExecutionUpdate" method instead of the "OnOrderUpdate" method? We discussed the problem with Rithmic and partialFill in another post some time ago: https://ninjatrader.com/support/foru...d-trailingstop.
                  Unfortunately i haven't found a solution for rithmic and partialFill yet. There are always problems with the order partial execution and its monitoring. So i thought i'd try to manage it with this solution using the "OnOrderUpdate" method. which method would you recommend me with Rithmic. i would like to perfect one of both variants and now i don't know which is the better one all about the "OnOrderUpdate" or about the "OnExecutionUpdate" method? It seems to me, that there will be some problems in the "OnOrderUpdate" method as well
                  sidlercom80
                  NinjaTrader Ecosystem Vendor - Sidi Trading

                  Comment


                    #10
                    Hello sidlercom80,

                    Strategy Positions are based on ExecutionUpdates. Order object assignment is done in OnOrderUpdate. Since this strategy aims to break out of the need of Order/Execution/Position event ordering, the strategy creates its own position object that we update in OnOrderUpdate. You would use that created position object instead of the built in Position object.

                    Since Order object assignment is done in OnOrderUpdate, you could not use OnExecutionUpdate for Order object assignment. We can also keep track of how the position size changes from filled/part filled OrderUpdates, so this is why OnOrderUpdate is used with Unmanaged.

                    Since the strategy just uses OrderUpdates and does not depend on event ordering between orders/execution/position updates, the approach can be used for Rithmic and Interactive Brokers.

                    We found workarounds for the current ordering in the past, but as these may subject to change, you would be best off doing something like is done in UnmanagedOnOrderUpdate.
                    JimNinjaTrader Customer Service

                    Comment


                      #11
                      Hi _Jim,

                      How must the Position.AveragePrice be calculated? Could PositionAccount.AveragePrice be used for this or does it all have to be calculated manually (how)?
                      Code:
                      calculatedPosition.AveragePrice = calculatedPosition.AveragePrice == 0 ? averageFillPrice : Instrument.MasterInstrument.RoundToTickSize(PositionAccount.AveragePrice);
                      I would also like to thank all NinjaTrader service employees, who take care of people like me with a lot of effort, who don't know much about programming, but who can learn it day by day with your help. Many many thanks!!!
                      Last edited by sidlercom80; 11-06-2020, 05:52 AM.
                      sidlercom80
                      NinjaTrader Ecosystem Vendor - Sidi Trading

                      Comment


                        #12
                        Hello sidlercom80,

                        PositionAccount will reflect the Account position and this updates on PositonUpdate events. You will not break away from Order/Execution/Position ordering if you use Position or PositionAccount.

                        Position.AveragePrice is not calculated in this example and I don't not have an example that demonstrates calculating it. However, it is the average entry price, so for an entry that fills with one Filled OrderUpdate event, the fill price of that order can be used. For Partial Fills on an entry order, you would have to multiply the fill price of each part fill times (quantity of partial fill / quantity of the order) and then add that together for each partial fill.
                        JimNinjaTrader Customer Service

                        Comment


                          #13
                          Hello Jim,

                          I Updated my strategy to work with Unmanaged approach and use OnOrderUpdate only, and create its own Position object to track the strategy position.
                          I need some time to test it in real.

                          My Questions are :

                          1. In case of connection lost ,how will I monitor my positions account and my account status since we break away(not using) from onExecutionUpdate/OnPositionUpdate(Of course after reconnection again) ?
                          2. How can I Sync with my IB account ?

                          Sincerely, Kobi
                          Last edited by levikNT; 11-07-2020, 10:30 AM.

                          Comment


                            #14
                            Hello Kobi,

                            This would be handled the same as any NinjaScript strategy.

                            If you want the strategy to stay running in the event of a connection loss, navigate to the Control Center's Tools > Options > Strategies menu, and under On Connection Loss, set handling to Keep Running and use a value for Disconnect Delay Seconds that will last the connection loss.

                            Syncing accounts would work in the same way, using the NinjaTrader Start Behaviors when we enable the strategy.

                            Start Behavior information

                            When a strategy is enabled, it processes historical data to determine trades that the strategy would have made on the data that is already on the PC/chart and to determine what position the strategy is in. (Strategy positions are separate from actual Account positions.)

                            Wait Until Flat will wait until this virtual/historical position is closed before live orders can be submitted. It ensures that the strategy starts trading live from a flat position. If you enable the strategy when the account is flat, the strategy will waiting until this position calculated from historical data is closed so it is logically making trades starting from an entry signal.

                            Immediately Submit automatically submits working orders from when the strategy processed historical data, and assumes the strategy position and account position are where you want it when you enable the strategy. This is typically used to have a strategy resume a position after disabling/enabling. If the strategy already had live orders running, the orders will resume with the new enablement of the strategy if they match the historically calculated orders. If the orders calculated from historical data do not match the live working orders, the live working orders will be cancelled and replaced by those calculated from historical data.

                            Sync Account Positions is an additional option that has NinjaTrader submit an order to sync the account position to the position calculated by the strategy. (Not the other way around.)

                            Adopt Account Position would be used if you want the strategy to inherit the Account Position on enablement. This requires additional programming.

                            If you do not want the strategy to calculate a position from processing historical data. Simply add if (State == State.Historical) return; to the top of your strategy logic so historical processing is skipped. The strategy will then always start from a flat position because it has not calculated any orders.

                            Strategy vs. Account Position — https://ninjatrader.com/support/help..._account_p.htm

                            Start Behaviors — https://ninjatrader.com/support/help..._positions.htm


                            Please let us know if you have any questions.
                            JimNinjaTrader Customer Service

                            Comment


                              #15
                              Hello Jim,
                              Sorry but you did not answer to my questions. That is not what I meant by my questions. I will phrase my questions differently.

                              I am familiar with NT Start Behavior information.

                              Suppose I sent an entry command in the amount of 200 and immediately afterwards an event of a connection loss happened. The OnOrderUpdate is not called and, I do not know what the real state of the real position account .
                              After some time the I received that the connection is OK.

                              Know I would like to know the real status of my account.
                              since I break away from onExecutionUpdate/OnPositionUpdate I have no information about the real status of the account, and I can not use the strategy position .
                              Maybe the order state is filled or partial filled , maybe know the real price far away from my limit price.

                              how can I monitor my real account position?
                              In order to sync my account I need to monitor my account. How I do that?

                              I think that it basic requirement to monitor my real position. I would like to handle my synchronize by my self.

                              Sincerely, Kobi

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by bortz, 11-06-2023, 08:04 AM
                              47 responses
                              1,607 views
                              0 likes
                              Last Post aligator  
                              Started by jaybedreamin, Today, 05:56 PM
                              0 responses
                              9 views
                              0 likes
                              Last Post jaybedreamin  
                              Started by DJ888, 04-16-2024, 06:09 PM
                              6 responses
                              19 views
                              0 likes
                              Last Post DJ888
                              by DJ888
                               
                              Started by Jon17, Today, 04:33 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post Jon17
                              by Jon17
                               
                              Started by Javierw.ok, Today, 04:12 PM
                              0 responses
                              16 views
                              0 likes
                              Last Post Javierw.ok  
                              Working...
                              X