Learn / Track VI / lesson 4 of 5
Features and labels for machine learning in trading
Most ML trading projects fail before the model is chosen. How to frame the target, build features, and avoid the classic traps.
Start with the label
“Predict tomorrow’s return” is the default framing and usually a poor one. Daily returns are mostly noise, the target ignores the path, and a model that is right 52% of the time can still lose money if its losers are larger than its winners.
Fixed-horizon labels
Label each bar if the return over the next bars exceeds a threshold, if below the negative threshold, else 0. Simple, but it ignores volatility (a fixed threshold is huge in calm markets and tiny in wild ones) and ignores the path (a trade that hit your stop on the way would have been closed).
The triple-barrier method
For each potential entry, set three barriers:
- an upper barrier (profit take) at ,
- a lower barrier (stop loss) at ,
- a vertical barrier (time limit) after bars.
The label is whichever barrier is touched first. Because the barriers scale with current volatility and mirror how a real trade is managed, the labels match what a strategy could actually have earned.
Meta-labelling
Use a simple primary model (say, a moving-average crossover) to decide the side. Train a secondary ML model only to predict whether the primary signal will be right, and use its probability to size the bet or skip the trade. This keeps the ML model’s job narrow, improves precision, and works well with the Kelly idea of sizing by confidence.
Features
Good features are stationary, available in real time, and economically motivated:
- Returns over several lookbacks, volatility, volatility-of-volatility.
- Distance from moving averages, measured in units of volatility.
- Volume, order-flow imbalance and spread (see the order book).
- Funding rates, basis, open interest in crypto (carry).
- Cross-asset context: market beta, sector moves, rates.
Raw prices are non-stationary and poor features. Plain returns are stationary but throw away memory. Fractional differentiation finds a middle ground: difference the series by a fractional order between 0 and 1, the smallest that passes a stationarity test, to keep as much memory as possible.
Models
Gradient-boosted trees (XGBoost, LightGBM) are the workhorse for tabular features: robust, fast, and interpretable via feature importance. Deep sequence models can help with large, high-frequency datasets but overfit easily on the small samples typical of daily data.
- Leakage through features computed with future data or labels that overlap test periods. Use purged cross-validation.
- Class imbalance: a model that always predicts “no move” can score high accuracy. Use precision, recall, and — most importantly — the P&L of the resulting strategy after costs.
- Non-stationary relationships: a model trained on one regime can fail in the next. Retrain on a schedule and monitor live drift.
- “AI-optimised” marketing: many vault specs labelled AI or ML are conventional indicators with an optimiser on top. Read the code.
Put it to work
Build this idea
A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.
/agenkit build an ML signal research pipeline with triple-barrier labelling, stationary fractionally-differentiated features, purged cross-validation, and meta-labelling for bet sizing- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- Get tests, backtest, risk controls and a paper-trading switch