Historical Bars (OHLC)
Introduction
Historical Bar data returns a candlestick value based on the requested duration and bar size. This will always return an open, high, low, and close values. Based on which whatToShow value is used, you may also receive volume data. See the whatToShow section for more details.
Request Historical Bars
EClient.reqHistoricalData(reqId, contract, endDateTime, durationStr, barSizeSetting, whatToShow, useRTH, formatDate, keepUpToDate, chartOptions)
A unique identifier which will serve to identify the incoming data.
The IBApi.Contract object you are working with.
The request’s end date and time. This should be formatted as “YYYYMMDD HH:mm:ss TMZ” or an empty string indicates current present moment. Please be aware that endDateTime must be left as an empty string when requesting continuous futures contracts.
The amount of time (or Valid Duration String units) to go back from the request’s given end date and time. See Duration for details.
The data’s granularity or Valid Bar Sizes. See Bar Sizes for details.
The type of data to retrieve. See Historical Data Types for details.
Whether (1) or not (0) to retrieve data generated only within Regular Trading Hours (RTH)
The format in which the incoming bars’ date should be presented. Note that for day bars, only yyyyMMdd format is available. See Format Date for details.
Whether a subscription is made to return updates of unfinished real time bars as they are available (True), or all data is returned on a one-time basis (False). If True, endDateTime cannot be specified. Supported whatToShow values: Trades, Midpoint, Bid, Ask. See Keep Up To Date for details.
This is a field used exclusively for internal use.
Python
Java
C++
C#
VB.NET
Receive Historical Bars
EWrapper.historicalData(reqId, bar)
The historical data will be delivered via the EWrapper.historicalData() method in a series of candlesticks. The time zone of returned bars is based on the Historical Date Format.
Request identifier used to track data.
The OHLC historical data Bar. The time zone of the bar is the time zone chosen on the TWS login screen. Smallest bar size is 1 second.
Python
Java
C++
C#
VB.NET
Updating Historical Bars
EWrapper.historicalDataUpdate(reqId, bar)
Receives bars in real time if keepUpToDate is set as True in EClient.reqHistoricalData(). Similar to EClient.reqRealTimeBars() function, except returned data is a composite of historical data and real time data that is equivalent to TWS chart functionality to keep charts up to date. Returned bars are successfully updated using real time data.
Request identifier used to track data.
The OHLC historical data Bar. The time zone of the bar is the time zone chosen on the TWS login screen.
Python
Java
C++
C#
VB.NET
Understanding Historical Updates
When using keepUpToDate=True, you will see several bars returned with the same timestamp. This is because data is updated approximately every 4-6 seconds. These updates compound until the end of the specified bar size.
In our example to the below, 15 second bars are requested, and we can see the 30 second bar built out incrementally until 20231204 13:30:30 is completed. At which point, we move on to the 45th second bars. This same logic extends into minute, hourly, or daily bars.
keepUpToDate is only available for whatToShow: Trades, Midpoint, Bid, Ask
End of Historical Bars
EWrapper.historicalDataEnd(reqId, start, end)
Marks the ending of the historical bars reception.
Request identifier used to track data.
Returns the starting time of the first historical data bar.
Returns the end time of the last historical data bar.
Python
Java
C++
C#
VB.NET
Historical Bar Sizes
Bar sizes dictate the data returned by historical bar requests. The bar size will dictate the scale over which the OHLC/V is returned to the API.
Historical Duration
The Interactive Brokers Historical Market Data maintains a duration parameter which specifies the overall length of time that data can be collected. The duration specified will derive the bars of data that can then be collected.
Valid Duration String Units:
Max Duration Per Bar Size.
The table below displays the maximum duration values allowed for a given bar.
As an example, the maximum allowed duration in Seconds for 5 second bars is 86400 S. This means that if I want to retrieve more than 1 day’s worth of 5 second bars, I will then need to request data in increments of D (days).
WhatToShow Values
Introduction
The historical bar types listed below can be used as the whatToShow value for historical bars. These values are used to request different data such as Trades, Midpoint, Bid_Ask data and more. While the bar fields continue to retain values, such as bar.open, the contained value may not be the market open within that bar’s timespan.
Some bar types support more products than others. Please note the Supported Products section for each bar type below.
AGGTRADES
Bar Values:
Supported Products: Cryptocurrency
ADJUSTED_LAST
Bar Values:
Supported Products: ETFs, Options, Stocks
ASK
Bar Values:
Supported Products: Bonds, CFDs, Commodities, Cryptocurrencies, ETFs, FOPs, Forex, Funds, Futures, Metals, Options, SSFs, Stocks, Structured Products, Warrants
BID
Bar Values:
Supported Products: Bonds, CFDs, Commodities, Cryptocurrencies, ETFs, FOPs, Forex, Funds, Futures, Metals, Options, SSFs, Stocks, Structured Products, Warrants
BID_ASK
Bar Values:
Supported Products: Bonds, CFDs, Commodities, Cryptocurrencies, ETFs, FOPs, Forex, Funds, Futures, Metals, Options, SSFs, Stocks, Structured Products, Warrants
FEE_RATE
Bar Values:
Supported Products: Stocks, ETFs
HISTORICAL_VOLATILITY
Bar Values:
Supported Products: ETFs, Indices, Stocks
MIDPOINT
Bar Values:
Supported Products: Bonds, CFDs, Commodities, Cryptocurrencies, ETFs, FOPs, Forex, Funds, Futures, Metals, Options, SSFs, Stocks, Structured Products, Warrants
OPTION_IMPLIED_VOLATILITY
Bar Values:
Supported Products: ETFs, Indices, Stocks
SCHEDULE
Bar Values:
Supported Products: Bonds, CFDs, Commodities, Cryptocurrencies, ETFs, Forex, Funds, Futures, Indices, Metals, SSFs, Stocks, Structured Products, Warrants
TRADES
Bar Values:
Supported Products: Bonds, ETFs, FOPs, Futures, Indices, Metals, Options, SSFs, Stocks, Structured Products, Warrants
YIELD_ASK
Bar Values:
Supported Products: Indices
YIELD_BID
Bar Values:
Supported Products: Indices
YIELD_BID_ASK
Bar Values:
Supported Products: Indices
YIELD_LAST
Bar Values:
Supported Products: Indices

