Discrete Wavelet Transform
A moving average smooths by discarding high-frequency detail outright, and pays for it in lag proportional to the window length. A wavelet transform takes a different approach: it splits a series into an approximation (the smooth trend at half resolution) and a detail band (exactly what the approximation could not represent), and the split can be repeated on the approximation to reach coarser scales. Nothing is thrown away in the process - decompose and reconstruct with no changes reproduces the original series to floating point precision, which this library's demo verifies directly rather than asserting. That reversibility is what makes threshold-based denoising trustworthy. Zero out the small detail coefficients (the ones most likely to be noise rather than signal), reconstruct, and what comes back is a smoothed series built from an orthogonal basis change rather than a sliding average - which is why it tracks a genuine trend change with noticeably less delay than a moving average of comparable smoothness. Two wavelets are included. Haar is the simplest possible transform - pairwise sum and difference with a sqrt(2) normalisation - and is the right choice when you want to reason exactly about what a step is doing. Daubechies-4 uses four taps instead of two and produces a visibly smoother approximation for one extra multiply per sample; it is the default for anything meant to look usable on a chart. Both use periodic (wrap-around) boundary handling rather than zero-padding, which avoids introducing an edge artefact right at the most recent, most important bars. Both hard and soft thresholding are provided. Hard thresholding zeroes small coefficients and leaves the rest untouched; soft thresholding additionally shrinks the survivors toward zero, trading a small bias for a smoother result. WaveletUniversalThreshold implements the Donoho-Johnstone estimate, derived from the finest detail band's own robust noise scale, as a reasonable starting point rather than a value to trust blindly across every symbol and timeframe. Neither MQL4 nor MQL5 can express a true jagged double[][] - dynamic arrays only resize their leftmost dimension on both platforms. The multi-level functions therefore use a small WaveletBand struct (one dynamic array per level) in an array-of-structs, which is the idiomatic way to hold a ragged collection in this language, on either platform. Notes and limitations - Every multi-level function requires a power-of-two length. Use WaveletTrimToPow2 on your bar count first; the library does not pad or truncate for you beyond that helper. - Periodic boundary handling means the transform implicitly treats the oldest and newest samples in the window as adjacent. On a genuinely trending series this introduces a small artefact at both ends - normal for this class of transform, and a reason to size the window generously rather than working right at the edge. - WaveletUniversalThreshold is a starting point derived from one noise estimate, not a calibrated parameter for trading decisions. Backtest the threshold you actually use. - The struct-of-array workaround for jagged levels is a language limitation on both platforms, not a design choice - copy the WaveletBand pattern if you extend this header rather than trying to force a plain 2D array. - This is a smoothing and analysis tool, not a signal generator. It contains no trading logic and places no orders.
Script · MT4 · v1.0 · Free
About Discrete Wavelet Transform
Discrete Wavelet Transform is a script for MetaTrader 4. It has 3 settings, such as Sample bars, Levels and Noise scale. MQL source code included.
Files in the download: WaveletTransform.mqh, WaveletTransform.txt, WaveletTransformDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Sample bars | 128 | Power-of-two length used for every test in this demo |
| Levels | 3 | Decomposition depth |
| Noise scale | 0.5 | Amplitude of injected noise in the synthetic test |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Moving Average · Trend · HFT · MT4 Script · MQL4 Source Code