API
Plotting Polymarket order books in Jupyter
A depth chart shows more than a table. Plotting cumulative size against price turns a snapshot into a curve you can read at a glance, and a spread timeline shows how the book behaves as settlement approaches.
What this guide covers
- A depth chart plots cumulative size against price for bids and asks
- The mid and spread are readable directly from the two curves
- Spread over time shows the book tightening near settlement
- Empty snapshots must be filtered before plotting to avoid gaps and zero artifacts
Building a depth chart
To plot a depth curve, take one snapshot's bids and asks and accumulate size as you move away from the touch. Bids accumulate downward from the best bid, asks upward from the best ask, producing the classic two-sided step curve.
The vertical gap between the curves at the mid is the spread, and the horizontal span is the depth. Reading a chart beats scanning a ladder because shape - a wall of size, a thin tail - is immediately visible.
Spread over time
Plot the spread from the flattened frame against time and you can see the market's life cycle: wide when the window opens, tightening as it matures, sometimes spiking in the final seconds. Annotating the settlement time makes the pattern legible.
Filter out empty snapshots before plotting. A one-sided book has no valid mid, and plotting a fabricated zero would create a spike that never happened.
Keeping notebooks reproducible
Notebooks tempt you to fetch inline, which makes them slow and non-reproducible. Fetch once, save to Parquet or a cached file, and have the notebook read the local file. The notebook then reruns instantly and produces the same chart.
Pin the market slug and the time window in the first cell so a reader knows exactly what is plotted. A chart without its window is decoration, not analysis.
Comparing two markets side by side
Overlaying two markets on one axis - for example Bitcoin and Zcash depth curves - makes depth differences obvious in a way two tables do not. Plot both on the same price axis and the thinner book is immediately visible.
For spread timelines, plotting several assets with a shared legend shows which markets tighten and which stay wide. This is the chart that makes the case for or against trading a given asset.
Keep the slug and window for each series in the chart title or legend. A comparison chart without labels is not reproducible, and reproducibility is the point of doing the work in a notebook.
Code examples
import matplotlib.pyplot as plt
# bids/asks: best-first [price, size] for one snapshot.
def depth_curve(levels):
out, cum = [], 0.0
for price, size in levels:
cum += size
out.append((price, cum))
return [p for p, _ in out], [c for _, c in out]
bid_prices, bid_cum = depth_curve(bids)
ask_prices, ask_cum = depth_curve(asks)
fig, ax = plt.subplots(figsize=(7, 4))
ax.step(bid_prices, bid_cum, where='post', label='bids')
ax.step(ask_prices, ask_cum, where='post', label='asks')
ax.set_xlabel('price')
ax.set_ylabel('cumulative size')
ax.legend()
ax.set_title('Depth at one snapshot')Free tier
The Starter plan is free, includes order books, prices, and metrics at 250ms resolution, with 3 days of history, 60 requests/min, 1,000/day, 1 free AI backtest, and 3 strategy backtests. No credit card required.
Paid data windows from $19/mo extend history to 30–120 days at higher throughput. Backtest AI add-on is +$19/mo or standalone at $29/mo.
FAQ
What is a depth chart?
Cumulative size plotted against price for bids and asks. The vertical gap at the mid is the spread; the horizontal span is depth.
Why filter empty snapshots before plotting?
A one-sided book has no valid mid. Plotting a fabricated zero creates a spike that did not occur in the data.
Should a notebook fetch data live?
Ideally not. Fetch once, cache to a local file, and have the notebook read that file so it reruns quickly and reproducibly.
How do I show the settlement effect?
Plot spread against time and annotate the settlement timestamp; the book usually tightens as maturity approaches and can spike in the final seconds.