stefan-jansen/machine-learning-for-trading

Code for Machine Learning for Trading, 3rd edition — from data sourcing to live execution.

20,863

stars

816

commits

Jupyter Notebook

primary language

Sep 11, 2026

updated

ml4trading.io
algorithmic-trading
artificial-intelligence
backtesting
data-science
deep-learning
finance
investment
investment-strategies
large-language-models
machine-learning
ml4t-workflow
polars
quantitative-finance
reinforcement-learning
synthetic-data
trading
trading-agent
trading-strategies

README

Machine Learning for Trading — 3rd Edition

Build, test, and deploy ML-driven trading strategies — from data sourcing to live execution.

This repository hosts the code for Machine Learning for Trading, 3rd Edition by Stefan Jansen — a ground-up rebuild, organized around one end-to-end workflow: how you define a research idea and develop it iteratively into a strategy you can actually run, and keep running, in a live market.

  • Nine case studies illustrate the workflow throughout the 27 chapters of the book, from raw data through features, models, backtests, costs, and risk to deployment.
  • Generative AI and autonomous agents are new to this edition and cut across that workflow, bringing retrieval-augmented generation, knowledge graphs, and multi-agent systems to financial research.
  • The companion website features 112 primers, 61 agent skills, and six production Python libraries that facilitate substantial parts of the workflow.

Start here: Installation walks a blank Linux, Windows or macOS machine to a running notebook, prerequisites included. The short version is under Quick Start below.

Next free session: How to Engineer a Multi-Agent System, a 30-minute live session on Wednesday, September 9, 2026, 12:00 PM ET / 16:00 UTC. All courses, workshops, and free lessons.

Machine Learning for Trading, 3rd Edition


What's New in the Third Edition

The whole book traces one path: from data infrastructure and strategy research, across an evidence boundary that separates tuning from evaluation, to deployment and monitoring — with a feedback loop that retrains, pauses, or retires a strategy as its edge decays.

The ML4T workflow: data infrastructure and strategy research, an evidence boundary separating tuning from evaluation, and deployment with a retrain/pause/retire feedback loop

Where earlier editions moved technique by technique, the third edition runs that one process end to end — and adds substantial new material:

  • A wider model toolkit: from gradient boosting (XGBoost, LightGBM, CatBoost) to deep time-series architectures (PatchTST, iTransformer, TSMixer, TCN, Mamba) and newer tabular and latent-factor models (TabPFN, TabM, conditional and supervised autoencoders).
  • Dedicated strategy-design chapters: transaction costs and risk management are now full chapters, neither of which existed before, joining portfolio construction and strategy synthesis so a raw signal is carried through to a sized, cost- and risk-aware portfolio.
  • A full production track: live trading systems (Interactive Brokers, Alpaca, QuantConnect), MLOps and governance (drift detection, safe rollout, circuit breakers, feature stores, experiment tracking), and the operational reality of running strategies, not just building them.
  • Generative AI: retrieval-augmented generation grounded in SEC filings, knowledge graphs and Graph RAG, and autonomous, multi-agent research systems.
  • Causal machine learning: Double ML, Bayesian structural time series, and causal discovery for separating real effects from spurious correlation.
  • Reinforcement learning: optimal execution, market making with inventory, and deep hedging.
  • Synthetic financial data: TimeGAN, Tail-GAN, Sig-CWGAN, and diffusion-based generators for validation when history is short.

Methodological rigor is treated as a first-class topic rather than an afterthought. The book draws an explicit line between exploration and confirmation — the evidence boundary — uses walk-forward cross-validation throughout, and confronts the multiple-testing and overfitting problems that quietly invalidate most backtests, with tools like the Deflated Sharpe Ratio, the Rademacher Anti-Serum, and White's Reality Check, plus conformal prediction for honest uncertainty estimates.

The data layer moves to Polars for fast, expression-based manipulation, and every chapter ships in reproducible Docker environments so results repeat across machines; PyTorch, LightGBM, Optuna, and Plotly round out the modeling and visualization stack.

Nine Case Studies

The structural centerpiece of the third edition is nine case studies that run the length of the book. ETFs, crypto perpetuals, intraday equities, options, FX, futures, and equity factor panels are each carried through the same pipeline — from raw data and labels to features, models, backtests, costs, risk overlays, and a final deployment assessment. One disciplined process applied to nine very different markets shows where it works, where it breaks, and why.

Case StudyAsset ClassFrequencyWhat It Explores
ETFsMulti-asset ETFsDailyCross-asset momentum and mean-reversion across 100 ETFs
Crypto PerpsCrypto8-hourlyFunding-rate arbitrage on perpetual futures
NASDAQ-100Equities15-minIntraday microstructure signals from order flow and the LOB
S&P 500 Equity + OptionsEquities + OptionsDailyEquity selection enhanced with implied-volatility features
US Firm CharacteristicsEquitiesMonthlyFirm-level characteristics panel (size, value, momentum, quality)
FX PairsFXDailyCarry and momentum across major currency pairs
CME FuturesFuturesDailyTerm-structure and roll-yield signals across commodity and financial futures
S&P 500 OptionsOptionsDailyOptions-only strategies (straddles, delta-hedged positions)
US EquitiesEquitiesDailyBroad cross-section of US stocks with classic factor exposures

Companion Resources

