AnyLearn
All lessons
AIadvanced

Labelling: Deciding What You Are Actually Predicting

The default label, the sign of the return over a fixed horizon, describes a trade nobody would take: no stop, no target, and a holding period chosen by the modeller rather than by the market. This lesson builds the triple-barrier method and meta-labelling, and shows with computed numbers how much of a fixed-horizon dataset is fiction.

Updated · AI-authored, review-gated · how lessons are made

Not signed in: your progress and quiz score won't be saved.
Progress1 / 10

The label is a modelling decision, not a given

In most supervised learning the label arrives with the data. This image is a cat. This review is negative. Nobody chooses.

In finance nothing is labelled. You have a price series, and turning it into a supervised problem requires you to decide what question you are asking. That decision is made before any modelling, is rarely revisited, and determines more about your results than the model does.

Key idea: A label encodes a trade. It says what position was taken, when it was closed, and under what conditions. If that implied trade is one no desk would ever place, then a model that predicts it perfectly is worth nothing, and no amount of validation rigour will reveal the problem, because the model is answering the question you asked.

Full lesson text

All 10 steps on one page, for reading, reference, and search.

Show

1. The label is a modelling decision, not a given

In most supervised learning the label arrives with the data. This image is a cat. This review is negative. Nobody chooses.

In finance nothing is labelled. You have a price series, and turning it into a supervised problem requires you to decide what question you are asking. That decision is made before any modelling, is rarely revisited, and determines more about your results than the model does.

Key idea: A label encodes a trade. It says what position was taken, when it was closed, and under what conditions. If that implied trade is one no desk would ever place, then a model that predicts it perfectly is worth nothing, and no amount of validation rigour will reveal the problem, because the model is answering the question you asked.

2. What the default label actually says

The near-universal starting point is the sign of the return over a fixed horizon:

yt=sign ⁣(pt+hpt1)y_t = \mathrm{sign}\!\left(\frac{p_{t+h}}{p_t} - 1\right)

Written as an instruction to a trader, that reads: open a position now, hold it for exactly hh periods whatever happens, use no stop-loss, take no profit, and close at the end regardless of where the price went in between.

Three things are wrong with it, and they compound.

  • No stop. The label calls a trade a winner even if it was deeply underwater on the way.
  • A fixed holding period. Real positions close when a target or a stop is reached, not when a calendar says so, so the horizon is an arbitrary parameter with a large effect on the labels.
  • No risk scaling. A 1 percent move is a large event in a quiet regime and noise in a volatile one, but the label treats both identically.

3. How much of the dataset is fiction

Predict first

Simulate 200,000 days of returns at 1 percent daily volatility and label each day by the sign of the next 10 days. Among the days labelled 'up', what fraction would have hit a stop-loss set at one daily standard deviation before the horizon expired?

These are the labels the model is being asked to learn. It will faithfully learn to predict outcomes that a real position, with a real stop, would not have collected.

Gotcha: This error does not show up as noise. It is systematic, it correlates with volatility and with drawdown depth, and it biases the model toward exactly the trades that look best on paper and behave worst in execution.

4. The triple-barrier method

López de Prado's answer, in Advances in Financial Machine Learning (2018), is to label by whichever of three barriers the price touches first.

  1. Upper barrier, a profit target, set above the entry price.
  2. Lower barrier, a stop-loss, set below it.
  3. Vertical barrier, a maximum holding period, in time rather than price.

The label is which one is hit first: 1 for the target, -1 for the stop, and either 0 or the sign of the realised return for a timeout.

Definition: The horizontal barriers are set as multiples of a volatility estimate at the time of entry, not as fixed percentages. A 2 percent target is ambitious in a calm week and trivial in a crisis, so the barrier has to scale with the prevailing risk or the label means something different on every date.

What this buys is that the label now describes a trade with a defined risk, a defined reward and a defined exit, which is to say a trade someone could actually place.

5. Choosing the barrier width

The barrier multiple is a real parameter with a real trade-off, and the label distribution shows it directly. Simulated on driftless returns with a 10-day vertical barrier:

