本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=3
study(title="Range Filter [DW]", shorttitle="RF [DW]", overlay=true)
//by Donovan Wall
//This is an experimental study designed to filter out minor price action for a clearer view of trends.
//Inspired by the QQE's volatility filter, this filter applies the process directly to price rather than to a smoothed RSI.
//First, a smooth average price range is calculated for the basis of the filter and multiplied by a specified amount.
//Next, the filter is calculated by gating price movements that do not exceed the specified range.
//Lastly the target ranges are plotted to display the prices that will trigger filter movement.
//Custom bar colors are included. The color scheme is based on the filtered price trend.
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Inputs
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Source
src = input(defval=close, title="Source")
//Sampling Period
per = input(defval=14, minval=1, title="Sampling Period")
//Range Multiplier
mult = input(defval=2.618, minval=0, title="Range Multiplier")
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Definitions
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Smooth Average Range
smoothrng(x, t, m)=>
wper = (t*2) - 1
avrng = ema(abs(x - x[1]), t)
smoothrng = ema(avrng, wper)*m
smoothrng
smrng = smoothrng(src, per, mult)
//Range Filter
rngfilt(x, r)=>
rngfilt = x
rngfilt := x > nz(rngfilt[1]) ? ((x - r) < nz(rngfilt[1]) ? nz(rngfilt[1]) : (x - r)) : ((x + r) > nz(rngfilt[1]) ? nz(rngfilt[1]) : (x + r))
rngfilt
filt = rngfilt(src, smrng)
//Filter Direction
upward = 0.0
upward := filt > filt[1] ? nz(upward[1]) + 1 : filt < filt[1] ? 0 : nz(upward[1])
downward = 0.0
downward := filt < filt[1] ? nz(downward[1]) + 1 : filt > filt[1] ? 0 : nz(downward[1])
//Target Bands
hband = filt + smrng
lband = filt - smrng
//Colors
filtcolor = upward > 0 ? lime : downward > 0 ? red : orange
barcolor = (src > filt) and (src > src[1]) and (upward > 0) ? lime : (src > filt) and (src < src[1]) and (upward > 0) ? green : (src < filt) and (src < src[1]) and (downward > 0) ? red : (src < filt) and (src > src[1]) and (downward > 0) ? maroon : orange
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Plots
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Range Filter
filtplot = plot(filt, color=filtcolor, linewidth=3, title="Range Filter")
//Target
hbandplot = plot(hband, color=aqua, transp=100, title="High Target")
lbandplot = plot(lband, color=fuchsia, transp=100, title="Low Target")
//Fills
fill(hbandplot, filtplot, color=aqua, title="High Target Range")
fill(lbandplot, filtplot, color=fuchsia, title="Low Target Range")
//Bar Color
barcolor(barcolor)