Asset Classes

Free investment financial education

Language

Multilingual content from IBKR

Close Navigation
Learn more about IBKR accounts
How to Code a Bracket Order in the Web API

How to Code a Bracket Order in the Web API

Posted June 3, 2021 at 9:42 am
IBKR API
Interactive Brokers
Quant

To help limit loss and lock in a profit you can bracket an order with additional opposite-side orders.

A BUY order is bracketed by a high-side sell limit and a low-side sell stop order. A SELL order is bracketed by a high-side buy stop order and a low side buy limit order.  

The endpoint /iserver/account/{accounted}/orders can be used to pass order request info in an Array to include the parent and child order(s).

A single customer order Id (cOID) is sent with the parent and the same value is specified in parentId for the child order(s).

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 () [

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

]

Response:

Array (){

  • string                                id                                       
    order_id = system generated order Id(s) for each order. local_order_id = cOID. parent_order_id = order_Id of the parent order.
  • Array of strings              message                         
    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:

Request:

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

Request Body:
{
  “orders”: [
  {
     “acctId”: “DU***14”,
     “conid”: “265598”,
     “cOID”: “66807300”,
     “orderType”: “LMT”,
     “listingExchange”: “SMART”,
     “outsideRTH”: true,
     “price”: 115.5,
     “side”: “BUY”,
     “ticker”: “AAPL”,
     “tif”: “DAY”,
     “referrer”: “ParentOrder”,
     “quantity”: 50,
     “useAdaptive”: false,
     “isClose”: false
  },
  {
     “acctId”: “DU***14”,
     “conid”: “265598”,
     “orderType”: “LMT”,
     “listingExchange”: “SMART”,
     “outsideRTH”: true,
     “price”: 125.5,
     “side”: “SELL”,
     “ticker”: “AAPL”,
     “tif”: “DAY”,
     “referrer”: “ProfitTakerOrder”,
     “quantity”: 50,
     “useAdaptive”: false,
     “isClose”: false,
     “parentId”: “66807300”,
  },
  {
     “acctId”: “DU***14”,
     “conid”: “265598”,
     “orderType”: “STP”,
     “listingExchange”: “SMART”,
     “outsideRTH”: false,
     “price”: 110.5,
     “side”: “SELL”,
     “ticker”: “AAPL”,
     “tif”: “DAY”,
     “referrer”: “StopLossOrder”,
     “quantity”: 50,
     “useAdaptive”: false,
     “isClose”: false,
     “parentId”: “66807300”,
  }
 ]
}

Response:
[
  {
    “order_id”: “1763237133”,
    “order_status”: “Submitted”,
    “local_order_id”: “66807300”
  },
  {
    “order_id”: “1763237135”,
     “order_status”: “PreSubmitted”,
    “parent_order_id”: “1763237133”
  }
]

If you have any further questions or issues creating a bracket order, 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.

See the previous installments in this series: 

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.

IBKR Campus Newsletters

This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.