How do I download prices from Interactive Brokers with ib_async?

Request a stock’s daily bars for regular trading hours from Interactive Brokers, and turn the reply into a pandas table that keeps its columns when no bars arrive.

After this lesson, you can request a stock’s daily prices from Interactive Brokers, IBKR, turn the reply into a price table, and handle a request that returns no bars.

How do I request daily bars?

ib.reqHistoricalData(stock, endDateTime="", durationStr="3 D", barSizeSetting="1 day", whatToShow="TRADES", useRTH=True) asks IBKR for the last three days of daily bars of trade prices in regular trading hours. A bar summarises the trades of one interval, its open and close the first and last trade prices, its high and low the highest and lowest.

The live request needs Trader Workstation, TWS, signed in to a paper account with socket clients and Read-Only API enabled, and the packages ib_async and pandas, as the post on connecting to IBKR shows. Stock("AAPL", "SMART", "USD") describes Apple’s shares with IBKR choosing the exchange, qualifyContracts fills in IBKR’s contract id, conId, and port 7497 is the paper account’s default. "TRADES" prices are adjusted for splits but not for dividends, and today’s bar can still change. util.df(bars) turns the bars into a table, and util.startLoop() lets the library wait inside a notebook, so a plain script leaves it out.

The following box, which does not run on this page, requests Apple’s bars and always disconnects.

from ib_async import IB, Stock, util

util.startLoop()
ib = IB()
stock = Stock("AAPL", "SMART", "USD")
try:
    ib.connect(host="127.0.0.1", port=7497, clientId=7, readonly=True)
    ib.qualifyContracts(stock)
    if not stock.conId:
        raise ValueError("Stock ID missing")
    bars = ib.reqHistoricalData(stock, endDateTime="", durationStr="3 D", barSizeSetting="1 day", whatToShow="TRADES", useRTH=True)
    print(util.df(bars) if bars else "No bars returned")
finally:
    ib.disconnect()

How do I keep the same columns when no bars arrive?

pd.DataFrame(reply, columns=Bar._fields) builds the table from the bars with the bar’s field names as columns, so a reply without bars still gives a table with the same six price columns and no rows, while util.df([]) gives None. AAA’s made-up bar of 3 March closes at 104 dollars with 1,800 shares traded.

A bar also carries average, the volume-weighted average price, and barCount, the number of trades. namedtuple makes a record type with named fields, here standing in for IBKR’s bars, and price_table keeps the six columns fields. A day without trading has no row, and filling it with zeros would invent a price.

The following box builds three made-up bars and turns them, and an empty reply, into tables.

from collections import namedtuple
from datetime import date

import pandas as pd

fields = ["date", "open", "high", "low", "close", "volume"]
Bar = namedtuple("Bar", fields + ["average", "barCount"])
bars = [
    Bar(date(2026, 3, 2), 100, 103, 99, 101, 1200, 101.0, 12),
    Bar(date(2026, 3, 3), 101, 105, 100, 104, 1800, 103.0, 15),
    Bar(date(2026, 3, 4), 104, 106, 102, 103, 1500, 104.0, 10),
]

def price_table(reply):
    return pd.DataFrame(reply, columns=Bar._fields)[fields]

daily = price_table(bars)
print(daily)
print(price_table([]).shape)
         date  open  high  low  close  volume
0  2026-03-02   100   103   99    101    1200
1  2026-03-03   101   105  100    104    1800
2  2026-03-04   104   106  102    103    1500
(0, 6)

Your turn

Print the date and close of the days with at least 1,500 shares traded. You should see 3 and 4 March.

>= keeps the day exactly at the cutoff.

print(daily.loc[daily["volume"] >= 1500, ["date", "close"]])
         date  close
1  2026-03-03    104
2  2026-03-04    103