How do I rebuild a prediction market order book from snapshots and updates?
Scope the rebuild to Kalshi Predictions event contracts. Store YES and NO bid ladders, replace the book on each snapshot, apply deltas in seq order, and fetch a fresh snapshot after any gap or reconnect. Derive asks by complement, then validate depth, spread, and epoch before trading. Polymarket CLOB needs its own adapter.
Scope the book and message contract
For Kalshi Predictions event contracts, model the book as two price-keyed bid ladders, yes_dollars and no_dollars, not as a conventional bid/ask book. The REST snapshot returns fixed-point string pairs and sorts prices ascending, so the last level is the best bid. Because Kalshi publishes bids only, derive YES asks from NO bids and NO asks from YES bids using the $1.00 complement. Do not apply this schema to Kalshi Margin/perps or Polymarket without a separate adapter.
Polymarket's CLOB exposes a separate public market WebSocket for order-book updates, so a multi-venue replay fixture needs venue-specific decoders and sequence rules. Kalshi's changelog covers Predictions and Margin exchanges separately; the orderbook_delta rules below are scoped to the Predictions event-contract channel unless the Margin documentation is checked. Store the venue, channel, market ticker, connection epoch, and subscription interval with every frame.
Apply snapshots, deltas, and sequence rules
Treat every orderbook snapshot as a replacement for that market's local book, not an incremental patch. Apply deltas in the subscription's seq order and delete a level when its count reaches zero. If update_interval_ms is 100 or 1000, Kalshi sends one net held delta per changed price level per window; zero-net levels are skipped, and your own order changes bypass the interval. Do not use clock as a global ordering key: it can decrease and cannot combine independently conflated streams.
Sequence gaps are recovery events. Kalshi documents two paths: call get_snapshot for all subscribed markets, or unsubscribe, wait for the unsubscribed acknowledgement, then subscribe again. A repeated subscribe adds markets to the same sid and does not resynchronize existing markets; use update_subscription with add_markets to extend, or unsubscribe/resubscribe to change interval. Mark the local book stale the moment a gap is detected, and block quoting until a replacement snapshot has been applied.
Sources: [2]
Reconnect and resynchronize deterministically
On reconnect, start a new connection epoch and rebuild from that connection's snapshot. Do not carry seq state across connections. Clock may match a market's updates across redundant connections, but it is not a delta order and cannot combine independently conflated streams. If a socket closes, resubscribe and fetch a snapshot; if the gap is isolated, get_snapshot may be enough. The replay fixture should include clean reconnect, gap-with-snapshot, unsubscribe/resubscribe, and interval-mismatch cases. Each case should fail closed if the local book remains stale.
Synthetic example: snapshot YES bids 0.40 x10, 0.42 x5; NO bids 0.50 x7, 0.55 x20. Best YES bid is 0.42; best YES ask is 1 - 0.55 = 0.45; spread 0.03. Delta seq 101 adds 3 at YES 0.42, giving 8. Delta seq 102 removes 5 at NO 0.55, leaving 15, so the ask stays 0.45. If NO 0.55 were fully removed, the next NO bid 0.50 would imply a 0.50 YES ask and 0.08 spread.
Validate derived asks and separate book state from exposure
After every replay step, assert no negative counts, ascending price order, monotonic seq within an epoch, and a single snapshot epoch per market. Recompute best bids and derived asks from the ladders rather than caching a top-of-book that can diverge. A crossed book can appear temporarily because public levels can lag your own orders; treat that as a validation flag, not permission to trade.
Do not infer fills, positions, locked capital, or settlement exposure from book snapshots; this fixture reconstructs market state only. Reconcile any capital or exposure ledger against separate fill and position records outside the replay. For Polymarket, use the CLOB market channel's own schema; do not transplant Kalshi seq or complement rules.
| Fixture case | Local action | Pass condition |
|---|---|---|
| Initial snapshot | Replace YES/NO ladders; record epoch and seq | Best YES ask equals 1.00 minus best NO bid; no stale levels |
| In-sequence delta | Add/subtract count at price; delete zero levels | seq increases; counts never negative; spread recomputes |
| Gap detected | Mark stale; get_snapshot or unsubscribe/resubscribe | No old-epoch delta applied after replacement snapshot |
| Reconnect or interval change | New epoch; rebuild from new snapshot; set interval | Subscription interval matches decoder; old seq discarded |
Assumptions and scope
- Synthetic prices and sizes in the worked example are hypothetical, not market data.
- Scope is Kalshi Predictions event contracts; Margin/perps and Polymarket US require separate schema checks.
- Assumes a snapshot can be associated with a seq or epoch; if not, treat receipt order as provisional and resubscribe.
- Clock is not used as a global order key; it only matches redundant streams when their scopes are otherwise identical.
Sources and verification
- Kalshi order book conventions
Source checked Nov 10, 2026. - Kalshi API changelog
Source checked Nov 10, 2026. - Polymarket API surfaces
Source checked Nov 10, 2026.
Prepared by Skybit Research with AI assistance and automated source checks. Examples and assumptions are identified in the article. No human reviewer is implied. Check the exact contract and current venue documentation before applying a method.