Indicator · Pine Script v6 · On the chart

Wavelet Denoising Filter (WAVELET)

by mihakralj

This indicator denoises a selected price source with an a trous, or stationary, Haar wavelet.

View on GitHub

GitHub stars99on the repository
Pine versionv6current
Lines of code114
Settings3inputs
LicenceApache-2.0
Last update4 weeks ago27 Sep 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

114 lines of Pine Script v6 from lib/filters/wavelet/wavelet.pine, published under Apache-2.0. Keep the author's credit and licence lines when you reuse it.

wavelet.pine
// Licensed under the Apache License, Version 2.0
// © mihakralj
//@version=6
indicator("Wavelet Denoising Filter (WAVELET)", "WAVELET", overlay=true)

//@function Soft thresholding — shrinks coefficient toward zero by threshold amount
//@param d Detail coefficient
//@param thresh Threshold value
//@returns Thresholded coefficient: sign(d) * max(0, |d| - thresh)
soft(float d, float thresh) =>
    float absD = math.abs(d)
    absD > thresh ? math.sign(d) * (absD - thresh) : 0.0

//@function À trous (stationary/undecimated) wavelet denoising with Haar basis
//     approximation is computed by averaging samples separated by 2^(j-1) bars
//     (inserting "holes" between taps). Detail coefficients are soft-thresholded
//     with a universal threshold scaled by robust noise estimate (MAD of level-1
//     details). Reconstruction sums the coarsest approximation and all denoised
//     details. O(levels) per bar, fully causal, no downsampling.
//@param src Input series to denoise
//@param levels Number of decomposition levels (each doubles the effective window)
//@param threshMult Threshold multiplier — scales the universal threshold
//@returns Denoised series
//@optimized O(levels) per bar; lookback = 2^levels bars; no arrays needed —
//           Pine's native series indexing handles all delays naturally
wavelet(series float src, simple int levels, simple float threshMult) =>
    if levels < 1 or levels > 8
        runtime.error("Levels must be between 1 and 8")
    if threshMult < 0.0
        runtime.error("Threshold multiplier must be >= 0")

    // --- Level 1 decomposition (step = 1) ---
    float c0 = nz(src, 0.0)
    float c1 = (c0 + nz(src[1], c0)) * 0.5
    float d1 = c0 - c1

    // --- Level 2 decomposition (step = 2) ---
    float c2 = levels >= 2 ? (c1 + nz(c1[2], c1)) * 0.5 : c1
    float d2 = levels >= 2 ? c1 - c2 : 0.0

    // --- Level 3 decomposition (step = 4) ---
    float c3 = levels >= 3 ? (c2 + nz(c2[4], c2)) * 0.5 : c2
    float d3 = levels >= 3 ? c2 - c3 : 0.0

    // --- Level 4 decomposition (step = 8) ---
    float c4 = levels >= 4 ? (c3 + nz(c3[8], c3)) * 0.5 : c3
    float d4 = levels >= 4 ? c3 - c4 : 0.0

    // --- Level 5 decomposition (step = 16) ---
    float c5 = levels >= 5 ? (c4 + nz(c4[16], c4)) * 0.5 : c4
    float d5 = levels >= 5 ? c4 - c5 : 0.0

    // --- Level 6 decomposition (step = 32) ---
    float c6 = levels >= 6 ? (c5 + nz(c5[32], c5)) * 0.5 : c5
    float d6 = levels >= 6 ? c5 - c6 : 0.0

    // --- Level 7 decomposition (step = 64) ---
    float c7 = levels >= 7 ? (c6 + nz(c6[64], c6)) * 0.5 : c6
    float d7 = levels >= 7 ? c6 - c7 : 0.0

    // --- Level 8 decomposition (step = 128) ---
    float c8 = levels >= 8 ? (c7 + nz(c7[128], c7)) * 0.5 : c7
    float d8 = levels >= 8 ? c7 - c8 : 0.0

    // --- Robust noise estimate from level-1 details ---
    // MAD (median absolute deviation) approximated by running mean of |d1|
    // over 2^levels bars. True MAD needs sorting; mean|d1| is a reasonable
    // streaming proxy. sigma = MAD / 0.6745 (Gaussian assumption).
    int madLen = int(math.pow(2, levels))
    float sumAbsD1 = 0.0
    for i = 0 to madLen - 1
        sumAbsD1 += math.abs(nz(d1[i], 0.0))
    float mad = sumAbsD1 / float(madLen)
    float sigma = mad / 0.6745

    // --- Universal threshold: T = sigma * sqrt(2 * ln(madLen)) * threshMult ---
    float T = sigma * math.sqrt(2.0 * math.log(madLen)) * threshMult

    // --- Apply soft thresholding to detail coefficients ---
    float td1 = soft(d1, T)
    float td2 = soft(d2, T)
    float td3 = soft(d3, T)
    float td4 = soft(d4, T)
    float td5 = soft(d5, T)
    float td6 = soft(d6, T)
    float td7 = soft(d7, T)
    float td8 = soft(d8, T)

    // --- Reconstruction: coarsest approximation + thresholded details ---
    float coarse = levels == 1 ? c1 :
                   levels == 2 ? c2 :
                   levels == 3 ? c3 :
                   levels == 4 ? c4 :
                   levels == 5 ? c5 :
                   levels == 6 ? c6 :
                   levels == 7 ? c7 : c8

    float result = coarse + td1 + td2 + td3 + td4 + td5 + td6 + td7 + td8
    result

