How to Code an OCA Order in the Web API

Articles From: Interactive Brokers
Website: Interactive Brokers

Quant

One-Cancels All (OCA) order type allows an investor to place multiple and possibly unrelated orders assigned to a group. It’s designed to allow investors to submit several orders aimed at taking advantage of the most desirable price within the group. Completion of one piece of the group causes cancellation of the remaining group orders while partial completion causes the group to rebalance.  

The endpoint /iserver/account/{accounted}/orders can be used to pass order request info in an Array by setting isSingleGroup to true in each order. All orders in the same group will be assigned the same oca_group_id. After the orders are successfully placed, send the endpoint /iserver/account/order/status/{orderId} to receive the oca_group_id. 

Please be sure to authenticate first in order to resolve the below URLs.
For authentication details, visit https://interactivebrokers.github.io/cpwebapi/index.html#login

POST 
https://localhost:5000/v1/api/iserver/account/{accountId}/orders 

Request Body: 

Array () [

  • acctId                                  string
                                                 The specified Account order is placed for.
                                                 This should be one of the accounts returned by /iserver/accounts
  • conid                                   string
                                                contract identifier of the security you want to trade.
                                                You can find the conid with /iserver/secdef/search  
  • secType                              string
                                               asset class formatted as conid:type, for example 265598:STK  
  • cOID                                  string
                                               Customer Order Id. Arbitrary string that can be used to identify the order,
                                               e.g “my-fb-order”.Value must be unique for 24h
  • parentId                            string
                                              When placing brack orders, specify with the child order.
                                              Must be equal to the cOID of the parent  
  • orderType                         string
                                              The type of order, example MKT (Market), LMT (Limit).
                                              Use the endpoint /info-and-rules to determine available orderType(s)  
  • listingExchange               string
    required : false                 By default we use “SMART” routing.
                                              Use the endpoint: /portal/iserver/contract/{conid}/info, to determine
                                              valid_exchange
  • outsideRTH                     boolean
                                              Set to true if the order can be executed outside regular trading hours
  • price                                 number        
                                              For LMT this is the limit price. For STP this is the stop price
  • auxPrice                         number
                                              For STOP_LIMIT orders this is the Stop Price  
  • side                                    string
                                              SELL or BUY  
  • ticker                                 string
                                              Contract Symbol
  • tif                                     string
                                              The time-in-force of an order, example DAY.
                                              Use the endpoint /info-and-rules to determine tifTypes
  • referrer                             string
                                              Order reference
  • quantity                            number
                                              the amount or shares of the order. Usually integer, for some special cases
                                             can be float numbers  
  • fxQty                                number
                                             double number, this is the cash quantity field used for FX conversion order  
  • useAdaptive                     boolean
                                              If true, system will use Adaptive Algo to submit the order,
                                              ref: https://www.interactivebrokers.com/en/index.php?f=19091
  • isCcyConv                        boolean
                                               Set to true for a FX conversion order
  • allocationMethod            string
                                              Set the allocation method when placing an order using an FA account
                                             for a group
  • isClose                              boolean
                                              Set to true if order is closing an existing position
  • isSingleGroup                  boolean
                                              Set to true for each order assigned to the same OCA Group.  
]


Response: 

Array () [

  • id                                      string
                                             order_id = system generated order Id(s) for each order.
                                             local_order_id = cOID. parent_order_id = order_Id of the parent order.
  • message                           Array of strings
                                             if the message is a question, you must use the endpoint /iserver/reply/{replyid}
                                             to confirm the message and submit the order.  

]

Please be sure to authenticate first in order to resolve the below URLs.
For authentication details, visit https://interactivebrokers.github.io/cpwebapi/index.html#login

Example: 

Submit two orders within the same /orders endpoint including isSingleGroup: true for both orders. 

Request: 

POST
https://localhost:5000/v1/api/iserver/account/DU***14/orders

Request Body: 
{ 
  “orders”: [ 
  { 
     “acctId”: “DU***14”, 
     “conid”: “265598”, 
     “secType”: “265598:STK”, 
     “cOID”: “66827301”, 
     “orderType”: “LMT”, 
     “listingExchange”: “SMART”, 
     “outsideRTH”: true, 
     “price”: 145.25, 
     “side”: “BUY”, 
     “ticker”: “AAPL”, 
     “tif”: “DAY”, 
     “referrer”: “testOCA”, 
     “quantity”: 1, 
     “useAdaptive”: false, 
     “isClose”: false 
     “isSingleGroup”: true 
  }, 
  { 
     “acctId”: “DU***14”, 
     “conid”: “8314”, 
     “secType”: “8314:STK”, 
     “cOID”: “66827302”, 
     “orderType”: “LMT”, 
     “listingExchange”: “SMART”, 
     “outsideRTH”: true, 
     “price”: 125.5, 
     “side”: “BUY”, 
     “ticker”: “IBML”, 
     “tif”: “DAY”, 
     “referrer”: “testOCA”, 
     “quantity”: 1, 
     “useAdaptive”: false, 
     “isClose”: false, 
     “isSingleGroup”: “true”, 
  } 
 ] 
} 
 
