Websocket Streaming: Live Market Data Through the API

You may observe how streaming truly works by observing one trading day. A WebSocket feed is commonly supplied via a free trading API trading. The server is pushing prices to you on the open connection. This is how that connection survives a market day.

Before the market opens

Connect to the secure URL (wss://) provided by the service. At this point, authenticate. This can be done with an API key, access token or JWT. Different services may require this in different places: in the URL, in the header, or in the initial message. Check which of these applies to your API. Once authenticated, data will begin to be sent.

At the opening bell

Subscribe to the data by submitting a simple JSON message. Include the action, type of feed, and instruments. There are many levels of feeds, from just the last traded price to a richer quote, and even full data. To reduce bandwidth usage, start with the lowest level of data required.

Mid-morning: the quiet stretch

Ensure that the feed includes the market depth and open interest if required by future and options. Data can be either sent as simple JSON or highly optimised binary. If binary is used, be very precise with decoding.

Compare this with polling, where every price check costs one request:

Polling

Streaming

Who starts the update

You

The server

Requests used

One per check

One connection

Delay

Set by your interval

Usually lower

Limit risk

Higher

Lower

Without warning: the link drops

Handle pings and pongs. If no messages are being received, the server may disconnect. If the connection drops, do not repeatedly attempt to reconnect. Instead, wait a short period of time and attempt to reconnect. After each disconnection, wait a slightly longer period of time.

On reconnecting, re-subscribe to all of the data. The server will have forgotten your previous subscriptions. To make this quick and easy, ensure that you have a list of subscriptions in your code so that they can be quickly recreated.

Housekeeping through the day

  • Subscribe only to what you watch.

  • Unsubscribe from the rest.

  • Respect any cap on instruments per connection.

  • Stamp each tick with its arrival time.

If you are not receiving tick data for longer than expected, consider it to be stale. In a bot, stop placing new orders until fresh data is received.

After the close

Ensure that you disconnect properly. Review your logs. Check when reconnections occurred and if any data was missed. Fix any issues and ensure everything is working properly before tomorrow’s open.

When polling is still fine

For data that doesn’t update frequently (such as portfolio and order history), polling is a good option. For price data that updates every second, streaming is a good option. Doing this means that you don’t make too many requests.

The takeaway

In a day of streaming, there are 5 steps to complete: connect, authenticate, subscribe, handle disconnections, and clean up. If these are all done properly, real-time data can be cheap and stable. To begin with, stream a few instruments and very small order sizes. Only increase this if the stream is behaving well.