Finance Fluent

Machine Learning for Stock Selection:Validation, Leakage, and Why Most Models Do Not Survive

Machine learning for stock selection without fooling yourself: which models to try, walk-forward validation, where data leakage hides, and what survives costs.

A walk-forward validation split: six folds of expanding training data, each with a one month embargo and a twelve month test windowA walk-forward validation split: six folds of expanding training data, each with a one month embargo and a twelve month test window
Machine LearningA walk-forward validation split: six folds of expanding training data, each with a one month embargo and a twelve month test window

Machine learning applied to stock selection means training a model to rank securities by expected future return, using features you supply, and holding the ones it ranks highest. The appeal is that a model can find interactions between variables that a linear factor screen cannot.

The reality is that most such models fail, and they fail for reasons that have little to do with the algorithm. They fail because the validation was wrong, because the data leaked, or because the edge they found was smaller than the cost of trading it. This article is mostly about those three things, because the modelling part is the easy part.

Start with quantitative analysis if the underlying testing discipline is new, since everything here builds on it.

What machine learning actually adds

A linear factor model says: rank stocks by some weighted combination of value, momentum and quality. Each feature contributes independently and in a straight line.

Machine learning relaxes both assumptions. A gradient-boosted tree can learn that momentum is predictive only when liquidity is above some threshold, or that cheapness helps in one volatility regime and hurts in another. Those are interactions and non-linearities, and they are genuinely present in markets.

The honest caveat is that this extra flexibility is exactly what makes the model dangerous. Financial data has a low signal-to-noise ratio, and a flexible model in a noisy dataset will fit the noise beautifully. A linear model can only be wrong in simple ways. A boosted tree can be wrong in extraordinarily specific ways that look, on your training data, exactly like insight.

Always fit the linear benchmark first. If the machine learning model cannot beat a regularised linear model out of sample, you have not found non-linearity, you have found a way to overfit more efficiently.

Which models to try, and in what order

Start with a regularised linear model, move to a tree ensemble such as gradient boosting only if the linear model leaves something on the table, and treat neural networks as a last resort that needs far more data than most stock-selection problems have. Each step adds flexibility, and flexibility is exactly what overfits.

Model What it adds When it earns its place
Ridge or lasso regression Shrinks noisy coefficients; lasso also drops weak features Always, first: the benchmark everything else has to beat
Random forest Interactions and non-linearity, averaged across many trees A sturdy second step, and hard to overfit badly
Gradient boosting (LightGBM, XGBoost) The same, fitted more aggressively Often the strongest on tabular features, and the easiest to overfit
Neural network Very flexible functions of many inputs Only with a wide feature set and a long, clean history

The largest public comparison of these families is Gu, Kelly and Xiu (2020) in the Review of Financial Studies, which tested them on decades of US stock returns. Trees and neural networks beat linear models there, with the gain coming mostly from interactions between predictors, and every method agreed on a dominant set of predictors that includes variations on momentum, liquidity and volatility.

Two cautions travel with that result. It rests on a dataset far larger and longer than most markets offer with clean fundamentals, and its headline numbers are before trading costs. On a smaller universe, expect the flexible models' advantage to shrink, because they have less evidence to learn interactions from, and expect costs to take a share of whatever is left.

Validation is where most of these projects die

This is the single most important section, and it is the one most tutorials get wrong.

Standard k-fold cross-validation splits data randomly into folds, trains on some and tests on others. On a time series that is invalid, because some of your training data comes from after your test data. The model is being shown the future and then asked to predict the past, and it will do so impressively.

Two validation schemes on a timeline. Random k-fold cross-validation places a test block in the middle with training data on both sides, so the model trains on data from after the test period. Walk-forward validation always trains on data that precedes the test block. Random k-fold: invalid on time series train test train, but this is the future information flows backwards

Walk-forward: train only on the past test test test earlier later

The top scheme reports excellent scores and predicts nothing, because the training set contains information from after the test period. The bottom scheme mimics what actually happens in production: you only ever know the past. Expect walk-forward results to be substantially worse, because they are substantially more honest.

Walk-forward validation trains on a block of history, tests on the period immediately following it, then slides forward and repeats. The results are less impressive and they are the only ones that mean anything.

Two refinements matter in practice. Leave a gap between the end of training and the start of testing, because a target defined as a forward return overlaps the training window and leaks. And test across several distinct market regimes, since a model trained and validated entirely within one bull market has demonstrated only that the bull market continued.

Purging, embargo and combinatorial purged cross-validation

That gap has standard names. Purging removes training rows whose label window overlaps the test period. An embargo also drops a short stretch after each test block. Combinatorial purged cross-validation applies both across many train and test splits, so you get a spread of results instead of one number. All three come from Marcos López de Prado's Advances in Financial Machine Learning.

Purging. Suppose the target is the return over the next 20 trading days. A training row dated 15 days before the test block starts has a label that ends 5 days inside it: the model learned from prices it is about to be tested on. Purging deletes every training row whose label window reaches into the test period, which for a 20-day target means the last 20 days before each test block.

