Rolling Risk Metrics Windows
A single whole-history Sharpe ratio hides regime change. A strategy that traded excellently for the first half of a backtest and mediocre for the second half reports exactly the same blended Sharpe as one that traded mediocre throughout - the two are indistinguishable from that one number alone. RrmCompute() walks a return series with a fixed-length rolling window and recomputes three figures at every step once a full window's worth of history has accumulated: a per-window Sharpe ratio (mean divided by standard deviation, scaled by the square root of the window length), a per-window Sortino ratio (mean divided by downside deviation - only observations below a minimum acceptable return, default zero, contribute to the denominator), and a stability figure - the R-squared of an ordinary least-squares line fit to the window's cumulative return curve against step index. A perfectly straight climb inside the window scores stability close to 1.0; a choppy, mean-reverting-around-flat window scores close to 0.0 regardless of its net return, which is a genuinely different signal from Sharpe or Sortino alone. All three output series are the same length as the input, with the warm-up bars before a full window exists filled with the sentinel RRM_UNDEFINED - check RrmIsDefined() before using a value, the same pattern as reading an indicator buffer's EMPTY_VALUE bars. Notes and limitations - Sharpe and Sortino here are PER WINDOW, not annualised. Multiply by the caller's own annualisation factor (e.g. sqrt(252) for daily bars, sqrt(52) for weekly) if a comparable annual figure is needed - this header has no notion of the caller's bar spacing and does not guess one. - Sortino's downside deviation divides by the FULL window length, not the count of below-MAR observations only - this is the standard Sortino convention (a window with fewer downside observations scores a smaller, better downside deviation), not a bug. - RrmStabilityRSquared() reports 0.0 for a window with zero variance in its cumulative-return line (a perfectly flat window), since R-squared is undefined (0/0) in that case - treat a reported 0.0 stability on an otherwise flat-looking window as "no signal", not necessarily "unstable". - The rolling computation is O(window) per step and O(count) steps, so O(count * window) overall - recomputes each window from scratch rather than maintaining an incremental running statistic, trading some CPU for simplicity and numerical robustness (no drift from a long-running incremental accumulator).
Script · MT4 · v1.0 · Free
About Rolling Risk Metrics Windows
Rolling Risk Metrics Windows is a script for MetaTrader 4. It has 2 settings, such as Total bars and Window. MQL source code included.
Files in the download: RollingRiskMetricsWindows.mqh, RollingRiskMetricsWindows.txt, RollingRiskMetricsWindowsDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Total bars | 100 | Total number of synthetic return observations |
| Window | 20 | Rolling window length in observations |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: MT4 Script · MQL4 Source Code