本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=6
indicator(title="Range Filter - Enhanced Accuracy", shorttitle="RF - Enhanced", overlay=true)
// --- Colors ---
const color BULL_COLOR = #089981
const color BEAR_COLOR = #f23645
const color NEUTRAL_COLOR = #5b9cf6
// --- Inputs ---
group_calc = "Core Range Filter Settings"
src = input.source(close, "Swing Source", group=group_calc)
per = input.int(20, "Swing Period", minval=1, group=group_calc)
mult = input.float(3.5, "Swing Multiplier", minval=0.1, step=0.1, group=group_calc)
group_accuracy = "Accuracy Filters"
useTrendFilter = input.bool(true, "Use Trend Filter (EMA 200)", group=group_accuracy, tooltip="Only allows Buy signals above the EMA and Sell signals below it.")
trendPeriod = input.int(200, "Trend EMA Period", minval=1, group=group_accuracy)
useVolFilter = input.bool(true, "Use Volatility Filter (ATR)", group=group_accuracy, tooltip="Filters out signals during low-volatility periods where whipsaws are common.")
volLookback = input.int(50, "Volatility Lookback", minval=1, group=group_accuracy)
group_style = "Visual Settings"
use_barcolor = input.bool(false, "Bar Colors On/Off", group=group_style)
showTrend = input.bool(true, "Show Trend EMA", group=group_style)
// --- Core Range Filter Calculations ---
// Volatility-adjusted range size
float av_rng = ta.ema(math.abs(src - src[1]), per)
float rng_size = ta.ema(av_rng, (per * 2) - 1) * mult
// Trend Filter
var float filt = na
if na(filt)
filt := src
float prev_filt = nz(filt[1], src)
if src - rng_size > prev_filt
filt := src - rng_size
else if src + rng_size < prev_filt
filt := src + rng_size
else
filt := prev_filt
// Filter Direction
var int fdir = 0
fdir := filt > nz(filt[1], filt) ? 1 : (filt < nz(filt[1], filt) ? -1 : fdir)
bool upward = fdir == 1
bool downward = fdir == -1
// --- Accuracy Filter Logic ---
// 1. Long Term Trend Filter
float mainTrend = ta.ema(src, trendPeriod)
bool trendUp = not useTrendFilter or src > mainTrend
bool trendDn = not useTrendFilter or src < mainTrend
// 2. Volatility Filter (ATR vs SMA of ATR)
float atr = ta.atr(14)
float atrAvg = ta.sma(atr, volLookback)
bool isVolatile = not useVolFilter or atr > atrAvg
// --- Signal Logic ---
// Base conditions
bool longCond = (src > filt and (src > src[1] or src < src[1])) and upward
bool shortCond = (src < filt and (src < src[1] or src > src[1])) and downward
// Refined conditions with accuracy filters
bool longActive = longCond and trendUp and isVolatile
bool shortActive = shortCond and trendDn and isVolatile
var int cond_ini = 0
cond_ini := longActive ? 1 : (shortActive ? -1 : cond_ini[1])
bool buy_signal = longActive and cond_ini[1] == -1
bool sell_signal = shortActive and cond_ini[1] == 1
// --- Visualization ---
// Filter Line
color filt_color = upward ? BULL_COLOR : downward ? BEAR_COLOR : color.gray
p_filt = plot(filt, "Filter", color.new(filt_color, 50), 3)
// Bands
p_h_band = plot(filt + rng_size, "High Band", color.new(BULL_COLOR, 100))
p_l_band = plot(filt - rng_size, "Low Band", color.new(BEAR_COLOR, 100))
// Fills
fill(p_h_band, p_filt, color.new(BULL_COLOR, 90), "High Fill")
fill(p_l_band, p_filt, color.new(BEAR_COLOR, 90), "Low Fill")
// Trend EMA
plot(showTrend ? mainTrend : na, "Trend EMA", color.new(NEUTRAL_COLOR, 50), 2)
// Bar Colors
color bar_col = upward and (src > filt) ?
(src > src[1] ? BULL_COLOR : color.new(BULL_COLOR, 30)) :
downward and (src < filt) ?
(src < src[1] ? BEAR_COLOR : color.new(BEAR_COLOR, 30)) : color.gray
barcolor(use_barcolor ? bar_col : na)
// Signal Labels
plotshape(
buy_signal,
"Buy",
shape.labelup,
location.belowbar,
color.new(BULL_COLOR, 0),
0,
"BUY",
chart.fg_color,
size=size.small
)
plotshape(
sell_signal,
"Sell",
shape.labeldown,
location.abovebar,
color.new(BEAR_COLOR, 0),
0,
"SELL",
chart.fg_color,
size=size.small
)
// Alerts
alertcondition(buy_signal, "Buy Alert", "Enhanced Range Filter Buy Signal")
alertcondition(sell_signal, "Sell Alert", "Enhanced Range Filter Sell Signal")