DEV Community

EmilyL
EmilyL

Posted on

Stop Letting Multi-Crypto WebSocket Streams Mess Up Your Charts — Use a Timestamp Merge Queue

If you’ve ever built a real-time dashboard or a trading bot that watches BTC, ETH, and LTC simultaneously, you’ve likely seen it: the chart flickers, the latest trade jumps to a candle that should already be closed, and your alerts fire for events that seem to happen out of sequence. The root issue? Cross-symbol tick disorder. Let me show you how to fix it with a simple, battle-tested pattern.

🕵️ The problem: why your ticks are out of order

Every symbol’s WebSocket feed is an independent stream. Even when using a single API that supports multi-symbol subscriptions, the frames don’t wait for each other. Three things go wrong:

  1. Network latency variance: a few milliseconds here and there are enough to shuffle the global sequence.
  2. Shared queue + multiple workers: putting all ticks in one queue and processing with several threads? The OS decides the order, not you.
  3. Timestamp mismatches: some APIs return local exchange time, some UTC, some with second precision, some with millisecond. Good luck sorting that reliably.

💡 The solution: per-symbol queues + a priority-based dispatcher

The fix is conceptually simple: separate reception from ordering.

  • Step 1: maintain an ordered list for each symbol. Since ticks usually arrive in order per symbol, you can just append them.
  • Step 2: use a min-heap (priority queue) that always looks at the first (earliest) tick of each symbol’s list. Pop the smallest timestamp, process it, then push the next tick from that same symbol back into the heap.

This gives you a perfectly time-ordered stream regardless of network jitter. Here’s a state table that explains the merge decision:

Symbol Ordered Buffer Head Timestamp Next to process?
BTC [tick1, tick2, tick3] 12:01:05 No
ETH [tick1, tick2] 12:01:06 No
LTC [tick1, tick2, tick3] 12:01:04 ✅ Yes

LTC’s tick is the oldest, so it goes first, then BTC, then ETH. Even if LTC’s data arrived last, it’s processed in its correct time slot.

🛠️ Code snippet (ingestion side)

I use a data provider that gives consistent UTC timestamps for every tick — for example, AllTick’s WebSocket API. The Python ingestion code is dead simple:

import websocket
import json

queues = {}  # each symbol gets its own list

def on_message(ws, message):
    data = json.loads(message)
    symbol = data['symbol']
    timestamp = data['timestamp']
    queues[symbol].append(data)  # per-symbol queue, later sorted by timestamp

ws = websocket.WebSocketApp("wss://api.alltick.co/ws",
                            on_message=on_message)
ws.run_forever()
Enter fullscreen mode Exit fullscreen mode

On the consumption side (not shown), you’d run a loop that uses heapq or your language’s priority queue to always pull the oldest tick across all buffers. If you’re using Node.js, a binary heap works just as well.

⚡ Performance and stability tweaks

  • Limit buffer sizes: set a maximum length (e.g., 1000 ticks per symbol) to avoid memory bloat during huge volume spikes.
  • Batch together: pop 10–50 ticks from the heap at once, sort that mini-batch (it’s nearly sorted already) and dispatch — cuts heap operations dramatically.
  • Reconnect wisely: on socket disconnect, clear all buffers and rebuild the heap. Stale data from a previous session will only confuse your strategy.

🧠 Final thoughts

Multi-symbol tick ordering isn’t a niche problem — it’s at the heart of any real-time crypto system. The per-symbol queue + timestamp merge pattern solves it elegantly without any heavy infrastructure. Try it in your next bot or dashboard, and you’ll immediately notice smoother charts and more reliable signals. If you have your own tricks for taming real-time streams, drop them in the comments!

Top comments (0)