Robust Statistics Toolkit
A single flash-spike bar, a fat-fingered fill, or one enormous winning trade in an otherwise ordinary sample can move an arithmetic mean and standard deviation by more than the entire rest of the sample combined - the breakdown point of the ordinary mean is 0%, meaning a single arbitrarily large value has unbounded influence on it. The median's breakdown point is 50%: half the sample would have to be corrupted before it can be moved arbitrarily far. Every estimator in this toolkit inherits some of that resistance, at different points on the efficiency-versus- robustness trade-off. MedianAbsoluteDeviation is the median's equivalent of standard deviation - the median of the absolute deviations from the median - scaled by 1.4826 by default so it estimates the standard deviation consistently on normally distributed data specifically, matching the convention most statistics packages use. TrimmedMean and WinsorisedMean sit between the ordinary mean and the median: both remove the influence of the most extreme fraction of the sample, but TrimmedMean DISCARDS that fraction entirely (the sample shrinks) while WinsorisedMean CLIPS it to the value at the trim boundary (the sample size stays the same, but no single point can pull the mean further than that boundary allows). Trading a little statistical efficiency on clean data for real protection against dirty data. HuberMean goes further: rather than a hard cutoff at some percentile, it downweights each point smoothly in proportion to how far its residual sits from the current estimate, computed by iteratively reweighted least squares (IRLS) - full weight for points close to the centre (behaving like the ordinary mean there), reduced weight further out (behaving more like the median in the tails). It starts from the median rather than the ordinary mean specifically so the very first iteration is not itself dragged off by the outliers it exists to resist. Notes and limitations - All sorting is an O(n^2) insertion sort, chosen for simplicity over an external sort include (every submission must stand alone). Fine for the tens-to-low-hundreds sample sizes typical of trade or bar statistics; not intended for thousands-of-points tick series per call. - HuberMean's scale is derived from MedianAbsoluteDeviation internally. If every point in the sample is identical (or more than half share one value, collapsing MAD to zero), HuberMean returns the median directly - there is nothing left to downweight once the scale itself is zero. - TrimmedMean and WinsorisedMean clamp trimFraction to 0.0..0.49; trimming 50% or more from each end would leave nothing (or invert the two ends), so the clamp is silent rather than an error - check the returned value against your intended fraction if you passed something outside that range. - These are location and spread estimators only - they describe where a sample's centre and typical scale sit, robustly. They do not detect or flag which specific points are outliers; a caller wanting that needs a separate residual-threshold check against one of these estimates.
Script · MT5 · v1.0 · Free
About Robust Statistics Toolkit
Robust Statistics Toolkit is a script for MetaTrader 5. It has 2 settings, such as Sample size and Outlier value. MQL source code included.
Files in the download: RobustStatisticsToolkit.mq5.mqh, RobustStatisticsToolkit.txt, RobustStatisticsToolkitDemo.ex5, RobustStatisticsToolkitDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Sample size | 30 | Number of clean points in the synthetic sample |
| Outlier value | 500.0 | Value of the single injected outlier |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: MT5 Script · MQL5 Source Code