You can use create_order ( ) and place_combination_orders ( ) to place a bracket order with One Cancel All (OCA) features.
Create_Order
User create_order to clear define all kinds orders that IB supports using critical parameters.
action: “BUY” or “SELL”
amount: number of shares
security: a security object that is created by symbol ( ) , symbols ( ) or superSymbol ( )
orderDetails: an instance created by MarketOrder ( ) or StopOrder ( ) or LimitOrder ( ) or TrailStopLimitOrder ( ) or any other order types supported by IB.
ocaGroup: OCA stands for One Cancel All. Users should put a unique string.
ocaType:
- 1 = Cancel all remaining orders with block
- 2 = Remaining orders are proportionately reduced in size with block
- 3 = Remaining orders are proportionately reduced in size with no bloc
transmit: Specifies whether the order will be transmitted. If set to false, the order will be created at TWS/Gateway but will not be sent.
parentId: the order ID of the parent order, used for bracket and auto trailing stop orders.
An example:
The following code will create a bracket order with three legs. Execution of any leg will cancel the other two legs. A limit order to purchase 2 share of SPY at $239.10, a limit order at $240.50 and a stop order at $238.50. An unique string (“test”) at ocaGroup is to make sure these three orders will be set as One Cancel All.
leg0=create_order('BUY', 2, symbol('SPY'), LimitOrder(239.10), ocaGroup='test') leg1=create_order('SELL', 2, symbol('SPY'), LimitOrder(240.50), ocaGroup='test') leg2=create_order('SELL', 2, symbol('SPY'), StopOrder(238.50), ocaGroup='test')
place_combination_orders
place_combination_orders(legList)
legList: a list containing the legs that are created by create_order ( )
place_combination_orders([leg0,leg1,leg2])