Triple-barrier label mix by barrier width (10-day vertical barrier)
upper barrier hitlower barrier hittimed out
percent of samples (%)0204060801000.50 sigma0.75 sigma1.00 sigma1.50 sigma
Source: Computed on 200,000 simulated driftless daily returns at 1 percent volatility; barriers set as multiples of the 10-day window sigma

Narrow barriers produce a nearly balanced two-class problem and label mostly noise, since at half a sigma the price touches something almost immediately. Wide barriers produce meaningful events and 80 percent timeouts, leaving few examples of what you care about.

In practice: The upper and lower shares are equal here because the simulation has no drift, which is a useful sanity check on any labelling implementation. If your barriers are symmetric and your labels are not roughly balanced on driftless synthetic data, the bug is in the labeller rather than in the market.

6. Implementing it

The mechanics are a search for the first touch, and the only subtle part is that the vertical barrier must be resolved together with the other two rather than after them.

import numpy as np

def triple_barrier(prices, t0, sigma, pt=1.0, sl=1.0, max_hold=10):
    """Return (label, exit_index) for an entry at index t0."""
    entry = prices[t0]
    upper = entry * (1 + pt * sigma)      # sigma is the vol estimate AT ENTRY
    lower = entry * (1 - sl * sigma)
    end = min(t0 + max_hold, len(prices) - 1)

    for t in range(t0 + 1, end + 1):
        if prices[t] >= upper:
            return 1, t
        if prices[t] <= lower:
            return -1, t
    return 0, end                          # vertical barrier: timed out

Two details decide whether this is correct. sigma must be estimated using data available at t0 and nothing after it, or you have inserted the future into the barrier itself. And the returned exit_index is not optional bookkeeping: it defines the label's time span, and the next two lessons need it to compute sample weights and to purge the validation folds.

Gotcha: On intraday or gapping data, checking only closing prices misses barriers that were touched and crossed within a bar. The label then records a timeout for a trade that was stopped out hours earlier, which reintroduces the exact error the method was designed to remove.

7. Meta-labelling: splitting direction from conviction

A primary model predicts direction. Meta-labelling adds a second model that predicts whether the primary model is right this time, and it turns out to be an easier and more useful question.

The procedure has three steps. Run the primary model to get a side, long or short. Apply the triple barrier to the trade that side implies, giving a binary outcome: did it work. Train a second model on that binary label, using features that may include the primary model's own confidence.

Primary modelMeta model
Predictswhich way to tradewhether to trade at all
Labeldirection, or triple-barrier sign1 if the primary call worked, 0 if not
Output used forthe sidethe size, or a go / no-go filter

Key idea: The split maps onto precision and recall. A primary model can be tuned for recall, catching most opportunities and accepting false positives, while the meta model supplies precision by filtering them. Optimising both at once in a single model forces a compromise; separating them lets each be tuned for what it is good at, and gives you a natural sizing signal as a by-product.

8. Where meta-labelling earns its keep

The technique is most valuable when you already have a signal you cannot or will not replace.

  • A discretionary or rules-based strategy. A portfolio manager's calls, or a moving-average crossover, produce sides. Meta-labelling learns when to back them without touching the underlying logic, which matters when that logic is a person or a system nobody wants rewritten.
  • A model you must keep interpretable. Regulators or risk committees may require an explainable primary model. The filter can be more complex, because it only ever reduces activity.
  • Anywhere you need position sizing. The meta model's predicted probability is a natural size, and this is often the largest practical gain: same trades, better allocation across them.

Gotcha: Meta-labelling cannot create an edge. If the primary model is at chance, then filtering its calls filters noise, and any apparent improvement is selection on the validation set. It improves an edge that already exists by allocating to it better, and treating it as a way to rescue a strategy that does not work is the standard misuse.

9. Which bars do you even label?

There is a decision before labelling that most pipelines never make consciously: which rows are samples at all.

Daily bars are the default and they are an odd choice. Markets do not deliver information at a constant rate; a quiet August day and a central bank announcement day are one row each. Sampling by clock time therefore oversamples quiet periods and undersamples exactly the events that carry information.

The alternatives sample on activity rather than on time.

Bar typeNew bar whenProperty
Time barsa fixed interval elapsessimple; over-samples quiet periods
Tick barsa fixed number of trades occuractivity-scaled
Volume barsa fixed volume tradescloser to constant information
Dollar barsa fixed traded value accumulatesrobust to price level and share splits

