DEV Community

Emily
Emily

Posted on

How I Subscribed to Multiple US Stocks Ticks with a Single WebSocket Connection

My daily routine as a retail algo trader used to be haunted by latency. I’d watch my logs fill up with stale prices while the market moved on. The culprit? HTTP polling. I was firing hundreds of requests per minute just to keep up with 10-15 symbols, and still the data had gaps. After collecting stats, I realized the average delay was over 700ms — a dealbreaker for tick-based strategies. So I ditched polling and embraced WebSocket streaming with the AllTick market data API. This post shows you exactly how I did it.

What’s Wrong with Polling?

When you poll, you’re taking snapshots at fixed intervals. Between those snapshots, anything can happen. High-frequency updates demand a continuous stream, not discrete photos. Polling also scales poorly: more stocks mean more loops, more sockets, more waste. WebSocket solves this with a single long-lived connection that pushes updates as they occur, preserving timeline integrity.

Code Walkthrough: One Connection to Rule Them All

Here’s my Python client. It subscribes to multiple US stocks at once and prints each tick. Simple, right?

import websocket
import json

def on_message(ws, message):
    # Parse tick update
    data = json.loads(message)
    print(data["symbol"], data["price"], data["time"])

def on_open(ws):
    # List of symbols to track
    symbols = ["AAPL", "TSLA", "AMZN"]

    req = {
        "action": "subscribe",
        "symbols": symbols
    }

    ws.send(json.dumps(req))

ws = websocket.WebSocketApp(
    "wss://api.alltick.co/ws/stock",
    on_open=on_open,
    on_message=on_message
)

ws.run_forever()
Enter fullscreen mode Exit fullscreen mode

Once running, you’ll see a continuous log of price updates — no more gaps or bursts of stale data.

Keep It Running in Production

For serious use, I added a few enhancements:

  • Auto-reconnect: Network drops? No problem. Reconnect in on_close with a delay.
  • Queue buffer: When ticks flood in, push them to a queue and let a worker thread handle processing.
  • Dedup logic: Avoid processing the same tick twice by caching recent (symbol, timestamp) pairs.
  • Multiple connections: For many symbols, split them among several connections to balance the load.

Where the Ticks Go

I pipe incoming ticks into two pipelines: one stores them in a TSDB for backtesting, the other drives my live trading engine. A small Redis buffer prevents database write spikes from slowing down my strategy execution.

One important tip: always normalize timestamps to a common format (I use UTC microseconds) right after receipt, so all downstream services speak the same language.

From Polling to Pushing

Switching to WebSocket turned my trading system from a laggy, unreliable mess into a smooth, real-time machine. Slippage is down, confidence is up, and I spend less time debugging data feeds. If you’re building any tick-sensitive application, take the leap to WebSocket — you won’t look back.

Top comments (0)