Loading…

Age Weighted Statistics

A plain arithmetic mean over an entire history weights a reading from a year ago exactly the same as one from a second ago, so it reacts to genuine drift very slowly. A fixed rolling window reacts faster, but has a hard edge: the instant an old sample falls out of the window it is discarded completely, producing a visible step in the statistic even though nothing special happened to the underlying series at that moment - and it needs a stored history array plus a window-length choice that is easy to get wrong. AgeWeightedStatistics fades every past observation out smoothly and continuously instead, governed by one parameter: a half-life expressed as a number of updates. AwsUpdate() is O(1) per call and needs no stored history at all - the running mean and variance are the entire state. The recursion used (mean += (1-lambda)*diff; variance = lambda*(variance + diff*incr), following West 1979) is the standard exponentially weighted moving variance formula, not a rolling-window average computed incrementally. AwsEffectiveSampleSize() reports the standard (1+lambda)/ (1-lambda) figure - roughly how many equally-weighted plain observations the current estimate is statistically "worth" - useful for deciding whether a given half-life has actually accumulated enough history yet to trust. Notes and limitations - The first observation seeds the mean directly and starts variance at 0.0, rather than blending against an arbitrary zero starting point - this avoids biasing every early reading toward zero, but means the variance estimate itself needs a few updates before it is meaningful. - The variance reported is a population-style age-weighted figure, not a small-sample-corrected (Bessel-style) one - there is no well-defined degrees-of-freedom correction for an infinite exponentially decaying window, so no such correction is applied. - AwsEffectiveSampleSize() is the standard asymptotic approximation (1+lambda)/(1-lambda), capped at the actual number of updates seen - treat it as a rough guide to how "settled" the estimate is, not an exact sample size. - This tracks ONE scalar series per AwsState. A tool tracking several properties (spread, slippage, ATR proxy, ...) needs one AwsState per property, updated independently.

Script · MT4 · v1.0 · Free

About Age Weighted Statistics

Age Weighted Statistics is a script for MetaTrader 4. It has 2 settings, such as Half life updates and Total updates. MQL source code included.

Files in the download: AgeWeightedStatistics.mqh, AgeWeightedStatistics.txt, AgeWeightedStatisticsDemo.mq4.

Settings

SettingDefaultWhat it does
Half life updates10.0Half-life of the exponential weighting, in update count
Total updates60Total number of synthetic updates fed to 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: MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria