Aggregating Interactive Brokers Tick Events into Five-Minute Bars
Summary
The document explains two ways to turn Interactive Brokers market data events into aggregated bars in MATLAB. For a live feed, the callback must retain prior observations and combine incoming prices or ticks over the desired interval; persistent or global state is suggested as one way to keep that data. If real-time updates are unnecessary, historical bar data can be requested, though the returned observations still arrive as separate events that the callback must collect.
A second response mentions a Java library with MATLAB examples and an integration product that handles aggregation. It also lists limits on the frequency of historical data requests, including restrictions on repeated requests and total requests over a time window. The document does not give aggregation code, define how to handle incomplete intervals or missing ticks, or discuss bar conventions such as price selection. The request limits are reported as part of the answer and may be specific to the API version or conditions at the time, so current platform guidance should be checked before use.
Key ideas
- A real-time bar builder must retain incoming events and aggregate them over each time interval.
- Historical bars can be requested when live updates are not needed, but the callback still has to collect event results.
- A Java library with MATLAB examples and an integration product are mentioned as alternatives.
- Interactive Brokers historical data requests are subject to frequency limits described in the document.
- The document does not specify bar construction rules or provide implementation code.
Tags
Full text
# Aggregate interactive brokers data in matlab
# Aggregate interactive brokers data in matlab
I am using matlab and interactive brokers API. I am getting real time data using
tickerID = ib.realtime({ct},'233',@(varargin)ibEventRealTimeData(varargin{:}));
where ib is the interface to interactive brokers TWS Activex object and ct is the contract.
I am getting all the events (ticksize, tickprice and so on) but I can't aggregate data/prices (for example, in 5 minute bars). What am I missing?
Thanks.
## Answer by dkhokhlov (score 3)
https://quant.stackexchange.com/a/18884
Here is a pure Java library with Matlab examples for getting daily and minute aggregated bars. It is based on IB Java API. I wrapped it to have a simple interface:
http://www.spreadvectors.com/wisentgenus#code
IB has limitations when requesting historical data:
- Making identical historical data requests within 15 seconds.
- Making six or more historical data requests for the same Contract, Exchange and Tick Type within two seconds.
- Do not make more than 60 historical data requests in any ten-minute period.
## Answer by Yair Altman (score 1)
https://quant.stackexchange.com/a/18580
In order to aggregate the 5-min bars, you need to add code within your `ibEventRealTimeData` function that remembers the previous values (possibly using a `global` or `persistent` variable) and appends the new data to it.
If you do not need realtime info, you could make a single request for 5-min historical data. But here too, IB sends the results in separate events that you would need to aggregate within your callback.
As an alternative you could use the IB-Matlab product, which connects IB and Matlab, and aggregates the information for you, in both the historical/intra-day request and the realtime bars request.Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.