Trend Scanning Labels

Calculate López de Prado’s trend-scanning labels: the sign of the most significant linear trend in the prices after every date.

For every date it fits a linear trend to the prices of each forward window from minimum_window to maximum_window periods and takes the window whose slope is the most significant. The label is the sign of that slope and its t-value the strength of the trend. Unlike a fixed horizon, the method lets the data decide how long the trend after a date lasts, and labels the dates without a clear trend with a small t-value rather than a forced sign.

The formula is as follows:

\[\text{for}\;\; L = \text{minimum\_window} \;\ldots \;\text{maximum\_window:} \;\text{OLS of}\; p_{t+l} = b_{0} + b_{1} x l + e,\;\; l = 0 \ldots L-1\] \[t(b_{1}) = \frac{b_{1}}{\sqrt{\frac{\frac{\text{SSR}}{L - 2}}{\sum((l - \operatorname{mean}(l))^{2})}}}\] \[L^{*} = \text{the}\; L \;\text{with the largest} \;\left|t(b_{1})\right|\] \[\text{Label} = \operatorname{sign}(t(b_{1}) \;\text{at} \;L^{*}),\;\; \text{t-Value} = t(b_{1}) \;\text{at} \;L^{*},\;\; \text{Holding Period} = L^{*} - 1\;\;\text{and}\;\; \text{Return} = p_{t} + L \cdot - \frac{1}{p_{t}} - 1\]

Labels look forward by construction: the label of a date describes the prices of up to maximum_window periods after it.

Also known as: trend scanning, trend-scanning method, t-value labels.

Example · AAPL, MSFT · 2024 to 2025

Install the Finance Toolkit with pip install financetoolkit -U, then call get_trend_scanning_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_trend_scanning_labels()

labels.xs("MSFT", 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 Microsoft by its strongest forward trend, with its t-value, return and holding period.

Which returns:

Parameters

get_trend_scanning_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"
minimum_windowintThe shortest forward window in prices, at least 3. Defaults to 5.5
maximum_windowintThe longest forward window in prices, at least the minimum window. A maximum window longer than the data leaves every date without a label, which is logged. Defaults to 20.20
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. (2020). “Machine Learning for Asset Managers.” Cambridge University Press, section 5.4 (Snippets 5.1 and 5.2).
  • Library Referenced against RiskLabAI’s find_trend_using_trend_scanning
Share