Loading…

Incremental Moment Engine

A sliding-window statistic looks simple to compute until the window has to slide: a value must leave as well as arrive, and the moment-based formulas most code copies from a textbook only cover a GROWING series, not one that also shrinks from the back. The lazy shortcut - keep a ring buffer and recompute mean/variance/skew/kurtosis from scratch across the whole window every time it moves - works, but its cost scales with the window size on every single call, which defeats the point of an "incremental" engine the moment the window or the update rate grows. This library keeps running power-sum moment accumulators (M1 through M4) using West's incremental extension of Welford's algorithm for insertion, and a second set of recurrences - the exact algebraic inverse, solved by hand from the same forward equations - for removal. Every Update() call does one insertion and, once the window is full, exactly one removal: bounded work regardless of how large the window is configured to be. A small ring buffer of raw values is still kept, but only as bookkeeping for "what value must leave next" - no moment is ever recomputed from it. The demo cross-checks every reading against a brute-force O(n) recompute over the same window contents, so the claim that the fast path tracks the reference implementation to floating-point precision is verified in the Experts log, not just asserted in this description. Notes and limitations - Variance/skewness/kurtosis are POPULATION forms (divide by n, not n-1) - the window is being described as a whole, not treated as a sample estimating a larger outside population. - Skewness and kurtosis both return 0.0 when the window's variance is effectively zero (a flat/constant window), rather than dividing by near-zero and returning a meaningless spike. - The moment accumulators are floating point and DO accumulate a tiny amount of drift over very many updates, the same as any running-sum technique - the demo's brute-force cross-check is there specifically to make that drift visible and bounded rather than assumed away. - BruteForceMean()/BruteForceVariance() exist purely for verification; calling them on every tick defeats the entire point of this library.

Script · MT4 · v1.0 · Free

About Incremental Moment Engine

Incremental Moment Engine is a script for MetaTrader 4. It has 2 settings, such as Window size and Sample count. MQL source code included.

Files in the download: IncrementalMomentEngine.mqh, IncrementalMomentEngine.txt, IncrementalMomentEngineDemo.mq4.

Settings

SettingDefaultWhat it does
Window size20Sliding window size (values)
Sample count200Total values fed through the engine

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