IBKR TWS Python API – Case Study in Pair Trades

In this video we move beyond core API functionality and build on previous lessons to examine a case study in which advanced order types may be advantageous in implementing a specific trading strategy.

The TWS API supports more than 60 of the order types and attributes available in TWS. Here we consider a scenario in which the Pegged-To-Primary/Relative order type is used, with orders in a hedging pair trade to place related orders for two different instruments, such that placement of the second order is handled automatically by the IBKR server.

We walk-through the code of a Python program to place the initial and the hedging order and demonstrate how they will appear in TWS. Throughout the lesson, please keep in mind that the examples discussed are purely for technical demonstration purposes, and do not constitute trading advice. Also, it is important to remember that placing trades in a paper account is recommended before any live trading.

To see other short videos in this series, visit our API news section. For the entire course in Python see ibkr.com/ibta.

Disclosure: Displaying Symbols on Video

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

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.

Disclosure: Margin Trading

Trading on margin is only for sophisticated investors with high risk tolerance. You may lose more than your initial investment.

For additional information regarding margin loan rates, see ibkr.com/interest