Loading…

Rolling Feature Scaler

Feature scaling for a live EA or ML-style threshold model looks simple until you notice the order of operations most examples use: keep a window of the last N values, push the newest one in, then normalise it against that same window. That leaks the current bar's own value into the mean, median and range used to score it. On a short window a single outlier drags the mean or median toward itself, which quietly flatters whatever downstream model or threshold consumes the scaled value - a bug that never shows up as a crash, only as a slightly-too-optimistic backtest. Every scaling function in this header takes the window as it stood before the new value arrives. Score first, push second - FeatureScalerUpdate() enforces that ordering directly so it cannot be gotten backwards by accident. Three scaling methods are provided: z-score (mean/stddev, the standard choice), min-max (rescaled into an arbitrary output range) and robust scaling (median/interquartile range, which resists the one wild tick that a z-score window has no defence against). A batch mode is also included for scaling a historical array in one call, useful for preparing training data for a model elsewhere in this catalogue - the warm-up period, before the window has enough history, comes back as EMPTY_VALUE rather than a misleadingly partial score. Notes and limitations - Standard deviation is population (divide by N), matching the common StandardScaler convention, not the sample form (N-1). Edit RollingWindowStdDev() if your downstream model was trained the other way. - Ring buffer order is not chronological once it wraps - fine for every statistic here, since none of them depend on the order values arrived in, only on the set of values currently in the window. - Min-max scaling extrapolates past [outMin, outMax] for a value outside the historical range rather than clamping it. Clamp the result yourself if a hard bound matters more than preserving how far past the prior extreme the new value is. - Batch series functions assume forward chronological order (index 0 = oldest). Passing a raw newest-first timeseries array without reversing it first will silently compute every window backwards.

Script · MT5 · v1.0 · Free

About Rolling Feature Scaler

Rolling Feature Scaler is a script for MetaTrader 5. It has 1 setting, such as Window. MQL source code included.

Files in the download: FeatureScaler.mq5.mqh, FeatureScaler.txt, FeatureScalerDemo.ex5, FeatureScalerDemo.mq5.

Settings

SettingDefaultWhat it does
Window5Rolling window length used throughout the demo

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 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: MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria