Use Cases

Backtesting Prediction Market Strategies with Historical Order Books

Learn how to backtest trading strategies on Polymarket using historical order book data from PolyHistorical.

Why Backtest with Order Book Data?

Most backtesting uses trade or price data alone, but prediction market strategies depend on order book state — depth, spread, and liquidity. PolyHistorical provides sub-second order book snapshots that let you simulate realistic execution conditions.

Backtesting Workflow

StepActionTool
1. Data CollectionFetch historical snapshots via APIPolyHistorical API
2. Strategy DefinitionDefine entry/exit rules based on order book signalsPython / your language
3. SimulationReplay snapshots and simulate trades with realistic slippageCustom backtester
4. EvaluationCalculate Sharpe ratio, drawdown, win ratepandas / numpy
5. OptimizationWalk-forward optimization to avoid overfittingPolyHistorical + scipy

Example: Spread-Based Entry

import requests

API_KEY = "your_api_key"
slug = "btc-5m-up-down-2026-04-27-1200"
resp = requests.get(
    f"https://api.polyhistorical.com/v1/markets/{slug}/snapshots",
    headers={"X-API-Key": API_KEY},
    params={"include_orderbook": "true"}
)
snapshots = resp.json()["data"]
for snap in snapshots:
    spread = float(snap["price_up"]) + float(snap["price_down"]) - 1
    if spread > 0.05:
        print(f"Wide spread at {snap['time']}: {spread:.4f}")

Key Considerations

  • Slippage modeling: Use order book depth to estimate fill prices, not just midpoint
  • Transaction costs: Include gas fees and Polymarket trading fees
  • Look-ahead bias: Only use data available at the time of each simulated decision
  • Market impact: For larger positions, model how your order would consume depth

Related Resources