Embargo. Features such as volatility or momentum are built from overlapping windows, so the days just after a test block still carry information from inside it. An embargo drops a few days after each test block before training resumes. It only needs to cover how long information from the test block lingers in your features; what matters is that its length is decided before you test, not tuned until the score improves.

Combinatorial purged cross-validation. Walk-forward testing gives you one path through history, and one path can be lucky. Splitting the history into, say, six blocks and testing on every combination of two, purging and embargoing each time, produces many backtest paths from the same data. A model whose score holds up across all of them is more convincing than one that wins on the single path walk-forward happens to take.

None of this protects you from trying too many models. Purging fixes leakage inside one experiment; it does nothing about running the experiment a hundred times and keeping the best. That is the multiple-testing problem, and the way to correct for it is in quantitative analysis.

Where leakage hides

Leakage is the use of information that was not available at decision time. It is rarely obvious and it is fatal.

Fundamentals belong to their publication date, not their period. A March quarter result is public weeks after March ends. Joining it to March dates means trading on numbers nobody had. This is the most common leak in Indian equity datasets and it produces spectacular backtests.

Index membership is known only in hindsight. Screening on today's constituent list applies a filter that embeds years of survival. Build the universe as it stood on each historical date, which the NIFTY index methodology describes well enough to reconstruct.

Prices need corporate-action adjustment before anything else. Bonuses and splits create artificial gaps, and any feature computed on an unadjusted series inherits the error.

Normalisation across the full sample leaks. Standardising a feature using the mean and standard deviation of the entire history embeds future information into every past observation. Compute those statistics on the training window only.

Choosing the target, which decides everything downstream

The target variable is a modelling decision people make carelessly and then live with.

Predicting raw forward return means the model spends most of its capacity learning the market direction, which is the same for every stock and is not what you wanted. Predicting return relative to the index isolates the cross-sectional question of which stocks beat which, which is usually the actual question.

Predicting a binary label such as top-quintile membership is easier to model and discards magnitude, so a stock that beats by 40 percent counts the same as one beating by half a percent. Predicting continuous excess return keeps the magnitude and is noisier.

Neither is right in general. What matters is that the target matches how you will trade: if you intend to hold the top decile, train the model to rank, and evaluate it on ranking quality rather than on classification accuracy.

The features worth building

Group What it captures Note
Momentum and trend Persistence over three to twelve months The most robust effect, and the most crowded
Value Price relative to fundamentals Slow-moving; needs long horizons to express
Quality Profitability, stable earnings, low leverage Works over longer horizons than most backtests cover
Liquidity and microstructure Spread, turnover, depth Frequently important, and often a proxy for a risk you do not want
Volatility regime Realised volatility, dispersion Useful as a conditioning variable rather than a signal

That fourth row deserves attention. Liquidity features often rank highly in importance scores, and the reason is usually that illiquid stocks have higher returns because they are illiquid. The model has rediscovered a risk premium and labelled it alpha, and the strategy will not survive the slippage of actually trading those names.

Overfitting, and how to tell

Every hyper-parameter you tune uses up information in your sample. Test enough configurations and one will look excellent by chance alone.

The practical defences are unglamorous. Keep a final holdout period you examine exactly once, at the end, and accept its verdict. Prefer fewer features that you can justify economically over many that merely correlate. Watch the gap between training and validation performance, because a large gap is the definition of overfitting, and be suspicious of improvement that arrives only after extensive tuning.

The most useful single check is stability. Re-fit the model on slightly different periods and see whether its important features stay important. A model whose drivers reshuffle completely between windows has not learned anything durable, whatever its scores say.

It also helps to keep a record of every configuration you tested rather than only the one you kept. The count matters: a result significant at the five percent level means little as the twentieth thing you tried, because roughly one in twenty random configurations clears that bar by chance. Most people do not keep that count, which is precisely why the published result so often fails to repeat.

From model scores to an actual portfolio

A trained model outputs a number per stock. Converting those numbers into positions involves several decisions that affect returns more than the model does, and they are usually made by default rather than deliberately.

How many names to hold. Taking the top decile of a 200-stock universe gives 20 positions, which is roughly where idiosyncratic risk stops mattering. Taking the top five concentrates into the names where the model is most confident, and model confidence is not the same as being right. Concentration amplifies both the signal and the estimation error.

Whether to weight by score. Equal weighting across the selected names is robust and ignores information. Weighting by score uses more of the model's output and gives the largest positions to the most extreme predictions, which are also the predictions most likely to be noise. Equal weighting wins more often than it should.

How often to rebalance. This is the decision that quietly determines whether the strategy is viable. Monthly rebalancing on a 20-stock portfolio can easily turn over a large fraction of holdings each period, and each turn pays brokerage, exchange fees, taxes and slippage. Compute the annual turnover, multiply by a realistic round-trip cost, and subtract it from the backtested return before deciding anything. Many models that appear to add value do so at a rate lower than the cost of harvesting it.

Constraints on the result. An unconstrained top decile can end up concentrated in one sector, which converts a stock-selection strategy into a sector bet without your having chosen one. Sector caps and position limits cost a little theoretical return and remove a large amount of unintended exposure.

