# L4 orderbook streaming

Maintain a real-time L4 orderbook locally using snapshot + delta sync.

### How it works

1. Subscribe to [l4BookUpdates](/readme/websocket/l4bookupdates.md) and buffer incoming updates
2. Fetch [L4PerpBookSnapshot](/readme/rest-api/market-data/l4book.md) snapshot with block height
3. Apply buffered updates where `height > snapshot_height`
4. Continue applying live updates

This gives you a complete L4 orderbook with user addresses for every resting order.

### Use cases

* **HIP-3 liquidity incentives** - monitor which users qualify for LP rewards based on their orderbook presence
* **BBO tracking** - identify which users are providing best bid/offer
* **Market making** - track your queue position, monitor competitor liquidity
* **Analytics** - real-time orderbook metrics, spread analysis, depth tracking

### SDK

The [Hydromancer Python SDK](https://github.com/hydromancerxyz/hydromancer-sdk) handles sync automatically:

```python
from hydromancer_sdk import L4OrderbookClient

client = L4OrderbookClient(coins=["ETH", "BTC"])
await client.run()

book = client.get_book("ETH")
print(book.best_bid_str(), book.best_ask_str())
```

See [examples](https://github.com/hydromancerxyz/hydromancer-sdk/tree/main/examples) for streaming orderbook and validation usage. Use [meta](/readme/rest-api/metadata/meta.md) to call correct markets.

### Manual implementation

If implementing without the SDK, handle the three diff types from [l4BookUpdates](/readme/websocket/l4bookupdates.md):

* **new** - insert order into book at price level, store `oid`, `user`, `side`, `px`, `sz`
* **update** - find order by `oid`, update `sz` (partial fill occurred)
* **remove** - delete order by `oid` (filled or cancelled)

Maintain orders indexed by `oid` for fast lookups. Use sorted structures (e.g. sorted map) keyed by price for efficient BBO queries.

**Important:** Order matching is mandatory. When inserting new orders, run your matching engine to cross any orders that would trade (bid >= ask). Without matching, your book will become stale and show crossed markets.

### Key details

* Updates arrive per-block (\~70ms block time)
* Perp markets only (no spot)
* Includes all resting limit orders with user addresses
* Does not include untriggered stop/take-profit orders


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.hydromancer.xyz/readme/builder-guides/l4-orderbook-streaming.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
