How to Build an IBKR Trading Bot in Python (Complete Guide 2026)
Interactive Brokers (IBKR) is one of the most powerful platforms for algorithmic trading. In this guide we walk you through building a fully automated IBKR trading bot using Python and the ib_insync library.
## What You Need
- Interactive Brokers account (paper or live)
- TWS (Trader Workstation) or IB Gateway installed
- Python 3.8 or higher
- ib_insync library
## Step 1 — Install ib_insync
pip install ib_insync
This is the most popular Python wrapper for the IBKR TWS API.
## Step 2 — Connect to TWS
from ib_insync import IB, Stock, MarketOrder
ib = IB()
ib.connect('127.0.0.1', 7497, clientId=1)
Make sure TWS is open and API connections are enabled.
## Step 3 — Define Your Instrument
contract = Stock('AAPL', 'SMART', 'USD')
ib.qualifyContracts(contract)
## Step 4 — Place an Order
order = MarketOrder('BUY', 10)
trade = ib.placeOrder(contract, order)
ib.sleep(1)
print(trade.orderStatus.status)
## Step 5 — Add Strategy Logic
A simple moving average crossover checks if SMA20 is above SMA50 for a bullish signal. Use reqHistoricalData to fetch bars and build your signal logic with pandas.
## Production IBKR Bot Features
A production-ready IBKR bot includes risk management with stop loss and take profit, position sizing based on account equity, real-time Telegram alerts, backtesting, and live/paper mode switching.
## Need a Custom IBKR Bot?
Team NAK has built 300+ custom IBKR bots for stocks, futures, options, and forex. Contact us at theteamnak.com for a free quote.