Nadaraya-Watson Envelope [Gabremoku]Nadaraya-Watson Envelope
This indicator builds a non-repainting Nadaraya-Watson envelope using a one-sided Gaussian kernel, so every value is computed from the current bar and past bars only. The goal is to provide a smoother adaptive baseline than a standard moving average while keeping the script operationally honest and suitable for live use.
What makes this script different:
- The central basis is a kernel-weighted Nadaraya-Watson estimate, not a classic SMA/EMA baseline.
- The main envelope is not built from standard deviation by default. It uses kernel-weighted mean absolute deviation (MAD), which is generally less sensitive to single-bar outliers and often produces a more stable channel.
- Standard deviation bands can still be enabled as an optional overlay, so users can compare MAD-based and Stdev-based dispersion around the same kernel basis.
- Signal logic is configurable. Breakout labels can be triggered by close crossing the band, wick piercing the band, or full body breakout, which makes the visual behavior easier to align with the trader’s interpretation.
How it works:
The script applies Gaussian weights to past bars inside the selected window. More recent bars receive the highest weight, while older bars progressively contribute less. The Bandwidth input controls how fast those weights decay. In practice, the effective lookback is usually much shorter than the full Window setting when Bandwidth is low. A practical rule of thumb is that the effective lookback is about 3 × Bandwidth bars, capped by the Window value.
The indicator computes:
1. A kernel-weighted mean, used as the Nadaraya-Watson basis.
2. A kernel-weighted MAD, used as the primary envelope width.
3. An optional kernel-weighted standard deviation, displayed only when the comparison bands are enabled.
The upper and lower MAD bands are then filled with a gradient that increases in strength as price moves away from the basis toward the envelope edges. This makes the visual intensity reflect displacement magnitude, not just bullish or bearish direction.
Compression logic:
The compression zone is based on min-max normalization of envelope width over a lookback period. This is not a statistical percentile rank. A threshold of 0.15 means the current envelope width is near the lower end of the observed width range over the selected compression lookback.
Signal modes:
- Close Cross: triggers only when the close crosses a band.
- Wick Pierce: triggers when the candle’s high or low exceeds a band.
- Body Breakout: triggers when the candle body exceeds a band.
Use Wick Pierce if you want signal labels to match the visible moment where candles extend outside the envelope.
How to use it:
- Use the basis as an adaptive trend reference.
- Use the MAD envelope to judge whether price is stretched relative to recent kernel-weighted behavior.
- Watch compression zones for narrow-range conditions that may precede expansion.
- Compare MAD and Stdev bands when you want to evaluate whether recent volatility is dominated by isolated spikes or by broader dispersion.
Practical notes:
- This script is non-repainting by construction because it does not use centered calculations or future bars.
- Low Bandwidth values create a more reactive basis and shorter effective memory.
- High Bandwidth values create a smoother basis and wider historical influence.
- Increasing Window far beyond roughly 3 × Bandwidth usually has little additional effect.
- Signal labels are state-machine filtered, so they are designed to mark sequence transitions rather than every repeated touch outside the bands.
This indicator is intended as a visual decision-support tool, not as a standalone trading system. It helps traders study adaptive trend, envelope displacement, compression, and breakout structure in a cleaner way than a standard volatility channel.
Penunjuk Pine Script®





















