Backtesting with Fivesight
Backtesting allows you to simulate trading strategies against historical Polymarket data using a Virtual Clock.
Prerequisites
- A running
fivesight-backend. fivesight-clob-clientinstalled.
Step-by-Step Guide
1. Initialize Client & Session
python
from fivesight_clob_client.client import ClobClient
from datetime import datetime, timedelta
client = ClobClient(
host="https://api.fivesight.ai/api/simulation",
key="dummy",
chain_id=137,
simulation_mode=True
)
# Create 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 will return the state of the market (Order Book) as it was at that exact timestamp.
python
start_time = datetime(2023, 12, 28, 12, 0, 0)
client.set_simulation_clock(start_time.isoformat())3. Fetch Historical Data
When you call get_order_book, the backend fetches the snapshot closest to your Virtual Clock.
python
token_id = "1234..." # Valid Token ID
book = client.get_order_book(token_id)
print(book)4. Backtest Loop
python
current_time = start_time
for _ in range(60): # Simulate 1 hour
# 1. Update Clock
client.set_simulation_clock(current_time.isoformat())
# 2. Get Data
book = client.get_order_book(token_id)
# 3. Execute Strategy
# ... logic to buy/sell ...
# client.create_and_post_order(...)
# 4. Advance Time
current_time += timedelta(minutes=1)Supported Features
- Historical Order Book: Fetched from secondary
pm_databackend. - Immediate Matching: Orders crossing the spread are executed immediately against the historical snapshot.
- Position Tracking: Your
SimulatedPositionandSimulatedWalletare updated automatically.