1Copy the scriptPress Copy script above, or the copy button on the code below.
2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.
Source Code
23 lines of Pine Script v4 from movings/hampel_filter.pine, published under GPL-3.0. Keep the author's credit and licence lines when you reuse it.
hampel_filter.pine
//@version=4
// Copyright (c) 2018-present, Alex Orekhov (everget)
// Hampel Filter script may be freely distributed under the terms of the GPL-3.0 license.
study("Hampel Filter", shorttitle="HF", overlay=true)
length = input(title="Length", type=input.integer, minval=1, defval=14)
scalingFactor = input(title="Scaling Factor", type=input.float, step=0.1, defval=3)
src = input(title="Source", type=input.source, defval=close)
_median(src, length) =>
percentile_nearest_rank(src, length, 50)
sampleMedian = _median(src, length)
// Median Absolute Deviation
mad = _median(abs(src - sampleMedian), length)
hf = abs(src - sampleMedian) <= scalingFactor * mad
? src
: sampleMedian
// Plot EMA of the filtered samples
plot(ema(hf, length), title="HF", linewidth=2, color=#6d1e7f, transp=0)
Overview
Hampel Filter calculates the rolling median and Median Absolute Deviation (MAD) of the chosen source series over a specified length. When a data point exceeds the user-defined scaling factor multiplied by the MAD, it is treated as an outlier and replaced with the median value. Finally, it plots an Exponential Moving Average (EMA) of the filtered values, providing a noise-reduced trend line.
Rolling median and MAD outlier detection
Adjustable scaling factor for outlier thresholds
Exponential moving average smoothing of filtered series
The inputs you can change from the gear icon on the chart. Defaults are the author's.
Setting
Type
Default
Variable
Length
int
14
length
Scaling Factor
float
3
scalingFactor
Source
source
close
src
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.
This overlay indicator plots a Supertrend from an ATR length and factor, four exponential moving averages, and a selectable VWAP or TWAP taken from a chosen timeframe on the price chart. It also shows MACD and stochastic with adjustable lengths, smoothing, ver
This indicator plots trade entry and exit signals on the price chart by combining Bollinger Bands with optional filter conditions. It calculates standard Bollinger Bands along with RSI, Aroon, and moving average filters. Reversal signals trigger when price bre
This indicator plots four customizable moving averages directly on the main price chart using default lengths of 3, 8, 21, and 50 periods. It fills the space between adjacent averages in green or maroon depending on trend alignment. Additionally, it computes B
This indicator plots three exponential moving averages and ATR volatility envelopes on the price chart. It compares the fast and slow EMAs to shade a trend ribbon and marks confirmed upward and downward crossovers with triangle labels. Settings change the thre
This indicator calculates an outlier-filtered moving average based on statistical filtering concepts. It stores close prices in a rolling array over a user-defined length, calculates the mean and standard deviation, and excludes values falling within a specifi
This indicator plots a custom Anchored Volume-Weighted Average Price (VWAP) directly on the price chart along with optional standard deviation bands. Users can anchor the VWAP to standard time periods like sessions, weeks, or months, or to corporate events suc