Loading…

Descriptive Statistics Over An Array

Mean and standard deviation tell you the centre and spread of a set of numbers, but nothing about its shape. Two trade histories can share the same mean and stdev while one is symmetric and the other has three enormous winners dragging a long right tail - a difference that changes how you should think about the strategy's risk, and one that mean and stdev alone hide completely. This library adds the statistics that need the data ordered, and therefore need it held in memory as a full array rather than streamed: Median and Percentile via linear interpolation (the same convention Excel's PERCENTILE.INC uses), Quartile1/Quartile3 and the interquartile range they imply, Mode with a tolerance for near-duplicate floating point values, and the two shape statistics - Skewness and Kurtosis (excess kurtosis, so a normal distribution reads as 0.0, not 3.0). Every order-based function sorts a private COPY of the input array, made with ArrayCopy before ArraySort runs. Nothing here reorders the caller's array. That matters because it is easy to write a stats helper that quietly sorts your price or trade array in place, and the bug it causes only shows up much later, somewhere that assumed the array was still in original order. If your data arrives incrementally rather than as a finished array - ticks, per-tick equity, anything unbounded - use Statistics.mqh from this same catalogue instead. It tracks mean, variance, min and max in O(1) memory with Welford's algorithm, but by design cannot produce a median: finding the middle value requires the values to be ordered, which means holding all of them. The two libraries are deliberately complementary rather than overlapping. Every function here is plain array and floating-point arithmetic - ArrayCopy, ArraySort, MathAbs, MathSqrt, MathFloor, MathMin and MathMax all behave identically on both platforms, so the MQL5 header is the exact same computation, unchanged, from the MQL4 source. Notes and limitations - DescSkewness returns 0.0 for fewer than 3 points and DescKurtosis for fewer than 4 - both are genuinely undefined below those sizes, not just noisy. - DescMode is built for discrete or rounded data (trade outcome buckets, R-multiples rounded to one decimal). Continuous data such as raw price returns essentially never repeats exactly, so the mode of a return series is rarely meaningful - that is a property of the data, not a bug in the function. - Every percentile-based function sorts a copy of the array on each call. For a large array (thousands of samples) called in a tight loop, sort the array once yourself and call DescPercentile-style logic manually rather than calling these repeatedly - the convenience functions favour clarity over raw speed. - Sample variance/stdev (the default) is almost always what you want for trading data, since the array you are holding is a sample of a longer process. Pass sample=false only when the array genuinely is the entire population.

Script · MT4 · v1.0 · Free

About Descriptive Statistics Over An Array

Descriptive Statistics Over An Array is a script for MetaTrader 4. It has 1 setting, such as Trade count. MQL source code included.

Files in the download: Descriptive.mqh, Descriptive.txt, DescriptiveDemo.mq4.

Settings

SettingDefaultWhat it does
Trade count15How many synthetic R-multiples to generate

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 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: MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria