Historical trading simulations with forward PnL analysis. Each script simulates a multi-leg trade at a specific historical date and shows what the PnL would have been at +1, +2, +4, and +7 days.
| Script | Date | Strategy | Assets | Outcome |
|---|---|---|---|---|
covid_crash_hedge.py |
2020-02-21 | Flight-to-safety | Long GOLD + Short SP500 | SP500 fell 12% in 7 days; short leg dominated |
ftx_crypto_contagion.py |
2022-11-08 | Crypto contagion hedge | Short BTC + Short ETH + Long GOLD | BTC -15%, ETH -18%; crypto shorts drove profit |
nvda_earnings_alpha.py |
2023-05-25 | Sector alpha pair | Long NVDA + Short SP500 | NVDA +5% while SP500 flat; pure alpha |
ukraine_oil_shock.py |
2022-02-24 | Commodity supply shock | Long OIL + Long GOLD + Short SP500 | Oil surged 16% in 7 days on sanctions |
No API keys or wallet configuration needed. Just build the backtest binary:
cargo build --release# Run any scenario
python3 examples/backtest/covid_crash_hedge.py
python3 examples/backtest/ftx_crypto_contagion.py
python3 examples/backtest/nvda_earnings_alpha.py
python3 examples/backtest/ukraine_oil_shock.py
# Override binary path
python3 examples/backtest/covid_crash_hedge.py --backtest /path/to/backtestEach script:
- Resets the portfolio to a clean state
- Fetches historical prices at the scenario date
- Executes simulated trades (spot buy/sell)
- Displays a PnL table at +1, +2, +4, +7 days for each leg
- Shows the final portfolio summary (cash balance, positions)
- Cleans up (resets portfolio)
The backtest CLI fetches historical OHLCV data from Yahoo Finance (with CoinGecko fallback for crypto) and computes forward PnL by looking up actual prices at future dates.
- Auto-pricing: If
--priceis omitted, the historical close price is used - Portfolio state: Trades are tracked in
~/.fintool/backtest_portfolio.json - Cash balance: Buying subtracts cost, selling adds proceeds (spot trades only)
- Perp trades: Use
perp_buy/perp_sellwith leverage for leveraged PnL
- Python 3.10+ (no third-party packages — uses only stdlib)
- backtest CLI binary (compiled from this repo)
Create a new Python script following this pattern:
#!/usr/bin/env python3
import json, os, subprocess
from pathlib import Path
REPO_DIR = Path(__file__).resolve().parent.parent.parent
BT = os.environ.get("BACKTEST", str(REPO_DIR / "target" / "release" / "backtest"))
DATE = "2024-01-15"
def cli(cmd, date=DATE):
r = subprocess.run([BT, "--at", date, "--json", json.dumps(cmd)],
capture_output=True, text=True, timeout=30)
return json.loads(r.stdout)
# Reset
cli({"command": "reset"})
# Quote
btc = cli({"command": "quote", "symbol": "BTC"})
print(f"BTC on {DATE}: ${btc['price']}")
# Trade
result = cli({"command": "buy", "symbol": "BTC", "amount": 0.01})
for p in result["pnl"]:
print(f" {p['offset']}: ${p['price']} (PnL: {p['pnl']}, {p['pnlPct']}%)")
# Cleanup
cli({"command": "reset"})Supported symbols include all major crypto (BTC, ETH, SOL, ...), stocks (AAPL, NVDA, TSLA, ...), commodities (GOLD, SILVER, OIL), and indices (SP500, NASDAQ, DOW).