◈   ⌘ api · Intermediate

Crypto Orderbook Depth API: A Trader's Complete Guide

A complete guide to using crypto orderbook depth APIs. Covers REST and WebSocket endpoints for Binance, Bybit, and OKX with real Python code examples and trading insights.

Uncle Solieditor · voc · 05.05.2026 ·views 61
◈   Contents
  1. → What Orderbook Depth Actually Tells You
  2. → Fetching Depth Data from Binance REST API
  3. → Bybit Orderbook API — Spot and Derivatives
  4. → Real-Time Depth Streaming via WebSocket
  5. → Reading Depth Data for Actionable Trading Signals
  6. → Frequently Asked Questions
  7. → Conclusion

The orderbook is the closest thing crypto trading has to reading minds. Every limit order sitting on Binance, Bybit, or OKX is a declared intention — and when you query the depth API, you're pulling back the curtain on exactly where the market is ready to absorb buys and sells. For algorithmic traders, this data is the foundation of everything from spread capture to large-order execution to predictive signal generation. The challenge isn't access — most major exchanges offer orderbook depth APIs for free — it's knowing what to pull, how to parse it, and what patterns actually matter.

What Orderbook Depth Actually Tells You

A raw price ticker tells you the last trade price. The orderbook depth tells you what happens next. At any moment, the depth snapshot shows you all resting bid orders (buyers waiting) and ask orders (sellers waiting), organized by price level with their cumulative quantities. This gives you three immediately useful data points: the best bid-ask spread, the wall of liquidity nearest to the current price, and the imbalance between buying and selling pressure.

Depth specifically refers to how far from the mid-price you need to go to fill a given order size without significant slippage. A market with 50 BTC resting within 0.1% of mid-price has very different execution characteristics than one with 2 BTC in the same zone. Platforms like OKX and Binance both expose depth snapshots up to 5000 levels via their APIs, though for most trading strategies the top 20-50 levels contain 90% of the actionable information.

Depth data is a snapshot — it reflects resting orders at that instant. High-frequency market makers update quotes hundreds of times per second, so a REST snapshot older than 500ms may already be stale in fast markets. Use WebSocket streams for anything latency-sensitive.

Fetching Depth Data from Binance REST API

Binance's REST depth endpoint requires no authentication for public market data, which makes it the easiest starting point. The endpoint returns bids and asks as arrays of [price, quantity] pairs, sorted from best to worst. The `limit` parameter controls depth: valid values are 5, 10, 20, 50, 100, 500, 1000, and 5000. Larger limits increase response size and latency, so start with 20-50 levels unless your strategy requires full book visibility.

import requests

def get_orderbook_depth(symbol="BTCUSDT", limit=20):
    url = "https://api.binance.com/api/v3/depth"
    params = {"symbol": symbol, "limit": limit}
    try:
        response = requests.get(url, params=params, timeout=5)
        response.raise_for_status()
        return response.json()
    except requests.exceptions.HTTPError as e:
        print(f"HTTP error: {e.response.status_code} — {e.response.text}")
        return None
    except requests.exceptions.RequestException as e:
        print(f"Connection error: {e}")
        return None

data = get_orderbook_depth()
if data:
    best_bid_price = float(data["bids"][0][0])
    best_bid_qty   = float(data["bids"][0][1])
    best_ask_price = float(data["asks"][0][0])
    best_ask_qty   = float(data["asks"][0][1])
    spread = best_ask_price - best_bid_price
    spread_pct = spread / best_bid_price * 100

    print(f"Best bid: {best_bid_price} x {best_bid_qty} BTC")
    print(f"Best ask: {best_ask_price} x {best_ask_qty} BTC")
    print(f"Spread:   {spread:.2f} USDT ({spread_pct:.4f}%)")
    print(f"Sequence: {data['lastUpdateId']}")

Notice the `lastUpdateId` field in the response. This sequence number is critical when you need to stitch REST snapshots together with WebSocket delta updates — you only apply WebSocket updates whose `u` (final update ID) is greater than your snapshot's `lastUpdateId`. Ignoring this will result in a corrupted book state that produces nonsensical signals.

Bybit Orderbook API — Spot and Derivatives

