Strategies

Order book strategies

Trade on what rests in the book, not just the last price. Book reads, recorded history for backtests, maker quotes that rest post-only on the venue, and orders capped to the depth near the middle.

What a strategy can read#

On a perp (Hyperliquid, Lighter) or a Polymarket outcome, a strategy reads the book at each bar:

ReadMeans
book.bid, book.ask, book.midBest prices and the middle
book.bidSize, book.askSizeWhat rests at the best prices
book.microThe microprice: the middle leaning toward the thinner side
book.spreadThe spread, in bps of the middle
book.bid2 … book.ask5Levels 2 to 5, with sizes
book.bidDepth, book.askDepth, book.imbalanceUSD on the top 20 levels, and their balance (+1 all bids)
book.bids10, book.asks50, book.imbalance25…USD within 0.1%, 0.25%, 0.5% and 1% of the middle

History for backtests#

A book is recorded every 15 seconds once a market is in your Sources, a strategy runs on it, or you record it. Backtests read what was recorded; bars before recording began read na and never trade. A run on 5s or 15s bars has its book recorded every second, so it steps on each push.

Maker quotes#

perp.bid({ usd, at }) and perp.ask({ usd, at }) rest post-only orders at the price you give. They fill when a later bar trades through them (a touch is not a fill), at your price, paying the maker fee. One order rests per line; the next replaces it, and perp.cancel({}) pulls them all.

On a live project's wallets, a run on one perp rests its quotes on the venue itself as post-only orders.

while (quoting) { perp.cancel({}); }
while (quoting && perp.sizeUsd() < max) {
  perp.bid({ usd: size, at: min(book.bid(market, interval), close * 0.9999) });
}

Clamp the price to the bar's close: the book a bar reads can be a little older than its close, and a bid over the close would take rather than rest, so it reverts.

Taking only what's there#

maxImpactBps on perp.long, perp.short, perp.target and perp.hedge caps an order to what rests that near the middle. In a backtest it's cut to what the recorded book held; live it goes as an immediate-or-cancel limit that far from the live middle, so a thin book never fills you far away.

Following a book live#

Outside a strategy, the books stream sends a market's book at most once a second (best prices, sizes, microprice, depth bands and the top 10 levels). A workflow can trigger on it, a source can filter it, and the app's live book panel draws it. See Sources.