Skip to content

Maintain an Order Book

Subscribe to orderbook for a single exchange contract. Keep separate books for each instrument, exchange, and settlement asset. The Hyperliquid example uses BTC settled in USDC.

The first message has type: "snapshot", sequence: 1, and previous_sequence: 0. Replace the entire local book with its bids and asks.

{
"op": "data",
"subscription_id": "sub_example_orderbook",
"channel": "orderbook",
"type": "snapshot",
"sequence": 1,
"previous_sequence": 0,
"timestamp": "2026-08-31T12:00:00.000Z",
"data": [{
"instrument": "BTC-PERP",
"venue": "hyperliquid",
"settlement_asset": "USDC",
"book_sequence": 1042,
"bids": [{ "price": "76999", "quantity": "1.25000" }],
"asks": [{ "price": "77001", "quantity": "0.90000" }],
"updated_at": "2026-08-31T12:00:00.000Z"
}]
}

sequence tracks this subscription’s messages. book_sequence identifies the exchange book state and may advance by several values between messages when changes are coalesced. REST book snapshots carry the same book-state counter.

Updates contain absolute quantities at the changed prices. Set the new quantity; remove the level when it is zero. A delta includes the changes needed to maintain the requested depth, including new boundary levels and removal of levels leaving that depth.

{
"op": "data",
"subscription_id": "sub_example_orderbook",
"channel": "orderbook",
"type": "update",
"sequence": 2,
"previous_sequence": 1,
"timestamp": "2026-08-31T12:00:00.100Z",
"data": [{
"instrument": "BTC-PERP",
"venue": "hyperliquid",
"settlement_asset": "USDC",
"book_sequence": 1045,
"bids": [{ "price": "76999", "quantity": "0.80000" }],
"asks": [
{ "price": "77001", "quantity": "0" },
{ "price": "77002", "quantity": "1.10000" }
],
"updated_at": "2026-08-31T12:00:00.100Z"
}]
}

After this update, the displayed best bid is 76999 for 0.80000 BTC, and the best ask is 77002 for 1.10000 BTC.

Require previous_sequence to match the last applied sequence and sequence to advance by one. Ignore older update sequences. If the connection changes, the book becomes stale, or either sequence check fails, discard the local book and resubscribe for a new snapshot.

A REST snapshot can be used for an independent current-state read. For streaming recovery, a new subscription supplies a snapshot and the following deltas on the same ordered stream.

Download order-book.mjs. It uses decimal strings for price keys and integer arithmetic for sorting, so nearby prices retain their precision.

import { OrderBook } from './order-book.mjs';
const book = new OrderBook();
function onData(message) {
try {
if (book.apply(message)) {
const bestBid = book.levels('bids')[0];
const bestAsk = book.levels('asks')[0];
console.log({ bestBid, bestAsk });
}
} catch (error) {
// The helper clears its state on an invalid update.
// Unsubscribe, then resubscribe for a fresh snapshot.
console.error(error.message);
}
}

Keep a separate freshness timer for each strategy input. A healthy socket can carry fresh data for one exchange while another exchange is degraded. Subscribe to exchange status and inspect each record’s updated_at.

Account access

Jurisdiction Restriction

Trade8 is not available in your jurisdiction.

You can still explore the product and read the documentation.

Explore the Docs