This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Multi-factor quantitative equity trading system for medium-term US equities. Uses 5 price-based alpha factors with mean-variance portfolio optimization, dynamic leverage via market regime detection, and automated execution through Alpaca API.
# Install dependencies
pip install -r requirements.txt
# Run full backtest (5-year default)
python run.py backtest --start 2021-03-16 --plot
# View current alpha signals
python run.py signal
# Run tests
pytest tests/
pytest tests/test_factors.py -v # single module
pytest --cov=quant tests/ # with coverage
# Paper trading (Alpaca)
python paper_trade.py --dry-run # preview trades
python paper_trade.py # execute rebalance
python paper_trade.py --status # check status
python paper_trade.py --force # force immediate rebalance
python paper_trade.py --reconcile # post-trade check
# Dashboard generation
python generate_site.py # outputs JSON to site/data/
# Verbose logging
python run.py -v backtest --start 2021-03-16run.py / paper_trade.py ← Entry points
│
quant/strategy.py ← MultiFactorStrategy orchestrator
│
┌────┼────────────┬──────────────┐
│ │ │ │
data/ signals/ portfolio/ execution/ backtest/
quant/strategy.py— Orchestrates everything:run_backtest(),get_current_signal(),get_current_portfolio()quant/data/market_data.py— yfinance wrapper for OHLCV and fundamentalsquant/signals/factors.py— Alpha factor calculations (momentum, 52-week high, reversal, vol contraction, volume momentum). Factors are industry-neutralized and winsorized to ±3σquant/portfolio/optimizer.py— Constrained MVO with Ledoit-Wolf shrinkage covariance. Includesdetect_regime()(SPY vol-based) andapply_vol_scaling()for dynamic leverage (0.8x–1.8x)quant/execution/broker.py—PaperBrokerfor simulation;quant/execution/alpaca_broker.pyfor livequant/execution/safety.py—PreTradeCheck: order size limits ($50k), daily limits ($500k), liquidity checks (1% ADV), loss limits ($25k/day)quant/backtest/engine.py— Event-driven daily simulator with Almgren-Chriss market impact modelconfig.yaml— All strategy parameters: universe (100 stocks), factor weights, portfolio constraints, risk limits, safety thresholds
Raw prices → factor scores (momentum 50%, high_proximity 20%, reversal 10%, vol_contraction 10%, volume_momentum 10%) → industry-neutral z-scores → trend filter (< 200d SMA → 0.5x) → blowoff filter (z > 4.0 → 0.5x) → composite score → MVO optimization → target weights
- Max 12 positions, 3%–12% per stock, max 50% per sector
- Rebalance every 21 trading days, max 40% turnover
- Transaction cost: 10 bps + Almgren-Chriss market impact
- Quality and value factors are disabled — yfinance only provides current snapshots, causing look-ahead bias in backtests
Tests use synthetic fixtures (3 years of 10 stocks + benchmark) defined in tests/conftest.py — fully offline, no API calls needed. Test modules cover factors, portfolio optimization, backtesting, broker, safety checks, and strategy-vs-baseline comparison.
setup_alpaca.sh— SetsALPACA_API_KEYandALPACA_SECRET_KEYenv varssetup_cron.sh— Installs cron job at 3:55 PM ET weekdays (before market close)- Logs go to
logs/paper_trade_YYYYMMDD.log; state tracked inlogs/paper_trade_state.json logs/paper_trade.lockprevents concurrent execution (auto-expires after 1 hour)
- Survivorship bias: Static 100-stock universe excludes delisted companies
- Same-day execution: Backtest signals and trades use same closing price
- Stop-loss: Enforced daily in both backtest and paper trading (15% threshold)
- No point-in-time fundamentals: Why quality/value factors are disabled