Bybit's v5 API unified their spot, linear, and inverse markets under a single endpoint, which is a significant ergonomic improvement over their older v2/v3 fragmentation. The key difference from Binance: Bybit requires a `category` parameter (`spot`, `linear`, `inverse`, `option`) and uses `retCode: 0` for success rather than HTTP status codes alone. Always check `retCode` explicitly — Bybit can return HTTP 200 with an error payload.

import requests

def get_bybit_orderbook(symbol="BTCUSDT", category="spot", limit=25):
    url = "https://api.bybit.com/v5/market/orderbook"
    params = {
        "category": category,
        "symbol": symbol,
        "limit": limit
    }
    response = requests.get(url, params=params, timeout=5)
    data = response.json()

    if data["retCode"] != 0:
        raise ValueError(f"Bybit API error {data['retCode']}: {data['retMsg']}")

    book = data["result"]
    bids = book["b"]  # [[price_str, qty_str], ...] best first
    asks = book["a"]  # [[price_str, qty_str], ...] best first

    # Calculate cumulative bid-side liquidity within top 10 levels
    top_bid_liquidity = sum(float(b[1]) for b in bids[:10])
    top_ask_liquidity = sum(float(a[1]) for a in asks[:10])
    imbalance = top_bid_liquidity / (top_bid_liquidity + top_ask_liquidity)

    print(f"Top-10 bid liquidity: {top_bid_liquidity:.4f} BTC")
    print(f"Top-10 ask liquidity: {top_ask_liquidity:.4f} BTC")
    print(f"Order imbalance (bid %): {imbalance * 100:.1f}%")
    return book

get_bybit_orderbook(symbol="BTCUSDT", category="linear")

Order imbalance — the ratio of bid volume to total volume in the top N levels — is one of the most widely studied short-term predictors of price direction. An imbalance consistently above 65% on the bid side in the top 10 Bybit linear futures levels has historically preceded upward price pressure within the next few seconds to minutes. This is the kind of signal that services like VoiceOfChain aggregate across exchanges to generate real-time trading alerts.

Real-Time Depth Streaming via WebSocket

REST polling has a floor: even at one request per second, you're paying TCP handshake overhead on every call and you're almost certainly rate-limited to fewer calls than that at depth levels above 100. For any strategy that needs sub-second orderbook awareness — spread monitoring, iceberg detection, front-running prevention — WebSocket streaming is the only viable approach. Binance pushes diff depth updates every 100ms or 1000ms (selectable via stream name suffix), while OKX offers a snapshot-then-delta model over WebSocket that's excellent for maintaining a local copy of the full book.

import websocket
import json
import threading

local_book = {"bids": {}, "asks": {}}

def apply_depth_update(data):
    # Remove price levels with qty=0, update others
    for price, qty in data.get("b", []):
        if float(qty) == 0:
            local_book["bids"].pop(price, None)
        else:
            local_book["bids"][price] = float(qty)
    for price, qty in data.get("a", []):
        if float(qty) == 0:
            local_book["asks"].pop(price, None)
        else:
            local_book["asks"][price] = float(qty)

def on_message(ws, message):
    data = json.loads(message)
    apply_depth_update(data)

    sorted_bids = sorted(local_book["bids"].keys(), key=float, reverse=True)
    sorted_asks = sorted(local_book["asks"].keys(), key=float)

    if sorted_bids and sorted_asks:
        best_bid = float(sorted_bids[0])
        best_ask = float(sorted_asks[0])
        spread_pct = (best_ask - best_bid) / best_bid * 100
        print(f"Bid: {best_bid:.2f} | Ask: {best_ask:.2f} | Spread: {spread_pct:.4f}%")

def on_error(ws, error):
    print(f"WebSocket error: {error}")

def on_close(ws, code, msg):
    print("Stream closed")

# @depth updates every 100ms; @depth20 gives top-20 snapshot each tick
ws = websocket.WebSocketApp(
    "wss://stream.binance.com:9443/ws/btcusdt@depth@100ms",
    on_message=on_message,
    on_error=on_error,
    on_close=on_close
)
thread = threading.Thread(target=ws.run_forever)
thread.daemon = True
thread.start()
When maintaining a local book from WebSocket deltas, initialize it with a REST snapshot FIRST, then apply only delta messages where U (first update ID) <= lastUpdateId + 1 <= u (final update ID). Binance's documentation calls this the "How to manage a local order book" process — skip it and your local book will drift from reality within minutes.

