Skip to content

Backtesting with Fivesight

Backtesting allows you to simulate trading strategies against historical Polymarket data using a Virtual Clock.

Concept: The Virtual Clock

Unlike Paper Trading which follows live time, Backtest Mode allows you to control time.

  • You set the simulation_clock.
  • All data fetches (get_order_book, get_trades) return data as it existed at that virtual time.
  • Orders are matched against the historical snapshot.

Step-by-Step Guide

1. Initialize Client & Session

python
from fivesight_clob_client.client import ClobClient
from datetime import datetime, timedelta

client = ClobClient(
    env="prod",
    key="YOUR_API_KEY",
    simulation_mode=True
)

# Start a fresh BACKTEST session
client.create_simulation_session(mode="BACKTEST", initial_balance=50000)

2. Set the Virtual Clock

You must manually advance the clock. The simulation engine handles the rest.

python
start_time = datetime(2023, 12, 28, 12, 0, 0)
client.set_simulation_clock(start_time.isoformat())

3. Backtest Loop

Run your strategy in a loop, advancing time and executing logic.

python
current_time = start_time
token_id = "YOUR_TOKEN_ID"

for _ in range(60): # Simulate 1 hour
    # 1. Update Clock
    client.set_simulation_clock(current_time.isoformat())
    
    # 2. Get Data (Returns historical snapshot)
    book = client.get_order_book(token_id)
    
    # 3. Execute Strategy
    if book.bids[0].price < 0.40:
         client.create_and_post_order(...)
    
    # 4. Advance Time
    current_time += timedelta(minutes=1)

Features

  • Historical Accuracy: Uses high-fidelity Tick Data sourced from Fivesight's archive.
  • Immediate Matching: Orders crossing the spread are executed immediately against the historical snapshot.
  • Position Tracking: Your SimulatedPosition and SimulatedWallet are automatically updated.

Released under the MIT License.