A modular algorithmic trading framework in Python for both live trading (Alpaca) and historical backtesting. It includes automated performance reporting, SPY benchmarking, and reproducible trade logs.
- Live trading with Alpaca (
src/live_trade.py) - Backtesting on historical data (
src/backtest.py) - Strategies (see
src/strategy.py):WeeklyReversal— cross-sectional 1-week mean-reversionCrossSectionalReversal— bottom-k contrarian over a configurable lookbackPairsTrading— cointegration-based pair usingstatsmodels.cointLowVol— low-volatility tiltCoveredCalls— covered-call overlay (simplified)CompositeStrategy— blends multiple strategies with weights
- Data utilities (
src/data_fetch.py) - Automated reporting (
generate_stats.py)- Equity curve, drawdown, rolling Sharpe, SPY comparison (alpha/beta/R²), per-order realized PnL, monthly returns table
- Universe management (
tools/update_symbols.py) — refreshessymbols.txtwith a SPY-like Top-N subset - Human-readable trade logs in
trades/(daily Markdown files)
src/— strategies, backtest runner, live trading, data fetchtools/— helper scripts (e.g.,update_symbols.py)report/— generated charts & CSVs (created bygenerate_stats.py)data/raw&data/clean— cached market datatrades/— dated Markdown logs of executed tradessymbols.txt— current trading universe (one ticker per line)generate_stats.py— builds lifetime performance report from Alpaca.github/workflows/daily-trade.yml— optional scheduled live trade runner
git clone https://github.com/dshan12/algo-trader.git
cd algo-trader
python -m venv .venv
# macOS/Linux
source .venv/bin/activate
# Windows
# .venv\Scripts\activate
pip install -r requirements.txt
Recommended Python: 3.10 (per
.python-version).
Dependencies include:alpaca-trade-api,pandas,numpy,statsmodels,matplotlib,yfinance,python-dotenv.
Live trading (src/live_trade.py):
- Uses:
ALPACA_API_KEYALPACA_API_SECRET
Stats/reporting (generate_stats.py):
- Accepts either set:
APCA_API_KEY_ID,APCA_API_SECRET_KEY(preferred by Alpaca docs)- or
ALPACA_API_KEY,ALPACA_API_SECRET
- Optional:
APCA_API_BASE_URL(defaults to paper:https://paper-api.alpaca.markets)
Refresh symbols.txt with a SPY-like Top-N subset (keeps you under free API limits):
python tools/update_symbols.py --n 50 --format alpaca
# use --format yahoo to keep tickers like BRK.B
Runs sample comparisons (weekly reversal horizons, pair trade, low-vol tilt, covered calls, and a composite):
python -m src.backtest
Executes end-of-day orders based on current signals:
python -m src.live_trade
Builds a lifetime (first→last) report from Alpaca portfolio_history + fills and benchmarks vs SPY:
python generate_stats.py
Artifacts are saved to ./report/.
The framework separates signals (strategy) from execution (backtest/live) and reporting:
- WeeklyReversal — Cross-sectional 1-week mean-reversion: short recent 1-week winners, long 1-week losers (equal-weight; constraints in code).
- CrossSectionalReversal — Bottom-k contrarian over a configurable lookback window (long the worst; see
lookback/bottom_kin class init). - PairsTrading — Chooses a pair and tests cointegration with
statsmodels.coint; trades the spread (long/short legs) when it diverges and mean-reverts. - LowVol — Selects lower-volatility names from the universe and allocates with an equal-weight tilt.
- CoveredCalls — Simple covered-call overlay on long positions (payoff approximations are encoded in the class).
- CompositeStrategy — Linear blend of the above with configurable weights.
- Backtesting (
src/backtest.py) reuses the samegenerate_weights(...)logic as live to ensure consistency. - Reporting (
generate_stats.py) computes total/annual returns, vol, Sharpe/Sortino, max DD, realized PnL by order (FIFO), and OLS alpha/beta vs SPY (usingyfinance).
Run generate_stats.py after you have trading history or a backtest; it writes:
Charts
report/equity_curve.pngreport/drawdown.pngreport/rolling_sharpe_30d.pngreport/equity_vs_spy.png
Tables/CSVs
report/monthly_returns_heatmap.csvreport/per_order_realized_pnl.csvreport/per_symbol_realized_pnl.csvreport/fills.csv
Quick gallery (auto-generated figures from this repo)

A GitHub Action (.github/workflows/daily-trade.yml) is included to run live trading EOD:
- Schedule:
21:05 UTCon weekdays - Secrets used:
ALPACA_API_KEY,ALPACA_API_SECRET, andPAT(GitHub token) - Runner command:
python -m src.live_trade
- No portfolio history / empty figures → Ensure Alpaca keys are set and the account has history; for testing, run a backtest first.
- SPY benchmark missing → Install
yfinance(seerequirements.txt) and ensure network access. - Universe too large / rate limits → Use
tools/update_symbols.py --n 50to keep a compact, SPY-like set. - Python version → The repo’s
.python-versionis 3.10; use 3.10+ for best compatibility with pinned deps.
MIT — see LICENSE.