The companion website carries three things the chapters lean on but do not reprint.

  • 112 primers are free, open explainers, one page per concept, covering what a chapter assumes you already know: limit order book mechanics, bitemporal data models, fractional differencing, multiple testing in factor research, conformal prediction, the deflated Sharpe ratio, hierarchical risk parity, Almgren-Chriss execution, walk-forward validation. Nothing to install and nothing to sign up for.
  • 61 agent skills are task recipes for coding agents, each carrying the same guards against lookahead bias, leakage, and multiple testing that the task needs when a person does it by hand. They span the research loop: building bars and triple-barrier labels, feature selection, purged walk-forward CV, cost models and tear sheets, position sizing, kill switches, live monitoring. Browsing the catalog is free; opening a skill's detail requires a website account.
  • Six Python libraries carry the pipeline the notebooks are built on, one per stage of the workflow. They are listed below and each is documented and usable on its own.

The ML4T Libraries

The notebooks are built on six production Python packages, each documented and usable on its own — one per stage of the workflow:

LibraryStageWhat it does
ml4t-dataDataUnified market-data acquisition from 19+ providers behind one interface
ml4t-engineerSignalFeatures, labels, alternative bars, and leakage-safe dataset preparation
ml4t-modelsModelsFinance-native latent factors, SDFs, direct prediction, and portfolio learning
ml4t-diagnosticEvaluationFeature validation, strategy diagnostics, and the Deflated Sharpe Ratio
ml4t-backtestStrategyEvent-driven backtesting with realistic execution
ml4t-liveDeploymentProduction trading with broker integrations

Courses, Workshops, and Free Lessons

Cohorts and workshops. Live, scheduled, and worked through with direct feedback on your own research.

StartsOfferingWhat you leave with
Sep 16 – Dec 2, 2026ML for Trading: From Research to ProductionTake one research idea from a question to a costed, monitored strategy, with the evidence trail that makes the result checkable.
Sep 26, 2026Engineering a Multi-Agent Forecasting SystemBuild a multi-agent forecasting system whose reasoning is auditable end to end.
Oct 10, 2026Loop Engineering: Reliable Work From Coding AgentsGet reliable work out of coding agents: harness design, verification, and recovery from a bad run.

Free live sessions. Thirty minutes to an hour, no cost, recording sent to everyone who registers.

WhenSession
Wed, Sep 9, 12:00 PM ET / 16:00 UTCHow to Engineer a Multi-Agent System
Wed, Sep 30, 12:00 PM ET / 16:00 UTCHow to Be Productive with Coding Agents, Beyond Code
Wed, Nov 4, 12:00 PM ET / 17:00 UTCWhy Multi-Agent Systems Break, and How To Fix It

Between cohorts, the Insights newsletter covers the same ground weekly, source by source.


The Book, Chapter by Chapter

An introduction and a closing chapter bookend six workflow-aligned parts. Every chapter title links to its own guide, which carries the full description, the notebooks, and the data each one needs.

Introduction

ChapterWhat it covers
1. The Process Is Your EdgeWhy process discipline beats model sophistication: the ML4T workflow as a research-to-production system, regime detection on factor returns and macro indicators, and the evidence boundary that separates exploration from confirmation.

Part I - Financial Data (Chapters 2-5)

The markets, instruments, and infrastructure the rest of the book builds on: a taxonomy of sources, raw exchange messages turned into feature-ready bars, point-in-time fundamentals, and synthetic histories for robust validation.

ChapterWhat it covers
2. The Financial Data UniverseEight asset classes surveyed, survivorship bias quantified, storage formats benchmarked (Parquet, DuckDB, kdb+, TimescaleDB), and the data-quality framework used throughout the book.
3. Market MicrostructureNASDAQ ITCH parsing, limit order book reconstruction from several sources, Lee-Ready trade classification validated, and bar-sampling methods compared; dollar bars deliver the best return normality.
4. Fundamental and Alternative DataPoint-in-time SEC EDGAR pipelines, entity resolution across identifier systems, macro and commodity fundamentals, and alternative-data evaluation including on-chain crypto and prediction markets (Kalshi, Polymarket).
5. Synthetic Financial DataTimeGAN, Tail-GAN, Sig-CWGAN, Diffusion-TS, and LLM-based tabular generation, each evaluated through a fidelity, utility, and privacy framework.

Part II - Research Design and Feature Engineering (Chapters 6-10)

Define the trading problem, then turn data into model-ready signals: research design, labels, features, and the evaluation that determines what any model can learn.

ChapterWhat it covers
6. Strategy Research FrameworkUniverse rules, decision schedule, cost model, evaluation protocol, and run logging. Introduces the nine case studies and the walk-forward cross-validation discipline that anchors Chapters 7 to 20.
7. Defining the Learning TaskForward-return, triple-barrier, and trend-scanning labels; information coefficients, quantile analysis, and feasibility screens; multiple-testing control with BH-FDR and the Deflated Sharpe Ratio; causal plausibility checks.
8. Financial Feature EngineeringFive feature families from price (momentum, reversal, volatility, liquidity, microstructure), structural and cross-instrument features, macro and calendar context, and selection with robustness testing.
9. Model-Based Feature ExtractionFeatures from fitted models: stationarity diagnostics, Kalman filters, Fourier and wavelet spectral features, GARCH volatility, and HMM regime probabilities, all point-in-time correct.
10. Text Feature EngineeringBag-of-words through transformers: TF-IDF, Word2Vec and GloVe embeddings, LSTM sequence models, FinBERT sentiment, financial NER fine-tuning, and news-return signal construction.

