The problem with polling market data
If you’ve ever tried to fetch live US stock prices using REST APIs, you know the struggle: rate limits kick in when you need data the most, and the time between requests creates blind spots. For tick-by-tick analysis — like detecting volume spikes or order-flow imbalances — polling just doesn’t cut it.
Real-time push with WebSocket
WebSocket lets the server push every trade directly to you, eliminating polling delays and rate limits. I recently built a real-time tick feed for hot US stocks using Python and a WebSocket API. The data provider I used (AllTick) delivers each trade as a JSON object with symbol, price, and volume — super easy to parse.
Let’s see the code
Here’s the simplest version to get you started:
import websocket
import json
def on_message(ws, message):
data = json.loads(message)
# Print price and volume for every trade
print(f"{data['symbol']} price: {data['price']} volume: {data['volume']}")
def on_open(ws):
# Subscribe to a few popular tickers
symbols = ["AAPL", "TSLA", "AMZN"]
for symbol in symbols:
ws.send(json.dumps({
"action": "subscribe",
"symbol": symbol
}))
ws = websocket.WebSocketApp("wss://apis.alltick.co/ws/stock-tick",
on_message=on_message,
on_open=on_open)
ws.run_forever()
Run it, and you’ll see a live stream of every trade for Apple, Tesla, and Amazon. It’s that fast.
What to do with the data
- Store it in Redis for ultra-fast access and real-time dashboards.
- Archive to SQLite or PostgreSQL for backtesting.
- Build a Streamlit dashboard to visualize volume leaders in real time.
Make it production-ready
-
Auto-reconnect: Handle
on_closeand reconnect with backoff. - Deduplication: Use the trade ID to avoid double-counting.
-
Async: Use
asyncioorwebsocketslibrary for non-blocking I/O. - Heartbeat: Keep the connection alive with ping/pong.
Final thoughts
Streaming real-time stock ticks is easier than most developers think. With a few lines of Python and a reliable WebSocket endpoint, you can turn your local machine into a powerful market monitoring station. I’ve been using this setup daily to feed our quant models, and it hasn’t missed a beat. Give it a try and see what insights you can uncover!

Top comments (0)