Adaptive LMS Filter
A fixed-coefficient filter - a moving average, an EMA, a regression fitted once on history - is tuned at one point in time and then held fixed while the market's statistics drift underneath it. An LMS adaptive filter does the opposite: after every single observation it nudges each of its weights in the direction that would have reduced the prediction error it just made. Over enough observations the weights converge toward whatever linear relationship currently best predicts the target from its inputs, and because the update never stops, the filter re-converges automatically as that relationship slowly changes, with no manual refitting and no discrete retraining step. The core update is the textbook LMS rule: weight[i] += stepSize * error * input[i], applied to a small ring of lagged inputs held in the filter's own state. Two uses share this one mechanism. As a self-predictor, the inputs are the series' own recent lagged values and the target is the next value - a genuinely adaptive linear predictor, useful mainly as a comparison baseline against a fixed model to see whether adaptivity actually buys anything on a given instrument. As a noise canceller, the inputs are a reference series correlated with unwanted noise in the target (a correlated instrument's return, for instance), and the filter learns to predict and subtract that noise component, leaving a cleaner residual as its output. Step size is the whole trade-off in one number: too large and the filter chases noise and can become unstable; too small and it adapts too slowly to track a real change. LmsSuggestStepSize gives a normalised starting point derived from the input's own average power - the standard way to keep plain LMS stable across inputs of very different scale rather than guessing a number that happened to work on one symbol. A second update rule, LmsUpdateNormalized, implements NLMS: it divides the effective step by the instantaneous input power at every single update rather than a fixed pre-computed value, which keeps convergence speed roughly consistent even while the input series' own volatility is changing over time - raw price is the textbook case, since its scale drifts with the instrument. The demo runs both variants side by side across a synthetic series whose amplitude jumps partway through and shows the difference directly in RMS error. A small optional leakage term decays every weight slightly toward zero on each update, which is cheap insurance against weight values drifting without bound on an input with a persistent, unmodelled bias. Notes and limitations - LMS finds a LINEAR relationship between its inputs and the target. A strongly nonlinear relationship will converge to the best linear approximation available, not the true nonlinear structure. - Step size choice matters more than tap count in practice. Start from LmsSuggestStepSize (plain LMS) or a small value like 0.05-0.1 (NLMS, which is already scale-normalised) and adjust from measured RMS error, not from theory alone. - An in-sample RMS error, as LmsFitPriceSelfPredictor reports, measures how well the filter tracked history it has already seen. It is not a forecast of out-of-sample accuracy - validate on data the filter has not adapted to before trusting it for anything live. - This is a signal-processing tool, not a trading system. It contains no order logic and places no trades. - The demo's convergence test uses MathRand() for noise, so its exact numbers vary between platforms; the qualitative result (weights settle near the true coefficients) should not.
Script · MT5 · v1.0 · Free
About Adaptive LMS Filter
Adaptive LMS Filter is a script for MetaTrader 5. It has 3 settings, such as Taps, Synthetic bars and Price bars. MQL source code included.
Files in the download: LmsAdaptiveFilter.mq5.mqh, LmsAdaptiveFilter.txt, LmsAdaptiveFilterDemo.ex5, LmsAdaptiveFilterDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Taps | 4 | Number of lagged inputs the filter learns from |
| Synthetic bars | 400 | Length of the synthetic training series |
| Price bars | 300 | Bars of live Close used for the price fit test |
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: Moving Average · MT5 Script · MQL5 Source Code