Part III - Model Development (Chapters 11-15)

Five model families applied to the same nine case studies, each building on the linear baseline.

ChapterWhat it covers
11. The ML PipelineRidge, LASSO, and Elastic Net as the baseline every later model must beat, logistic regression for direction, SHAP interpretability, conformal prediction for uncertainty, and a comparison across all nine case studies.
12. Gradient Boosting and Advanced Tabular ModelsXGBoost, LightGBM, and CatBoost with Optuna multi-objective tuning, plus the deep tabular alternatives TabPFN and TabM. TreeSHAP explainability, and gradient boosting the strongest tabular model in most case studies.
13. Deep Learning for Time SeriesLSTM, N-BEATS, PatchTST, iTransformer, TFT, TSMixer, TCN, and Mamba set against the LTSF-Linear debate, with a selection framework and cross-dataset evidence on when depth helps.
14. Latent Factor ModelsPCA eigenportfolios, IPCA with time-varying loadings, conditional and supervised autoencoders, adversarial SDF estimation, and yield-curve decomposition.
15. Causal Machine LearningDouble Machine Learning for isolating factor treatment effects, Bayesian Structural Time Series for event impact, and causal discovery with PCMCI, NOTEARS, and VAR-LiNGAM.

Part IV - Strategy Implementation (Chapters 16-20)

From predictions to deployable strategies: backtesting, portfolio construction, costs, risk, and synthesis.

ChapterWhat it covers
16. Strategy SimulationBacktesting as falsification: trading-protocol specification, vectorized against event-driven engines, an ETF baseline, regime diagnostics, and overfitting control with the Deflated Sharpe Ratio, the Rademacher Anti-Serum, and White's Reality Check.
17. Portfolio ConstructionFrom scores to portfolios: mean-variance optimization and its pitfalls, Hierarchical Risk Parity, the Kelly criterion, conformal position sizing, deep allocation, and a controlled allocator comparison.
18. Transaction CostsCost taxonomy, spread estimation, market-impact calibration, execution algorithms (VWAP, TWAP, Almgren-Chriss), transaction-cost analysis, and breakeven costs that vary widely by asset class.
19. Risk ManagementVaR and CVaR tail measurement, drawdown and path-risk controls, factor and sector decomposition, stress testing, adaptive risk overlays, deep hedging, and kill switches; overlay effectiveness turns out to be strategy-specific.
20. Strategy SynthesisWhat nine experiments reveal about turning predictions into strategies: IC-Sharpe decorrelation, Fundamental Law diagnostics, the model-family cascade, cost-survival analysis, and holdout failure modes.

Part V - Advanced AI (Chapters 21-24)

Reinforcement learning, large language models, knowledge graphs, and autonomous agents for finance.

ChapterWhat it covers
21. Reinforcement Learning for Execution and HedgingMDP formulation for finance, DQN, PPO and SAC, optimal execution, market making with inventory management, deep hedging with PFHedge, inverse RL for strategy recovery, and the sim-to-real gap.
22. RAG for Financial ResearchRetrieval-augmented generation grounded in SEC filings: ingestion, domain-specific embeddings, hybrid retrieval with re-ranking, constraint-based prompting, evaluation and failure diagnostics, and the move to agentic workflows.
23. Knowledge GraphsWhen graphs earn their infrastructure cost: construction from SEC filings, Graph RAG for multi-hop reasoning, GNN embeddings and centrality as ML features, financial networks, and temporal-leakage prevention.
24. Autonomous AgentsReAct, Tree of Thoughts, and Reflexion architectures, memory systems, tool contracts, the engineering stack (LangGraph, Claude SDK), a stateful equity-research agent, multi-agent forecasting with adversarial debate, and production reliability.

Part VI - Production (Chapters 25-26)

Taking strategies live: trading systems and the operational infrastructure that keeps them running.

ChapterWhat it covers
25. Live Trading SystemsA framework bridging research and production: Interactive Brokers and Alpaca integration, managed platforms (QuantConnect), order-lifecycle management, pipeline verification, and operational readiness.
26. MLOps and GovernanceA failure taxonomy separating pipeline divergence from performance decay, drift detection, safe model rollout, circuit breakers, feature stores, and experiment tracking.

Conclusion

ChapterWhat it covers
27. The Systematic EdgeThe systematic philosophy, quant career paths, learning resources, research frontiers, and how to build your own edge. The closing bookend to Chapter 1: the process is the edge.

Quick Start

New here? Read these three, in order.

  1. What this repository is, and what it is not - what reproduces with one command, what a configuration change buys you, what needs real compute or licensed data, and what is not promised. Five minutes, before you install anything.
  2. Installation - Linux, Windows WSL2, macOS, Docker, and GPU, in full.
  3. Running notebooks - the case-study pipeline, the run log, and how to experiment without disturbing the downloaded results.

Everything below is typed into a terminal on your own computer, not into GitHub, and run from the repository root. New to the command line? Start with Before You Begin.

1. Clone and install

git clone https://github.com/stefan-jansen/machine-learning-for-trading.git
cd machine-learning-for-trading
cp .env.example .env   # the defaults work as-is; nothing in it needs editing to start

Then pick one environment. Option A, Docker, carries every dependency and needs no compiler:

docker compose pull ml4t

Option B, a local uv environment, on macOS, Linux, or inside WSL2. Install uv with its own installer, not with pip, which is missing or refuses to install on most current systems:

