Polymarket Paper Trading & py-clob-client L3 Backtesting in Python
Most open-source Polymarket paper-trading scripts check 15-minute midpoint candles or assume a resting limit order at $0.52 fills the instant a single trade prints at $0.52. On the live Polymarket Central Limit Order Book (CLOB), binary YES/NO outcome shares sit behind tens of thousands of contracts of existing FIFO queue depth — and during FOMC, CPI, or breaking-news shocks, informed takers sweep stale maker quotes while your HTTP DELETE /cancel-all is still in flight.
1. Boot a Local Polymarket Shadow-CLOB in 10 Seconds
Fera Terra ships a prebuilt C++23 Price-Time FIFO matching engine (arena-server) inside standard Python wheels for Linux, macOS (Apple Silicon & Intel), and Windows:
pip install feraterra feraterra doctor # Mirror live Polymarket binary order books locally on 127.0.0.1:8085 (Diagnostics UI on :8090) feraterra up --venue polymarket --auto-top-markets 4 --cancel-latency-ms 140
2. Point Unmodified py-clob-client at 127.0.0.1:8085
Your strategy code requires zero rewrites and zero live wallet private keys — point ClobClient at http://127.0.0.1:8085:
from py_clob_client.client import ClobClient
client = ClobClient(host="http://127.0.0.1:8085", chain_id=137)
markets = client.get_markets()
book = client.get_order_book(markets["data"][0]["tokens"][0]["token_id"])
print("Local Shadow-CLOB Top Bid/Ask:", book.bids[:1], book.asks[:1])
3. Conditional Inventory Enforcement & News-Shock Replay
Unlike perpetual futures, Polymarket outcome tokens cannot be naked-shorted without splitting USDC collateral via the Conditional Token Framework (/ctf/split & /ctf/merge). Fera Terra enforces real conditional share inventory by default and runs a parallel Naive Paper Tracker alongside the C++23 L3 FIFO engine:
# Record a live Polymarket market from your own IP, then replay deterministically in <1s: feraterra record --venue polymarket --duration-sec 600 --out fed_market.trace.jsonl feraterra replay --trace fed_market.trace.jsonl --bot my_polymarket_mm.py --html-out report.html
On the built-in polymarket_news_shock benchmark (140 ms cancel latency), a naive touch = fill paper trader reports +$154.05 across 38 fills, whereas true C++23 L3 FIFO queue priority + cancel lag realizes -$244.67 across 24 fills — a $398.72 Backtest Illusion Gap. Clone the runnable starter at FeraTerra/polymarket-paper-trading.