Code · Python · Runnable Examples

Algorithmic Trading in Python — Runnable Walkthroughs

Every walkthrough on this page runs. Every number shown was produced by actually executing the script in a clean virtual environment on 2026-06-05. The code is real, the output is real, and the honesty is intentional: no invented returns, no cherry-picked windows.

Stack disclosure: These walkthroughs use yfinance (free Yahoo Finance wrapper, no API key), vectorbt (vectorized backtesting, Apache-2.0 + Commons Clause), and Alpaca paper API (free paper trading account, no minimum deposit) — the full stack costs $0 to run. Stack choice is unaffiliated; it was chosen because it is free, well-documented, and sufficient for learning the architecture of systematic trading before you choose production tools.

Regulatory firewall: AlgoDrill teaches the architecture of algorithmic trading. These scripts never execute trades, never hold broker credentials, and never connect to a live account. You wire your own paper (or live) account on your own infrastructure. Past backtest results do not predict future returns.

Walkthroughs

Each walkthrough is a focused, end-to-end Python script with a line-by-line explanation. Seven walkthroughs ship across two waves; all are live now.

Start here

Your First Trading Bot in ~30 Lines

Fetch SPY daily data, compute 10/50 SMA signals, backtest with realistic fees, and read the output honestly. The point is not that the bot wins — it doesn’t beat buy-and-hold — but that you can measure exactly how it behaves before risking anything.

vectorbt yfinance SMA backtest

Fetch OHLCV Data with yfinance

Download 10 years of daily bars, understand adjusted vs unadjusted closes (the difference matters for backtesting), inspect data quality, and save to CSV. Covers rate-limit gotchas and why survivorship bias is not a yfinance problem.

yfinance OHLCV adjusted close data quality

SMA Crossover Backtest in Depth

Extends the hero script with fee sensitivity and a 3-combo parameter sweep. The sweep is the lesson: the same dataset gives results ranging from 106.6% to 134.4% depending purely on which windows you pick. That spread IS the overfitting risk.

vectorbt parameter sweep fees overfitting

Position Sizing: Kelly + Fixed-Fractional

~25 lines of numpy/pandas to compute full Kelly, half-Kelly, quarter-Kelly, and fixed-fractional risk from a trade series. Uses the same worked example as the Kelly Criterion guide so the two pages agree.

Kelly Criterion position sizing numpy pandas

Walk-Forward Split in Python

Rolling and anchored IS/OOS windows in pandas+vectorbt. One full WFA pass on the SPY SMA strategy: grid-search parameters in-sample, measure on fresh OOS data. Reveals the IS-to-OOS Sharpe gap — the core overfitting signal.

walk-forward IS/OOS vectorbt validation

Reading Backtest Stats by Hand

Compute Sharpe, Sortino, max drawdown, and profit factor from a returns series in numpy, then reconcile with vectorbt. Reveals the 252 vs 365-day annualization difference that causes Sharpe discrepancies across tools.

Sharpe Sortino max drawdown numpy

Alpaca Paper Trading First Order

Connect to Alpaca paper ($0), fetch account info, place + cancel one paper order via alpaca-py. Keys read from environment variables — AlgoDrill never holds credentials or executes trades. Architecture only; run it on your own paper account.

alpaca-py paper trading broker API authentication

How to Run These Scripts

All walkthroughs use a single virtual environment. Create it once, then run any script:

python -m venv .venv
source .venv/bin/activate   # Windows: .venv\Scripts\activate
pip install vectorbt yfinance numpy pandas alpaca-py

python first_trading_bot.py

Each script file includes the exact library versions used to produce the recorded output (e.g., vectorbt 1.0.0 · yfinance 1.4.1 · pandas 2.3.3). If you re-run months later, yfinance may serve slightly different adjusted closes due to corporate actions — small drift is normal; large discrepancies signal a data-source change worth investigating.

New to algo trading? Start with the 30-line bot, then drill the concepts in the flashcard deck.

Start: First Trading Bot →   Flashcard Drills →