curl -LsSf https://astral.sh/uv/install.sh | sh
source $HOME/.local/bin/env   # the installer's own line; puts uv on PATH here and now
uv sync

Option B compiles several dependencies from source, scikit-learn among them, so it needs a C/C++ compiler and the Python headers: sudo apt install build-essential python3-dev on Ubuntu, Debian and WSL2, xcode-select --install on macOS. Budget about 16 GB of disk (11 GB environment, 4 GB free datasets, 0.9 GB of git history).

PlatformTakeWhy
LinuxeitherBoth paths are exercised on every release
macOS, Apple SiliconBWalked on real hardware before each release. Docker there earns its disk only for the twelve pre-executed ml4t-py312 notebooks, which have no arm64 build, and Chapter 2's containerized database benchmarks
macOS, IntelAPyTorch publishes no macOS x86_64 wheel, so the local path cannot resolve
Windowseither, inside WSL2Run wsl --install -d Ubuntu from an Administrator PowerShell, restart, then run it a second time: the first run usually installs the WSL runtime without a distribution. Then follow the Linux instructions in the Ubuntu terminal. Installing into Windows Python is unsupported and does not work

2. Download data

Most notebooks need datasets. Start with the free ones, which need no API keys:

uv run python data/download_all.py --free-only

That fetches seven datasets, about 4 GB and twelve minutes, almost all of it the firm-characteristics panel first needed in Chapter 4. To start in about 75 MB and fetch it when a chapter asks:

uv run python data/download_all.py --free-only --skip-firm-characteristics

On the Docker path there is no host Python: run the same command without uv run, in the Jupyter Lab terminal (File → New → Terminal). The data guide documents every dataset, API-key setup, the loaders, and the storage tiers.

Optional: pre-computed results. To explore the nine released Chapter 11-20 case studies without retraining, download their verified registries, predictions, model files, and backtest artifacts:

uv run python scripts/download_artifacts.py

3. Run notebooks

First confirm the install, with the one command that answers it. It prints a PASS or FAIL line per component and exits non-zero on any required failure:

uv run python scripts/verify_installation.py                     # Option B
docker compose run --rm ml4t python scripts/verify_installation.py   # Option A

Notebooks are paired Jupytext files, a .py source and a generated .ipynb. uv sync already installed Jupyter Lab. Start it from the repository root, on one of the two paths:

# Option B, local uv. Open the tokenized URL it prints, in full.
ML4T_DATA_PATH="${ML4T_DATA_PATH:-$PWD/data}" uv run jupyter lab

# Option A, Docker. Then open http://localhost:8888 in your browser — no token.
docker compose up ml4t

The ML4T_DATA_PATH prefix on the local path gives the loaders an absolute path, because Jupyter runs each notebook with its chapter folder as the working directory and the loaders would otherwise search inside that folder and report the datasets as missing. It keeps a value you have already exported and defaults to this repository's data/. The Docker path needs no prefix: the compose file sets the variable inside the container. See running notebooks for the first-notebook walkthrough, case-study pipelines, Papermill parameters, and the experiment workflow.

Docker images

Most notebooks run on the default ml4t image; a few need a specialized one, and each such notebook says so in its preamble. Full details in the Docker environments guide.

ImageCoversWhen you need it
ml4tAll 27 chapters + 9 case studies (CPU)Default for everything
ml4t-gpuSame ml4t image, run with the NVIDIA runtime (--profile gpu)Deep-learning chapters
ml4t-py312Python 3.12 for signatory, esig, gensim, tfcausalimpact~10 notebooks
benchmarkDatabase clients (TimescaleDB, ClickHouse, QuestDB, InfluxDB)Ch02 storage benchmarks
rapidsRAPIDS cuML + LightGBM CUDA (build locally)One Ch12 GPU benchmark

Looking for the second edition? It is complete and stable on the second-edition branch — git checkout second-edition, and everything is exactly where the book describes it.


Repository Layout

machine-learning-for-trading/
├── 01_process_is_edge/ … 27_systematic_edge/   27 chapters — Jupytext .py + .ipynb, each with a README
├── case_studies/     nine datasets carried through the full pipeline (Ch6 → Ch20)
├── data/             download scripts and loaders for every dataset      → data/README.md
├── utils/            shared config, paths, styling, modeling, and CV code → utils/README.md
├── scripts/          reader utilities (install check, notebook sync, artifacts) → scripts/README.md
├── tests/            Papermill notebook execution + unit guards, run in CI → tests/README.md
├── envs/             Dockerfiles for every image                          → envs/README.md
├── docs/             what-this-is, installation, and notebook-execution guides
├── docker-compose.yml    all Docker services
├── pyproject.toml · uv.lock    pinned dependencies (uv)
└── matplotlibrc      figure styling, auto-applied from the repo root

Contributing and Feedback

Found an error, a broken link, or have a suggestion? Early feedback is especially valuable before the book launches.


License

Code: MIT License · Book content: © 2026 Stefan Jansen. All rights reserved.

data/equities/market/sp500/daily_bars.parquet is © AlgoSeek LLC, redistributed here with AlgoSeek's permission for readers of the book. AlgoSeek retains all rights to it; cite algoseek.com as the source in anything you publish from it. The MIT license covers the code, not this file. See data/README.md.

Get the bookml4trading.ioGitHub

Contributors

stefan-jansen

793 commits

minggnim

4 commits

ryanrussell

3 commits

ssilverac

2 commits

stefan-jansen/machine-learning-for-trading

