StructureX

Home › Guides › Trading bots & code

Interactive Brokers API basics: TWS, IB Gateway and Python

Interactive Brokers' API doesn't talk to the internet directly. It talks to a program on your own computer — TWS or IB Gateway — which talks to IBKR. Once that clicks, the rest is ordinary code.

Beginner guide · 9-minute read · Updated 2026-10-21

How the pieces connect

Most broker APIs are a web address. Interactive Brokers (IBKR) is different for its classic API, called the TWS API:

  1. Your script connects to a program running on your machine.
  2. That program is either TWS (Trader Workstation, the full trading app) or IB Gateway (a smaller app with no charts, made for API use).
  3. TWS or IB Gateway connects to IBKR's servers.

So TWS or IB Gateway must be open and logged in for your script to work. If it closes, your script loses its connection — something your code must handle.

IBKR also offers web-based APIs. This page covers the TWS API, which is the one most Python examples use.

Paper first: the paper account

IBKR accounts come with a paper trading account (simulated money). You log into TWS or IB Gateway and choose paper mode.

Each mode listens on its own default port (a numbered "door" on your computer):

ProgramPaper portLive port
TWS74977496
IB Gateway40024001

These are defaults and can be changed in the settings. The port in your script must match the one in TWS or IB Gateway. Using the paper port is a simple guard: a script pointed at 7497 can't reach a live TWS on 7496.

The 2 settings that trip up beginners

In TWS or IB Gateway, look for the API settings (in TWS, under the global configuration, then API, then Settings). Then:

  1. Turn on "Enable ActiveX and Socket Clients". Without it, scripts can't connect at all.
  2. Look at "Read-Only API". It's on by default and blocks all orders from code. Leave it on while you're only reading data. Turn it off only on the paper account, when you're ready to test orders.

While you're there, set Trusted IPs to only your own computer (127.0.0.1), so nothing else on the network can connect.

Which Python library?

The example below uses ib_async (pip install ib_async pandas). IBKR's support team can't help with third-party libraries, so keep its docs handy.

Step 1: Connect and read prices

Example code for learning — test it on paper first; it is not a ready-made trading system.

from ib_async import IB, Stock, util
ib = IB()
ib.connect("127.0.0.1", 7497, clientId=1)          # 7497 = TWS paper (default)
contract = Stock("SPY", "SMART", "USD")
ib.qualifyContracts(contract)                      # ask IBKR to fill in the details
bars = ib.reqHistoricalData(
    contract,
    endDateTime="",                                # "" = up to now
    durationStr="60 D",
    barSizeSetting="1 day",
    whatToShow="TRADES",
    useRTH=True,                                   # regular hours only
)
df = util.df(bars)
print(df[["date", "open", "high", "low", "close"]].tail(3))

In plain words:

Market data may need a subscription on your account. If you see an error about market data permissions, check the market data settings in your IBKR account.

Step 2: Errors, disconnects and a bracket order

def on_disconnect():
    print("Disconnected. No new orders until we reconnect and re-check positions.")
ib.disconnectedEvent += on_disconnect
def on_error(reqId, errorCode, errorString, contract):
    print("IB error", errorCode, errorString)      # log it, don't ignore it
ib.errorEvent += on_error
bracket = ib.bracketOrder(
    "BUY", 10,
    limitPrice=500.00,                             # entry (a limit order)
    takeProfitPrice=510.00,                        # target
    stopLossPrice=495.00,                          # stop
)
for order in bracket:                              # parent, target, stop
    trade = ib.placeOrder(contract, order)
    print(trade.order.orderType, trade.orderStatus.status)
ib.sleep(2)                                        # let messages arrive
ib.disconnect()

In plain words:

The numbers here are placeholders. In a real rule, the stop comes from the chart (for example, just past the last place price turned around), and the share count comes from the stop:

A math example, not a promise. Gaps can fill a stop beyond its price. Our position size calculator does the same math.

The risk controls to add next

The example sends orders. A bot needs more around it:

More: trading bot risk controls.

Test it properly

  1. Test the rule on past data first, avoiding look-ahead bias (using data before it existed) and overfitting (tuning until the past looks perfect). See how to backtest in Python.
  2. Run it on the paper account for weeks. See paper trading a bot.
  3. Compare its trades with what your written rule says should have happened.

Paper fills are simulated and can look better than real fills, so expect real results to differ. Losses are possible with any bot.

The live port is one number away. 7497 vs 7496. Keep paper and live setups separate — different settings files, different login — and treat going live as a written decision, not a quick edit.

Common mistakes

Questions

Does Interactive Brokers have an API?

Yes. The TWS API works through TWS or IB Gateway running on your computer and supports Python, Java, C++ and C#, among others. IBKR also offers web-based APIs.

What is the difference between TWS and IB Gateway?

TWS is the full trading app with charts. IB Gateway is a lighter app with no trading screens, made to stay connected for API use. Both let scripts connect.

What port does the Interactive Brokers API use?

By default, TWS uses 7497 for paper and 7496 for live; IB Gateway uses 4002 for paper and 4001 for live. You can change these in the API settings.

Can I paper trade with the IBKR API?

Yes. Log into TWS or IB Gateway in paper mode and connect your script to the paper port. Test orders there for weeks before considering live trading.

Is ib_insync still maintained?

No. Its successor is ib_async, a community-maintained library that works the same way. Switching mostly means changing the import name.

Want the next step? We'll walk you through it.

Four free lessons by email, one every few days — each with a 10-minute exercise on a real chart.

Send Me the Free Lessons 4 short emails · No spam · Unsubscribe anytime

Or see the whole method, step by step:

See What’s Inside the Course Try everything free first. Decide when you're ready. · Open the free chart tool (works in the TradingView phone app)

One chart idea a day, in plain English. Follow along:

Follow on XFollow on TradingView