Machine Learning for Algorithmic Trading, through Its Code
Citation. Stefan Jansen. Machine Learning for Algorithmic Trading: Predictive models to extract signals from market and alternative data for systematic trading strategies with Python, 2nd edition. Packt, 2020. ISBN 978-1-83921-771-5, from Open Library's record. This page reviews the book's code, not its text. The code is open under MIT at stefan-jansen/machine-learning-for-trading. Its main branch is now the third edition, with 27 chapters. The second edition's code, which this issue names, is on the second-edition branch, with 156 notebooks.
Status. Not applicable. The project has its own splitters, metrics and selection tests, and nothing here should replace them. The code is a useful worked example of two things the harness guards against. One is many trials on the same data. The other is preprocessing that sees the held-out period. Its splitter is a sound walk-forward with a purge, and its tuning does hold out a final year. So the lesson is about counting, not about a broken method.
Read. Read on 2026-11-28 from the second-edition branch, through the GitHub API and raw downloads. I read utils.py in full, which holds the book's splitter, MultipleTimeSeriesCV. I read the code cells of five notebooks, without their outputs:
07_linear_models/03_preparing_the_model_data07_linear_models/05_predicting_stock_returns_with_linear_regression12_gradient_boosting_machines/05_trading_signals_with_lightgbm_and_catboost12_gradient_boosting_machines/06_evaluate_trading_signals12_gradient_boosting_machines/08_making_out_of_sample_predictions
I didn't read the book's text, run the notebooks, or read the other 151 notebooks. The third edition's README describes an "evidence boundary" that "separates tuning from evaluation". I didn't read its code.
Summary
The splitter. MultipleTimeSeriesCV(n_splits=3, train_period_length=126, test_period_length=21, lookahead=None). It sorts the dates from newest to oldest and builds n_splits test windows back from the end. Each training window ends lookahead trading days before its test window starts. A training label is the return over the next lookahead days, so the last one ends where the test starts. That gap is a purge. Training always comes before the test, so the splitter is a walk-forward and needs no embargo.
The tuning in chapter 12.
- LightGBM's grid. Learning rates
[.01, .1, .3], tree depths[2, 3, 5, 7], feature fractions[.3, .6, .95]and minimum leaf sizes[250, 500, 1000]. That is 108 parameter sets, of which the code samples half. - The settings around it. Label horizons
[1, 5, 21]days and training lengths of 4.5 years and 1 year, so 6 settings. Each model is scored at 13 numbers of boosting rounds, from 10 to 500. - The count. 54 parameter sets × 6 settings × 13 lengths is about 4,200 scored combinations, by the information coefficient (IC). CatBoost adds its own grid.
- The split in time. Tuning uses data "train & validation period" up to 2016, with the last two years as validation. Notebook 08 then takes the best model by IC and predicts a "1-year out-of-sample period" after it.
Preprocessing. Notebook 07/03 clips each return series at its 0.01% and 99.99% quantiles, taken over the whole sample. In the linear models of notebook 07/05, StandardScaler sits inside the pipeline that the splitter fits, so each fold scales on its own training data.
Findings. I didn't run the notebooks or read their outputs, so I report no numbers from them.
Where the claims go beyond the evidence
About 4,200 trials, then one year. The final year is held out, which is right. But it is one year of daily ICs for a model chosen as the best of thousands. The validation IC that chose it is biased up by the search. The held-out year is the only honest number, and one year has a wide error (#8).
The clipping sees the future. The quantiles are taken over every date, including the held-out year. The effect is small at 0.01%, but it is a look-ahead in the inputs, and it is the kind that open_familyoffice/oracle.py can't detect (#87).
Two small faults in the splitter. shuffle calls np.random.shuffle(list(train_idx)), which shuffles a copy that is then discarded, so it does nothing. And with its default lookahead=None, split raises a TypeError. The caller must always pass it.
The backtests use unmaintained tools. Zipline and pyfolio came from Quantopian, which closed in 2020. Their cost models are whatever each notebook sets.
Credit. The splitter purges labels that look forward. The scalers are fitted inside each fold. The tuning sets a final year aside before it looks.
Use in this repo
Count the grid as trials. A strategy built this way enters the trial ledger once for each scored combination, not once for the model chosen (#7). With N in the thousands, the haircut and the deflated Sharpe ratio charge heavily (open_familyoffice/haircut.py, #94), as they should. The held-out year's result is the one to report.
Fit every transform inside the window. Clipping, ranking and scaling must use only the training window's data. The project's splitters give the windows (open_familyoffice/splits.py, #10).
The splitter matches the project's own. A walk-forward with a gap of the label's horizon is what splits.py does for forward labels. The book adds nothing the project lacks, and its faults are reasons not to import it.
The information coefficient needs its error. A mean daily IC is a mean of correlated values when labels overlap. Its standard error needs a long-run variance (metrics.long_run_variance), and it is one more metric to report with its error (#8).
The book's text is unread. Its treatment of alternative data, text and GANs for synthetic data is left for a later review. So is the third edition's "evidence boundary", if the maintainer wants one.