Hilbert Transform Sine Wave (HT_SINE)

Calculate the Hilbert Transform SineWave (HT_SINE), or the MESA Sine Wave over a fixed window: the sine of the phase of the prices’ main cycle and its lead, whose crossings mark the turning points of the cycle.

The sine wave is the sine of the dominant cycle phase and the lead sine the sine of the phase advanced by 45 degrees. While the price cycles, the two lines cross shortly before the turning points of the cycle: the sine crossing above the lead sine marks a low, crossing below a high. While the price trends, the phase barely moves and the two lines run apart without crossing. The MESA Sine Wave is Ehlers’ sinewave indicator over a fixed window: the phase of the price within a cycle of that window, measured with a discrete Fourier transform, where the Hilbert Transform SineWave measures the window as the dominant cycle.

The formula is as follows, per method:

“standard”:

\[\text{Smooth} = \frac{4 \cdot \text{Price} + 3 \cdot \operatorname{Price}(t-1) + 2 \cdot \operatorname{Price}(t-2) + \operatorname{Price}(t-3)}{10}\] \[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 \operatorname{Period}(t-1) + 0.54)\] \[\text{Detrender} = H(\text{Smooth}),\;\; Q_{1} = H(\text{Detrender}),\;\; I_{1} = \operatorname{Detrender}(t-3)\] \[I_{2} = 0.2 \cdot (I_{1} - H(Q_{1})) + 0.8 \cdot \operatorname{I2}(t-1)\;\;\text{and}\;\; Q_{2} = 0.2 \cdot (Q_{1} + H(I_{1})) + 0.8 \cdot \operatorname{Q2}(t-1)\] \[\text{Re} = 0.2 \cdot (I_{2} \cdot \operatorname{I2}(t-1) + Q_{2} \cdot \operatorname{Q2}(t-1)) + 0.8 \cdot \operatorname{Re}(t-1)\] \[\text{Im} = 0.2 \cdot (I_{2} \cdot \operatorname{Q2}(t-1) - Q_{2} \cdot \operatorname{I2}(t-1)) + 0.8 \cdot \operatorname{Im}(t-1)\] \[\text{Period} = \frac{360}{\operatorname{arctan}\left(\frac{\text{Im}}{\text{Re}}\right) \;\text{in degrees}},\;\; \text{limited to between 0.67 and 1.5 times Period(t-1) and to between 6 and 50 periods},\;\; \text{then smoothed as}\; 0.2 \cdot \text{Period} + 0.8 \cdot \operatorname{Period}(t-1)\] \[\text{Dominant Cycle Period} = 0.33 \cdot \text{Period} + 0.67 \cdot \text{Dominant Cycle Period}(t-1)\] \[N = \text{the Dominant Cycle Period rounded to the nearest whole period}\] \[\text{Real} = \sum \sin\left(\frac{360 \cdot i}{N}\right) \cdot \operatorname{Smooth}(\text{t-i})\;\;\text{and}\;\; \text{Imag} = \sum \cos\left(\frac{360 \cdot i}{N}\right) \cdot \operatorname{Smooth}(\text{t-i}) \;\text{for}\; i = 0 \;\text{to}\; N - 1\] \[\text{Dominant Cycle Phase} = \operatorname{arctan}\left(\frac{\text{Real}}{\text{Imag}}\right) + 90 + \frac{360}{\text{Dominant Cycle Period in degrees}},\;\; \text{plus 180 when Imag is negative and minus 360 when above 315}\] \[\text{Sine} = \sin(\text{Dominant Cycle Phase})\] \[\text{Lead Sine} = \sin(\text{Dominant Cycle Phase} + 45 \;\text{degrees})\]

“mesa”:

\[\text{Real} = \sum \sin\left(\frac{360 \cdot i}{\text{Window}}\right) \cdot \operatorname{Price}(\text{t-i})\;\;\text{and}\;\; \text{Imaginary} = \sum \cos\left(\frac{360 \cdot i}{\text{Window}}\right) \cdot \operatorname{Price}(\text{t-i}) \;\text{for}\; i = 0 \;\text{to Window} - 1\] \[\text{Phase} = \operatorname{arctan}\left(\frac{\text{Real}}{\text{Imaginary}}\right) + 90 \;\text{degrees},\;\; \text{plus 180 when Imaginary is negative},\;\; \text{or}\;\; 90 \cdot \operatorname{sign}(\text{Real}) + 90 \;\text{when} \;\left|\text{Imaginary}\right| \;\text{is at most}\; 0.001\] \[\text{Sine} = \sin(\text{Phase})\] \[\text{Lead Sine} = \sin(\text{Phase} + 45 \;\text{degrees})\]

Also known as: HT_SINE, Hilbert Transform SineWave, sine wave, lead sine, MSW, MESA Sine Wave, MESA sinewave.

Example · AAPL, MSFT · 2021 to 2025

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

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_sine_wave().xs("AAPL", level=1, axis="columns")

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

How did the Hilbert sine wave and lead sine of Apple move in 2025?

Which returns:

Parameters

get_hilbert_sine_wave 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”, in which case the high and low of the median price are adjusted for dividends in the same way."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 sine wave. 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
methodstrThe sine wave. Options are: “standard” (the Hilbert Transform SineWave), “mesa” (the MESA Sine Wave over a fixed window). Defaults to “standard”."standard"
median_priceboolWhether to filter the median price (High + Low) / 2, as Ehlers does, or the close of close_column, as Tulip Indicators and pandas-ta-classic do. Only used when method=”mesa”. Defaults to True.True
windowintThe number of periods of the cycle. Only used when method=”mesa”. Defaults to 5.20
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.
  • Paper Ehlers, J.F. (1992). “MESA and Trading Market Cycles.” John Wiley & Sons.
  • Library Referenced against TA-Lib’s HT_SINE
  • Library Referenced against Tulip Indicators’ msw (MESA Sine Wave)
  • Library Referenced against pandas-ta-classic’s msw (MESA Sine Wave)
  • Library Shares similar logic as pandas-ta-classic’s ht_sine
Share