---
title: "Level 5: machine learning and deep learning for trading"
description: "Level 5 of the Python Trading Course: features, triple barrier, meta-labeling, purged CV, boosting, Keras 3, PyTorch, foundation models and RL."
url: "https://pythonparatrading.com/en/python-trading-course/level-5/"
lang: "en"
image: "https://pythonparatrading.com/og/en/python-trading-course/level-5.png"
---

# Level 5: Machine learning and deep learning (Keras/PyTorch)

> Financial machine learning under the rules of the domain: little signal, a lot of noise and non-stationary data. Labelling, leak-free validation and an honest comparison with simple baselines.

- Hours: 120–140
- Prerequisite: Levels 1 and 2; linear algebra, probability
- Profile: Data scientist, junior quant
- Course: [Python Trading Course](https://pythonparatrading.com/en/python-trading-course/)

## ML fundamentals applied to finance

- `5.1.1` Why financial ML is different: low signal-to-noise ratio, non-stationarity, scarce and contested data.
- `5.1.2` Supervised, unsupervised and reinforcement learning; scikit-learn: pipelines, transformers, hyperparameter search.
- `5.1.3` Appropriate metrics, not just accuracy: economic metrics (signal Sharpe, P&L after costs).

## Feature engineering

- `5.2.1` Price and volume features and microstructure features: multi-horizon returns, volatility, normalised indicators, imbalance, VPIN.
- `5.2.2` Point-in-time fundamentals, macro, sentiment, alternative data.
- `5.2.3` Fractional differencing to preserve memory while achieving stationarity.
- `5.2.4` Normalisation without leakage (rolling statistics), cross-sectional rank transform.
- `5.2.5` Feature selection and importance: MDI, MDA, SFI, SHAP; feature clustering.

## Labelling and sampling

- `5.3.1` Fixed-horizon labelling and its problems.
- `5.3.2` The triple-barrier method.
- `5.3.3` Meta-labeling: primary model and secondary model for sizing or filtering.
- `5.3.4` Sample uniqueness, weights by overlap and by return; sequential bootstrapping.
- `5.3.5` Event-based sampling (CUSUM filter).

*Fig. — Triple-barrier method: profit-taking, stop and time limit (schematic).* Price path from an entry at 100, between an upper barrier at 104, a lower one at 97 and a vertical one at bar 25. The price touches the upper barrier first at bar 19, highlighted point, and the observation gets the label +1.

## Validation for financial series

- `5.4.1` Why random k-fold fails.
- `5.4.2` Purged K-Fold with embargo; CPCV.
- `5.4.3` Walk-forward with retraining; expanding versus rolling windows.
- `5.4.4` Backtest of the ML signal with costs; Deflated Sharpe over the experiments run.

## Classical models

- `5.5.1` Regularised regressions (Ridge, Lasso, Elastic Net); logistic.
- `5.5.2` Trees, Random Forest, bagging.
- `5.5.3` Gradient boosting: XGBoost, LightGBM, CatBoost, the workhorses for tabular data.
- `5.5.4` Hyperparameter tuning with Optuna, and the risk of overfitting the validation.
- `5.5.5` Unsupervised: PCA, clustering (regimes, asset grouping), autoencoders.
- `5.5.6` Cross-sectional ranking models (learning to rank) for stock selection.

## Deep learning with Keras 3

- `5.6.1` Multi-backend Keras 3 (JAX, TensorFlow, PyTorch): configuration, functional and subclassing API.
- `5.6.2` MLPs for financial tabular data; regularisation (dropout, weight decay, early stopping).
- `5.6.3` 1D CNNs and LSTM/GRU for sequences; windows, scaling and leakage.
- `5.6.4` Callbacks, custom losses (for example, Sharpe-based), export and inference.

## Deep learning with PyTorch

- `5.7.1` Tensors, autograd, `nn.Module`, Dataset and DataLoader, training loop; GPU and MPS, `torch.compile`, Lightning.
- `5.7.2` Time series architectures: TCN, N-BEATS and N-HiTS, PatchTST, iTransformer; transformers with attention and patching.
- `5.7.3` Probabilistic models: quantile loss, DeepAR, confidence intervals.
- `5.7.4` Graph neural networks (GNNs) for relationships between assets (introduction).
- `5.7.5` End-to-end portfolio optimisation (differentiable layers, Sharpe loss).

## Time series foundation models

- `5.8.1` What they are: massive pre-training and zero-shot forecasting.
- `5.8.2` Families: Chronos, Chronos-Bolt and Chronos-2, TimesFM-2.5, Moirai-2.0, TiRex, TimeGPT; Kronos, pre-trained on OHLCV.
- `5.8.3` Practical use: zero-shot inference, covariates, fine-tuning, probabilistic forecasting.
- `5.8.4` Evidence in finance: in a 2026 benchmark (arXiv:2606.27100) on the daily returns of AAPL, AMZN, GOOG, JPM and META, Moirai-2.0 and TimesFM-2.5 achieved the best average ranks (2.9 and 3.1), but their improvements over the random walk were “small and sparse”. Another study found that strategies built on these models perform considerably worse than specialised reference models. Lesson: useful for volatility, covariates and series with structure; scepticism about forecasting returns directly.
- `5.8.5` Lab: foundation model versus GARCH and LightGBM in forecasting realised volatility.

## Reinforcement learning

- `5.9.1` MDPs, Q-learning, policy gradient, PPO, SAC; Gymnasium environments and reward design with costs.
- `5.9.2` Realistic applications: optimal execution, market making, dynamic hedging.
- `5.9.3` Why RL tends to overfit in directional trading; FinRL as a critical reference.

## Interpretability, robustness and model risk

- `5.10.1` SHAP, partial dependence, stability of importances.
- `5.10.2` Data drift and concept drift, regime detection.
- `5.10.3` Ensembles, signal combination and synthetic data (GANs and diffusion: state of the art and limits).

## Level project

A complete financial ML pipeline: features, triple barrier, meta-labeling, LightGBM and a deep learning model in Keras or PyTorch, CPCV, a backtest with costs and Deflated Sharpe, plus an honest comparison with a simple baseline and with a zero-shot foundation model.

Previous level: [Level 4: Portfolios, risk, execution and live trading](https://pythonparatrading.com/en/python-trading-course/level-4/)

Next level: [Level 6: Generative AI, LLMs, agents, big data and alternative data](https://pythonparatrading.com/en/python-trading-course/level-6/)
