ib-insync is a Pythonic framework for Interactive Brokers API, offering both synchronous and asynchronous interfaces. It abstracts much of the complexity of the underlying `ibapi` library, providing a high-level, event-driven API for trading, market data, and account management. The current version is 0.9.86, and the library maintains a frequent release cadence with minor updates and bug fixes.
pip install ib-insync ibapiVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to connect to the Interactive Brokers API using `ib-insync`, qualify a stock contract, and retrieve historical data. It uses the asynchronous interface, which is the recommended modern approach. Ensure TWS (Trader Workstation) or IB Gateway is running and configured to accept API connections on the specified port.
Always install `ibapi` alongside `ib-insync` and keep both libraries reasonably up-to-date. Refer to `ib-insync`'s official documentation for recommended `ibapi` version ranges. If issues occur, try upgrading or downgrading `ibapi`.
For script-based execution, `asyncio.run(your_async_main_function())` is preferred. For synchronous interactive use (e.g., in Jupyter), `ib.run()` can be used, but be mindful of its blocking nature. Ensure all `ib-insync` methods are `await`ed in an `async` context.
Verify TWS/IB Gateway is running. Check your firewall settings. Confirm the port (default TWS: 7497, Gateway: 4002) and Client ID. In TWS/Gateway, go to File -> Global Configuration -> API -> Settings and ensure 'Enable ActiveX and Socket Clients' is checked.
Check your IB account subscriptions to ensure you have the necessary data packages (e.g., Level 1, Level 2, specific exchanges) for the data you are requesting. Paper trading accounts often have fewer default subscriptions.