Executing orders with the IB API
In Chapter 10, Set Up the Interactive Brokers Python API, we created contract
and order
objects. Using these, we can use the IB API to execute trades. But before we can execute trades, we have to understand the concept of the next order ID.
The next order ID (nextValidOrderId
) is a unique identifier for each order. Since up to 32 instances of a trading app can run in parallel, this identifier makes sure individual orders are traceable within the trading system. nextValidOrderId
is used to preserve order integrity and prevent overlap between multiple orders submitted simultaneously or in rapid succession. When our trading app connects to the IB API, it receives an integer variable called nextValidOrderId
from the server that is unique to each client connection to TWS. This ID must be used for the first order submission. Subsequently, we are responsible for incrementing this identifier for each new order.
Getting ready
We assume you’ve...