Trend Detection Index (TDI)

Calculate M.H. Pee’s Trend Detection Index (TDI) and its Direction Indicator: how strongly the momentum keeps one sign over a window, which shows whether the prices trend and in which direction.

The Trend Detection Index tells a trending market from a consolidating one. It compares the net momentum over the window with how much the momentum moved over a longer window: when the momentum keeps one sign, its sum outweighs the absolute momentum of the earlier periods and the TDI is positive (a trend); when it changes sign, the sum cancels out and the TDI is negative (a consolidation). The Direction Indicator, the sum of the momentum, says which way the trend goes.

The formula is as follows:

\[\text{Momentum} = \operatorname{Close}(t) - \operatorname{Close}(t - \text{window})\] \[\text{Direction Indicator} = \operatorname{Sum}(\text{Momentum},\; \text{window})\] \[\text{TDI} = \left|\text{Direction Indicator}\right| - (\operatorname{Sum}(\left|\text{Momentum}\right|,\; \text{multiple} \cdot \text{window}) - \operatorname{Sum}(\left|\text{Momentum}\right|,\; \text{window}))\]

Pee buys when both the TDI and the Direction Indicator are positive, and sells when the TDI is positive and the Direction Indicator negative. A negative TDI says to stay out, or to trade the range. Both are in price units, so they compare over time but not across stocks with different prices.

Also known as: TDI, trend detection index, Pee’s trend detection index.

Example · AAPL, MSFT · 2021 to 2025

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

from financetoolkit import Toolkit

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

toolkit.technicals.get_trend_detection_index().xs("AAPL", level=1, axis="columns")

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

Was Apple trending in 2025 according to the Trend Detection Index?

Which returns:

Parameters

get_trend_detection_index 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"
windowintThe number of periods of the momentum and of the Direction Indicator. Defaults to 20, as in TTR’s TDI.20
multipleintThe longer window of the absolute momentum, as a multiple of window. Defaults to 2, Pee’s own.2, Pee's own
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
growthboolWhether to calculate the growth of the indicator values. Defaults to False.True
lagint | list[int]The lag to use for the growth calculation. Defaults to 1.4
standardizeboolWhether to standardize (Z-Score) the result. When combined with growth=True, standardizes the growth values instead of the raw values. Defaults to False.True
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 Pee, M.H. (2001). “Trend Detection Index.” Technical Analysis of Stocks & Commodities, 19(10).
  • Library Referenced against TTR (R)’s TDI
Share