Exclusive 22% OFF All Quantra by QuantInsti Courses for IBridgePy Users Master algorithmic trading from beginner to advanced — Python for trading, machine learning strategies, options trading, and more.
HUI22 Use HUI22 for 22% off
HUI7 Stack HUI7 for an additional 7% off
Browse Courses →
← Back to Blog

Use IBridgePy to place a group order with One Cancel All (OCA)

April 27, 2017

IBridgePy OCA order functions let you place sophisticated bracket orders with One Cancel All features using Interactive Brokers. You can use create_order() and place_combination_orders() to implement algorithm trading strategies with multiple order legs that cancel each other automatically. Learning how to place group order combinations is essential for advanced trading strategies.

Create_Order to Place Group Order<\/h2>

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. When you place group order combinations like this, you create 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 ( ). Learn more in our documentation.

place_combination_orders([leg0,leg1,leg2])