DEV Community

kalos
kalos

Posted on

How to Stream Stable Millisecond Ticks for Gold & Silver

3D abstract financial visualization showing glowing gold and silver price streams
If you’ve ever built a trading dashboard, bot, or real‑time monitor for XAUUSD / XAGUSD, you know the pain:
Extreme volatility, dozens of ticks per second, laggy polling, missing data, rate limits, and frozen UIs.
After testing many approaches, I found the simplest, most reliable way to get stable millisecond quotes:
One WebSocket connection → multi‑symbol subscription.

This post covers:
• Why old methods fail
• Why WebSocket wins
• Full copy‑paste Python code
• Production‑grade processing tips

The Problem: Why Traditional Methods Break

  1. HTTP Polling Even at 500ms intervals, you lose ticks during spikes. Backtests become inaccurate. Higher frequency = instant rate limits. Not suitable for millisecond reactivity.
  2. One WebSocket Per Symbol Gold, silver, platinum — each with its own connection. • Connection bloat • Higher resource usage • Unstable • Hard to debug It doesn’t scale.

The Solution: Single WebSocket + Multi‑Symbol Subscribe
WebSocket is built for real‑time data. One connection, many instruments is the pro pattern.

Benefits
• Server push: Ticks arrive instantly
• True millisecond latency
• Lightweight & stable
• Clean code, easy maintenance
• No more UI freezes during volatility

Symbol Formats You Must Use
Wrong format = no data.
• Gold: XAUUSD
• Silver: XAGUSD
• Platinum: XPTUSD
Subscribe with an array:
["XAUUSD", "XAGUSD", "XPTUSD"]
Or comma‑separated string:
"XAUUSD,XAGUSD,XPTUSD"

Full Working Code (Python)
Copy, paste, run. Uses AllTick API.
`import websocket
import json

url = "wss://api.alltick.co/ws/stock"

def on_message(ws, message):
data = json.loads(message)
for tick in data.get("ticks", []):
print(f"Symbol: {tick['symbol']}, Price: {tick['price']}, Time: {tick['time']}")

def on_open(ws):
sub_msg = {
"action": "subscribe",
"symbols": ["XAUUSD", "XAGUSD"]
}
ws.send(json.dumps(sub_msg))

ws = websocket.WebSocketApp(url, on_message=on_message)
ws.on_open = on_open
ws.run_forever()`

You’ll see live, millisecond‑accurate ticks immediately.

3 Pro Tips for Stable Processing
Store ticks in a dictionary
Fast O(1) access per symbol.

Batch UI updates (~50ms)
Don’t render every tick — keep apps smooth.

Use async queues
Prevent main thread blocking under high load.

Final Thoughts
Stable millisecond precious metals prices don’t need complex infrastructure.
You just need:
• Correct protocol (WebSocket)
• Clean subscription (one connection)
• Smart data processing

This works for hobby projects, trading bots, dashboards, and lightweight production systems.

Top comments (0)