Docs

L2 Order Book - Aggregated Price Levels

Stream aggregated Level 2 order book data from Hyperliquid. Real-time bid/ask levels with configurable depth and price aggregation.

Stream aggregated order book data showing total size at each price level. The L2 book provides a consolidated view of market depth, with configurable levels and price aggregation for bandwidth optimization.

Code Examples Repository

How to Subscribe

Send a subscription message to the WebSocket endpoint:

JSON
{
  "method": "subscribe",
  "subscription": {
    "type": "l2Book",
    "coin": "BTC",
    "nSigFigs": 5,
    "nLevels": 20,
    "strict": true
  }
}

Subscription Parameters

ParameterTypeRequiredDefaultDescription
typestringYes-Must be "l2Book"
coinstringYes-Trading pair symbol (e.g., "BTC", "ETH", "xyz:MSTR", "@150")
nSigFigsnumberNo5Price aggregation: 2-5 significant figures
nLevelsnumberNo20Number of levels per side: 1-100
strictboolNoFalseOnly send data on updates within the subscribed level interval

HIP3 Markets

For HIP3 (permissionless perpetuals) markets, use the full coin label format with the xyz: prefix. For example, use "xyz:MSTR" for the MicroStrategy perpetual, not just "MSTR". Standard perpetuals like BTC and ETH do not require a prefix.

Spot Markets

For spot markets, use the @{index} format where the index is the spot asset index. For example, use "@150" for a specific spot market.

Understanding nSigFigs (Price Aggregation)

The nSigFigs parameter controls how prices are rounded for aggregation:

nSigFigsBTC ExampleEffect
5$106,217Most granular - individual price points
4$106,200Moderate aggregation
3$106,000Coarse aggregation
2$110,000Very coarse - major levels only

Lower nSigFigs values reduce message frequency and bandwidth but sacrifice price precision.

The first websocket message after subscribing is a subscriptionResponse; subsequent messages carry the l2Book payloads.

Response Field Reference

FieldTypeDescription
channelstringAlways "l2Book" for this subscription type
data.coinstringTrading pair symbol
data.timenumberUnix timestamp in milliseconds
data.levelsarrayTwo-element array: [bids, asks]
levels[0]arrayBid levels (buyers), sorted best (highest) to worst
levels[1]arrayAsk levels (sellers), sorted best (lowest) to worst

Level Object Fields

FieldTypeDescription
pxstringPrice level
szstringTotal size at this price level
nnumberNumber of orders aggregated at this level

Use Cases

Market Making

Monitor spread and depth to optimize quote placement:

  • Spread monitoring: Track bid-ask spread for quote width
  • Inventory management: Adjust quotes based on book imbalance
  • Competition analysis: Count orders at each level to gauge competition

Liquidity Analysis

Assess market depth for large order execution:

  • Slippage estimation: Calculate expected price impact for order sizes
  • Best execution: Find optimal order sizes and timing
  • Market quality metrics: Track spread, depth, and resilience

Support/Resistance Detection

Identify significant price levels from order concentration:

  • Order clusters: Large n values indicate many orders at a level
  • Size walls: High sz values suggest strong support/resistance
  • Level breaks: Monitor when significant levels get absorbed

Algorithmic Trading Signals

Generate trading signals from book dynamics:

  • Imbalance signals: Buy when bids dominate, sell when asks dominate
  • Spread compression: Trade when spread narrows (high liquidity)
  • Depth changes: Alert on sudden liquidity additions or removals

Subscription Management

Adjusting Parameters

Change depth or aggregation by resubscribing:

Python
# Switch from 20 levels to 50 levels
await websocket.send(json.dumps({
    "method": "unsubscribe",
    "subscription": {"type": "l2Book", "coin": "BTC", "nLevels": 20}
}))

await websocket.send(json.dumps({
    "method": "subscribe",
    "subscription": {"type": "l2Book", "coin": "BTC", "nLevels": 50}
}))

Bandwidth Optimization

For high-frequency updates, use lower nSigFigs:

JSON
{
  "method": "subscribe",
  "subscription": {
    "type": "l2Book",
    "coin": "BTC",
    "nSigFigs": 3,
    "nLevels": 10
  }
}

This reduces message size and frequency while maintaining key price levels.

Get Access

Ready to integrate real-time Hyperliquid L2 order book data?


Stream institutional-grade Hyperliquid order book data with Dwellir's ultra-low latency infrastructure.