A FastAPI service that runs vectorized backtests over daily OHLCV data and returns risk-adjusted performance metrics.
Submit a strategy config — symbol, date range, SMA parameters, commission, and slippage — and get back Sharpe, Sortino, max drawdown, CAGR, the full equity curve, and every executed trade. Results are persisted in Postgres so you can retrieve or compare runs later.
This is the initial commit. The module structure is in place:
app/metrics.py— Sharpe, Sortino, max drawdown, CAGR (fully implemented, pure numpy)app/backtester.py— vectorized SMA-crossover backtester with slippage and commissionapp/schemas.py— Pydantic request/response modelsapp/models.py— SQLAlchemy ORM (BacktestRun, Trade, EquityCurvePoint)app/database.py— async SQLAlchemy engine and session factoryapp/crud.py— DB operations (create run, persist trades, fetch results)app/routers/backtest.py— POST /backtests, GET /backtests/{id}, GET /backtestsapp/main.py— FastAPI app with lifespan startup and /health endpoint
Tests, Alembic migrations, CI, and Fly.io deployment config come in later commits.
You need a running Postgres instance. Set DATABASE_URL in a .env file:
DATABASE_URL=postgresql+asyncpg://user:pass@localhost/quantlab
Install dependencies and start the server:
pip install -r requirements.txt
uvicorn app.main:app --reloadThe API docs are at http://localhost:8000/docs.
Submit a strategy and run a backtest.
Request body:
{
"symbol": "SPY",
"start_date": "2020-01-01",
"end_date": "2023-12-31",
"strategy_type": "sma_crossover",
"strategy_params": { "fast_window": 20, "slow_window": 50 },
"initial_capital": 100000,
"commission_bps": 1.0,
"slippage_model": "fixed_bps",
"slippage_value": 0.5
}Response: BacktestResult with run_id, metrics, trades, and equity_curve.
Retrieve a completed run by UUID.
List recent runs. Query params: limit (default 20) and offset.
OHLCV data is loaded from CSV files in data/<SYMBOL>.csv. Each file needs columns: date, open, high, low, close, volume. The date column should be in ISO 8601 format.
Live market data provider support is planned for a later commit.
docker build -t quantlab .
docker run -p 8000:8000 -e DATABASE_URL=postgresql+asyncpg://user:pass@host/quantlab quantlab