Viewing: Depth

Real-time WebSocket - Depth

Stream real-time level-by-level bid/ask depth updates through the depth channel.

WS /ws/v1/market/depth/

Pro+

Real-time depth channel for level-by-level bid/ask streaming.

Endpoint

wss://api.sahmk.sa/ws/v1/market/depth/?api_key=YOUR_API_KEY

Data availability by plan:

Market depth requires separate approval. Having an eligible subscription plan does not automatically enable access.

Connected message

json
{
  "type": "connected",
  "channel": "depth",
  "symbol": null,
  "plan": "enterprise",
  "entitled_levels": 20,
  "limits": {
    "max_symbols_per_connection": 200,
    "max_symbols_per_call": 20,
    "wildcard_allowed": true
  },
  "timestamp": "2026-07-09T15:05:37.840967+00:00"
}

Optional: add symbol=2222 to receive connected followed by an initial depth_snapshot.

Client actions

json
{"action":"snapshot","symbol":"2222","levels":5}
{"action":"subscribe","symbols":["2222","1120"],"levels":20}
{"action":"unsubscribe","symbols":["1120"]}
{"action":"ping"}
{"action":"subscribe","symbols":["*"],"levels":20}

The last command, subscribing to all symbols, is available on the Enterprise plan only.

SAHMK sends a WebSocket protocol ping about 20 seconds after the last data frame from your client. If neither a protocol pong nor another data frame arrives within 30 seconds, the server closes the socket with code 1006 and no close frame. Browsers and Python's websockets library answer protocol pings automatically. The JSON ping in the client actions is a data frame and resets the same timer.

Use symbols for subscribe/unsubscribe. For single-symbol requests, the server also accepts symbol.

Depth message ordering

  • One depth_snapshot for each symbol, then subscribed.
  • Later updates use the same depth_snapshot message.

Subscribed message

json
{
  "type": "subscribed",
  "symbols": ["1120", "2222"],
  "total": 2,
  "limit": 200
}

Depth snapshot message

WebSocket messageJSON
{
  "type": "depth_snapshot",
  "symbol": "2222",
  "available": true,
  "updated_at": "2026-07-09T15:05:37.840967+00:00",
  "session": "postmarket",
  "book_state": "normal",
  "levels": 20,
  "best_bid": 26.68,
  "best_ask": 26.72,
  "spread": 0.04,
  "spread_bps": 14.99,
  "total_bid_quantity_top5": 29085,
  "total_ask_quantity_top5": 504644,
  "total_bid_quantity": 29085,
  "total_ask_quantity": 504644,
  "level_imbalance": -0.891,
  "level_imbalance_top5": -0.891,
  "bids": [
    {
      "level": 0,
      "price": 26.68,
      "quantity": 79,
      "order_count": 10
    }
  ],
  "asks": [
    {
      "level": 0,
      "price": 26.72,
      "quantity": 41188,
      "order_count": 38
    }
  ],
  "market_order": {
    "bid_quantity": 79938,
    "bid_order_count": 22,
    "bid_updated_at": "2026-07-09T15:05:30+00:00",
    "ask_quantity": 87743,
    "ask_order_count": 11,
    "ask_updated_at": "2026-07-09T15:05:15+00:00"
  },
  "entitled_levels": 20,
  "timestamp": "2026-07-09T15:05:37.872738+00:00"
}

The bids and asks arrays in this example are shortened for readability. When levels is 20, a complete response can include up to 20 priced levels on each side.

Market orders

  • market_order uses the same object as REST depth.
  • It is null when neither side has a positive quantity.
  • Price 0 is not a priced level and is not included in the best bid, best ask, spread, or quantity totals.
  • A book that contains only market orders is still delivered.

Last updated on