Skip to main content
Replay historical prediction market data through the exact same pipeline you use in live trading. Get equity curves, trade logs, Sharpe ratios, Brier scores, and CSV exports from a single function call.

Full Code

How It Works

The backtest engine replays your data through the same pipeline that hz.run() uses in live trading:
  1. Data normalization: Your input (dicts, CSV, or DataFrame) is converted into a chronological timeline of Tick objects with timestamp, price, bid, ask, and volume fields.
  2. Timeline construction: All ticks across all feeds are merged into a single sorted timeline. Each timestamp carries forward the latest state of every feed (carry-forward interpolation).
  3. Pipeline execution: At each tick, the engine builds a Context with current feed data and inventory, then runs your pipeline functions in order. The output quotes are submitted to the internal paper exchange.
  4. Paper matching: The paper exchange matches resting orders against the current feed price. Fills update positions and P&L.
  5. Metrics computation: After all ticks are processed, BacktestResult lazily computes Sharpe, Sortino, Calmar, drawdown, win rate, profit factor, and prediction-market-specific metrics like Brier score.
Rate limits and dedup windows are automatically relaxed during backtests for maximum throughput. The risk pipeline (position limits, drawdown, etc.) still runs normally.

Data Formats

hz.backtest() accepts four input formats for the data parameter:

list[dict]

The simplest format. Each dict must have a timestamp field and at least one of price or bid:
If only price is provided, bid and ask are set equal to price. If only bid and ask are provided, price is derived as the midpoint.

CSV file path

Pass a string path to a CSV file with a header row:
Expected CSV columns: timestamp, price, and optionally bid, ask, volume.

pandas DataFrame

Pass a DataFrame directly, no conversion needed:

dict[str, data] for multi-feed

Map feed names to their data sources for strategies that consume multiple feeds:

Interpreting Results

The result.summary() output contains three sections:

Returns

Risk

Trades

Prediction Market Metrics

Multi-Feed Backtesting

Test strategies that consume multiple data sources, such as a BTC-priced prediction market:
At each timestamp in the merged timeline, both feeds carry forward their latest values, so the btc feed updates even when book has no new data at that timestamp and vice versa.

CSV Input Example

With Outcomes for Brier Score

Pass known outcomes to compute Brier score and average edge. This is essential for evaluating your probability calibration:
A Brier score below 0.25 means your model forecasts better than a coin flip. Below 0.10 is considered excellent calibration for prediction markets.

Run It