MESA Sine Wave

The MESA Sine Wave (MSW), from Ehlers’ MESA work (1992), measures the phase over a fixed window of n periods instead of the measured dominant cycle, without smoothing the price: it suits a market whose cycle length is known. Angles are in degrees.

The formula for the MESA Sine Wave first takes a discrete Fourier transform of the price over the last n periods, which gives a real and an imaginary part:

\[\text{Real} = \sum_{i=0}^{n - 1} \sin\left(\frac{360 \cdot i}{n}\right) \cdot \text{Price}_{t-i}\] \[\text{Imaginary} = \sum_{i=0}^{n - 1} \cos\left(\frac{360 \cdot i}{n}\right) \cdot \text{Price}_{t-i}\]

Their angle is the phase within the cycle, a quarter cycle in the direction of the real part when the imaginary part is almost zero:

\[\text{Angle} = \arctan\left(\frac{\text{Real}}{\text{Imaginary}}\right) \quad \text{if } \left\lvert \text{Imaginary}\right\rvert > 0.001\] \[\text{Angle} = 90 \cdot \operatorname{sign}\left(\text{Real}\right) \quad \text{if } \left\lvert \text{Imaginary}\right\rvert \leq 0.001\]

The Phase turns that angle a quarter cycle and places it in the right half of the circle:

\[\text{Phase} = \text{Angle} + 90 \quad \text{if } \text{Imaginary} \geq 0\] \[\text{Phase} = \text{Angle} + 270 \quad \text{if } \text{Imaginary} < 0\]

The sine and the lead sine are the sines of that phase and of the phase 45 degrees ahead:

\[\text{Sine} = \sin\left(\text{Phase}\right)\] \[\text{Lead Sine} = \sin\left(\text{Phase} + 45\right)\]

Where Price(t-i) is the median price or the close i periods ago. It is read like the Hilbert Transform SineWave: the sine crossing above the lead sine marks a cycle low and crossing below it a cycle high. With a window that does not match the cycle of the market, the crossings come early or late, and in a trend they give false signals.

A sine wave indicator measures where the price stands within its cycle, its phase, and plots the sine of that phase together with a lead sine, the sine of the phase advanced by 45 degrees. John Ehlers developed it to time the turns of a cycle without the lag of an oscillator: while the price cycles, the two lines cross shortly before the turning points of the cycle, the sine crossing above the lead sine at a low and below it at a high. While the price trends, the phase barely moves and the two lines run apart without crossing, which tells a cycle from a trend.

Also known as: MSW, MESA sinewave.

In the Finance Toolkit this is get_hilbert_sine_wave with method="mesa", the same function as Hilbert Transform Sine Wave (HT_SINE).

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

Which returns:

References

  • Paper Ehlers, J.F. (1992). “MESA and Trading Market Cycles.” John Wiley & Sons.
  • Library Referenced against Tulip Indicators’ msw (MESA Sine Wave)
  • Library Referenced against pandas-ta-classic’s msw (MESA Sine Wave)
Share