Skip to content

About

FastAPI service for vectorized backtests, risk metrics, and persisted trade results.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

quantlab

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.

What exists right now

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 commission
  • app/schemas.py — Pydantic request/response models
  • app/models.py — SQLAlchemy ORM (BacktestRun, Trade, EquityCurvePoint)
  • app/database.py — async SQLAlchemy engine and session factory
  • app/crud.py — DB operations (create run, persist trades, fetch results)
  • app/routers/backtest.py — POST /backtests, GET /backtests/{id}, GET /backtests
  • app/main.py — FastAPI app with lifespan startup and /health endpoint

Tests, Alembic migrations, CI, and Fly.io deployment config come in later commits.

Running locally

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 --reload

The API docs are at http://localhost:8000/docs.

API surface

POST /backtests

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.

GET /backtests/{run_id}

Retrieve a completed run by UUID.

GET /backtests

List recent runs. Query params: limit (default 20) and offset.

Data

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.

Running with Docker

docker build -t quantlab .
docker run -p 8000:8000 -e DATABASE_URL=postgresql+asyncpg://user:pass@host/quantlab quantlab

About

FastAPI service for vectorized backtests, risk metrics, and persisted trade results.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages