Rebuild order books
Replay book_delta rows into a full Polymarket order book at any moment in Python, carry it across hours and check it against the quotes channel.
API key. Pick a plan on Pricing and create a key, then install the SDK for your language: the Go SDK, Python SDK, Rust SDK or TypeScript SDK.
book_delta holds every change to every price level of a book. Replay its rows in order and you have the full book at any moment. This guide loads an hour of one Polymarket market, rebuilds its book, prints the top of book at a chosen time and checks the result against the quotes channel.
How the rows fit together
Each row is one changed price level of one instrument: side (bid or ask), price and new_size. new_size is the size now resting at that price, not the change in size: set the level to it, and remove the level when it is 0.
Files cover one UTC hour each, by recv_ts. Within a file, rows are sorted by instrument_id, then recv_ts. Rows with the same recv_ts stay in the order they were processed, so apply them in file order.
The first time an instrument's book changes in an hour, the book just before that change comes first, as rows with is_snapshot true. They are stamped with the start of the hour in recv_ts, with no exch_ts or seq. The changes follow. For example, a market that first changes at 14:07 might start like this:
recv_ts | side | price | new_size | is_snapshot |
|---|---|---|---|---|
| 14:00:00.000000 | bid | 0.45 | 1200 | true |
| 14:00:00.000000 | bid | 0.44 | 800 | true |
| 14:00:00.000000 | ask | 0.47 | 950 | true |
| 14:07:12.481203 | bid | 0.46 | 300 | false |
| 14:07:15.002914 | ask | 0.47 | 0 | false |
A book that appears for the first time is written whole as snapshot rows at the time it arrived, not at the start of the hour. Either way, every hour can be replayed on its own: start from the snapshot rows and apply the rest.
Two cases have no snapshot rows. If an instrument's book does not change in an hour, the hour has no rows for it; its book is the one from the last hour that has rows. If the book was empty before its first change, there is nothing to restate, and the replay starts from an empty book.
Load an hour of one market
Book files are large, so pass instruments and the SDK reads only the parts of the file that hold that instrument. Polymarket books are kept for the first outcome of each market. decimals="exact" keeps prices and sizes as decimal.Decimal instead of float64:
import pandas as pd
from predbooks import Client
pb = Client("pb_...")
market = pb.markets("polymarket", q="bitcoin")[0]
token = market["instrument_ids"][0]
deltas = pb.load("polymarket", "book_delta", "2026-12-05",
hours=[14], instruments=[token], decimals="exact")If deltas is empty, the file has no rows for that instrument in that hour: its book did not change, or it was not recorded then. Pick another hour or another market.
Replay the rows
The book is two dicts, price to size. A new block of snapshot rows restates the whole book, so the replay clears both sides when one starts:
class Book:
"""One instrument's book from book_delta rows: price -> size on each side."""
def __init__(self):
self.bids, self.asks = {}, {}
self.snap_at = None
def apply(self, r):
if r.is_snapshot and r.recv_ts != self.snap_at:
# the first row of a snapshot: it restates the whole book
self.bids.clear()
self.asks.clear()
self.snap_at = r.recv_ts if r.is_snapshot else None
levels = self.bids if r.side == "bid" else self.asks
if r.new_size == 0:
levels.pop(r.price, None)
else:
levels[r.price] = r.new_size
def top(self):
"""(bid price, bid size, ask price, ask size); None for an empty side."""
bid = max(self.bids, default=None)
ask = min(self.asks, default=None)
return bid, self.bids.get(bid), ask, self.asks.get(ask)The book at a chosen time
Apply every row received up to that time. Timestamps are UTC, so compare with a UTC timestamp:
at = pd.Timestamp("2026-12-05 14:30:00", tz="UTC")
book = Book()
for r in deltas[deltas.recv_ts <= at].itertuples(index=False):
book.apply(r)
bid_px, bid_sz, ask_px, ask_sz = book.top()
print(f"bid {bid_sz} @ {bid_px} ask {ask_sz} @ {ask_px}")
for price, size in sorted(book.bids.items(), reverse=True)[:5]:
print("bid", price, size)
for price, size in sorted(book.asks.items())[:5]:
print("ask", price, size)Filtering a DataFrame keeps its row order. If you sort the rows yourself, for example after combining files, sort only by recv_ts and use a stable sort (deltas.sort_values("recv_ts", kind="stable")): pandas' default sort can reorder rows with the same recv_ts, and two changes to one level in the same microsecond would then apply in the wrong order.
Check against quotes
quotes is written from the same book state as book_delta: a row each time the best bid or ask changes in price or size, stamped with the recv_ts of the change that caused it. So the top of your rebuilt book should equal the latest quote at every moment.
Several quote rows can share one recv_ts when several messages arrived in the same microsecond. Compare the last of them with the book after all delta rows up to that time:
from bisect import bisect_right
quotes = pb.load("polymarket", "quotes", "2026-12-05",
hours=[14], instruments=[token], decimals="exact")
book = Book()
after = {} # recv_ts -> top of book after the last delta row at that time
for r in deltas.itertuples(index=False):
book.apply(r)
after[r.recv_ts] = book.top()
times = list(after) # already in recv_ts order
last = quotes.drop_duplicates("recv_ts", keep="last")
bad = 0
for q in last.itertuples(index=False):
i = bisect_right(times, q.recv_ts) - 1
want = after[times[i]] if i >= 0 else (None, None, None, None)
if want != (q.bid_px, q.bid_sz, q.ask_px, q.ask_sz):
bad += 1
print(f"{bad} of {len(last)} quote times differ")Load quotes for the same hours as deltas; the same check works over several hours. Don't check against book_10 this way: it has at most one row per instrument and second, the last state in that second, so it skips changes.
Carry the book across hours
Load several hours of one instrument and replay them with the same Book:
deltas = pb.load("polymarket", "book_delta", "2026-12-05",
hours=[14, 15], instruments=[token], decimals="exact")
deltas = deltas.sort_values("recv_ts", kind="stable") # hours in order, ties as they wereAt hour 15 the snapshot rows restate the book, and Book.apply replaces its state with them instead of merging. For a market that changes in both hours, that restated book is the one your replay held at the end of hour 14. An hour flagged cold_start or cold_changes was processed without part of the book state from before; pb.files("polymarket", "book_delta", ...) lists each file's flags, and Data quality explains them.
The same structure means you can start at any hour: the first rows of each instrument in it are its book. A market with no rows in that hour did not change in it, so look back to the last hour that has rows for it.
Decimals
With decimals="exact", prices and sizes are decimal.Decimal, exactly as stored, and work as dict keys and in comparisons. Converting them to Python objects is slow, which matters little for one market. The default, decimals="float", turns them into float64: each price still maps to one float, so the replay works, but values such as 0.1 are no longer exact and sums of sizes can pick up rounding. Convert to float deliberately, after the replay, if you need it.
Other venues
- Predict.fun and Limitless have
book_deltawith the same layout; the replay above works unchanged. - Kalshi books are snapshots taken by polling (active markets every 10 to 60 seconds, quoted-only markets every 15 minutes), not a stream of every change. Each poll's differences from the last become
book_deltarows that share the poll'srecv_ts, and changes that came and went between polls are not seen. Kalshi gives no exchange timestamp for books, soexch_tsis empty. The instrument is the market's YES contract: NO bids appear as YES asks at1 - price. - Hyperliquid HIP-4 and Polymarket Perps have no
book_delta: their venues send capped books (20 levels a side on Hyperliquid, the top 50 on Perps), not level changes. Usebook_10for depth andquotesfor every change of the best bid and ask.
Order book history on every venue starts on December 5, 2026. See Schemas for the columns and the Glossary for the terms.