Loading…

Savitzky-Golay Smoothing And Derivative Filter

A moving average fits a degree-0 polynomial - a constant - to each window and keeps that constant. That is why it always flattens and lags a rounded peak: a constant cannot bend to follow one. A Savitzky-Golay filter fits a higher-degree polynomial to the same window by least squares and keeps the fitted curve's value at the window's centre instead, which tracks curvature a constant fit necessarily cannot. This is the standard filter in signal processing wherever the shape of a feature matters, not just its rough location - and the same property is what makes it useful for finding a turning point in price data more precisely than a moving average crossing. Because the least-squares fit is linear in the window's values, the whole operation reduces to a fixed convolution kernel that depends only on the window size and the polynomial order, never on the data itself. This header derives that kernel in closed form by solving the fit's normal equations with a small Gauss-Jordan matrix inversion - at most a 7x7 system for any sane polynomial order - rather than shipping a lookup table of precomputed kernels or depending on another library's matrix code. The kernel is also exposed directly through SavitzkyGolayCoefficients for anyone who wants to inspect it or apply it with their own convolution loop. The same closed-form derivation gives derivative estimates almost for free: requesting derivative order 1 returns a noise-resistant slope estimate, order 2 a curvature estimate, and so on up to the polynomial's own degree. This matters because a plain finite difference (price[i] - price[i-1]) amplifies noise rather than filtering it - differentiating noisy data directly is usually the wrong move, and a Savitzky-Golay derivative is the standard fix. The demo's slope output crosses zero close to the true peak in a series built with a known peak location, which a raw difference on the same noisy data does not do cleanly. Edge bars - the first and last halfWindow entries of a series, which do not have a full window on both sides - are filled with SG_NO_VALUE rather than computed from an asymmetric one-sided fit, which this header deliberately does not attempt. See Notes And Limitations for why that is an honest simplification rather than an oversight. Notes and limitations - Edge bars are filled with SG_NO_VALUE, not computed. A proper edge treatment needs a different, asymmetric kernel per edge position, which this header does not build - always check for SG_NO_VALUE before reading an entry within halfWindow bars of either end of the array, most commonly the newest (rightmost) bars of a live series, which is exactly where a trading decision is usually being made. - polyOrder is capped at SG_MAX_ORDER (6) because the internal matrix has a fixed compile-time size. This is far beyond any sane setting in practice - values above 4 or 5 rarely improve anything and mainly reintroduce noise sensitivity - but the cap is a hard limit, not a soft recommendation. - The derivative functions assume the samples are one unit apart. On a price series that unit is one bar; if the true spacing is not uniform (a tick series, a session with gaps) the raw output needs an external time scale correction the header does not attempt. - Coefficients are recomputed on every call rather than cached. For a fixed halfWindow/polyOrder/derivativeOrder combination called every tick inside a tight loop, compute the kernel once with SavitzkyGolayCoefficients and apply it yourself rather than calling SavitzkyGolaySmooth repeatedly.

Script · MT4 · v1.0 · Free

About Savitzky-Golay Smoothing And Derivative Filter

Savitzky-Golay Smoothing And Derivative Filter is a script for MetaTrader 4. It has 2 settings, such as Half window and Poly order. MQL source code included.

Files in the download: SavitzkyGolay.mqh, SavitzkyGolay.txt, SavitzkyGolayDemo.mq4.

Settings

SettingDefaultWhat it does
Half window5Half-window; full window is 2*this+1 samples
Poly order3Polynomial degree fitted to each window

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