Recursive Least Squares
A rolling-window regression (refit OLS on the last N bars every bar) throws away every observation older than N and recomputes from scratch each time - O(N) work per bar, and a fit that can jump the instant an old, influential point drops out of the window. RLS instead keeps a running estimate of the parameter vector and its inverse-covariance (uncertainty) matrix, and updates BOTH in O(m^2) work per new observation (m = number of regressors) with no window and no refit - every past observation still influences the estimate, just geometrically down- weighted by the forgetting factor lambda. lambda=1 gives infinite memory (equivalent to a plain growing-window OLS); lambda<1 (typically 0.95-0.999) exponentially discounts old data, letting the fit adapt as the true relationship drifts - the standard trade-off in adaptive filtering and adaptive control, applied here to a rolling regression coefficient. The demo shows both halves of that trade-off directly: on a stationary relationship, lambda=1 converges cleanly and stays put; on a relationship whose slope steps to a new value partway through, lambda=1 stays anchored near a blend of both regimes (it never forgets the old data), while a forgetting factor below 1 re-tracks the new slope. Notes and limitations - The m x m inverse-covariance matrix P is carried as a flat array of length m*m (row-major), since MQL4 has no matrix type usable across function boundaries with a caller-chosen dimension. Every function takes m explicitly - passing a theta[]/P[] pair sized for a different m is the caller's responsibility to avoid, the same way it would be with any flat-array matrix convention. - RlsParameterVariance reads the diagonal of P as a rough "how settled is this coefficient" indicator, not a formal confidence interval - a real confidence interval also needs an estimate of the observation noise variance, which varies by application and is not tracked internally. - A very small or near-singular regressor (x approximately zero, or columns of x nearly collinear over a long run) can make the RLS recursion numerically fragile, as with any recursive least squares implementation; RlsUpdate guards the degenerate all-zero-regressor case by skipping the update rather than dividing by zero, but does not detect slow-building collinearity. - lambda values very close to 1 approach infinite memory (slow to adapt, very stable); lambda values well below 0.95 adapt fast but tolerate much less noise before the fit becomes jittery - the same speed/stability trade-off as any exponentially-weighted adaptive filter.
Script · MT5 · v1.0 · Free
About Recursive Least Squares
Recursive Least Squares is a script for MetaTrader 5. It has 2 settings, such as Bars per regime and Forgetting. MQL source code included.
Files in the download: RecursiveLeastSquares.mq5.mqh, RecursiveLeastSquares.txt, RecursiveLeastSquaresDemo.ex5, RecursiveLeastSquaresDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Bars per regime | 200 | observations per regime in the regime-shift test |
| Forgetting | 0.97 | forgetting factor used for the adaptive case |
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