Fetching historical market data
Requesting historical market data using the IB API is an asynchronous process, emphasizing non-blocking, event-driven data retrieval. To kick off the process, we send a request for historical data by invoking the reqHistoricalData
method, specifying parameters such as the financial instrument’s identifier, the duration for which data is needed, the bar size, and the type of data required. Once the request is made, the IB API processes it and begins sending back the data. However, instead of waiting for all data to be received before continuing with other tasks, the API employs a callback mechanism, specifically the historicalData
method. This method is called asynchronously for each piece of data received from IB. Each invocation of historicalData
provides a snapshot of market data for a specific time interval, which we can then process or store. In this recipe, we’ll set up the code to request and receive historical market data.