Add local backtest snapshot runner
Deploy / lint (push) Successful in 8s
Deploy / test (push) Successful in 1m22s
Deploy / deploy (push) Successful in 43s

This commit is contained in:
2026-07-03 18:34:07 +02:00
parent 14327ab25a
commit 66ef0564c1
5 changed files with 380 additions and 12 deletions
+44
View File
@@ -247,6 +247,50 @@ cd frontend
npm run build
```
### Local Backtest Snapshots
For research loops, run the production backtest locally from a SQLite snapshot
instead of deploying and clicking the Admin job. The snapshot contains only the
tables needed by `run_backtest`: tickers, OHLCV bars, SPY benchmark closes, and
activation/recommendation settings. Secrets and cached reports are not copied.
1. Open an SSH tunnel to the production Postgres instance:
```bash
ssh -N -L 15432:127.0.0.1:5432 deploy@your-server
```
2. In another terminal, create or refresh the snapshot:
```bash
# macOS/Linux
python scripts/create_backtest_snapshot.py \
--database-url "postgresql+asyncpg://stock_backend:PASSWORD@127.0.0.1:15432/stock_data_backend" \
--output backtest_snapshots/prod.sqlite \
--force
# Windows PowerShell
.venv\Scripts\python.exe scripts\create_backtest_snapshot.py `
--database-url "postgresql+asyncpg://stock_backend:PASSWORD@127.0.0.1:15432/stock_data_backend" `
--output backtest_snapshots\prod.sqlite `
--force
```
3. Run the backtest fully offline from the snapshot:
```bash
# macOS/Linux
python scripts/run_backtest_snapshot.py backtest_snapshots/prod.sqlite --workers 8
# Windows PowerShell
.venv\Scripts\python.exe scripts\run_backtest_snapshot.py backtest_snapshots\prod.sqlite --workers 12 --allow-spawn
```
The runner writes `reports/backtest-<timestamp>.json` and prints the headline
metrics. Keep the SSH tunnel open only while creating the snapshot; the backtest
run itself is local/offline. `backtest_snapshots/` and generated backtest reports
are git-ignored.
## Environment Variables
Configure in `.env` (copy from `.env.example`):