Code for Machine Learning for Trading, 3rd edition — from data sourcing to live execution.

20,863

stars

816

commits

Jupyter Notebook

primary language

Sep 11, 2026

updated

ml4trading.io
algorithmic-trading
artificial-intelligence
backtesting
data-science
deep-learning
finance
investment
investment-strategies
large-language-models
machine-learning
ml4t-workflow
polars
quantitative-finance
reinforcement-learning
synthetic-data
trading
trading-agent
trading-strategies

README

Machine Learning for Trading — 3rd Edition

Build, test, and deploy ML-driven trading strategies — from data sourcing to live execution.

This repository hosts the code for Machine Learning for Trading, 3rd Edition by Stefan Jansen — a ground-up rebuild, organized around one end-to-end workflow: how you define a research idea and develop it iteratively into a strategy you can actually run, and keep running, in a live market.

  • Nine case studies illustrate the workflow throughout the 27 chapters of the book, from raw data through features, models, backtests, costs, and risk to deployment.
  • Generative AI and autonomous agents are new to this edition and cut across that workflow, bringing retrieval-augmented generation, knowledge graphs, and multi-agent systems to financial research.
  • The companion website features 112 primers, 61 agent skills, and six production Python libraries that facilitate substantial parts of the workflow.

Start here: Installation walks a blank Linux, Windows or macOS machine to a running notebook, prerequisites included. The short version is under Quick Start below.

Next free session: How to Engineer a Multi-Agent System, a 30-minute live session on Wednesday, September 9, 2026, 12:00 PM ET / 16:00 UTC. All courses, workshops, and free lessons.

Machine Learning for Trading, 3rd Edition


What's New in the Third Edition

The whole book traces one path: from data infrastructure and strategy research, across an evidence boundary that separates tuning from evaluation, to deployment and monitoring — with a feedback loop that retrains, pauses, or retires a strategy as its edge decays.

The ML4T workflow: data infrastructure and strategy research, an evidence boundary separating tuning from evaluation, and deployment with a retrain/pause/retire feedback loop

Where earlier editions moved technique by technique, the third edition runs that one process end to end — and adds substantial new material:

  • A wider model toolkit: from gradient boosting (XGBoost, LightGBM, CatBoost) to deep time-series architectures (PatchTST, iTransformer, TSMixer, TCN, Mamba) and newer tabular and latent-factor models (TabPFN, TabM, conditional and supervised autoencoders).
  • Dedicated strategy-design chapters: transaction costs and risk management are now full chapters, neither of which existed before, joining portfolio construction and strategy synthesis so a raw signal is carried through to a sized, cost- and risk-aware portfolio.
  • A full production track: live trading systems (Interactive Brokers, Alpaca, QuantConnect), MLOps and governance (drift detection, safe rollout, circuit breakers, feature stores, experiment tracking), and the operational reality of running strategies, not just building them.
  • Generative AI: retrieval-augmented generation grounded in SEC filings, knowledge graphs and Graph RAG, and autonomous, multi-agent research systems.
  • Causal machine learning: Double ML, Bayesian structural time series, and causal discovery for separating real effects from spurious correlation.
  • Reinforcement learning: optimal execution, market making with inventory, and deep hedging.
  • Synthetic financial data: TimeGAN, Tail-GAN, Sig-CWGAN, and diffusion-based generators for validation when history is short.

Methodological rigor is treated as a first-class topic rather than an afterthought. The book draws an explicit line between exploration and confirmation — the evidence boundary — uses walk-forward cross-validation throughout, and confronts the multiple-testing and overfitting problems that quietly invalidate most backtests, with tools like the Deflated Sharpe Ratio, the Rademacher Anti-Serum, and White's Reality Check, plus conformal prediction for honest uncertainty estimates.

The data layer moves to Polars for fast, expression-based manipulation, and every chapter ships in reproducible Docker environments so results repeat across machines; PyTorch, LightGBM, Optuna, and Plotly round out the modeling and visualization stack.

Nine Case Studies

The structural centerpiece of the third edition is nine case studies that run the length of the book. ETFs, crypto perpetuals, intraday equities, options, FX, futures, and equity factor panels are each carried through the same pipeline — from raw data and labels to features, models, backtests, costs, risk overlays, and a final deployment assessment. One disciplined process applied to nine very different markets shows where it works, where it breaks, and why.

Case StudyAsset ClassFrequencyWhat It Explores
ETFsMulti-asset ETFsDailyCross-asset momentum and mean-reversion across 100 ETFs
Crypto PerpsCrypto8-hourlyFunding-rate arbitrage on perpetual futures
NASDAQ-100Equities15-minIntraday microstructure signals from order flow and the LOB
S&P 500 Equity + OptionsEquities + OptionsDailyEquity selection enhanced with implied-volatility features
US Firm CharacteristicsEquitiesMonthlyFirm-level characteristics panel (size, value, momentum, quality)
FX PairsFXDailyCarry and momentum across major currency pairs
CME FuturesFuturesDailyTerm-structure and roll-yield signals across commodity and financial futures
S&P 500 OptionsOptionsDailyOptions-only strategies (straddles, delta-hedged positions)
US EquitiesEquitiesDailyBroad cross-section of US stocks with classic factor exposures

Companion Resources