Response: 
[ 
  { 
    “order_id”: “1297028126”, 
    “order_status”: “PreSubmitted”, 
    “local_order_id”: “66827302” 
  }, 
  { 
    “order_id”: “1297028125”, 
     “order_status”: “Submitted”, 
    “parent_order_id”: “66827301” 
    “encrypt_message":"1” 
  } 
] 

Once orders are successfully transmitted, you will return a unique order_id for each order in the Group. Send a separate /status endpoint for each order_id to identify the oca_group_id

Request: 

POST
https://localhost:5000/v1/api/iserver/account/order/status/1297028125

Response: 

{ 

  "sub_type": null, 

  "request_id": "193136", 

  "order_id": 1297028125, 

  "conidex": "265598", 

  "conid": 265598, 

  "symbol": "AAPL", 

  "side": "B", 

  "contract_description_1": "AAPL", 

  "listing_exchange": "NASDAQ.NMS", 

  "option_acct": "c", 

  "company_name": "APPLE INC", 

  "size": "1.0", 

  "total_size": "1.0", 

  "currency": "USD", 

  "account": "DU***14", 

  "order_type": "LIMIT", 

  "limit_price": "145.25", 

  "cum_fill": "0.0", 

  "order_status": "Submitted", 

  "order_status_description": "Order Submitted", 

  "tif": "DAY", 

  "fg_color": "#000000", 

  "bg_color": "#00F000", 

  "order_not_editable": false, 

  "editable_fields": "\u001e", 

  "cannot_cancel_order": false, 

  "outside_rth": true, 

  "deactivate_order": false, 

  "use_price_mgmt_algo": true, 

  "sec_type": "STK", 

  "available_chart_periods": "#R|1", 

  "order_description": "Buy 1 Limit 145.25 DAY", 

  "order_description_with_contract": "Buy 1 AAPL Limit 145.25 DAY", 

  "alert_active": 1, 

  "child_order_type": "3", 

  "size_and_fills": "0/1", 

  "oca_group_id": "oco-1297028125", 

  "exit_strategy_display_price": "145.25", 

  "exit_strategy_chart_description": "Buy 1 Limit 145.25 DAY", 

  "exit_strategy_tool_availability": "1", 

  "allowed_duplicate_opposite": true, 

  "order_time": "210901211533" 

} 

Request:

POST
https://localhost:5000/v1/api/iserver/account/order/status/1297028126

Response: 

{ 

  "sub_type": null, 

  "request_id": "193367", 

  "order_id": 1297028126, 

  "conidex": "8314", 

  "conid": 8314, 

  "symbol": "IBM", 

  "side": "B", 

  "contract_description_1": "IBM", 

  "listing_exchange": "NYSE", 

  "option_acct": "c", 

  "company_name": "INTL BUSINESS MACHINES CORP", 

  "size": "1.0", 

  "total_size": "1.0", 

  "currency": "USD", 

  "account": "DU***14", 

  "order_type": "LIMIT", 

  "limit_price": "125.50", 

  "cum_fill": "0.0", 

  "order_status": "PreSubmitted", 

  "order_status_description": "Order Submitted", 

  "tif": "DAY", 

  "fg_color": "#FFFFFF", 

  "bg_color": "#0000CC", 

  "order_not_editable": false, 

  "editable_fields": "\u001e", 

  "cannot_cancel_order": false, 

  "outside_rth": false, 

  "deactivate_order": false, 

  "use_price_mgmt_algo": true, 

  "sec_type": "STK", 

  "available_chart_periods": "#R|1", 

  "order_description": "Buy 1 Limit 125.50 DAY", 

  "order_description_with_contract": "Buy 1 IBM Limit 125.50 DAY", 

  "alert_active": 1, 

  "child_order_type": "3", 

  "size_and_fills": "0/1", 

  "oca_group_id": "oco-1297028125", 

  "exit_strategy_display_price": "125.50", 

  "exit_strategy_chart_description": "Buy 1 Limit 125.50 DAY", 

  "exit_strategy_tool_availability": "1", 

  "allowed_duplicate_opposite": true, 

  "order_time": "210901211533" 

} 

If you have any further questions or issues creating OCA orders, please reach out to the API Group, ref: https://www.interactivebrokers.com/en/index.php?f=47047

Visit the IBKR API Center for Downloads, Resources, and Technical Details: 
https://www.interactivebrokers.com/en/trading/ib-api.php.

Any stock, options or futures symbols displayed are for illustrative purposes only and are not intended to portray recommendations.

Disclosure: Interactive Brokers

The analysis in this material is provided for information only and is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad-based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation by IBKR to buy, sell or hold such investments. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

The views and opinions expressed herein are those of the author and do not necessarily reflect the views of Interactive Brokers, its affiliates, or its employees.

Disclosure: Order Types / TWS

The order types available through Interactive Brokers LLC’s Trader Workstation are designed to help you limit your loss and/or lock in a profit. Market conditions and other factors may affect execution. In general, orders guarantee a fill or guarantee a price, but not both. In extreme market conditions, an order may either be executed at a different price than anticipated or may not be filled in the marketplace.