Hilbert Transform Trend vs Cycle Mode (HT_TRENDMODE)

Calculate the Hilbert Transform Trend vs Cycle Mode (HT_TRENDMODE): whether the prices are trending (1) or moving in a cycle (0).

The trend mode tells whether the price trends (1) or moves in a cycle (0). Ehlers’ rules: a crossing of the sine and the lead sine starts a cycle, which lasts at least half a dominant cycle period; a phase that advances at the rate of the dominant cycle is a cycle; and a smoothed price 1.5% or more away from the instantaneous trendline is always a trend. Cycle indicators (oscillators) fit the cycle mode, trend-following indicators the trend mode.

The formula is as follows:

\[\text{Trend} = 1,\;\; \text{or 0 with the periods in trend restarting from zero when the Sine and the Lead Sine cross (see the Hilbert Transform SineWave)}\] \[\text{Trend} = 0 \;\text{while the periods in trend are fewer than half the Dominant Cycle Period}\] \[\text{Trend} = 0 \;\text{when the change of the Dominant Cycle Phase is between}\; 0.67 \;\text{and}\; 1.5 \;\text{times} \;\frac{360}{\text{Dominant Cycle Period}}\] \[\text{Trend} = 1 \;\text{when} \;\frac{\left|\text{Smooth} - \text{Trendline}\right|}{\text{Trendline}} \geq 0.015 \;(\text{see the Instantaneous Trendline})\]

Example · AAPL, MSFT · 2021 to 2025

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

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_hilbert_trend_mode()

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

Were Apple and Microsoft trending or moving in cycles in 2025, according to the Hilbert Trend Mode?

Which returns:

Parameters

get_hilbert_trend_mode 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"
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 trend mode. 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 Ehlers, J.F. (2001). “Rocket Science for Traders: Digital Signal Processing Applications.” John Wiley & Sons.
  • Library Referenced against TA-Lib’s HT_TRENDMODE
  • Library Shares similar logic as pandas-ta-classic’s ht_trendmode
Share