Loading…

Adaptive Notch Filter

A fixed-frequency notch filter has to be told the frequency to remove in advance, and stops working the moment that frequency drifts - which real market cycles (session-length effects, an instrument's own periodicity) routinely do. This header implements a genuinely ADAPTIVE notch instead: the classic constrained-pole-and-zero structure from Nehorai (1985) and Regalia (1991), where the notch's centre coefficient tracks the input's dominant frequency automatically rather than being fixed once and left. The filter has one time-varying coefficient A[n] = 2*cos(theta[n]), theta[n] being the tracked angular frequency. AdaptiveNotchUpdate runs the notch recursion itself (which drives its own output toward zero energy at theta[n] by construction) alongside a second, structurally identical "gradient filter" that computes the EXACT derivative of the notch output with respect to A. A is then nudged each sample by -Mu * y[n] * gradient[n] - textbook stochastic gradient descent minimising the notch output's instantaneous squared energy, which is minimised precisely when the notch is centred on the input's true dominant frequency. This is the real constrained adaptive notch structure from the DSP literature, not a moving-average-based "detrender" wearing a notch-filter name. Rho (the pole radius, 0<Rho<1) trades off notch narrowness against adaptation speed: closer to 1 gives a narrower notch that adapts more slowly and rejects noise in the frequency estimate better; further from 1 adapts faster but tracks a noisier frequency estimate. Mu is the gradient step size, trading the same speed-versus-noise balance on the coefficient update itself. AdaptiveNotchGetFrequencyCycles recovers the current tracked frequency (as a fraction of the sample rate) via MathArccos - which does exist in MQL4, unlike the two-argument MathArctan2 this catalogue's other spectral libraries need to hand-roll. Notes and limitations - Tracks exactly ONE dominant cyclical component. A signal with two or more strong, comparably-sized cycles will have the notch converge toward whichever is momentarily dominant, and can hunt between them rather than settling cleanly. - The gradient step Mu needs tuning to the signal's own scale and noise level - too large causes the tracked frequency to oscillate rather than converge; too small tracks a genuine frequency drift too slowly to be useful. The demo's default values are tuned to its own synthetic signal's amplitude, not a universal setting. - Convergence takes a warm-up period (hundreds of samples in the demo) before the tracked frequency settles near the true value - it is not instantaneous, and the removed-component estimate during that warm-up window is unreliable. - Assumes the underlying cycle amplitude and frequency are each roughly stationary over the adaptation timescale set by Rho/Mu; a cycle that changes frequency faster than the filter can track will not be cleanly removed.

Script · MT5 · v1.0 · Free

About Adaptive Notch Filter

Adaptive Notch Filter is a script for MetaTrader 5. It has 7 settings, such as Sample count, True freq cycles and Wrong init guess. MQL source code included.

Files in the download: AdaptiveNotchFilter.mq5.mqh, AdaptiveNotchFilter.txt, AdaptiveNotchFilterDemo.ex5, AdaptiveNotchFilterDemo.mq5.

Settings

SettingDefaultWhat it does
Sample count2000Number of samples to process
True freq cycles0.08True tone frequency, in cycles per sample
Wrong init guess0.20Deliberately wrong starting frequency guess
Noise amp0.30Independent noise amplitude added to the tone
Rho0.96Pole radius (notch narrowness / adaptation speed trade-off)
Mu0.0008Gradient step size
Random seed314Seed so the synthetic tone's noise is repeatable

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: Trading Sessions · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria