Machine Learning for Trading
Using historical data to predict returns — what a feature is, why validation is non-negotiable, and how overfitting quietly destroys strategies that look perfect on paper.
What problem is ML solving?
The question that drives every ML model in trading is simple: can historical data predict future returns? If past prices, volumes and volatility carry any repeatable structure, then a model can identify useful patterns and turn them into trading signals.
We already build signals by hand. Momentum, mean reversion and volume rules are explicit, human-written if/then statements. ML asks a different question: instead of writing the rule, can we let an algorithm learn the rule from the data?
| Dimension | Rule-based signals | Machine learning |
|---|---|---|
| How the rule is made | Hand-written by a researcher | Learned from historical data |
| Inputs | One or two indicators | Many features at once |
| Flexibility | Rigid, fixed logic | Captures complex, non-linear relationships |
| Interpretability | Easy to explain | Harder to interpret |
What is machine learning?
Machine learning is the practice of learning patterns from historical data, using features as inputs to generate predictions about something we care about — here, future returns.
Quant firms lean on ML because the modern data environment plays to its strengths:
Large datasets
Years of tick-level prices, volumes and quotes across thousands of assets.
Many variables
Dozens or hundreds of candidate inputs that no human can weigh by hand.
Complex relationships
Non-linear interactions a simple rule cannot express.
Scalable
One trained model can score the entire universe automatically.
ML does not replace the research workflow — it slots into one stage of it:
flowchart LR
A([Data]) --> B([Features])
B --> C([Model])
C --> D([Prediction])
D --> E([Backtest])
E -.->|Refine features / model| B
classDef s fill:#eef2ff,stroke:#4f46e5,stroke-width:1px,color:#3730a3;
classDef d fill:#ecfdf5,stroke:#0d9488,color:#0d9488;
class A,B,C,D s
class E d
Where ML fits in research: raw data becomes features, a model maps features to a prediction, and the backtest decides whether to keep going.
Firms built largely on data-driven and ML-style research include Renaissance Technologies, Two Sigma, Citadel and AQR.
Prediction vs explanation — and why ML is not magic
There is a genuine trade-off between the two approaches. A hand-written rule is easy to explain: "buy when the 20-day return is positive." An ML model is more flexible but harder to interpret — it may use fifty features in combinations no one can narrate.
- Markets are noisy — most price movement is random, not signal.
- Relationships change — a pattern that held in 2020 can vanish by 2024.
- No guaranteed profits — a fancy model is still a bet, not a certainty.
- Validation is critical — without it, you cannot tell luck from skill.
Features: the inputs to a model
A feature is an input variable: a numerical representation of some piece of information the model uses to make a prediction. The thing being predicted (e.g. the next day's return) is the target.
Typical features are derived from raw OHLCV data:
5-day return
Short-horizon momentum.
20-day return
Medium-horizon trend.
Rolling volatility
Recent risk / dispersion of returns.
Volume ratio
Today's volume vs its recent average.
A concrete setup might use return, volatility and volume ratio as features, with future return as the target.
Good features vs bad features
Good features
Relevant (plausibly linked to returns), stable (behave consistently over time), and intuitive (you can explain why they matter).
Bad features
Random (no real link), unstable (meaning shifts), or contain future information — the cardinal sin of leakage.
A feature must use only information available at decision time. "Tomorrow's return" is not a feature — it is the answer. Including any value that would not be known until after the trade leaks the future into the model and produces backtests that look brilliant and fail instantly when traded live.
More features → more complexity → higher overfitting risk. Adding inputs is cheap, but every extra feature gives the model another way to fit noise. More is not better.
Validation: training data vs test data
Validation exists to answer one question: will this model work on data it has never seen? That property is called generalization, and it is the only thing that matters for live trading.
Training data
Historical examples the model learns from to build its rule.
Test data
Unseen data held back to evaluate performance and simulate the future.
Train on 2019–2023, then test on 2024–2025. The model never touches the 2024–2025 data while learning, so its performance there is an honest proxy for how it would have traded going forward.
No future information may enter training. This is information leakage, and it produces unrealistically good results that evaporate live. Validation only means something if the test set is genuinely untouched.
Overfitting: memorizing noise
Overfitting is when a model memorizes noise instead of learning real structure. The tell-tale signature is strong training performance with weak test performance — it generalizes poorly.
The core distinction is signal vs noise: a signal is a persistent pattern that recurs out of sample; noise is random variation that happens not to repeat. An overfit model has confused the second for the first.
- Model A scores 95% on training but only 48% on test — a huge gap between what it learned and what it can repeat.
- Model B scores 60% on training and 58% on test — a tiny gap; its test performance nearly matches its training performance.
- A 95% → 48% collapse means Model A memorized the training set's noise; 48% is no better than a coin flip on unseen data.
- Model B's small train/test gap shows it captured a real, repeatable pattern.
Why it happens
Too many features, small datasets, excessive model complexity — and markets that are inherently noisy (news, economic shocks, investor behaviour, randomness).
How to reduce it
Use simpler models, gather more data, insist on out-of-sample testing, and engineer better features.
Out-of-sample testing & walk-forward validation
Out-of-sample testing evaluates the model on unseen, independent data, giving a realistic proxy for future performance. It is more honest than in-sample scores, validates better, and exposes overfitting early.
Walk-forward validation makes this repeatable: train on a window, test on the next slice, then roll the window forward and repeat. The model is always tested on data that came after its training window — exactly how it would face the real world.
flowchart LR
subgraph R1[Round 1]
A1([Train])-->B1([Test])
end
subgraph R2[Round 2]
A2([Train])-->B2([Test])
end
subgraph R3[Round 3]
A3([Train])-->B3([Test])
end
B1 -.->|move window| A2
B2 -.->|move window| A3
classDef s fill:#eef2ff,stroke:#4f46e5,stroke-width:1px,color:#3730a3;
classDef d fill:#ecfdf5,stroke:#0d9488,color:#0d9488;
class A1,A2,A3 s
class B1,B2,B3 d
Walk-forward validation: train, test on the next window, move the window forward, repeat — so every test is genuinely out of sample.
Evaluating predictions: R², MAE, MSE
We measure prediction quality to compare models and detect overfitting. The common metrics are R², MAE (mean absolute error) and MSE (mean squared error). The course focuses on R².
where the residual and total sums of squares are
- $R^2 = 1$ — perfect predictions ($SS_{res}=0$).
- $R^2 = 0$ — no better than predicting the mean every time.
- $R^2 < 0$ — worse than the mean; the model actively hurts.
- Actual values $[10, 12, 14]$, predicted $[11, 12, 13]$. The mean of the actuals is $\overline{\text{actual}} = \tfrac{10+12+14}{3} = 12$.
- Residual sum of squares: $SS_{res} = (10-11)^2 + (12-12)^2 + (14-13)^2 = 1 + 0 + 1 = 2$.
- Total sum of squares: $SS_{tot} = (10-12)^2 + (12-12)^2 + (14-12)^2 = 4 + 0 + 4 = 8$.
- Combine: $R^2 = 1 - \dfrac{SS_{res}}{SS_{tot}} = 1 - \dfrac{2}{8}$.
Models: regression and decision trees
Linear regression intuition
A linear regression takes feature inputs — momentum, volatility, volume — and combines them with learned weights to output a predicted return. For example: positive momentum, low volatility and high volume might combine into a positive predicted future return.
Decision trees
A decision tree is a series of yes/no decisions arranged like a flowchart. It is easy to interpret — you can read the path that led to any decision.
flowchart TD
A{Volume > average?} -->|Yes| B{Momentum positive?}
A -->|No| H([Hold])
B -->|Yes| C([Buy])
B -->|No| H
classDef s fill:#eef2ff,stroke:#4f46e5,stroke-width:1px,color:#3730a3;
classDef d fill:#ecfdf5,stroke:#0d9488,color:#0d9488;
class A,B s
class C,H d
A simple decision tree: only when volume is above average and momentum is positive does it signal Buy; otherwise Hold.
| Aspect | Linear regression | Decision trees |
|---|---|---|
| Complexity | Simpler, faster | More flexible, more complex |
| Relationships | Linear weighting of inputs | Non-linear, rule-based splits |
| Overfitting risk | Lower | Higher (can split until it memorizes) |
| Interpretability | Coefficients per feature | Readable decision path |
Why ML often fails in finance
- Noise — most movement is random, drowning real signal.
- Regime changes — the relationship the model learned stops holding.
- Overfitting — great in-sample, useless out-of-sample.
- Transaction costs — fees, spread and slippage erase a thin edge.
- Signal decay — once an edge is found and traded, it fades.
Self-check: Why might ML help when many candidate signals exist?
Self-check: Is "tomorrow's return" a valid feature for predicting tomorrow's return?
Self-check: Model A scores 95% train / 48% test; Model B scores 60% / 58%. Which is overfit?
Self-check: Why not just train on all available data?
Key takeaways
- Features matter — relevant, stable, intuitive, and never leaking the future.
- Validation matters — test on unseen data; train/test splits exist to measure generalization.
- Overfitting is dangerous — a big train/test gap means memorized noise, not signal.
- Simpler can be better — fewer features and simpler models often generalize more.
- ML is a tool, not magic — markets are noisy and there are no guaranteed profits.