Triple-Barrier Labels

Calculate the triple-barrier labels of López de Prado: for every date (or event) the first of three barriers the price reaches.

A profit-taking barrier lies profit_taking daily volatilities above the price, a stop-loss barrier stop_loss daily volatilities below it and a vertical barrier horizon periods later. The label is 1 when the upper barrier is reached first, -1 for the lower barrier and the sign of the return at the vertical barrier. Unlike a label on the return over a fixed horizon, it follows the path of the price the way a trade with a profit target, a stop-loss and a time limit does, and its barriers widen and narrow with the volatility.

With a side, such as the crossovers of get_signals, the labels are meta-labels: the returns are those of the bet (multiplied by the side) and the label is 1 when the bet made money and 0 when it did not. A second model trained on them learns when to act on the first model’s signals and how much to bet, rather than which side to take.

The formula is as follows:

\[\sigma_{t} = \text{the EWM standard deviation of the period returns with} \;\text{span},\;\; \text{up to } t\] \[R_{t,k} = \left(\frac{p_{t+k}}{p_{t}} - 1\right) x \;\text{side}_{t},\;\; \text{for}\;\; k = 1 \ldots \;\text{horizon} \;(\text{side 1 without a side})\] \[\text{upper}_{t} = \text{profit\_taking} x \sigma_{t}\;\;\text{and}\;\; \text{lower}_{t} = - \text{stop\_loss} x \sigma_{t}\] \[k^{*} = \text{the first}\; k \;\text{with}\; R_{t,k} > \text{upper}_{t} \;\text{or}\; R_{t,k} < \text{lower}_{t},\;\; \text{else horizon}\] \[\text{Label} = 1 \;(\text{upper}),\;\; \text{-1 (lower) or } \operatorname{sign} (R_{t,k}^{*}) \text{ (vertical)};\;\; \text{with a side 1 when}\; R_{t,k} \cdot > 0,\;\; \text{else 0}\]

Labels look forward by construction: the label of a date describes the prices after it, up to horizon periods later, and is only known once a barrier is reached. The volatility of the barriers uses the returns up to the date only. Train a model on the labels with the features of the date itself and leave the labels’ spans out of the test set (purging, see get_label_uniqueness).

Also known as: triple barrier method, triple-barrier labeling, meta-labeling, meta labels, profit-taking and stop-loss labels.

Example · AAPL, MSFT · 2024 to 2025

Install the Finance Toolkit with pip install financetoolkit -U, then call get_triple_barrier_labels:

from financetoolkit import Toolkit

toolkit = Toolkit(
    ["AAPL", "MSFT"],
    api_key="FINANCIAL_MODELING_PREP_KEY",
    start_date="2024-01-01",
    end_date="2025-12-31",
)

labels = toolkit.technicals.get_triple_barrier_labels()

labels.xs("AAPL", level=1, axis="columns").dropna()

Or, with the Finance Toolkit MCP set up, ask an AI assistant such as Claude or ChatGPT:

Label each day of 2025 for Apple with the triple-barrier method: which barrier the price hit first, its return and how long it took.

Which returns:

Parameters

get_triple_barrier_labels accepts the following parameters.

ParameterTypeDescriptionExample
periodstrThe time period to consider for historical data. Options are: “intraday” (intraday prices), “daily” (daily prices), “weekly” (weekly prices), “monthly” (monthly prices), “quarterly” (quarterly prices), “yearly” (yearly prices). Defaults to “daily”."weekly"
close_columnstrThe column name for closing prices in the historical data. Defaults to “Adj Close”."Close"
horizonintThe number of periods to the vertical barrier. Defaults to 10.5
profit_takingfloatThe multiple of the volatility of the upper barrier, 0 for no upper barrier. Defaults to 1.0.1.0
stop_lossfloatThe multiple of the volatility of the lower barrier, 0 for no lower barrier. Defaults to 1.0.1.0
spanintThe span of the exponentially weighted volatility. Defaults to 100, as López de Prado.100, as López de Prado
minimum_targetfloatThe volatility a date needs to exceed to be labeled. Defaults to 0.0.0.0
eventsstr | pd.DataFrame | NoneThe dates to label. Options are: “cusum” (the events of the CUSUM filter of get_event_filter with its defaults), “z_score” (the events of its z-score filter with its defaults), or a DataFrame per date and ticker that is non-zero on the dates to label, such as the result of get_event_filter with other settings. Defaults to None, which labels every date.["CPI", "Unemployment"]
sidestr | pd.DataFrame | NoneThe side of the bets for meta-labels: the name of a signal of get_signals, such as “Moving Average Crossover” or “MACD Crossover”, whose crossovers are the bets, or a DataFrame per date and ticker of 1 (long), -1 (short) and 0 (no bet). Only the dates with a bet are labeled. Defaults to None, which gives the triple-barrier labels themselves.None, which gives the triple-barrier labels themselves
vertical_barrier_zeroboolWhether a date whose vertical barrier is reached first is labeled 0 rather than with the sign of its return. Defaults to False.False
roundingint | NoneThe number of decimals to round the results to. Defaults to None, which uses the rounding of the Toolkit (4 unless set otherwise).2
show_columnsstr | list[str] | NoneThe columns to keep, a name or a list of names, matched on every column level, or the rows when no column has those names (such as the line items of a financial statement). Names that do not exist are logged, and when none exist the result is returned in full. Defaults to None, which keeps every column.None, which keeps every column

References

  • Paper López de Prado, M. (2018). “Advances in Financial Machine Learning.” Wiley, chapter 3 (Snippets 3.1 to 3.7).
  • Library Referenced against RiskLabAI’s triple_barrier
  • Library Referenced against RiskLabAI’s meta_labeling
Share