Reading Depth Data for Actionable Trading Signals

Raw depth data is noise until you know what patterns to look for. These are the four signals that experienced algorithmic traders extract from orderbook depth on exchanges like Binance, Bybit, and Gate.io:

VoiceOfChain aggregates orderbook depth signals across multiple exchanges in real time, generating composite alerts when depth imbalance, spread anomalies, and price momentum align simultaneously. Rather than monitoring five exchange APIs independently, traders use platform signals to get notified when the setup actually forms.

Orderbook Depth API Comparison — Major Exchanges
ExchangeEndpointMax DepthWebSocket UpdateAuth Required
Binance/api/v3/depth5000 levels100ms / 1000msNo (public)
Bybit/v5/market/orderbook500 levels10ms (linear)No (public)
OKX/api/v5/market/books400 levelsSnapshot+deltaNo (public)
Coinbase Adv./api/v3/brokerage/market/product_book250 levels50msNo (public)
KuCoin/api/v1/market/orderbook/level2100 levelsLevel2 diffNo (public)

Frequently Asked Questions

Do I need an API key to access orderbook depth data?
No. All major exchanges including Binance, Bybit, OKX, and KuCoin expose orderbook depth as public market data — no authentication required. API keys are only needed for private endpoints like placing orders or querying account balances.
How often should I poll the REST depth endpoint?
For most strategies, polling faster than once per second via REST is counterproductive — you'll hit rate limits and the data will still be stale between calls. If you need sub-second updates, switch to WebSocket streaming, which pushes updates every 100ms on Binance and even faster on Bybit linear contracts.
What is the difference between depth level 20 and depth level 500?
Depth level 20 gives you the 20 best bid and ask price levels closest to the mid-price. Level 500 gives you 500 levels on each side, covering a much wider price range. For most short-term trading signals, the top 20-50 levels contain all the actionable information. Level 500+ is mainly used for full book reconstruction or slippage modeling on large orders.
Why does my locally maintained orderbook drift from the exchange's book?
This almost always means you're applying WebSocket delta updates out of sequence or missing the REST snapshot initialization step. Each delta message has a sequence ID range (U and u on Binance). You must verify that your first applied update's U is exactly lastUpdateId+1 from your REST snapshot. Any gap means you need to re-initialize with a fresh snapshot.
Can I use orderbook depth data to predict price direction?
Order imbalance — the ratio of bid to ask volume in the top N levels — has documented short-term predictive value on liquid pairs like BTCUSDT. However, it's not a standalone signal: sophisticated market makers on Bybit and Binance actively spoof depth to mislead directional traders. Best combined with trade flow data, funding rates, and cross-exchange signals.
Is there a free tier for high-frequency depth data access?
Yes — Binance, Bybit, and OKX all provide public WebSocket depth streams at no cost with generous rate limits (typically 1200+ requests per minute for REST). For institutional-grade co-location and direct market data feeds at microsecond latency, paid solutions from the exchanges' prime brokerage arms are required, but these are only relevant for HFT shops running strategies that compete on raw speed.

Conclusion

Orderbook depth APIs are one of the most information-dense and freely accessible data sources in crypto trading. Whether you're building a simple spread monitor, a slippage model for a larger trading desk, or feeding depth imbalance signals into a machine learning pipeline, the infrastructure is the same: a REST snapshot to initialize, WebSocket deltas to maintain the live state, and clean parsing logic that handles sequence gaps and zero-quantity level removals correctly. Start with Binance's public depth endpoint — it's the most documented and forgiving for beginners — then expand to Bybit and OKX as your strategy demands cross-exchange coverage. The traders who consistently extract edge from this data aren't doing anything exotic; they're just maintaining clean, low-latency book state while everyone else is working with stale snapshots.

◈   more on this topic
◉ basics Mastering the ccxt library documentation for crypto traders ⌂ exchanges Mastering the Binance CCXT Library for Crypto Traders ⌬ bots Best Crypto Trading Bots 2025: Profitable AI-Powered Strategies