Hampel Despiking Filter
A common despiking shortcut compares each point to a moving average and a moving standard deviation. Both statistics are themselves dragged toward any spike inside their own window - one bad tick inflates the mean it is being measured against and inflates the standard deviation even more, so the very spike the filter is meant to catch can weaken its own detection threshold. This header uses the textbook Hampel identifier instead, which is built specifically to resist that failure mode. At every point, HampelFilter takes the surrounding window (clamped to the array bounds near the edges, not padded or wrapped), computes the window's MEDIAN as the reference value, and computes the MEDIAN ABSOLUTE DEVIATION (MAD) of the window around that median as the spread measure. A single extreme value shifts a median by at most one rank position and barely moves a MAD at all, unlike a mean/standard-deviation pair which both move directly toward the outlier. MAD is scaled by the constant 1.4826 (the standard factor that makes MAD comparable to a Gaussian standard deviation) before being compared against the caller's threshold K, so a K of 3.0 behaves similarly to "3 sigma" in the usual sense. Any point whose deviation from the window median exceeds K scaled-MADs is replaced with the window median and flagged as an outlier; every other point passes through unchanged. A window with zero spread (a flat run of identical values) is handled explicitly rather than dividing by a zero scale. Notes and limitations - The window shrinks near the array's edges rather than padding with fabricated data - the first and last HalfWidth points are tested against a genuinely smaller window, which is honest but slightly less reliable than the interior. - Insertion sort is used for the per-window median, which is O(window^2) - fine for the small windows (a handful to a few dozen points) this kind of despiking normally uses, but not intended for windows in the hundreds or more. - This is a POINT filter, not a smoother - it leaves non-outlier points completely unchanged. It does not reduce ordinary measurement noise, only removes points that are extreme relative to their local neighbourhood. - Threshold K = 3.0 is the common literature default but is not universal; a series with genuinely heavy local volatility swings (news events, thin markets) may need a higher K to avoid flagging real moves as spikes.
Script · MT4 · v1.0 · Free
About Hampel Despiking Filter
Hampel Despiking Filter is a script for MetaTrader 4. It has 4 settings, such as Series length, Half width and Threshold k. MQL source code included.
Files in the download: HampelDespikingFilter.mqh, HampelDespikingFilter.txt, HampelDespikingFilterDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Series length | 200 | Length of the synthetic test series |
| Half width | 5 | Window half-width (window size = 2*HalfWidth+1) |
| Threshold k | 3.0 | Outlier threshold, in scaled-MAD "sigmas" |
| Random seed | 42 | Seed so the synthetic series is repeatable |
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 · MT4 Script · MQL4 Source Code