Basic Prerequisites: 01 · From Zero and 02 · Feature Construction.


1. Intuition & Practical Objective

The target is the question the model is trained to answer. Get the question wrong and no amount of modelling helps. This page diagnoses the standard question - “predict the return over the next bars” - and replaces it with a question whose answer is exactly the P&L of the trade you would actually have made.

The diagnosis has three parts:

  1. Fixed horizons are arbitrary. No economic force expires in exactly bars. The meaningful limits are profit, loss, and time.
  2. Fixed horizons ignore the path. The label is a function of the endpoint; the trade is a function of the whole path. Paths that would have been stopped out get labelled as if the stop never existed.
  3. Fixed horizons mishandle volatility. A 2% move is a triumph in a 0.5%-vol regime and a rounding error in a 5%-vol regime. Barriers must scale with ex-ante volatility, .

The Triple-Barrier Method fixes all three at once: place an upper barrier (profit target), a lower barrier (stop-loss), and a vertical barrier (maximum holding time); label by the first barrier touched. It is the labelling scheme the folder is built on, and the numbers below reproduce its behaviour.

The one-sentence essence. “Label each observation by which barrier the price path touches first - profit, stop, or time - where the horizontal barriers scale with a volatility estimate the model could not have known at entry, and the vertical barrier is your maximum holding period.”


2. Mathematical Ground Truth & Derivations

The three barriers. At event time with price and ex-ante volatility target :

  • Upper (profit-taking): ;
  • Lower (stop-loss): ;
  • Vertical (expiration): .

The first-touch time and label are

Barrier configurations (LdP §3.4). A configuration is a triplet with active / disabled; the eight cases include three useful ones - standard, “exit on time unless stopped”, “profit unless stopped” - and two illogical ones ( aimless, no label). Never use or .

The volatility target must be ex-ante. must be estimated from data . A standard choice is the EWMA (RiskMetrics) recursion

with – bars. Equivalently, a trailing realised standard deviation over a look-back window. Using a full-sample makes the barrier widths depend on the future - the leak analysed on 05 · Failure Modes.

Event sampling. Labels exist per event, and events need not occur every bar. Sampling every bar produces massively overlapping labels (one label per return, each used by up to of them). A symmetric CUSUM filter starts a new event only when the cumulative move since the last event exceeds a threshold (often one daily ), concentrating sampling where information is (LdP §2.5.2 and the exercises of Ch 3).

Choosing the vertical-barrier value. The vertical barrier is a holding-period limit and should match the frequency at which your features are refreshed. A 20-bar horizon on daily bars is a one-month maximum holding period - a decision about the strategy, not about the data.


3. Computational Implementation - triple-barrier labels on a realistic series

This builds an EWMA volatility target, samples events, and labels each by the first barrier touched. It also shows the key empirical fact: the triple-barrier label distribution is much closer to balanced than a naive horizon sign, and the barriers move with volatility. Standard library only.

For symmetric barriers under a driftless random walk, the +1/−1 split is near 50/50 (here 49.9%/50.1%) - as it must be. The information is not in the balance; it is in the fact that each label now corresponds to an actual exit event, and that the barrier width changed over time (mean 0.0201, but varying). With asymmetric barriers (), the split becomes deliberate - e.g. encodes a stop that is twice as wide as the target, mapping the “cut losses, let profits run” structure directly into the target.


4. Failure Modes & First-Principles Breakdowns

  1. Look-ahead in the volatility target. Using a full-sample or centred lets the barrier width “know” future volatility. Fix: EWMA/rolling estimators computed only from data .
  2. Choosing by looking at labels. Tuning the vertical barrier so that labels look balanced (or so the eventual model score is highest) is backtest overfitting applied to the label. Fix from the strategy, then evaluate.
  3. Symmetric-by-default barriers. Real strategies often want asymmetric barriers (); forcing bakes in a stylised bet and silently changes the label’s meaning.
  4. Vertical-barrier label ambiguity. When the vertical barrier is first, the label is the sign of the return (LdP’s preference) or (a third class). Mixing the convention between train and test corrupts the target; pick one and document it.
  5. Ignoring transaction costs in the barrier. If the profit target is smaller than round-trip cost, the “+1” label is economically a loss. Barriers should sit outside the cost band (see Backtesting Hygiene · Failure Modes).
  6. Events on every bar by reflex. Overlapping labels inflate the apparent sample and leak across CV folds; neither is visible in a train/test score. Sample events on information (CUSUM) and weight for overlap (page 06).

5. References

  • López de Prado, M.: Advances in Financial Machine Learning (2018)
  • Tsay, Ruey S.: Analysis of Financial Time Series
  • Hastie, Tibshirani & Friedman: The Elements of Statistical Learning

6. Connected Graph Bridges