The companion website carries three things the chapters lean on but do not reprint.

  • 112 primers are free, open explainers, one page per concept, covering what a chapter assumes you already know: limit order book mechanics, bitemporal data models, fractional differencing, multiple testing in factor research, conformal prediction, the deflated Sharpe ratio, hierarchical risk parity, Almgren-Chriss execution, walk-forward validation. Nothing to install and nothing to sign up for.
  • 61 agent skills are task recipes for coding agents, each carrying the same guards against lookahead bias, leakage, and multiple testing that the task needs when a person does it by hand. They span the research loop: building bars and triple-barrier labels, feature selection, purged walk-forward CV, cost models and tear sheets, position sizing, kill switches, live monitoring. Browsing the catalog is free; opening a skill's detail requires a website account.
  • Six Python libraries carry the pipeline the notebooks are built on, one per stage of the workflow. They are listed below and each is documented and usable on its own.

The ML4T Libraries

The notebooks are built on six production Python packages, each documented and usable on its own — one per stage of the workflow:

LibraryStageWhat it does
ml4t-dataDataUnified market-data acquisition from 19+ providers behind one interface
ml4t-engineerSignalFeatures, labels, alternative bars, and leakage-safe dataset preparation
ml4t-modelsModelsFinance-native latent factors, SDFs, direct prediction, and portfolio learning
ml4t-diagnosticEvaluationFeature validation, strategy diagnostics, and the Deflated Sharpe Ratio
ml4t-backtestStrategyEvent-driven backtesting with realistic execution
ml4t-liveDeploymentProduction trading with broker integrations

Courses, Workshops, and Free Lessons

Cohorts and workshops. Live, scheduled, and worked through with direct feedback on your own research.

StartsOfferingWhat you leave with
Sep 16 – Dec 2, 2026ML for Trading: From Research to ProductionTake one research idea from a question to a costed, monitored strategy, with the evidence trail that makes the result checkable.
Sep 26, 2026Engineering a Multi-Agent Forecasting SystemBuild a multi-agent forecasting system whose reasoning is auditable end to end.
Oct 10, 2026Loop Engineering: Reliable Work From Coding AgentsGet reliable work out of coding agents: harness design, verification, and recovery from a bad run.

Free live sessions. Thirty minutes to an hour, no cost, recording sent to everyone who registers.

WhenSession
Wed, Sep 9, 12:00 PM ET / 16:00 UTCHow to Engineer a Multi-Agent System
Wed, Sep 30, 12:00 PM ET / 16:00 UTCHow to Be Productive with Coding Agents, Beyond Code
Wed, Nov 4, 12:00 PM ET / 17:00 UTCWhy Multi-Agent Systems Break, and How To Fix It

Between cohorts, the Insights newsletter covers the same ground weekly, source by source.


The Book, Chapter by Chapter

An introduction and a closing chapter bookend six workflow-aligned parts. Every chapter title links to its own guide, which carries the full description, the notebooks, and the data each one needs.

Introduction

ChapterWhat it covers
1. The Process Is Your EdgeWhy process discipline beats model sophistication: the ML4T workflow as a research-to-production system, regime detection on factor returns and macro indicators, and the evidence boundary that separates exploration from confirmation.

Part I - Financial Data (Chapters 2-5)

The markets, instruments, and infrastructure the rest of the book builds on: a taxonomy of sources, raw exchange messages turned into feature-ready bars, point-in-time fundamentals, and synthetic histories for robust validation.

ChapterWhat it covers
2. The Financial Data UniverseEight asset classes surveyed, survivorship bias quantified, storage formats benchmarked (Parquet, DuckDB, kdb+, TimescaleDB), and the data-quality framework used throughout the book.
3. Market MicrostructureNASDAQ ITCH parsing, limit order book reconstruction from several sources, Lee-Ready trade classification validated, and bar-sampling methods compared; dollar bars deliver the best return normality.
4. Fundamental and Alternative DataPoint-in-time SEC EDGAR pipelines, entity resolution across identifier systems, macro and commodity fundamentals, and alternative-data evaluation including on-chain crypto and prediction markets (Kalshi, Polymarket).
5. Synthetic Financial DataTimeGAN, Tail-GAN, Sig-CWGAN, Diffusion-TS, and LLM-based tabular generation, each evaluated through a fidelity, utility, and privacy framework.

Part II - Research Design and Feature Engineering (Chapters 6-10)

Define the trading problem, then turn data into model-ready signals: research design, labels, features, and the evaluation that determines what any model can learn.

ChapterWhat it covers
6. Strategy Research FrameworkUniverse rules, decision schedule, cost model, evaluation protocol, and run logging. Introduces the nine case studies and the walk-forward cross-validation discipline that anchors Chapters 7 to 20.
7. Defining the Learning TaskForward-return, triple-barrier, and trend-scanning labels; information coefficients, quantile analysis, and feasibility screens; multiple-testing control with BH-FDR and the Deflated Sharpe Ratio; causal plausibility checks.
8. Financial Feature EngineeringFive feature families from price (momentum, reversal, volatility, liquidity, microstructure), structural and cross-instrument features, macro and calendar context, and selection with robustness testing.
9. Model-Based Feature ExtractionFeatures from fitted models: stationarity diagnostics, Kalman filters, Fourier and wavelet spectral features, GARCH volatility, and HMM regime probabilities, all point-in-time correct.
10. Text Feature EngineeringBag-of-words through transformers: TF-IDF, Word2Vec and GloVe embeddings, LSTM sequence models, FinBERT sentiment, financial NER fine-tuning, and news-return signal construction.

Part III - Model Development (Chapters 11-15)