// ---------- Main loop ----------

// Inputs
i_levels    = input.int(4, "Decomposition Levels", minval=1, maxval=8,
              tooltip="Number of wavelet decomposition levels. Each level doubles the effective smoothing window. Lookback = 2^levels bars.")
i_threshold = input.float(1.0, "Threshold Multiplier", minval=0.0, maxval=5.0, step=0.1,
              tooltip="Scales the universal noise threshold. 0 = no denoising (passthrough). Higher = more aggressive smoothing.")
i_source    = input.source(close, "Source")

// Calculation
wav_val = wavelet(i_source, i_levels, i_threshold)

// Plot
plot(wav_val, "Wavelet", color=color.new(color.yellow, 0), linewidth=2)

Overview

Wavelet Denoising Filter (WAVELET) decomposes the series into an approximation and detail coefficients across one to eight levels, estimates noise from the mean absolute level-one details, and soft-thresholds those details with a universal threshold. Reconstruction adds the coarsest approximation to the thresholded details. Decomposition levels, the threshold multiplier, and the source change smoothness and lag. It draws the denoised series on the price chart. The code describes the filter as fully causal, with lookback equal to two raised to the level count.

Details

Published by

mihakralj on GitHub

Type

Indicator, Pine Script v6

Draws

On the price chart

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Topics

algorithmic-tradingcsharpdotnetpinescriptpythonquantitative-financesimdta-libtechnical-analysistechnical-indicators

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Decomposition Levelsint4i_levels
Threshold Multiplierfloat1.0i_threshold
Sourcesourceclosei_source
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

948 stars

The Adaptive Laguerre Filter calculates a smoothed, dynamically adjusted trendline plotted directly on the main price chart. It dynamically adjusts its damping factor (alpha) based on recent price range variations and median percentile calculations over a spec

Indicator v4 TrendMoving averages
Updated 10 days ago · GPL-3.0 GitHub repository of Adaptive Laguerre Filter
575 stars

The indicator plots ICT displacement candles on the price chart and can switch to retracement scanning. Displacement requires a move larger than a set share of the 14-bar average high-low range, lasting at least a chosen number of bars, with optional close, pr

Indicator v6 SMC / ICT / price actionCandlestick patterns
Updated 1 year ago · MPL-2.0 GitHub repository of ICT Displacement Candles Indicator
330 stars

This script plots three moving averages on the price chart: a fast EMA or SMA with 10 periods, a slow EMA or SMA with 20 periods, and a trend EMA or SMA with 100 periods. It signals trend changes by drawing a white cross when the fast moving average crosses ab

Indicator v4 TrendMoving averages
Updated 2 months ago · None stated GitHub repository of 3 EMA/SMA + Cross [Alorse]
318 stars

This indicator calculates an ATR trailing stop line derived from the highest high minus an average true range multiple over a specified lookback period. It plots the trailing stop line directly on the price chart and colors price bars based on whether the clos

Indicator v5 TrendVolatility & bands
Updated 2 months ago · MIT GitHub repository of ATR Trailing Stoploss

1833 Tracker

by Allen1212
303 stars

1833 Tracker is an overlay indicator that visualizes historical buy and sell transactions executed by ARK Invest ETFs for the stock on the main price chart. It displays trade dates using green buy and red sell labels positioned relative to price bars, indicati

Indicator v4
Updated 2 years ago · None stated GitHub repository of 1833 Tracker

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers