Loading…

Weighted Least Squares

Ordinary least squares assumes every observation is equally reliable and minimises the plain sum of squared residuals - which silently lets the noisiest points dominate the fit exactly as much as the cleanest ones. Financial and market data is routinely heteroscedastic: a recent, choppy stretch is often noisier than a calm historical one, and a high-volatility regime is noisier than a low-volatility one. Fitting ordinary least squares straight through such data lets a handful of wild high-variance points drag the line away from where the low-variance majority actually sits. Weighted least squares minimises the WEIGHTED sum of squared residuals instead: give each point a weight inversely proportional to its own variance and the fit is pulled toward the points that deserve more trust, which is the statistically efficient thing to do once the variances are known or can be reasonably estimated. WlsWeightsFromVariances builds exactly those inverse-variance weights from a supplied variance-per-point array, and WlsFit solves the resulting weighted normal equations in closed form for the intercept and slope - no iteration required for the single-predictor case this header covers. The demo builds a synthetic dataset on a known line where the noise standard deviation grows steadily with x, fits both plain OLS and WLS side by side, and compares each against the true generating intercept and slope to show WLS recovering the line more accurately once the noisier tail is properly down-weighted. Notes and limitations - Single predictor only (y = intercept + slope*x). A multiple-regression version needs matrix algebra this plain-MQL4 header deliberately avoids. - WlsFit needs the true (or a reasonably estimated) per-point variance to build correct weights - if the variances themselves are guessed badly, WLS can do worse than plain OLS, not better. This header solves the weighting arithmetic; estimating realistic variances from real market data is a separate problem for the caller. - Passing an array of all 1.0 as weights reproduces plain OLS exactly, since every point is then trusted equally - useful for a quick side-by-side comparison, as the demo does. - WlsFit returns false if the weighted x-spread is too close to zero (e.g. every weighted x value effectively identical) - a slope cannot be estimated from a single effective x value, and the function refuses rather than dividing by a near-zero denominator.

Script · MT5 · v1.0 · Free

About Weighted Least Squares

Weighted Least Squares is a script for MetaTrader 5. It has 5 settings, such as Sample size, True intercept and True slope. MQL source code included.

Files in the download: WeightedLeastSquares.mq5.mqh, WeightedLeastSquares.txt, WeightedLeastSquaresDemo.ex5, WeightedLeastSquaresDemo.mq5.

Settings

SettingDefaultWhat it does
Sample size50number of synthetic observations
True intercept2.0known generating intercept
True slope0.5known generating slope
Noise base0.2noise stdev at x=1
Noise growth0.06extra noise stdev per unit of x

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