Hilbert Transform And Analytic Signal
A moving average answers "what has price done lately." It cannot answer "where in the current cycle is price right now" - that needs an estimate of phase, not just level. The Hilbert transform builds a second series alongside price, shifted 90 degrees ("quadrature"), so that price and its quadrature companion behave like the real and imaginary parts of a rotating vector. The angle of that vector is the instantaneous phase; its length is the instantaneous amplitude; and the rate the angle rotates at gives the dominant cycle's period - all three updated every bar, with no fixed lookback window to choose. This is a direct port of the FIR-based quadrature construction published in John Ehlers' "Rocket Science for Traders" - the same building block underneath MESA, the Sinewave indicator and Ehlers' adaptive moving averages. A smoothed price feeds a detrending FIR filter, which feeds a pair of further FIR filters producing the in-phase (I) and quadrature (Q) components; those are smoothed and rotated against their own prior values to extract phase change per bar, from which the period is derived and bounded to a sane 6-to-50-bar range so one noisy bar cannot make the estimate jump to something implausible. The FIR coefficients (0.0962, 0.5769) and the period-adaptive bandwidth term are Ehlers' published constants, not free parameters - changing them changes the filter's passband and produces a different filter, not a tuned version of this one. The whole thing is streaming: HilbertState holds only fixed-size 7-bar lag buffers for each intermediate series, updated one bar at a time by HilbertUpdate. Nothing here re-scans chart history on every call, so it is cheap enough to run on every tick rather than only once per new bar. Notes and limitations - The first 7 bars after HilbertInit are warm-up; HilbertUpdate returns false during that window and the outputs should not be used yet. - Real markets rarely contain one clean dominant cycle the way the demo's synthetic sine wave does - expect the period estimate to wander within its 6-to-50-bar bounds rather than lock onto a single value, especially in a trending or choppy market with no genuine periodicity. - The phase output uses a plain arctangent of Q2/I2, which has a 180-degree ambiguity at the quadrant boundaries the same way Ehlers' original TradeStation code does - treat phase as useful for tracking rotation rate and rough cycle position, not as an exact 0-360 degree reading at every bar. - This is a signal-processing primitive, not a trading signal on its own. It places no trades and issues no buy/sell decision; combine its output with your own entry logic.
Script · MT4 · v1.0 · Free
About Hilbert Transform And Analytic Signal
Hilbert Transform And Analytic Signal is a script for MetaTrader 4. It has 3 settings, such as Synthetic bars, Synthetic period and Live price bars. MQL source code included.
Files in the download: HilbertTransform.mqh, HilbertTransform.txt, HilbertTransformDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Synthetic bars | 200 | Bars of synthetic sine wave to process |
| Synthetic period | 20.0 | True period (bars/cycle) of the synthetic wave |
| Live price bars | 300 | Bars of live close prices to process |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Moving Average · Buy/Sell Arrows · MT4 Script · MQL4 Source Code