Machine learning in trading
Models that learn patterns from data, such as recent returns and indicators, to predict what a price does next.
4 min readLesson 57 of 57
In short
A model is shown features and what happened afterward, and learns a rule. Markets are mostly noise, so the honest test is whether it beats a lucky guess on data it never trained on, after trading costs. Most models don't.
Why it matters
Machine learning has transformed image recognition, translation and search, and some of the most successful investment firms use it. The tools are now free, so it's natural to ask: can a model learn from past prices to predict the next move?
Sometimes, a little. But markets are a hard place for it. The signal is faint, the noise is loud, the patterns change as people trade on them, and there are only so many years of data. Learning how to test a model honestly matters far more than which model you pick, because the easiest thing to build is one that fools you.
How it works
A model learns from examples. Each example is a set of features, numbers known at the time, such as the last week's and month's returns, the RSI, the MACD, the gap between the price and its moving averages, recent volatility and trading volume. Each has a label: what happened next, such as whether the stock rose over the following week. The model looks for a rule that maps features to labels.
Models differ in how flexible they are. Logistic regression weighs each feature and adds them up into a probability: simple, and hard to overfit. A random forest grows many decision trees on different samples of the data and lets them vote. Gradient boosting builds trees one after another, each correcting the last one's errors. Flexible models can capture more complex patterns, and fit noise more easily, which is Overfitting again.
The test must copy real life: train only on the past, predict the next stretch, then move forward and repeat. This is walk-forward testing, and every prediction is out of sample. Randomly mixing past and future data when training lets the model peek at the answers, and makes results look far better than they can be.
Then compare with honest baselines. Chance isn't always 50%: if a stock rose in 54% of weeks, always guessing "up" scores 54%. A shuffled-label baseline trains the same model on labels randomly shuffled, which destroys any real link; whatever it scores is luck. And a strategy built on the predictions should beat buy and hold after trading costs, since switching in and out every week is expensive.
Standard error of accuracy = √(accuracy × (1 − accuracy) ÷ number of predictions); 95% range ≈ accuracy ± 1.96 × standard error
With few predictions, a measured accuracy could easily be a few points off the truth.
A worked example
A model makes 1,000 weekly up-or-down predictions in a walk-forward test, and gets 530 right. Over the same weeks, the stock rose 540 times. A shuffled-label version of the same model scored 52.5%.
- Accuracy: 530 ÷ 1,000 = 53%.
- The lazy baseline, always guessing "up": 540 ÷ 1,000 = 54%. The model is worse than not thinking at all.
- Uncertainty: √(0.53 × 0.47 ÷ 1,000) ≈ 0.0158, so the 95% range is 53% ± 3.1 points, from 49.9% to 56.1%.
- That range includes 50%, 54% and the shuffled model's 52.5%: the model can't be told apart from luck.
- Costs: if acting on the predictions means about 20 round trips a year at 0.2% each, that's 20 × 0.2% = 4% a year to overcome.
Result: 53% sounds better than a coin flip, but it's below the lazy guess, within the range luck produces, and the trading it needs would cost about 4% a year. This is how most trading models look when tested honestly.
How to read it
- Accuracy above the base rate, not just 50%
- The first bar to clear. Compare with always guessing the more common outcome.
- Beats the shuffled-label baseline
- Only meaningful if the gap is bigger than the confidence ranges. A model that can't beat shuffled labels learned nothing real.
- Deflated Sharpe ratio
- A Sharpe ratio adjusted for how many variations were tried and how unusual the returns were. A low value means the result is likely luck from trying many things.
- High turnover
- Predictions that flip often mean frequent trades, and costs can erase a small edge entirely.
In the real world
- A few secretive firms, such as Renaissance Technologies, have earned extraordinary returns with statistical models. They combine many tiny edges, vast amounts of data, low trading costs and decades of engineering, which is very different from one model on a few features.
- Academic studies have found that machine learning can predict stock returns slightly better than simple methods, but much of the gain shows up in small, illiquid stocks that are expensive to trade, and it shrinks after costs.
- Researchers Marcos López de Prado and David Bailey developed the deflated Sharpe ratio because so many published and marketed strategies were the lucky survivors of thousands of backtests.
Common mistakes
- Shuffling the data randomly before splitting it into training and testing. The model then learns from the future, which is impossible in real trading.
- Comparing accuracy with 50% instead of the base rate and a shuffled-label baseline.
- Trying many models, features and settings, then reporting only the best. That's Overfitting with extra steps.
- Ignoring trading costs. A model that trades every week needs a big edge just to stand still.
- Using features that weren't known at the time, such as a figure revised later. Even a small leak of future information makes a backtest look great.
See it in MarketLab
Test yourself
Optional: 4 quick questions on the words, using the idea, and how it plays out in real markets.
Loading your answers…