Interactive Brokers bot: the IBKR API, ib_insync and code
Interactive Brokers is the professional’s brokerage — global access to stocks, options, futures, forex and bonds through a single account, with the deepest market data and lowest costs in the business. Its automation model is unusual: instead of a pure REST API, your bot connects to a running TWS or IB Gateway instance over a socket, and that local gateway brokers every request. It is more powerful and more finicky than Alpaca, and the gateway-uptime quirk catches every beginner. This guide covers the IBKR API model, the ib_insync Python library, and the realities of automating a pro platform.
The gateway API model
Unlike Alpaca’s pure cloud REST API, an IBKR bot talks to a local Trader Workstation (TWS) or IB Gateway process running on your machine or VPS. Your Python code opens a socket to that gateway, which holds the authenticated session and relays orders to IBKR. The upside is one account spanning every asset class worldwide; the downside is you must keep the gateway alive and logged in.
Connecting with ib_insync
The community ib_insync library wraps IBKR’s low-level API into clean, modern Python.
python · ibkr_bot.pyfrom ib_insync import IB, Stock, MarketOrder
ib = IB()
ib.connect('127.0.0.1', 7497, clientId=1) # 7497 = paper gateway
contract = Stock('AAPL', 'SMART', 'USD')
ib.qualifyContracts(contract)
trade = ib.placeOrder(contract, MarketOrder('BUY', 1))
ib.sleep(2)
print(trade.orderStatus.status)
Port 7497 is the paper gateway and 7496/4001 the live one — connect to paper first, exactly as in the paper-trading guide.
Global multi-asset access
A single IBKR connection can trade US and international stocks, options, futures, forex and more, which makes it the natural home for a multi-strategy portfolio bot spanning asset classes. The SMART routing in the contract picks the best venue automatically.
The gateway-uptime gotcha
TWS and IB Gateway force a daily restart and re-authentication, and by default the gateway logs out overnight — your bot silently loses its connection and stops trading. You must configure auto-restart (IBC is the common tool) and have the bot detect and reconnect on a dropped socket. This single quirk breaks more IBKR bots than any strategy flaw.
Pro-platform risks
IBKR’s power is also its danger: margin, futures and options leverage can blow an account fast. Real-time data requires paid subscriptions per exchange, and the API’s pacing limits will throttle a bot that polls too aggressively. Respect the rate limits, subscribe to the data you need, and size every position from a stop with the calculator.
Getting started
Install TWS, enable API access, connect ib_insync to the paper gateway on port 7497, and forward-test for weeks with auto-restart configured. Backtest the idea on the backtester before risking real capital.
Frequently asked questions
How does the Interactive Brokers API work for a bot?
Instead of a pure cloud REST API, an IBKR bot connects over a socket to a local Trader Workstation or IB Gateway process that holds the authenticated session and relays orders. The upside is one account covering global stocks, options, futures and forex; the downside is you must keep that gateway running and logged in.
What is ib_insync?
ib_insync is a popular community Python library that wraps IBKR’s low-level API into clean, modern, async-friendly code. It handles the socket connection, contract qualification and order placement, making an Interactive Brokers bot far easier to write than using the raw API directly.
Why does my Interactive Brokers bot keep disconnecting?
TWS and IB Gateway force a daily restart and re-authentication, and by default the gateway logs out overnight, silently dropping your bot’s connection. The fix is to configure auto-restart (commonly with IBC) and have the bot detect a dropped socket and reconnect automatically.
Is Interactive Brokers good for automated trading?
Yes for advanced users — it offers global multi-asset access, deep data and low costs in one account, which suits a multi-strategy portfolio bot. But it is more complex than Alpaca, requires paid data subscriptions per exchange, enforces API pacing limits, and its leverage can blow an account quickly if risk is not controlled.