DEV Community

kalos
kalos

Posted on

Crypto Order Book Snapshots: How Often Do Real-Time APIs Update?


When working on crypto trading bots or market data tools, one question always comes up: how fresh is the order book data you’re getting?

Early on, I’d poll APIs for snapshots and see updates every few seconds—way too slow for real-time strategies. That’s when I realized: understanding order book mechanics matters more than reading candlesticks.

What Is an Order Book Snapshot?
A snapshot is a full record of all buy/sell orders at a single moment, listing price levels and volumes.
Real-time APIs rarely send full snapshots continuously. The standard pattern is snapshot + incremental updates:
Snapshot: Full data to initialize your local order book.
Incremental (diff): Small, frequent updates for only the changes.
Think: snapshot = a screenshot; increments = new messages arriving.

REST vs. WebSocket: The Speed Difference
REST
Update: Seconds to tens of seconds
Use case: Historical data, analytics, low-frequency tasks
Cons: Too slow for live trading

WebSocket
Update: Milliseconds to hundreds of ms
Use case: Live monitoring, automated trading, real-time strategies
Pros: Low latency, event-driven, near real-time
I’ve wasted hours on REST-based setups that lagged behind price moves. WebSocket is non-negotiable for real-time work.

Why Updates Aren’t Fixed Intervals
You might ask: why not update every 1 second?

  • High-vol pairs: Rapid changes would overload bandwidth with full snapshots.
  • Low-vol pairs: Little movement means no need for frequent refreshes.
  • Efficiency: Snapshot + incremental balances speed and cost. Even if labeled “real-time,” updates adapt to market activity.

Best Practice: Snapshot + Incremental
The reliable approach:
Fetch one full snapshot via REST.
Open a WebSocket connection for incremental updates.
Apply diffs locally to keep your order book synced.
I use this pattern with AllTick API. Here’s a minimal Python example:

import websocket
import json

def on_message(ws, message):
    data = json.loads(message)
    # Update local order book with incremental data
    process_orderbook_update(data)

if __name__ == "__main__":
    ws = websocket.WebSocketApp(
        "wss://apis.alltick.co/ws/stock",
        on_message=on_message
    )
    ws.run_forever()
Enter fullscreen mode Exit fullscreen mode

This keeps your local book nearly in sync with the exchange, avoiding REST polling delays.

Key Takeaways

  • Snapshots ≠ real-time: increments are the real-time backbone. WebSocket > REST for live data.
  • Latency adds up: network distance impacts perceived speed. Stick to snapshot + incremental for reliability.

Final Thoughts
Snapshot frequency isn’t everything. What counts is stable incremental ingestion and correct local state management.
Don’t chase advertised refresh rates—focus on solid data pipelines. That’s how you build reliable trading systems.

Top comments (0)