Introduction
This library provides a comprehensive Rust implementation of the Interactive Brokers TWS API, offering a robust and user-friendly interface for TWS and IB Gateway. Designed with performance and simplicity in mind, ibapi
is a good fit for automated trading systems, market analysis, real-time data collection and portfolio management tools.
With this fully featured API, you can retrieve account information, access real-time and historical market data, manage orders, perform market scans, and access news and Wall Street Horizons (WSH) event data. Future updates will focus on bug fixes, maintaining parity with the official API, and enhancing usability.
If you encounter any issues or require a missing feature, please review the issues list before submitting a new one.
Available APIs
The Client documentation provides comprehensive details on all currently available APIs, including trading, account management, and market data features, along with examples to help you get started.
Install
Run the following Cargo command in your project directory:
Or add the following line to your Cargo.toml
:
= "1.0.0"
Note: Check crates.io/crates/ibapi for the latest available version.
Examples
These examples demonstrate key features of the ibapi
API.
Connecting to TWS
The following example shows how to connect to TWS.
use Client;
Note: Use
127.0.0.1
instead oflocalhost
for the connection. On some systems,localhost
resolves to an IPv6 address, which TWS may block. TWS only allows specifying IPv4 addresses in the allowed IP addresses list.
Creating Contracts
Here’s how to create a stock contract for TSLA using the stock helper function.
// Create a contract for TSLA stock (default currency: USD, exchange: SMART)
let contract = stock;
The stock, futures, and crypto methods provide shortcuts for defining contracts with reasonable defaults that can be modified after creation.
For contracts requiring custom configurations:
// Create a fully specified contract for TSLA stock
Contract
For a complete list of contract attributes, explore the Contract documentation.
Requesting Historical Market Data
The following is an example of requesting historical data from TWS.
use datetime;
use Contract;
use ;
use Client;
Requesting Realtime Market Data
The following is an example of requesting realtime data from TWS.
use Contract;
use ;
use Client;
In this example, the request for realtime bars returns a Subscription that is implicitly converted into a blocking iterator over the bars. The subscription is automatically cancelled when it goes out of scope. The Subscription
can also be used to iterate over bars in a non-blocking fashion.
// Example of non-blocking iteration
loop
Explore the Subscription documentation for more details.
Since subscriptions can be converted to iterators, it is easy to iterate over multiple contracts.
use Contract;
use ;
use Client;
Note: When using
zip
, the iteration will stop if either subscription ends. For independent processing, consider handling each subscription separately.
Placing Orders
Multi-Threading
The Client can be shared between threads to support concurrent operations. The following example demonstrates valid multi-threaded usage of Client.
use Arc;
use thread;
use Contract;
use ;
use Client;
Some TWS API calls do not have a unique request ID and are mapped back to the initiating request by message type instead. Since the message type is not unique, concurrent requests of the same message type (if not synchronized by the application) may receive responses for other requests of the same message type. Subscriptions using shared channels are tagged with the SharesChannel trait to highlight areas that the application may need to synchronize.
To avoid this issue, you can use a model of one client per thread. This ensures that each client instance handles only its own messages, reducing potential conflicts:
use thread;
use Contract;
use ;
use Client;
In this model, each client instance handles only the requests it initiates, improving the reliability of concurrent operations.
Fault Tolerance
The API will automatically attempt to reconnect to the TWS server if a disconnection is detected. The API will attempt to reconnect up to 30 times using a Fibonacci backoff strategy. In some cases, it will retry the request in progress. When receiving responses via a Subscription, the application may need to handle retries manually, as shown below.
use Contract;
use ;
use ;
Contributions
We welcome contributions of all kinds. Feel free to propose new ideas, share bug fixes, or enhance the documentation. If you'd like to contribute, please start by reviewing our contributor documentation.
For questions or discussions about contributions, feel free to open an issue or reach out via our GitHub discussions page.