Incremental Beta Engine
The naive way to track a rolling beta is to keep the last N returns of both the asset and the benchmark in an array and re-run the whole covariance and variance sum every time a new bar closes - O(N) work per update, and N grows without bound if nobody remembers to cap and trim the array. This library instead keeps six running accumulators (sample weight, both means, and three sums for covariance/variance-of-benchmark/ variance-of-asset) and updates them in O(1) per new (benchmark, asset) return pair using West's online covariance recurrence: the OLD mean's residual for the benchmark is multiplied against the NEW mean's residual for the asset (and symmetrically for the two variances). That specific ordering is what keeps the running sums numerically exact without ever storing or revisiting a past sample. An optional decay factor (lambda) turns the same accumulators into an exponentially-weighted engine instead of a plain cumulative one - existing accumulators are scaled down by lambda before each new sample is folded in, so older observations fade out smoothly rather than needing a fixed-size window to drop out of. Lambda = 1.0 (the default) disables decay entirely and behaves as plain cumulative statistics since the account's history began. Notes and limitations - This is a single-factor (market-model) regression only - one benchmark, not a multi-factor model. - BetaEngineGetBeta() and BetaEngineGetRSquared() return 0.0 until at least two samples have been fed in, and also 0.0 if the benchmark has shown exactly zero variance so far (nothing to regress against) - callers should treat a very early or degenerate reading as provisional, not a real estimate. - With decay enabled (lambda < 1.0), sampleCount is a raw count of updates fed in and does NOT reflect the decayed effective sample size - use it only to know "has enough history accumulated to trust this yet", not as an exact weight. - Returns must be computed by the caller (e.g. simple percentage returns from consecutive closes, as the demo does) - this library only consumes return pairs, it does not read price series itself.
Script · MT5 · v1.0 · Free
About Incremental Beta Engine
Incremental Beta Engine is a script for MetaTrader 5. It has 4 settings, such as Benchmark symbol, Bars to use and Decay lambda. MQL source code included.
Files in the download: IncrementalBetaEngine.mq5.mqh, IncrementalBetaEngine.txt, IncrementalBetaEngineDemo.ex5, IncrementalBetaEngineDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Benchmark symbol | EURUSD | Benchmark symbol to regress against |
| Bars to use | 60 | How many daily closed bars to stream through the engine |
| Decay lambda | 1.0 | 1.0 = plain cumulative, e.g. 0.95 = fading weight on older bars |
| Print every | 10 | Print a progress line every N samples |
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