Five model families applied to the same nine case studies, each building on the linear baseline.

ChapterWhat it covers
11. The ML PipelineRidge, LASSO, and Elastic Net as the baseline every later model must beat, logistic regression for direction, SHAP interpretability, conformal prediction for uncertainty, and a comparison across all nine case studies.
12. Gradient Boosting and Advanced Tabular ModelsXGBoost, LightGBM, and CatBoost with Optuna multi-objective tuning, plus the deep tabular alternatives TabPFN and TabM. TreeSHAP explainability, and gradient boosting the strongest tabular model in most case studies.
13. Deep Learning for Time SeriesLSTM, N-BEATS, PatchTST, iTransformer, TFT, TSMixer, TCN, and Mamba set against the LTSF-Linear debate, with a selection framework and cross-dataset evidence on when depth helps.
14. Latent Factor ModelsPCA eigenportfolios, IPCA with time-varying loadings, conditional and supervised autoencoders, adversarial SDF estimation, and yield-curve decomposition.
15. Causal Machine LearningDouble Machine Learning for isolating factor treatment effects, Bayesian Structural Time Series for event impact, and causal discovery with PCMCI, NOTEARS, and VAR-LiNGAM.

Part IV - Strategy Implementation (Chapters 16-20)

From predictions to deployable strategies: backtesting, portfolio construction, costs, risk, and synthesis.

ChapterWhat it covers
16. Strategy SimulationBacktesting as falsification: trading-protocol specification, vectorized against event-driven engines, an ETF baseline, regime diagnostics, and overfitting control with the Deflated Sharpe Ratio, the Rademacher Anti-Serum, and White's Reality Check.
17. Portfolio ConstructionFrom scores to portfolios: mean-variance optimization and its pitfalls, Hierarchical Risk Parity, the Kelly criterion, conformal position sizing, deep allocation, and a controlled allocator comparison.
18. Transaction CostsCost taxonomy, spread estimation, market-impact calibration, execution algorithms (VWAP, TWAP, Almgren-Chriss), transaction-cost analysis, and breakeven costs that vary widely by asset class.
19. Risk ManagementVaR and CVaR tail measurement, drawdown and path-risk controls, factor and sector decomposition, stress testing, adaptive risk overlays, deep hedging, and kill switches; overlay effectiveness turns out to be strategy-specific.
20. Strategy SynthesisWhat nine experiments reveal about turning predictions into strategies: IC-Sharpe decorrelation, Fundamental Law diagnostics, the model-family cascade, cost-survival analysis, and holdout failure modes.

Part V - Advanced AI (Chapters 21-24)

Reinforcement learning, large language models, knowledge graphs, and autonomous agents for finance.

ChapterWhat it covers
21. Reinforcement Learning for Execution and HedgingMDP formulation for finance, DQN, PPO and SAC, optimal execution, market making with inventory management, deep hedging with PFHedge, inverse RL for strategy recovery, and the sim-to-real gap.
22. RAG for Financial ResearchRetrieval-augmented generation grounded in SEC filings: ingestion, domain-specific embeddings, hybrid retrieval with re-ranking, constraint-based prompting, evaluation and failure diagnostics, and the move to agentic workflows.
23. Knowledge GraphsWhen graphs earn their infrastructure cost: construction from SEC filings, Graph RAG for multi-hop reasoning, GNN embeddings and centrality as ML features, financial networks, and temporal-leakage prevention.
24. Autonomous AgentsReAct, Tree of Thoughts, and Reflexion architectures, memory systems, tool contracts, the engineering stack (LangGraph, Claude SDK), a stateful equity-research agent, multi-agent forecasting with adversarial debate, and production reliability.

Part VI - Production (Chapters 25-26)

Taking strategies live: trading systems and the operational infrastructure that keeps them running.

ChapterWhat it covers
25. Live Trading SystemsA framework bridging research and production: Interactive Brokers and Alpaca integration, managed platforms (QuantConnect), order-lifecycle management, pipeline verification, and operational readiness.
26. MLOps and GovernanceA failure taxonomy separating pipeline divergence from performance decay, drift detection, safe model rollout, circuit breakers, feature stores, and experiment tracking.

Conclusion

ChapterWhat it covers
27. The Systematic EdgeThe systematic philosophy, quant career paths, learning resources, research frontiers, and how to build your own edge. The closing bookend to Chapter 1: the process is the edge.

Quick Start

New here? Read these three, in order.

  1. What this repository is, and what it is not - what reproduces with one command, what a configuration change buys you, what needs real compute or licensed data, and what is not promised. Five minutes, before you install anything.
  2. Installation - Linux, Windows WSL2, macOS, Docker, and GPU, in full.
  3. Running notebooks - the case-study pipeline, the run log, and how to experiment without disturbing the downloaded results.

Everything below is typed into a terminal on your own computer, not into GitHub, and run from the repository root. New to the command line? Start with Before You Begin.

1. Clone and install

git clone https://github.com/stefan-jansen/machine-learning-for-trading.git
cd machine-learning-for-trading
cp .env.example .env   # the defaults work as-is; nothing in it needs editing to start

Then pick one environment. Option A, Docker, carries every dependency and needs no compiler:

docker compose pull ml4t

Option B, a local uv environment, on macOS, Linux, or inside WSL2. Install uv with its own installer, not with pip, which is missing or refuses to install on most current systems:

