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

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>

EXCLUSIVE OFFER

Extra 7% OFF All Quantra Courses

Level up your quant trading skills with courses from Quantra by QuantInsti — Python for trading, machine learning strategies, options trading, and more.

HUI7 Browse Courses → Use code HUI7 at checkout

User create_order to clear define all kinds orders that IB supports using critical parameters.

action: “BUY” or “SELL”

amount: number of shares

Need help building this strategy?

Our Rent-a-Coder team builds custom automated trading programs. From simple scripts to complex multi-account strategies — we handle it all.

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])
EXCLUSIVE OFFER

Extra 7% OFF All Quantra Courses

Level up your quant trading skills with courses from Quantra by QuantInsti — Python for trading, machine learning strategies, options trading, and more.

HUI7 Browse Courses → Use code HUI7 at checkout

Ready to Automate Your Trading?

Download IBridgePy free or let our coders build your strategy.

Download Free Hire a Coder