This post shows how to retrieve 1 minute candle data for a stock ticker using the historical data available via the Interactive Brokers (IBKR) API for Python.
The code prints out the Open, Close, High, and Low prices as well as the timestamp and volume for each 1 minute candle.
We include data from Pre-Market and After Hours (i.e. outside Regular Trading Hours).
The data includes the full date for each candle; we parse out the time only, omitting the common date for the day.
The value bar passed into the historicalData call stores all of the candle attributes.
The date and time range in the API is specified using durationStr which is set to “1 D” (one day), and endDateTime set to 8PM on the same day (the end of the After Hours session).
Note that the volumes returned in “lots”, one lot being 100 shares traded.
We read the ticker symbol and date from the user on standard input.
The format for the date required is “YYYYMMDD”, for example “20260616”.
A class extending the IBKR client classes is used as in previous examples.
Complete script:
import threading
import time
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
# Class extending IBKR client classes.
class IBapi(EWrapper, EClient):
def __init__(self):
EClient.__init__(self, self)
def historicalData(self, reqId, bar):
# Parse out time only, remove date.
candleTime = bar.date.split(" ")[1]
print(
candleTime,
f"O: {bar.open}",
f"H: {bar.high}",
f"L: {bar.low}",
f"C: {bar.close}",
f"Vol: {bar.volume}"
)
#
# Main script.
#
ticker = input("Ticker symbol: ")
date = input("Date (YYYYMMDD): ")
TWS_HOST = "127.0.0.1"
TWS_PORT = 7497 # Paper account assumed.
CLIENT_ID = 12345 # Arbitrary.
ibApi = IBapi()
ibApi.connect(TWS_HOST, TWS_PORT, CLIENT_ID)
threading.Thread(target=ibApi.run).start()
time.sleep(1) # Allow time for subscription.
contract = Contract()
contract.symbol = ticker
contract.secType = "STK"
contract.exchange = "SMART"
contract.currency = "USD"
ibApi.reqHistoricalData(
reqId=12345, # Arbitrary.
contract=contract,
endDateTime=date + " 20:00:00 US/Eastern",
durationStr="1 D", # 1 Day.
barSizeSetting="1 min",
whatToShow="TRADES",
useRTH=0, # Include outside Regular Trading Hours.
formatDate=1, # Human readable, not Epoch.
keepUpToDate=False,
chartOptions=[]
)
time.sleep(5) # Allow time to receive data.
ibApi.disconnect()
Example run (truncated output):
Ticker symbol: SUGP Date (YYYYMMDD): 20260616 04:00:00 O: 0.8537 H: 0.8537 L: 0.8537 C: 0.8537 Vol: 1 04:01:00 O: 0.8537 H: 0.8537 L: 0.8537 C: 0.8537 Vol: 0 04:02:00 O: 0.8537 H: 0.8537 L: 0.8537 C: 0.8537 Vol: 0 04:03:00 O: 0.8537 H: 0.8537 L: 0.8537 C: 0.8537 Vol: 0 04:04:00 O: 0.8537 H: 0.8537 L: 0.8537 C: 0.8537 Vol: 0 04:05:00 O: 0.8537 H: 0.8537 L: 0.8537 C: 0.8537 Vol: 0 04:06:00 O: 0.8537 H: 0.8537 L: 0.8537 C: 0.8537 Vol: 0 04:07:00 O: 0.8537 H: 0.8537 L: 0.8537 C: 0.8537 Vol: 0 04:08:00 O: 0.8537 H: 0.8537 L: 0.8537 C: 0.8537 Vol: 0 04:09:00 O: 0.8537 H: 0.8537 L: 0.8537 C: 0.8537 Vol: 0 04:10:00 O: 0.8537 H: 0.8537 L: 0.8537 C: 0.8537 Vol: 0 ... 08:00:00 O: 2.2898 H: 2.42 L: 2.22 C: 2.41 Vol: 10862 08:01:00 O: 2.41 H: 2.57 L: 2.38 C: 2.41 Vol: 16444 08:02:00 O: 2.41 H: 2.46 L: 2.33 C: 2.37 Vol: 7858 08:03:00 O: 2.37 H: 2.55 L: 2.35 C: 2.48 Vol: 14620 08:04:00 O: 2.47 H: 2.56 L: 2.44 C: 2.56 Vol: 12354 08:05:00 O: 2.56 H: 2.91 L: 2.56 C: 2.87 Vol: 35939 08:06:00 O: 2.87 H: 3.1 L: 2.84 C: 3.07 Vol: 36626 08:07:00 O: 3.06 H: 3.12 L: 2.87 C: 2.9399 Vol: 25553 08:08:00 O: 2.9387 H: 2.9599 L: 2.87 C: 2.9099 Vol: 12700 08:09:00 O: 2.9098 H: 2.99 L: 2.71 C: 2.72 Vol: 19726 08:10:00 O: 2.72 H: 2.82 L: 2.72 C: 2.79 Vol: 9744 ... 19:50:00 O: 1.05 H: 1.05 L: 1.05 C: 1.05 Vol: 25 19:51:00 O: 1.05 H: 1.06 L: 1.05 C: 1.0503 Vol: 110 19:52:00 O: 1.05 H: 1.06 L: 1.05 C: 1.06 Vol: 77 19:53:00 O: 1.05 H: 1.0503 L: 1.05 C: 1.0503 Vol: 26 19:54:00 O: 1.05 H: 1.0597 L: 1.05 C: 1.0597 Vol: 82 19:55:00 O: 1.05 H: 1.0599 L: 1.05 C: 1.05 Vol: 45 19:56:00 O: 1.06 H: 1.06 L: 1.05 C: 1.05 Vol: 14 19:57:00 O: 1.05 H: 1.0501 L: 1.05 C: 1.05 Vol: 50 19:58:00 O: 1.06 H: 1.06 L: 1.04 C: 1.04 Vol: 99 19:59:00 O: 1.04 H: 1.07 L: 1.04 C: 1.06 Vol: 110
