Loading…

Whittaker Henderson Smoother

A moving average smooths by locally averaging nearby points, with no global notion of "how smooth" the result is as a whole. A Whittaker-Henderson smoother instead defines smoothness explicitly as the size of the order-th finite difference of the fitted series and finds the SINGLE series that best trades fidelity to the data against that smoothness penalty, globally, across every point at once - every fitted point depends on every observation, not just a local window. This is the same idea as a cubic smoothing spline (order=2 is closely related to one), phrased as a discrete, exactly-solvable linear system instead of a continuous spline basis. order controls what "rough" means: order=1 penalises point-to-point jaggedness (large first differences, pulling toward a locally constant fit); order=2 (the most common choice) penalises curvature (large second differences, pulling toward locally straight-line behaviour); order=3 penalises changes in curvature (pulling toward locally quadratic behaviour). lambda controls how hard the penalty is enforced: lambda=0 returns the data unchanged; a very large lambda pulls the fit all the way into the penalty's null space (a constant, a straight line, or a quadratic, depending on order) - the demo verifies both of these limiting cases directly against a plain OLS line fit. Minimising the penalised sum of squares reduces to the linear system (I + lambda*D'*D) z = y, where D is the order-th finite-difference operator. D'*D is symmetric positive definite and BANDED with bandwidth exactly equal to order. This library exploits that directly: it builds D'*D in compact banded storage (never a full n*n matrix) and factorises it with a banded Cholesky decomposition, giving an exact, O(n*order^2) solve - not an iterative approximation, and not the O(n^3) cost a naive dense solve would need. Notes and limitations - order is restricted to 1, 2 or 3 - the three orders with a natural trading interpretation (flat / straight-line / quadratic pull). Higher orders are mathematically possible but need a wider band and a less intuitive null space, outside this "Lite" library's scope. - lambda is supplied by the caller, not chosen automatically (no generalised-cross-validation or similar automatic-lambda-selection routine is included). The demo's Check 3 shows roughness at a few lambda values as a starting point for picking one by eye. - This is a NON-CAUSAL, whole-series smoother: every fitted point depends on every observation, including ones that come after it in time. It is the right tool for smoothing a finished historical series (backtesting, chart display, feature engineering on closed bars) - it is NOT a live/causal filter, and re-running it as new bars arrive will change already-reported past values near the growing edge of the series. - The banded Cholesky factorisation assumes I + lambda*D'*D is positive definite, which it provably is for any lambda >= 0 - WhsBandedCholesky still checks every pivot and returns false rather than trusting that invariant blindly, in case of an invalid order or malformed input.

Script · MT4 · v1.0 · Free

About Whittaker Henderson Smoother

Whittaker Henderson Smoother is a script for MetaTrader 4. It has 1 setting, such as Bars. MQL source code included.

Files in the download: WhittakerHendersonSmoother.mqh, WhittakerHendersonSmoother.txt, WhittakerHendersonSmootherDemo.mq4.

Settings

SettingDefaultWhat it does
Bars150length of the synthetic test series

How to install

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

MQL4 & MQL5 scripts · PolyMaria