• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.


No announcement yet.

Partner 728x90


Can I check whether OCO string is reused?

  • Filter
  • Time
  • Show
Clear All
new posts

    Can I check whether OCO string is reused?


    Is there a way to check whether OCO string is reused?
    Or the Dictionary<string, bool> that filled in OnExecutionUpdate() is one only way?

    When OCO string become reused?
    Does it happened after one of orders became Filled or OverFilled?

    Say, I have a working order. It was submitted 100 bars ago, partial filled 50 bars ago.
    May I read it OCO string and submit another order in this bracket?

    Strategy have 100 contracts position and 2 OCO Unmanaged protective orders ("stop loss" and "take profit") on InteractiveBrokers account.
    Than connection lost.
    Than "take profit" partial filled with 30 contracts.
    Than "stop loss" partial filled with 70 contracts.

    What will remain on account?
    Position will be flat, but what about orders?
    Does IB automatically adjust OCO brackets quantity?
    And what if "stop loss" and "take profit" initially had different quantity?

    Thank you.
    NinjaTrader Ecosystem Vendor - fx.practic

    Hello fx.practic,

    Thanks for your post.

    RE 1: Order objects have an OCO string property that tells you the OCO ID associated with the order. I would however suggest focusing your approach to create a unique OCO ID whenever you need to submit both orders for the OCO pair. I.E. a bracketed entry or target/stop. This would ensure that a unique ID is generated for each instance an OCO pair is needed.

    RE 2: OCO ID's generally become invalid when the order starts to fill. Taking the approach above will avoid situations like this because you are creating a unique ID whenever a pair needs to be submitted, and then you can generate a new unique ID for any future pairs. Some brokers like TDA will require submitting an OCO pair at the same time and will not allow you to submit an order at a later time.

    RE 3: I would not expect the OCO to be valid because it is already filling. You could consider taking an approach to logically check some conditions, cancel the existing order/orders, and then to submit new orders in OnOrderUpdate when you see the cancellation come back.

    RE 4: The stop and target should be fully cancelled at this point because they are OCO'd and their quantities should be reduced by their fills.

    As a tip with Interactive Brokers, you can use a paper trading account to observe their specific dynamics with OCO handling.

    As another tip, Order events and Execution events may come in a different order than 1. Order update 2. Execution update 3. Position update where Execution updates may be seen first. I have attached a Managed Approach strategy that handles OnExecutionUpdate related events in a private method triggered from OnPositionUpdate. This could be used to help guarantee ExecutionUpdate related events happen after OrderUpdate events. The technique can be adopted for Unmanaged Approach as well.

    Let us know if you have any questions.
    Attached Files
    JimNinjaTrader Customer Service


      Great example, thank you, Jim.

      In general case order needs multiple tick to be completely filled.
      Ever more:

      Say, entry long orders with quantity 100 submitted.
      It was filled with quantity 40.
      Then price reversed and touch stop loss order; and filled stop loss with 20 contracts.
      At this time connection interrupted.
      And then price falls down forever.

      What is the best way to submit unmanaged OCO protective orders to be prepared for this situation?

      At the moment, the strategy doesn't use OCO.
      Once entry order partially filled the first time, the strategy submits Sl and Tp.
      After each next execution, the strategy submitting ChangerOrders() to make Sl and Tp quantity same to Position quantity.
      This is reliable while connection established.

      But connection may be interrupted. That's why I thinking about server-side OCO.

      But what is the best way to submit them?
      Can use the same algorithm with OCO-bracket? I mean, can strategy multiple times change quantity for OCO paired orders?
      Last edited by fx.practic; 01-20-2020, 02:45 PM.
      NinjaTrader Ecosystem Vendor - fx.practic


        Hello fx.practic,

        It would be best to submit OCO protective orders based on the quantity of the entry order's quantity. When the order is filling, the OCO orders for target and stop would be updated with ChangeOrder to reflect the new quantity. I have responded to your other thread with an example that is based on our SampleOnOrderUpdate strategy. I'll link this thread for the forum's reference.


        NinjaTrader will need to be running to see the execution of the entry order to adequately place full targets and stops in this fashion. If this situation happens, the person running the strategy should call their Order's desk and intervene immediately because further fills on the entry order will not have the protective orders managing this quantity. (NinjaTrader will not see the addition entry order fills and will not be able to amend the protective orders with a new quantity.)

        Please let me know if I can be of further assistance.
        JimNinjaTrader Customer Service


          Thanks a lot, Jim. It is clear now.
          NinjaTrader Ecosystem Vendor - fx.practic


          Latest Posts


          Topics Statistics Last Post
          Started by sledge, 11-28-2018, 08:42 AM
          2 responses
          Last Post aligator  
          Started by imalil, Today, 09:50 PM
          0 responses
          Last Post imalil
          by imalil
          Started by rtwave, Today, 07:06 PM
          0 responses
          Last Post rtwave
          by rtwave
          Started by patpat, Today, 04:25 PM
          4 responses
          Last Post patpat
          by patpat
          Started by bboyle1234, Today, 06:25 PM
          0 responses
          Last Post bboyle1234