Backtesting
Polymarket backtesting
The Marketlens Python SDK backtests strategies against 604,843 recorded Polymarket markets in two modes: Execution replays real order books tick by tick with FIFO queue priority, latency, slippage, partial fills, and Polymarket fees, while Alpha replays one bar per market for signal research over long windows. This page is the overview; the full parameter reference lives in the docs and provider comparisons on compare.
Execution mode: order level realism
Subclass Strategy, implement hooks like on_book, on_trade, and on_fill, and the engine calls them on every replayed tick. Orders fill against the actual historical depth: limit orders queue with FIFO priority at their level, submissions arrive after a configurable latency, fills can be partial, slippage comes from the real book rather than a mid price assumption, and fees follow Polymarket's schedule. Resolved markets settle by the stored winning outcome (see resolution history), not the last print.
from marketlens import MarketLens, Strategy
class Dip(Strategy):
def on_book(self, ctx, market, book):
if book.midpoint < 0.35:
ctx.buy_yes(size=100)
client = MarketLens()
result = client.backtest(
Dip(),
"btc-up-or-down-5m",
initial_cash=10_000,
after="2026-07-01T00:00:00Z",
before="2026-07-01T06:00:00Z",
)
print(result.summary())The id argument accepts a market UUID, a series slug, or a list of either, so the same strategy runs on one 5 minute window or a whole chain from the data catalog.
Alpha mode: signal research at scale
Execution level replay is overkill when the question is whether a signal has edge at all. Alpha mode replays one bar per market and trades to target weights, which makes sweeping a signal across thousands of resolved markets fast enough to iterate on. Promote survivors to Execution mode to learn whether the edge survives queues, latency, and fees. Both modes share subtype filters, metrics, saved runs, and the dashboard; see the Alpha reference.
Offline runs and AI agents
Pass data_dir to backtest() and the first run downloads the window as Parquet, with every later run replaying from disk. Bulk exports feed the same path for fully offline iteration. For agent driven work, Marketlens ships an MCP server, so Claude Code, Claude Desktop, and Cursor can search markets, pull books, and run backtests directly.
How other backtesters compare
One line each, from the verified comparison table as of August 2026. depthfeed.com offers an in browser Backtest Lab with paper trading, over 7 to 90 days of history. polybacktest.com has an AI agent that writes strategy code, over a rolling 31 days of crypto data. polyhistorical.com offers strategy replay, with an AI backtest agent on higher tiers, over 300ms snapshots. pmdata.dev integrates with hftbacktest for crypto up/down markets. Marketlens runs Execution and Alpha modes with queue priority, latency, slippage, and fees, on every market type with the full archive since March 2026. Per vendor detail is linked from the comparison page.
FAQ
Common questions
How do I backtest a strategy on Polymarket?
Subclass Strategy from the Marketlens Python SDK (pip install marketlens), implement on_book or on_trade, and call client.backtest(strategy, "btc-up-or-down-5m"). The engine replays recorded L2 order books tick by tick and fills simulated orders against real historical depth. The archive covers 604,843 markets since March 2026.
Does the Polymarket backtester model queue priority and slippage?
Yes. Execution mode fills limit orders with FIFO queue position against the actual historical book, applies configurable latency and slippage, supports partial fills, and charges Polymarket accurate fees. Resolved markets settle by the stored winning outcome, not the last traded price.
What is the difference between Alpha and Execution backtests?
Execution mode replays every book update for order level realism, suited to execution sensitive strategies on short windows. Alpha mode replays one bar per market and trades to target weights, suited to signal research across thousands of markets over long windows. Both share the same data, metrics, and saved run dashboard.
Can I backtest Polymarket strategies offline?
Yes. Pass data_dir to backtest() and the first run downloads the window as Parquet, with subsequent runs replaying from disk. Bulk export results feed the same path, so fully offline iteration on a fixed dataset works without further API calls.
Can AI agents run Polymarket backtests?
Yes. Marketlens ships an MCP server, so agents like Claude Code, Claude Desktop, and Cursor can search markets, pull books, and write and run backtests directly. As of August 2026 it is the only Polymarket MCP server that runs backtests; other providers' MCP endpoints are read-only data tools.
What data does the backtest replay?
The same archive served by the API: full L2 snapshots with millisecond price changes between them, trades, and resolution outcomes for 604,843 markets across 644 recurring series, crypto, sports, weather, and macro alike.
Try it
Run your first backtest in two minutes
The free tier includes 5M events per day with full API and full archive access, no card required.
$ pip install marketlens