In practice: A second approach is event-based sampling: label only bars where something happened, such as a volatility breakout or a structural break. It shrinks the dataset and raises the share of samples that carry a decision, which is usually the better trade when the base rate of interesting events is low.

10. A labelling checklist

Before any feature engineering, the labelling decision should survive these questions.

  1. Write the label as a trade instruction. Entry, exit condition, stop, target, maximum hold. If it is not a trade you would place, stop here.
  2. Are the barriers scaled by volatility at entry? Fixed percentage barriers mean something different in every regime, and the volatility estimate must use only past data.
  3. Do you know each label's time span? The exit index is required for sample weighting and for purging, and a pipeline that discards it cannot be validated honestly later.
  4. What is the class balance, and is it an artefact? Heavy timeout classes usually mean the barriers are too wide for the horizon.
  5. Does the sampling scheme match where information arrives? Time bars are a default, not a decision.
  6. Sanity-check on synthetic driftless data. Symmetric barriers should give symmetric labels. If they do not, the labeller has a bug.

Key idea: Everything downstream inherits this choice. A leak introduced in the labels cannot be detected by a model, corrected by a better architecture, or caught by the validation scheme in lesson 4, because from every one of their perspectives the label simply is the truth.

Check your understanding

The lesson ends with a 5-question quiz. Take it in the player above to see your score.

  1. In a simulation at 1 percent daily volatility with 10-day fixed-horizon labels, what fraction of 'up' labels would have hit a one-sigma stop-loss first?
    • About 5 percent
    • About 31 percent
    • About 50 percent
    • Essentially none, since the label is positive
  2. Why must triple-barrier levels be set as multiples of volatility estimated at entry?
    • To keep the class balance exactly even
    • Because volatility is the only feature available at entry
    • To reduce the number of timeouts
    • Because a fixed percentage barrier represents a different-sized event in each volatility regime
  3. What does a meta-labelling model predict?
    • Whether the primary model's call will work, giving a filter and a position size
    • The direction of the next price move, more accurately than the primary model
    • The volatility used to set the barriers
    • Which bar sampling scheme to use
  4. As triple-barrier width increases from 0.5 to 1.5 sigma, what happens to the label mix?
    • The upper barrier share rises as targets become more attainable
    • The classes become progressively more imbalanced between up and down
    • Timeouts rise sharply, from about 9 percent to about 81 percent
    • The mix is unchanged, since barriers are volatility-scaled
  5. Why are dollar bars sometimes preferred to daily time bars?
    • They produce more samples, improving statistical power
    • They eliminate the need for volatility scaling in the barriers
    • They guarantee independent samples, removing the need for purging
    • Information does not arrive at a constant rate, so sampling on traded value is closer to constant information per bar

Related lessons

AI
advanced

Validation: Measuring Anything at All

Shuffled cross-validation reports 54 percent accuracy on data containing no signal, because neighbouring samples share their futures. This lesson builds purging, embargo and sample uniqueness weights, shows the illusion scaling with label overlap and vanishing when the fix is applied, and covers why a single walk-forward path is weak evidence.

10 steps·~15 min
AI
advanced

Features: Stationarity Without Erasing the Memory

Prices are non-stationary and returns are stationary but forget everything, so the standard fix throws away the level information a model needed. This lesson builds fractional differentiation, which makes the choice a dial rather than a switch, and shows by computation that most of the memory can be kept while still passing a stationarity test.

10 steps·~15 min
AI
advanced

Evaluating a Book Model Honestly

If your cost per round trip equals the move you are trying to capture, you need 100 percent directional accuracy to break even. This lesson computes that hurdle, replaces accuracy with metrics tied to a tradeable decision, and covers the capacity and latency limits that decide whether a real edge is worth anything.

10 steps·~15 min
AI
advanced

Why Reported Order Book Results Do Not Replicate

At a one-event horizon, 92 percent of mid-price labels are exactly no-change, so a model that always predicts flat scores 92 percent accuracy. This lesson computes that baseline across horizons and works through the four mechanisms that turn a genuine measurement into a number nobody can reproduce.

10 steps·~15 min