None of these are modelling questions, and all of them are decided before a single position is opened.

When the answer is not machine learning

Complexity has to earn its place, and often it does not.

If the model cannot beat a linear factor model after costs, the extra machinery is adding risk of error without adding return. If the strategy demands high turnover to express its edge, transaction costs will consume it, and this is worth calculating early rather than discovering late. And if the dataset is small, and Indian equity history with clean fundamentals is not long, a flexible model has too much freedom relative to the evidence available.

For portfolio-level implementation of whatever signal survives, see portfolio construction, and for sizing it responsibly, risk management. For the analytics primitives, quantflow-finance is the open-source library I maintain.

The algorithm is the least interesting part of this problem. Data hygiene and honest validation decide it, and they decide it long before the model is trained. Spend your time there and the choice of model becomes almost incidental.

Read next

The rest of the shelf

Everything else on the site, newest first. On a wide screen this index travels sideways as you scroll. Tab into it and it becomes an ordinary grid, so nothing you are focused on is ever parked off-screen.

Income each year from a five-rung bond ladder and from a single bond when rates fall two points, against an unchanged-rate baseline

Portfolio Management

Income Portfolio Construction

Build a portfolio for cash flow rather than headline yield: duration and reinvestment, yield traps, REITs and InvITs, and stress-testing the income itself.

22 Jun 202515 min

A simulated macro composite score with its regimes shaded, and beneath it the capped factor tilt it implies a month later

Portfolio Management

Macro-Driven Factor Rotation

How to turn liquidity, PMI and yield-curve data into modest factor tilts, with guardrails, honest measurement, and the case that timing often fails.

14 Jun 202513 min

A year of simulated daily returns tested against a normal-model 99 percent VaR, with the exceptions marked and placed on the Basel traffic light

Risk Management

Monte Carlo Value-at-Risk, Step by Step

How Monte Carlo VaR works end to end: covariance, Cholesky shocks, full revaluation, fat tails, convergence, backtesting and expected shortfall.

06 Jun 202518 min

A simulated price series above its RSI and MACD panels, with the bars where both indicators agree marked

Technical Analysis

RSI and MACD Confluence, Tested Honestly

How RSI and MACD are computed, what combining them can and cannot add, and how to test a confluence rule against overfitting, costs and data snooping.

30 May 202516 min

The z-score of a simulated pair spread crossing its two-standard-deviation entry bands and reverting to zero

Quantitative Analysis

Statistical Arbitrage and Pairs Trading

How pairs trading works, why cointegration beats correlation, how half-life and z-score thresholds set the trade, and why costs and breaks decide the result.

24 May 202514 min

The payoff of a long call butterfly at expiry, with the maximum at the middle strike, both breakevens and the debit marked

Options & Trading

Butterfly Option Structures Explained

How butterfly options work: the payoff, how a butterfly settles at expiry, why iron and long butterflies are the same trade, and the risk in a broken wing.

16 May 202513 min

An iron condor payoff at expiry, showing the two credit zones, the flat band of net credit and the capped loss on each wing

Options & Income

Credit Spreads and Iron Condors

How credit spreads and iron condors work, what the premium compensates you for, the margin reality, and the payoff shape that catches option sellers out.

11 May 202511 min

A protective put payoff drawn against the unhedged portfolio line, with the floor, the breakeven and the constant cost of the premium

Options & Hedging

Hedging a Portfolio with Index Options

How to hedge an equity portfolio with index options: how much of it to hedge, sizing the contracts, what puts and collars really cost, and when not to bother.

29 Apr 202512 min

An equity curve against its high-water mark with the underwater plot beneath, and the gain the deepest drawdown demands to recover

Risk Management

Risk Management Essentials

Why drawdowns are harder to recover than they look, what Value at Risk does and does not tell you, and how to size positions so a bad month stays survivable.

22 Apr 202510 min

A two-asset efficient frontier, the straight line it would be if the assets were perfectly correlated, and the minimum-variance point

Portfolio Management

Portfolio Construction: Theory and Process

What portfolio construction is, the theory behind it, the process step by step, and why mean-variance optimisers build portfolios nobody should hold.

13 Apr 202513 min

A scatter of 150 forward returns against factor exposure, with the fitted least-squares line and its standard error

Quantitative Analysis

Quantitative Analysis in Finance

How quantitative analysis works: handling market data properly, the statistics that earn their place, factor models, and the four ways a backtest lies to you.

06 Apr 202513 min

A price series with its 50-bar moving average, and a dimension line measuring how many bars later the average turns

Technical Analysis

An Introduction to Technical Analysis

Technical analysis explained honestly: what the charts actually show, which claims survive testing, and how to check whether an indicator works at all.

25 Mar 202511 min

The balance sheet identity drawn as two stacked columns of equal height, assets on one side and liabilities plus equity on the other

Fundamental Analysis

Fundamental Analysis Explained

Fundamental analysis in practice: which financial statement to trust, what the ratios really answer, and the governance red flags that precede most failures.

24 Mar 20259 min

One subject, worked out in full rather than summarised.

Open the full index