Book

Python for Algorithmic Trading Cookbook

What this book is

A practitioner's manual for building a complete quantitative-trading stack in Python — not the math behind alpha, not a treatise on market microstructure, but the working engineering pipeline: how you get the data, what you do with it, how you test a strategy properly, how you ship it to a live broker, and how you keep it running.

Jason Strimpel writes from inside the toolchain. The book is a cookbook in the literal sense: each topic is a runnable recipe with a specific library focus — yfinance and pandas for ingest, VectorBT and Zipline Reloaded for backtesting, Alphalens and Pyfolio for evaluation, the Interactive Brokers Python API for execution. It assumes you already know Python and basic finance; it teaches you to bolt the parts together.

The shape of the pipeline

The shape of the pipeline

Executive summary

Systematic trading is mostly a data-engineering problem with a finance hat on. The math behind most factor strategies is undergraduate-level statistics; the difficulty lies in plumbing — getting clean data fast, structuring it for both vectorised and event-based experiments, producing comparable performance metrics across runs, and shipping a strategy to a broker without it doing something catastrophic on the first tick.

Three architectural distinctions that shape the whole book

  1. Vector vs. event-based backtesting. Topic 6 (VectorBT) tests a strategy by computing signals across the full history at once — fast, ideal for parameter sweeps, but bad at modeling realistic order fills and intra-day events. Topic 7 (Zipline) simulates the strategy one bar at a time, calling your handle_data function on each — slower but closer to what live trading actually looks like. Most serious researchers use both: VectorBT for exploration, Zipline for the final go/no-go test.

  2. Alpha generation vs. risk evaluation. Topic 5 builds factors that predict returns. Topics 8 and 9 evaluate the behaviour of those factors — Alphalens looks at factor quality (information coefficient, quantile spreads), Pyfolio looks at portfolio outcomes (drawdown, Sharpe, return decomposition). Conflating these is the most common mistake: a "good" factor in Alphalens can produce a poor portfolio if turnover or sizing rules eat the edge.

  3. Research and live as separate environments. Topics 10-13 confront the gap between backtest and live execution — slippage you didn't model, orders that partially fill, market data that disconnects, and your strategy logic that needs to be reentrant across restarts. The IB API is the canonical Python broker integration; what the book teaches generalises to any broker that exposes an async callback model.

What this stack lets you actually do

The book's payoff is a reproducible loop: hypothesise an alpha (price/volume, fundamental, alt-data), test it vector-style for fast feedback, validate it event-style for realistic conditions, package the validated rules as production code, run it against an IB paper account, then promote to live. The full cycle — from a new hypothesis to a paper-trading deploy — should take days, not months, once the stack is in place.

Who this is for

Topic index

Topic 1 — Acquire Free Financial Market Datayfinance, pandas-datareader, Alpha Vantage, Quandl — the free-tier libraries and how to combine them around their gaps.Topic 2 — Analyse and Transform with pandasThe book's heaviest topic. DataFrames, multi-index frames, resampling, alignment — the lingua franca of every later topic.Topic 3 — VisualiseWhen matplotlib is enough, when seaborn earns its place, and when Plotly Dash makes sense as an interactive front-end.Topic 4 — Store Financial DataSQLite for tabular OHLCV, parquet for columnar research data, partitioning patterns that survive at scale.Topic 5 — Build Alpha FactorsWhat an alpha factor is, how to construct momentum/value/quality variants, and the cross-sectional ranking that turns scores into trades.Topic 6 — Vector Backtesting with VectorBTThe fast-feedback path. Parameter sweeps in seconds; the right tool for early-stage hypothesis testing.Topic 7 — Event Backtesting with ZiplineThe realistic path. Bar-by-bar simulation with a calendar, an order-book model, and `handle_data` callbacks.Topic 8 — Factor Evaluation with AlphalensInformation coefficient, quantile returns, turnover — the metrics that separate a real factor from a noisy one.Topic 9 — Portfolio Metrics with PyfolioFrom returns series to a tearsheet — Sharpe, drawdown, exposure decomposition, the standard institutional read-out.Topic 10 — Interactive Brokers API SetupThe async callback model of `ib_insync`, connection management, contract resolution, account-snapshot patterns.Topic 11 — Orders, Positions, PortfoliosMarket, limit, stop, bracket — when each is appropriate. How positions and portfolios reconcile across restarts.Topic 12 — Deploy to LiveThe longest topic. Scheduling, state persistence, restart safety, alerting — the operations layer most researchers underestimate.Topic 13 — Advanced RecipesThe grab bag — websocket-driven data, multi-asset orchestration, performance attribution, and the patterns that emerge once you have a year of live runs.

How to read these summaries

The topics depend on each other. Topic 2 (pandas) is referenced by every topic that follows; without it, the rest read as code without context. Topic 5 (alpha factors) is the conceptual centre; topics 6-9 are all evaluating something topic 5 produced. Topics 10-12 are an integrated thread — the IB API is one library, split across three topics for digestibility.

Concept companions

Topics

  1. 01Acquire Free Financial Market Data with Cutting-Edge Python Libraries
  2. 02Analyze and Transform Financial Market Data with pandas
  3. 03Visualize Financial Market Data with Matplotlib, Seaborn, and Plotly Dash
  4. 04Store Financial Market Data for Easy Retrieval
  5. 05Build Alpha Factors for Stock Portfolios
  6. 06Vector-Based Backtesting with VectorBT
  7. 07Event-Based Backtesting Factor Portfolios with Zipline Reloaded
  8. 08Evaluate Factor Risk and Performance with Alphalens Reloaded
  9. 09Assess Backtest Risk and Performance Metrics with Pyfolio
  10. 10Set Up the Interactive Brokers Python API
  11. 11Manage Orders, Positions, and Portfolios with the IB API
  12. 12Deploy Strategies to a Live Environment
  13. 13Advanced Recipes for Market Data and Strategy Management