Install & Compatibility
Where this runs
tested against v2.1.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
py 3.9
✕ build_error
✕ build_error
91MB installed
● package 91MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
IB
✓ from ib_async import IB
✗ from ib_insync import IB
This quickstart demonstrates how to connect to the Interactive Brokers TWS/Gateway, define a stock contract, and retrieve 1-minute historical trade data for the last 10 minutes using the `IB.run()` method to manage the asyncio event loop. Ensure TWS/Gateway is running and API access is enabled.
from ib_insync import IB, Contract, util
import asyncio
async def main():
ib = IB()
try:
# Connect to TWS/Gateway. Default is localhost:7497 for TWS, 4002 for Gateway.
# Change host/port if TWS/Gateway is running elsewhere.
await ib.connect('127.0.0.1', 7497, clientId=1)
print("Connected to IB TWS/Gateway")
# Define a contract
contract = Contract(symbol='SPY', secType='STK', exchange='SMART', currency='USD')
# Request market data
# Ensure you have subscribed to market data for SPY in TWS/Gateway
# Data from reqMktData is real-time, consider historical data for backtesting
# for data in ib.reqMktData(contract):
# print(f"Time: {data.time}, Bid: {data.bid}, Ask: {data.ask}")
# Request historical data for the last 10 minutes
bars = await ib.reqHistoricalData(
contract,
endDateTime='',
durationStr='10 T',
barSizeSetting='1 min',
whatToShow='TRADES',
useRTH=True
)
if bars:
print(f"Retrieved {len(bars)} historical bars for {contract.symbol}")
for bar in bars:
print(f"Time: {bar.date}, Open: {bar.open}, Close: {bar.close}, Volume: {bar.volume}")
else:
print(f"No historical data found for {contract.symbol}")
except ConnectionRefusedError:
print("Connection refused. Is TWS/Gateway running and configured for API access?")
except Exception as e:
print(f"An error occurred: {e}")
finally:
if ib.isConnected():
ib.disconnect()
print("Disconnected from IB TWS/Gateway")
# ib-async recommends using IB.run() for managing the event loop for simpler cases
# or asyncio.run() for more complex applications.
# To run the async main function:
if __name__ == '__main__':
# Use IB.run() for simple script execution without manually managing asyncio loop
IB.run(main())
# Alternatively, for more control over the asyncio event loop:
# asyncio.run(main())
Debug
Known issues
gotchaThe PyPI package name is `ib-async`, but the actual Python package to import is `ib_insync`.fixAlways use `from ib_insync import ...` in your Python code after `pip install ib-async`.
affects: All versions (2.x)
gotchaib-async requires a running Interactive Brokers TWS (Trader Workstation) or IB Gateway instance to connect to. It does not connect directly to IB servers.fixEnsure TWS or IB Gateway is running and configured to allow API connections on the specified host and port (default TWS: 7497, Gateway: 4002). Check TWS/Gateway API settings for `Enable ActiveX and Socket Clients` and `Read-Only API` permissions.
affects: All versions
breakingBetween versions 1.x (original ib_insync) and 2.x (ib-async fork), there were significant internal changes and some API adjustments. For example, `util.startLoop()` is less commonly used, with `IB.run()` or direct `asyncio.run()` preferred for managing the event loop.fixReview the official ib-async documentation for the latest API usage, especially for event loop management and certain data request methods. Update your code to use `IB.run()` or `asyncio.run()` to start your main async function.
affects: Upgrading from ib_insync 1.x to ib-async 2.x
gotchaIncorrectly mixing `asyncio.run()` with `IB.run()` or calling `IB.run()` multiple times in a single script can lead to `RuntimeError: Event loop is already running.`fixUse either `IB.run()` (which manages the asyncio loop internally for convenience) OR `asyncio.run()` (for direct asyncio control), but not both to start the main application. If using `IB.run()`, ensure it's called only once per application run.
affects: All versions
Upgrade
Version history
2.1.0latest on PyPI · released Dec 8, 2025
Audit
Dependencies
No dependency data recorded yet.