Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Historical Data Websocket for Real-Time OHLC Data #38

Open
jackroc97 opened this issue Dec 23, 2024 · 4 comments
Open

Using Historical Data Websocket for Real-Time OHLC Data #38

jackroc97 opened this issue Dec 23, 2024 · 4 comments

Comments

@jackroc97
Copy link

I am trying to stream 1 min OHLC data in real-time. I've tried using the market history example as a starting point for this. I am a little confused on how to interpret the data. The first response is OHLC for the 1 min bar for the previous minute (for example, if I run the script at 17:06, it would be the bar for 17:05). A sample response is shown below:

QueueAccessor(key=IbkrWsKey.MARKET_HISTORY, size=1) {'serverId': '588547', 'symbol': 'AAPL', 'text': 'APPLE INC', 'priceFactor': 100, 'startTime': '20241223-17:05:00', 'high': '25495/56.04/1', 'low': '25482/245.49/0', 'timePeriod': '60s', 'barLength': 60, 'mdAvailability': 'S', 'mktDataDelay': 0, 'outsideRth': False, 'volumeFactor': 100, 'priceDisplayRule': 1, 'priceDisplayValue': '2', 'negativeCapable': False, 'messageVersion': 2, 'data': [{'o': 254.91, 'c': 254.91, 'h': 254.94, 'l': 254.82}, {'o': 254.9, 'c': 254.9, 'h': 254.95, 'l': 254.9}], 'points': 1, 'travelTime': 9, 'topic': 'smh+265598', 'payload': '{"period": "1min", "bar": "1min", "outsideRTH": true, "source": "trades", "format": "%o/%c/%h/%l"}'}

The subsequent responses however only contain either just "open" or "open" and "close" data, with no time associated. For example:

QueueAccessor(key=IbkrWsKey.MARKET_HISTORY, size=2) {'serverId': '588547', 'tickNum': 36, 'negativeCapable': False, 'data': [{'o': 254.8}], 'points': 0, 'travelTime': 44668, 'topic': 'smh+265598', 'payload': '{"period": "1min", "bar": "1min", "outsideRTH": true, "source": "trades", "format": "%o/%c/%h/%l"}'}

Is it possible to use this websocket endpoint to get a real-time stream of OHLC bars, or would that be a misuse of it? It appears to return data in realtime, but seems to be incomplete... or perhaps I am misinterpreting the response.

@Voyz
Copy link
Owner

Voyz commented Dec 23, 2024

hey @jackroc97 That's right the Market History is not quite what you'd expect it to be. In fact, the documentation states:

https://www.interactivebrokers.com/campus/ibkr-api-page/cpapi-v1/#ws-sub-historical-data

NOTE: Historical data will only respond once, though customers will still need to unsubscribe from the endpoint.


To get live data, you want to use the MARKET_DATA channel as shown in ws_02 example:

https://github.com/Voyz/ibind/blob/master/examples/ws_02_intermediate.py

You can find the list of fields you may want to use here:

https://github.com/Voyz/ibind/blob/master/ibind/client/ibkr_definitions.py

Or here:

https://www.interactivebrokers.com/campus/ibkr-api-page/cpapi-v1/#market-data-fields

Let me know if it makes sense 👍

@jackroc97
Copy link
Author

Thanks for the response @Voyz! So it seems that there is no "native" way to stream live OHLC values through the Web API, similar to how you can using the TWS API? I had previously been using the TWS API, but I have been frustrated with it recently and decided to try switching over to the Web API.

My goal is to construct a live feed of OHLCV candles on the minute timescale. My initial approach was to use the MARKET_DATA channel as you mentioned, but I had some confusion about that as well. According to IBKR's own documentation (and the documentation you posted), the Open, High, Low, Close, and Volume fields in MARKET_DATA are all on the daily time-frame. It seems like it would be possible to construct an OHLC series using a MARKET_DATA subscription that reportslast_price andlast_size. However, when I create a subscription for that, some of the responses have either last_price or last_size as empty. For example:

_updated, last_price, last_size
1734971603246,254.68,200
1734971604747,254.65,
1734971606247,,300
1734971651976,,
1734971652476,254.70,100
1734971653977,254.69,

I'm not really sure what to do with the data that is missing one of the two fields. How can there be a price with no size, or a size with no price?

@Voyz
Copy link
Owner

Voyz commented Dec 23, 2024

Understood, thanks for pointing out that MARKET_DATA's ohlc are daily, not live.

Did you try talking to IBKR support and see what they say on the topic?

I'm not really sure what to do with the data that is missing one of the two fields. How can there be a price with no size, or a size with no price?

I've seen this before. IBKR support told me that these are incomplete data points and that I can collect or ignore these until a full datapoint comes out.


As for your application - can I suggest calling the market history REST endpoint on a minutely interval? Would that work for you?

@jackroc97
Copy link
Author

Thanks again for the response.

Did you try talking to IBKR support and see what they say on the topic?

Not yet, but I may reach out to them and see what their recommendations are.

As for your application - can I suggest calling the market history REST endpoint on a minutely interval? Would that work for you?

I think that would probably be an acceptable solution for me, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants