Historical Market Data

View as MarkdownOpen in Claude

get_historical_data(

contract: [Contract Object](/tws-api/doc/contracts-financial-instruments/the-contract-object). Contract to retrieve market data for.

end_date_time: String. The request’s end date and time. This should be formatted as “YYYYMMDD HH:mm:ss TMZ”. You may also pass an empty string to indicate the current moment
Please be aware that endDateTime must be left as an empty string when requesting continuous futures contracts or certain whatToShow values like ADJUSTED_LAST.

duration_str: String. The total timespan the bars should cover. See [Duration](/tws-api/doc/market-data-historical/historical-bars/duration) for details.

bar_size_setting: String. The time span covered by each bar. See [Bar Sizes](/tws-api/doc/market-data-historical/historical-bars/historical-bar-sizes) for details.

what_to_show: String. Determines what kind of data should be returned. See [whatToShow](/tws-api/doc/market-data-historical/historical-bar-what-to-show/introduction) for more details.

use_rth: Boolean. Define if data should only be returned from the regular trading session or if extended trading hours should be included.

Default: True is passed by default, only returning data from the regular trading sesions.

format_date: Integer. Determine the return structure of the date. Supports (1) to return a datetime formatting string or 2 to return a epoch Unix timestamp.

Default: Set to 1, returning a datetime string.

timeout: Integer. A default value of 30 is supplied.

)

contract = Contract()
contract.symbol = "AAPL"
contract.secType = "STK"
contract.exchange = "SMART"
contract.primaryExchange = "NASDAQ"
contract.currency = "USD"
app.get_historical_data(contract=contract, end_date_time="", duration_str="1 W", bar_size_setting="1 day", what_to_show="TRADES", use_rth=True)

Response Object

Requesting historical bars will return return a list containing all [Bar] objects for the duration. Please be aware that directly printing this information may result in the memory address being displayed.

[2524872613328: Date: 20251013, Open: 249.31, High: 249.69, Low: 245.56, Close: 247.66, Volume: 187465.43, WAP: 247.952, BarCount: 105768, 2524872614864: Date: 20251014, Open: 246.6, High: 248.85, Low: 244.7, Close: 247.77, Volume: 176034.99, WAP: 247.21, BarCount: 100507, 2524872615120: Date: 20251015, Open: 249.49, High: 251.82, Low: 247.47, Close: 249.34, Volume: 172136.46, WAP: 249.754, BarCount: 96331, 2524872615248: Date: 20251016, Open: 248.28, High: 249.04, Low: 245.13, Close: 247.45, Volume: 235179.94, WAP: 247.351, BarCount: 132811, 2524872615376: Date: 20251017, Open: 248.08, High: 253.38, Low: 247.27, Close: 252.29, Volume: 260673.48, WAP: 250.408, BarCount: 125863]