Guide

Backtesting strategies on Polymarket order books

Backtesting on Polymarket order books lets you replay historical markets and simulate execution against real L2 data. This guide covers methodology, fill simulation, slippage modeling, and walk-forward analysis.

What this guide covers

  • Replay historical L2 books to simulate realistic execution
  • Fill simulation walks the order book to compute actual fill prices
  • Slippage modeling is essential for realistic performance estimates
  • Walk-forward analysis avoids overfitting to historical data

Backtesting methodology

A backtest replays historical data and simulates trading decisions as if they were made in real time. The key constraint is that you can only use information available at the time of each decision.

For Polymarket order books, this means using the L2 snapshot at each timestamp to make trading decisions and simulate execution.

The backtest loop is: receive snapshot, generate signal, simulate execution, record fill price, advance to next snapshot. Repeat for the full market lifecycle.

The output is a series of simulated trades with entry prices, exit prices, and P&L. Account for spread cost, slippage, and any trading fees.

Fill simulation

Fill simulation determines whether your order would have been executed and at what price. Walk through the order book levels from best to worst, accumulating size.

For a buy order: start at the best ask, subtract available size from your order, move to the next level, and repeat until the order is filled. The VWAP of your fills is your execution price.

If the order cannot be fully filled (not enough depth), you have two options: assume partial fill or skip the trade. Partial fills are more realistic.

Account for the spread in fill simulation. If your signal says buy at 0.65 but the best ask is 0.67, your execution starts at 0.67, not 0.65.

Walk-forward analysis

Walk-forward analysis splits your historical data into in-sample (training) and out-of-sample (testing) periods. You optimize parameters on in-sample data and evaluate on out-of-sample data.

Roll the window forward: optimize on period 1, test on period 2; optimize on period 2, test on period 3; and so on.

Walk-forward analysis reveals whether your strategy generalizes or overfits. A strategy that performs well in-sample but poorly out-of-sample is overfit.

For Polymarket markets, use resolved markets for walk-forward testing. You know the outcome, so you can evaluate correctness.

Common pitfalls

Ignoring slippage: a strategy that looks profitable on midpoint data may be unprofitable after accounting for spread and slippage.

Overfitting: optimizing parameters to fit historical data perfectly often produces a strategy that fails on new data.

Look-ahead bias: using information that was not available at the time of the trading decision. Ensure your signal only uses the current and past L2 snapshots.

Survivorship bias: testing only on markets that exist today. Use the full market history from the /markets endpoint.

Code examples

def simulate_fill(book, order_size, side="buy"):
    levels = book["asks"] if side == "buy" else book["bids"]
    remaining = order_size
    total_cost = 0
    for level in levels:
        fill = min(remaining, level["size"])
        total_cost += fill * level["price"]
        remaining -= fill
        if remaining <= 0:
            break
    if remaining > 0:
        return None
    return total_cost / order_size

Free tier

The Starter plan is free, includes order books, prices, and metrics at 1-second 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

How many markets do I need for a reliable backtest?

At least 20 to 50 markets of the same type (e.g., BTC 5-minute) to get statistical significance.

Should I use 1s or 5s resolution for backtesting?

1s resolution for the most accurate fill simulation. 5s is acceptable for strategies that do not depend on sub-second timing.

How do I avoid overfitting?

Use walk-forward analysis, keep parameter counts low (2 to 3 at most), test on out-of-sample data, and validate on markets not used in optimization.

What P&L should I expect from a good strategy?

After accounting for spread and slippage, even modest positive P&L is good. Watch for strategies that are only profitable when ignoring execution costs.