curl -LsSf https://astral.sh/uv/install.sh | sh
source $HOME/.local/bin/env   # the installer's own line; puts uv on PATH here and now
uv sync

Option B compiles several dependencies from source, scikit-learn among them, so it needs a C/C++ compiler and the Python headers: sudo apt install build-essential python3-dev on Ubuntu, Debian and WSL2, xcode-select --install on macOS. Budget about 16 GB of disk (11 GB environment, 4 GB free datasets, 0.9 GB of git history).

PlatformTakeWhy
LinuxeitherBoth paths are exercised on every release
macOS, Apple SiliconBWalked on real hardware before each release. Docker there earns its disk only for the twelve pre-executed ml4t-py312 notebooks, which have no arm64 build, and Chapter 2's containerized database benchmarks
macOS, IntelAPyTorch publishes no macOS x86_64 wheel, so the local path cannot resolve
Windowseither, inside WSL2Run wsl --install -d Ubuntu from an Administrator PowerShell, restart, then run it a second time: the first run usually installs the WSL runtime without a distribution. Then follow the Linux instructions in the Ubuntu terminal. Installing into Windows Python is unsupported and does not work

2. Download data

Most notebooks need datasets. Start with the free ones, which need no API keys:

uv run python data/download_all.py --free-only

That fetches seven datasets, about 4 GB and twelve minutes, almost all of it the firm-characteristics panel first needed in Chapter 4. To start in about 75 MB and fetch it when a chapter asks:

uv run python data/download_all.py --free-only --skip-firm-characteristics

On the Docker path there is no host Python: run the same command without uv run, in the Jupyter Lab terminal (File → New → Terminal). The data guide documents every dataset, API-key setup, the loaders, and the storage tiers.

Optional: pre-computed results. To explore the nine released Chapter 11-20 case studies without retraining, download their verified registries, predictions, model files, and backtest artifacts:

uv run python scripts/download_artifacts.py

3. Run notebooks

First confirm the install, with the one command that answers it. It prints a PASS or FAIL line per component and exits non-zero on any required failure:

uv run python scripts/verify_installation.py                     # Option B
docker compose run --rm ml4t python scripts/verify_installation.py   # Option A

Notebooks are paired Jupytext files, a .py source and a generated .ipynb. uv sync already installed Jupyter Lab. Start it from the repository root, on one of the two paths:

# Option B, local uv. Open the tokenized URL it prints, in full.
ML4T_DATA_PATH="${ML4T_DATA_PATH:-$PWD/data}" uv run jupyter lab

# Option A, Docker. Then open http://localhost:8888 in your browser — no token.
docker compose up ml4t

The ML4T_DATA_PATH prefix on the local path gives the loaders an absolute path, because Jupyter runs each notebook with its chapter folder as the working directory and the loaders would otherwise search inside that folder and report the datasets as missing. It keeps a value you have already exported and defaults to this repository's data/. The Docker path needs no prefix: the compose file sets the variable inside the container. See running notebooks for the first-notebook walkthrough, case-study pipelines, Papermill parameters, and the experiment workflow.

Docker images

Most notebooks run on the default ml4t image; a few need a specialized one, and each such notebook says so in its preamble. Full details in the Docker environments guide.

ImageCoversWhen you need it
ml4tAll 27 chapters + 9 case studies (CPU)Default for everything
ml4t-gpuSame ml4t image, run with the NVIDIA runtime (--profile gpu)Deep-learning chapters
ml4t-py312Python 3.12 for signatory, esig, gensim, tfcausalimpact~10 notebooks
benchmarkDatabase clients (TimescaleDB, ClickHouse, QuestDB, InfluxDB)Ch02 storage benchmarks
rapidsRAPIDS cuML + LightGBM CUDA (build locally)One Ch12 GPU benchmark

Looking for the second edition? It is complete and stable on the second-edition branch — git checkout second-edition, and everything is exactly where the book describes it.


Repository Layout

machine-learning-for-trading/
├── 01_process_is_edge/ … 27_systematic_edge/   27 chapters — Jupytext .py + .ipynb, each with a README
├── case_studies/     nine datasets carried through the full pipeline (Ch6 → Ch20)
├── data/             download scripts and loaders for every dataset      → data/README.md
├── utils/            shared config, paths, styling, modeling, and CV code → utils/README.md
├── scripts/          reader utilities (install check, notebook sync, artifacts) → scripts/README.md
├── tests/            Papermill notebook execution + unit guards, run in CI → tests/README.md
├── envs/             Dockerfiles for every image                          → envs/README.md
├── docs/             what-this-is, installation, and notebook-execution guides
├── docker-compose.yml    all Docker services
├── pyproject.toml · uv.lock    pinned dependencies (uv)
└── matplotlibrc      figure styling, auto-applied from the repo root

Contributing and Feedback

Found an error, a broken link, or have a suggestion? Early feedback is especially valuable before the book launches.


License

Code: MIT License · Book content: © 2026 Stefan Jansen. All rights reserved.

data/equities/market/sp500/daily_bars.parquet is © AlgoSeek LLC, redistributed here with AlgoSeek's permission for readers of the book. AlgoSeek retains all rights to it; cite algoseek.com as the source in anything you publish from it. The MIT license covers the code, not this file. See data/README.md.

Get the bookml4trading.ioGitHub

Contributors

stefan-jansen

793 commits

minggnim

4 commits

ryanrussell

3 commits

ssilverac

2 commits

Languages

Jupyter Notebook

91.0%

Python

9.0%