## Intro
If you build quantitative monitoring scripts or real-time trading data pipelines, you’ve definitely faced this common ask: ingest real-time tick data for individual A-share stocks and ETFs at the same time. Traders usually combine single-stock data (tracking individual corporate capital flow) with ETF data (gauging sector/broad market sentiment) to get a full view of intraday momentum.
A lot of new devs fall into two inefficient anti-patterns: spinning up two separate WebSocket connections, or polling APIs on a fixed interval. Both approaches waste cloud resources, bloat your codebase, and introduce data gaps during volatile price swings. In this post, I’ll walk through a clean, scalable single-stream architecture, share runnable Python code, and cover production tweaks for long-running cloud deployments.
Common Pitfalls With Separate Data Pipelines
1. Dual WebSocket Connections Waste Bandwidth & Maintenance
Running independent WebSocket clients for stocks and ETFs doubles your network overhead and connection pool usage. Every piece of error handling, reconnection logic has to be duplicated. Later, if you want to add indices or convertible bonds, you’ll copy-paste huge blocks of code and create a messy, hard-to-debug repo.
2. Periodic Polling Creates Missing Tick Data
HTTP polling can’t capture rapid intraday price spikes or flash orders. When you miss ticks, your backtest datasets become distorted, and any live trading signals generated from that data lose reliability. Polling is never a fit for 24/7 real-time quant monitoring.
3. Hardcoded Tickers Hurt Iteration Speed
Embedding stock/ETF symbols directly inside your connection logic means you have to edit core network code every time you update your watchlist. This introduces unnecessary risk of breaking the streaming client during research iteration.
Single Unified Stream Architecture Breakdown
Stock and ETF tick payloads share nearly all core fields: real-time price, volume, order book levels, and timestamps. The only difference is asset type classification, which we can handle post-ingestion instead of splitting the stream upfront.
My standard workflow rules:
- Maintain one central watchlist storing both stock and ETF tickers;
- Open a single persistent WebSocket connection and batch-subscribe all instruments at once;
- Parse incoming unified tick data, then branch business logic for stocks vs ETFs locally;
- Decouple watchlist config from low-level WebSocket logic so ticker changes never touch connection code.
Minimal Working Python Code
import websocket
import json
# Unified watchlist with A-share stock + ETF codes
watchlist = ["600000", "510300"]
def on_open(ws):
# Send bulk subscription request once connection establishes
sub_payload = {
"cmd": "subscribe",
"symbol_list": watchlist
}
ws.send(json.dumps(sub_payload))
def on_message(ws, raw_msg):
tick = json.loads(raw_msg)
ticker_code = tick.get("symbol")
live_price = tick.get("price")
ts = tick.get("timestamp")
# Insert custom stock/ETF separate processing logic here
print(f"Ticker: {ticker_code} | Price: {live_price} | Timestamp: {ts}")
if __name__ == "__main__":
ws_endpoint = "wss://quote.alltick.co/quote-b-ws-api"
ws_client = websocket.WebSocketApp(ws_endpoint, on_open=on_open, on_message=on_message)
print("Streaming live data for stocks + ETFs in a single connection")
ws_client.run_forever()
Production Optimizations For 24/7 Cloud Uptime
This minimal snippet works for local testing, but you need these four tweaks for stable long-running deployments:
- Normalize ticker formatting: Different data providers add market prefixes (SH/SZ) inconsistently. Standardize all codes before writing to time-series DBs to avoid matching errors during backtesting.
- Unify timestamp timezone: Convert all raw millisecond/nanosecond timestamps to Beijing Time to eliminate misalignment when building minute K-lines or cross-asset momentum calculations.
- Auto-reconnect + resubscribe logic: Cache your full watchlist in memory. If the WebSocket drops from network blips or rate limits, automatically re-subscribe all instruments after reconnecting without manual restarts.
- Data freshness filtering: Low-liquid ETFs and small-cap stocks can sit quiet for minutes. Add a time threshold to deprioritize outdated tickers in dashboards so stale data doesn’t skew market rankings.
Wrap Up
Building separate streaming pipelines for stocks and ETFs creates unnecessary engineering overhead. A single WebSocket connection paired with a unified watchlist reduces resource usage, simplifies maintenance, and lets you extend coverage to other asset types with minimal code changes.
The core pillars of reliable continuous ingestion are standardized tick formatting, consistent timestamps, and automatic reconnection handling — these small tweaks separate throwaway test scripts from production-grade quant data pipelines.
Top comments (0)