Loading…

Running Statistics Accumulator

Mean and variance are usually computed the batch way: put every value in an array, sum it when asked. That works until the stream has no natural end - ticks across a session, equity checked every OnTick, spread sampled all week. Storing all of it just to get two numbers is wasteful, and on a long-running EA it eventually exhausts memory. CRunningStats keeps four numbers - count, mean, a running sum of squared deviations, and the sum - and updates them in O(1) per sample with Welford's algorithm. The textbook alternative, sum(x^2) - n*mean^2, looks equivalent on paper and is numerically dangerous: it subtracts two large, nearly equal numbers, and when the true variance is small that subtraction can come out negative from floating point rounding alone. Welford's update never forms that subtraction, so it stays accurate no matter how long the stream runs. Min, max, sum and count come along for free from the same single pass. StdError() gives the standard error of the mean, useful for a confidence band on a live-updating average without waiting for the stream to finish. This library is for data you are receiving incrementally and want a running answer for at every step. If you already hold a fixed array in memory and want median, quartiles, skewness or kurtosis - none of which a single pass can produce - use Descriptive.mqh instead. The two are complementary, not overlapping: this one trades shape statistics for the ability to run forever on O(1) memory. Notes and limitations - Mean, Variance and StdDev are only meaningful once Count() > 0 (Variance and StdError need Count() >= 2). The class returns 0.0 rather than an error code in the undefined cases - always check Count() before trusting the output on a stream that might be empty. - Sample variance (the default) is what you want almost always in trading code, because the stream you are watching is a sample of a longer process, not the whole of it. Pass sample=false only when the values genuinely are the entire population. - The class has no notion of time or windowing - it is a running total since the last Reset(), not a moving window. For a fixed-size rolling window, use RingBuffer.mqh to hold the last N values and recompute, or keep two accumulators and difference them if the window boundary is known. - Not thread safe in the sense either platform understands threads (neither has any per chart), but two EAs sharing one global instance would need external synchronisation - normally you simply declare one instance per chart.

Script · MT4 · v1.0 · Free

About Running Statistics Accumulator

Running Statistics Accumulator is a script for MetaTrader 4. It has 1 setting, such as Sample count. MQL source code included.

Files in the download: Statistics.mqh, Statistics.txt, StatisticsDemo.mq4.

Settings

SettingDefaultWhat it does
Sample count12How many synthetic samples to stream through the demo

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: Trading Sessions · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria