Loading…

Empirical Mode Decomposition

A moving average, a wavelet band, or a Fourier band-pass all share one assumption: that a fixed cutoff, chosen once, stays the right cutoff. On an instrument whose dominant cycle length actually changes over time, that assumption quietly degrades every indicator built on it. Empirical Mode Decomposition does not assume a fixed band at all - it derives its bands from the data itself, extracting whichever oscillation is currently fastest, subtracting it, and repeating on what remains until only a slow, essentially monotonic residual is left. Each extracted component is called an intrinsic mode function. The extraction method - sifting - is simple to state and easy to implement sloppily: find the local maxima and minima of the working signal, spline a smooth envelope through each set, subtract the mean of the two envelopes, and repeat until the result is close enough to satisfy the IMF criteria. This header follows that classical recipe with a natural cubic spline envelope (solved with a standard Thomas-algorithm tridiagonal system, no external dependency) and the Huang stopping criterion on envelope-mean amplitude, capped at a fixed maximum iteration count so a pathological or near-flat input can never hang the caller. Completeness is the property that makes this trustworthy rather than merely plausible: every IMF plus the final residual sums back to the original series exactly, because each step only ever subtracts what it just extracted. The demo verifies this directly on a synthetic series built from a known fast cycle, a known slow cycle and a linear trend, rather than asserting it. Reconstruction is selective by design. EmdReconstruct sums any contiguous range of IMFs, optionally with the residual, so a caller can rebuild a denoised series (drop the fastest IMF or two), an adaptive trend line (residual only), or a band-limited oscillator (a middle range of IMFs) from one decomposition. Neither platform can express a true jagged array, so IMFs are held as an array of a small EmdImf struct, each owning its own dynamic values[] array - the same ragged-collection pattern on both platforms, since MQL5 supports a struct with a dynamic array member exactly the way MQL4 does here. Notes and limitations - Sifting is O(iterations x n) per IMF and the spline solve is O(knots) per envelope; on a very long series with many IMFs this is real work. Running the full decomposition on every tick on a long lookback is not advised - run it on new bar close instead. - The classical algorithm has known edge effects: the spline envelope near the boundary of the window is less constrained than in the interior, which can distort the last few IMF samples. Give the window some margin beyond exactly the bars you plan to act on. - EMD_STOP_SD and the iteration cap trade sifting purity against runtime. The defaults follow the commonly cited Huang criterion; treat them as a reasonable starting point, not a proven-optimal setting for every symbol. - This is a decomposition and smoothing tool, not a signal generator. It contains no trading logic and places no orders. - Mode mixing - a single IMF containing components of clearly different scales - is a known limitation of classical EMD (Ensemble EMD, not implemented here, addresses it by averaging over noise-assisted trials at significant extra compute cost).

Script · MT5 · v1.0 · Free

About Empirical Mode Decomposition

Empirical Mode Decomposition is a script for MetaTrader 5. It has 2 settings, such as Sample bars and Max imfs. MQL source code included.

Files in the download: EmpiricalModeDecomposition.mq5.mqh, EmpiricalModeDecomposition.txt, EmpiricalModeDecompositionDemo.ex5, EmpiricalModeDecompositionDemo.mq5.

Settings

SettingDefaultWhat it does
Sample bars150Length of the synthetic and price test series
Max imfs6Cap on IMFs extracted in both tests

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: Moving Average · Trend · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria