When building quant systems, trading dashboards, or live monitoring tools, stable, low-latency, reproducible 1‑minute candlestick data is the foundation. In my experience, relying on third-party K-line APIs or HTTP polling leads to consistent issues with latency, rate limits, and data consistency. A far more reliable approach is to consume raw tick data via WebSocket and generate candles locally.
This article walks through a complete, production-ready implementation: real-time data access, tick aggregation, and data storage.
Use Case & Core Requirements
For financial applications, minute-level data needs three key properties:
Low latency: Real-time updates for live signals
High stability: No data loss, no disconnections, no throttling
Consistency: Identical logic for backtesting and live trading
Traditional methods fail at all three. A WebSocket + tick aggregation architecture solves them cleanly.
Pain Points of Traditional Solutions
HTTP polling is slow and easily rate‑limited, making it unsuitable for live environments.
Third-party K-line services use black‑box logic you can’t verify or reproduce.
Data loss during peak trading hours breaks candles and distorts indicators.
Limited customization: You can’t easily add derived metrics like average price or amplitude.
The better approach is to ingest ticks directly and build your own candles.
How Tick Data Becomes 1‑Minute K-Lines
A tick represents a single transaction: price, volume, timestamp. We group ticks into 1‑minute windows to form standard candles:
Open: first tick price in the window
Close: last tick price in the window
High: maximum price in the window
Low: minimum price in the window
Volume: total volume in the window
Implementation: Tick → 1‑Minute Candle
`from datetime import datetime
minute_cache = {}
def update_with_tick(tick):
minute_key = datetime.fromtimestamp(tick['time']).strftime('%Y-%m-%d %H:%M')
if minute_key not in minute_cache:
minute_cache[minute_key] = {
'open': tick['price'],
'high': tick['price'],
'low': tick['price'],
'close': tick['price'],
'volume': tick['volume']
}
else:
minute_cache[minute_key]['close'] = tick['price']
minute_cache[minute_key]['high'] = max(minute_cache[minute_key]['high'], tick['price'])
minute_cache[minute_key]['low'] = min(minute_cache[minute_key]['low'], tick['price'])
minute_cache[minute_key]['volume'] += tick['volume']`
Real-Time Access with WebSocket (AllTick API)
WebSocket is the industry standard for low‑latency market data. Below is a complete, robust connector using AllTick, a reliable API for US stock data.
Full WebSocket Client
`import websocket
import json
def on_message(ws, message):
tick = json.loads(message)
update_with_tick(tick)
def on_error(ws, error):
print(f"error: {error}")
def on_close(ws):
print("connection closed")
def on_open(ws):
# Subscribe to a US stock (AAPL, TSLA, MSFT, etc.)
ws.send(json.dumps({"sub": "AAPL"}))
if name == "main":
ws = websocket.WebSocketApp(
"wss://api.alltick.co/stock/ws",
on_open=on_open,
on_message=on_message,
on_error=on_error,
on_close=on_close
)
ws.run_forever(ping_interval=30, ping_timeout=10)`
This setup delivers millisecond‑level updates and builds candles locally in real time.
Storage Options for Minute Data
Once you generate clean 1‑minute candles, you can store them based on your workflow:
CSV/Parquet is simple, easy to debug, and great for research and backtesting.
SQLite is lightweight, file‑based, and supports SQL queries for small‑scale strategies.
Redis provides in‑memory speed for real‑time dashboards and live strategy queries.
Time‑series databases offer high throughput and efficient time‑range queries for production.
In practice, I validate logic with Parquet and deploy with Redis or a time‑series database.
Production Hardening Tips
Add automatic reconnection and heartbeat logic.
Support batch subscription for multiple symbols.
Extend the code to compute derived metrics: average price, amplitude, change rate.
Use historical tick data to regenerate consistent candles for backtesting.
Add structured logging and monitoring for reliability.
Wrap-Up
Building your own 1‑minute K‑line service using WebSocket tick data + local aggregation gives you low latency, full transparency, and full control. It outperforms third‑party APIs and polling for quant strategies, live dashboards, and monitoring systems.
This lightweight, reliable architecture is widely used in real trading systems and makes an excellent foundation for any financial data pipeline.
Top comments (0)