Hilbert Transform Instantaneous Trendline

The Hilbert Transform Instantaneous Trendline of John Ehlers (2001) averages the price over its dominant cycle, measured with the Hilbert transform, so that the average removes exactly that cycle and leaves the trend. Angles are in degrees.

The formula for the Hilbert Transform Instantaneous Trendline first smooths the price with a four-period weighted moving average:

\[\text{Smooth} = \frac{4 \cdot \text{Price} + 3 \cdot \text{Price}_{t-1} + 2 \cdot \text{Price}_{t-2} + \text{Price}_{t-3}}{10}\]

It splits the smoothed price into an in-phase component \(I_{1}\) and a quadrature component \(Q_{1}\), a quarter cycle apart, with the Hilbert transform H, whose bandwidth follows the Period of the period before:

\[\text{where}\;\; H(x) = (0.0962 \cdot x + 0.5769 \cdot x_{t-2} - 0.5769 \cdot x_{t-4} - 0.0962 \cdot x_{t-6}) \cdot (0.075 \cdot \text{Period}_{t-1} + 0.54)\] \[\text{Detrender} = H(\text{Smooth})\] \[Q_{1} = H(\text{Detrender})\] \[I_{1} = \text{Detrender}_{t-3}\]

It advances both by a quarter cycle and smooths them against noise:

\[I_{2} = 0.2 \cdot (I_{1} - H(Q_{1})) + 0.8 \cdot I_{2,t-1}\] \[Q_{2} = 0.2 \cdot (Q_{1} + H(I_{1})) + 0.8 \cdot Q_{2,t-1}\]

The homodyne discriminator multiplies each by the value of the period before, which measures how far the phase turned in one period:

\[\text{Re} = 0.2 \cdot (I_{2} \cdot I_{2,t-1} + Q_{2} \cdot Q_{2,t-1}) + 0.8 \cdot \text{Re}_{t-1}\] \[\text{Im} = 0.2 \cdot (I_{2} \cdot Q_{2,t-1} - Q_{2} \cdot I_{2,t-1}) + 0.8 \cdot \text{Im}_{t-1}\]

A full cycle of 360 degrees divided by that turn is the period, which is limited to between 0.67 and 1.5 times the period before and to between 6 and 50 periods, then smoothed twice into the Dominant Cycle Period:

\[\text{Raw Period} = \frac{360}{\arctan\left(\frac{\text{Im}}{\text{Re}}\right)}\] \[\text{Limited Period} = \max\left(\min\left(\text{Raw Period}, 1.5 \cdot \text{Period}_{t-1}\right), 0.67 \cdot \text{Period}_{t-1}\right)\] \[\text{Bounded Period} = \min\left(\max\left(\text{Limited Period}, 6\right), 50\right)\] \[\text{Period} = 0.2 \cdot \text{Bounded Period} + 0.8 \cdot \text{Period}_{t-1}\] \[\text{Dominant Cycle Period} = 0.33 \cdot \text{Period} + 0.67 \cdot \text{Dominant Cycle Period}_{t-1}\]

The trendline is the simple average of the price over that cycle, rounded to the nearest whole number of periods N, smoothed with the same four-period weighted moving average:

\[N = \left\lfloor \text{Dominant Cycle Period} + 0.5\right\rfloor\] \[\text{Average} = \frac{\sum_{i=0}^{N - 1} \text{Price}_{t-i}}{N}\] \[\text{Trendline} = \frac{4 \cdot \text{Average} + 3 \cdot \text{Average}_{t-1} + 2 \cdot \text{Average}_{t-2} + \text{Average}_{t-3}}{10}\]

Where Price is the close and x(t-k) the value of x k periods ago. Because it averages over exactly one cycle, the trendline follows the trend without the swings of the cycle. Ehlers compares it with the smoothed price to tell a trend from a cycle: the further the price stays from the trendline, the more the market trends.

A moving average smooths the price by averaging it over the last n periods, so that the trend shows through the noise of single periods. It is the oldest and most widely used trend indicator: the direction of the average shows the direction of the trend, the price above (below) it an uptrend (downtrend), and other indicators, from the MACD to the Bollinger Bands, are built on it. Every moving average trades smoothness against lag: the more periods it averages, the smoother it is and the later it turns after the price. The averages differ in how they weight the prices within the window and whether they adapt their speed to the market, each a different answer to that trade-off.

Also known as: HT_TRENDLINE, instantaneous trendline, Hilbert trendline.

In the Finance Toolkit this is get_moving_average with method="hilbert_trendline", the same function as Moving Average.

Example · ^GSPC · 2021 to 2025

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

from financetoolkit import Toolkit

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

toolkit.technicals.get_moving_average(method="hilbert_trendline", window=50)

Which returns:

References

  • Paper Ehlers, J.F. (2001). “Rocket Science for Traders: Digital Signal Processing Applications.” John Wiley & Sons.
  • Library Referenced against TA-Lib’s HT_TRENDLINE (Hilbert Transform Instantaneous Trendline)
  • Library Referenced against pandas-ta-classic’s ht_trendline (Hilbert Transform Instantaneous Trendline)
Share