Loading…

Total Variation Denoiser

A moving average treats every jump in a series the same way: it blurs it over the width of its window, whether that jump is noise or a real level change. That is exactly backwards for data with genuine structural breaks - a real support/resistance flip, a session-open gap, a regime change - which a trader usually wants preserved sharply while the noise around it gets cleaned up. Total variation (TV) denoising minimises a different objective: the sum of squared deviation from the raw data, plus a penalty on the TOTAL AMOUNT of variation in the output (the sum of |consecutive differences|), not its shape. A single large jump costs the same in this penalty whether it happens over one bar or spread across ten - so the optimiser has no incentive to smear it out, and every incentive to remove variation from flat, noisy stretches where it is "cheap" to flatten without moving far from the data. This is the real Rudin-Osher-Fatemi (ROF) technique, solved here by explicit gradient descent on an epsilon-smoothed version of the objective (sqrt(d^2+eps^2) standing in for |d|, since a genuine absolute value has no gradient exactly where a flat region needs one) - a documented and standard approach, distinct from Chambolle's dual-variable algorithm but minimising the same energy. TotalVariationOf reports the sum of |consecutive differences| directly, so a script can confirm a denoised series really did reduce total variation versus the raw input - the concrete, checkable claim this technique makes that a moving average cannot. Notes and limitations - This is an EXPLICIT gradient-descent scheme, not Chambolle's dual- variable algorithm - chosen so the whole method fits in one straight loop with no auxiliary dual array, at the cost of needing more iterations and a genuine numerical-stability constraint on epsilon and stepSize: the local diffusion coefficient scales roughly like 1/epsilon^2 exactly where a flat region sits (d_i near zero), so too small an epsilon combined with too large a stepSize makes the iteration oscillate and diverge rather than converge. The shipped defaults (epsilon 0.2, stepSize 0.05) are stable for typical price-scale data; rescale both together if your series' amplitude is very different from a handful of pips. - TvDenoise returns false rather than a meaningless result when n < 2 or any parameter is non-positive. - This smooths a fixed, already-collected window - it is not a causal real-time filter. Re-running it as new bars arrive will shift the denoised values near the trailing edge of the window as the optimiser sees more context, the same edge effect any offline smoother has. - Larger lambda can flatten a genuine but small level change along with the noise - there is no free parameter setting that is correct for every series; the demo's step-function test is a reasonable way to sanity- check a chosen lambda against data of a similar noise-to-jump ratio.

Script · MT4 · v1.0 · Free

About Total Variation Denoiser

Total Variation Denoiser is a script for MetaTrader 4. It has 4 settings, such as Lambda, Epsilon and Iterations. MQL source code included.

Files in the download: TotalVariationDenoiser.mqh, TotalVariationDenoiser.txt, TotalVariationDenoiserDemo.mq4.

Settings

SettingDefaultWhat it does
Lambda1.5TV regularisation strength
Epsilon0.2Smoothing constant (keep well above 0.05, see header)
Iterations1000Gradient descent iterations
Step size0.05Gradient descent step size

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 · Support & Resistance · Trading Sessions · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria