areed1192 / interactive-broker-python-api

A python packaged used to interact with the Interactive Brokers REST API.
MIT License
374 stars 122 forks source link

Implementing streaming market data #26

Open orshemtov opened 3 years ago

orshemtov commented 3 years ago

Hi,

Thank you for all the work you did on this API.

I've been following the YouTube videos, it seems like the API does not provide support for streaming data.

Could you explain how one might integrate streaming data with the existing functionality?

It would be nice to have a stream of market data, and for those using technical indicators, a bit of historical data to warm up the indicators that require more than 1 pricing data point.

Thank you!

leowotzak commented 3 years ago

Fixes #26

Heres how I have it implemented. Its a little bit more complicated than simply going to the endpoint, you need to use a websocket (and SSL) to stream data, which also requires that you use a little bit of asyncio. Let me know if you have any questions. The print messages are where my logic would go

import websockets
import asyncio

async def stream_data():
        async with websockets.connect('wss://localhost:5000/v1/api/ws', ssl=context) as ibsock:
            async for message in ibsock:
                print(message)

asyncio.run(stream_data()