Loading…

Wiener Filter Denoiser

Most "adaptive smoothing" is a moving average or exponential filter with a fixed or heuristically-adjusted weight - it has no notion of what is actually signal versus noise in the data it is given. A real Wiener filter is different: it is the LINEAR filter that is mathematically optimal (minimum mean squared error) given the signal's and noise's own second-order statistics, derived by solving the Wiener-Hopf equations. This header solves those equations from the data itself rather than asking the caller to specify a smoothing factor. The textbook Wiener filter is usually presented in the frequency domain as H(f) = Sss(f) / (Sss(f) + Snn(f)), which needs an FFT - MQL4 has none. This header instead solves the exactly equivalent time-domain (autocorrelation) formulation, guaranteed identical by the Wiener- Khinchin theorem: WienerAutocorrelation estimates the noisy signal's own sample autocorrelation Rx(k) directly; WienerEstimateNoiseVariance estimates the measurement noise level from the mean squared FIRST DIFFERENCE of the data (a standard, model-free estimator - a real signal is assumed smooth relative to noise, so most of the difference energy is noise); the implied CLEAN signal's autocorrelation is then Rs(k)=Rx(k) for k>0 and Rx(0) minus the noise variance at k=0; and the resulting Toeplitz system Rx * w = Rs is solved EXACTLY by Gaussian elimination with partial pivoting for the optimal FIR tap weights. WienerApplyFilter then convolves those fitted taps across the series, renormalising the truncated sum at the very start of the series (before a full tap window of history exists) so the filter degrades gracefully rather than silently under-weighting its first few outputs. Notes and limitations - WIENER_MAX_TAPS (24) is a compile-time constant at the top of the header - raise it there for a longer filter, at O(tapCount^3) fit cost from the Gaussian elimination solve. - Assumes the measurement noise is white (uncorrelated between samples) - the noise variance estimator and the Rx(0)-only correction both rely on this. Correlated noise (e.g. from an upstream smoothing step applied before this filter sees the data) will bias the fit. - WienerFitFilter requires at least 4x tapCount observations for a usable autocorrelation estimate; fewer will refuse rather than fit an unreliable filter silently. - This is a NON-CAUSAL-optimal filter applied CAUSALLY (using only past and current samples, never future ones) - it is the best linear smoother available under that causal constraint, not the theoretical best possible smoother that would also use future samples (that is what a genuine two-sided/RTS smoother, such as this catalogue's KalmanSmootherEm.mqh, is for instead).

Script · MT4 · v1.0 · Free

About Wiener Filter Denoiser

Wiener Filter Denoiser is a script for MetaTrader 4. It has 5 settings, such as Series length, Ar coefficient and True noise var. MQL source code included.

Files in the download: WienerFilterDenoiser.mqh, WienerFilterDenoiser.txt, WienerFilterDenoiserDemo.mq4.

Settings

SettingDefaultWhat it does
Series length400Length of the synthetic test series
Ar coefficient0.97AR(1) coefficient generating the smooth true signal
True noise var1.0True measurement noise variance injected on top
Tap count12Number of FIR taps the Wiener filter fits
Random seed808Seed so the synthetic series is repeatable

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 · Buy/Sell Arrows · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria