本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Twin Range Filter – Buy/Sell Signals", overlay=true)
// === Input Source ===
source = input.source(close, title="Source")
// === Parameters ===
per1 = input.int(27, minval=1, title="Fast Period")
mult1 = input.float(1.6, minval=0.1, title="Fast Range")
per2 = input.int(55, minval=1, title="Slow Period")
mult2 = input.float(2.0, minval=0.1, title="Slow Range")
// === Smooth Range Function ===
smoothrng(x, t, m) =>
wper = t * 2 - 1
avrng = ta.ema(math.abs(x - x[1]), t)
ta.ema(avrng, wper) * m
smrng1 = smoothrng(source, per1, mult1)
smrng2 = smoothrng(source, per2, mult2)
smrng = (smrng1 + smrng2) / 2
// === Range Filter Function ===
rngfilt(x, r) =>
var float rf = x
rf := x > rf[1] ? (x - r < rf[1] ? rf[1] : x - r) : (x + r > rf[1] ? rf[1] : x + r)
filt = rngfilt(source, smrng)
// === Trend Detection ===
var float upward = 0
var float downward = 0
upward := filt > filt[1] ? nz(upward[1]) + 1 : filt < filt[1] ? 0 : nz(upward[1])
downward := filt < filt[1] ? nz(downward[1]) + 1 : filt > filt[1] ? 0 : nz(downward[1])
// === Entry Conditions ===
longCond = (source > filt and source > source[1] and upward > 0) or (source > filt and source < source[1] and upward > 0)
shortCond = (source < filt and source < source[1] and downward > 0) or (source < filt and source > source[1] and downward > 0)
CondIni = 0
CondIni := longCond ? 1 : shortCond ? -1 : CondIni[1]
long = longCond and CondIni[1] == -1
short = shortCond and CondIni[1] == 1
// === Plot Buy/Sell Signals ===
plotshape(long, title="Long Signal", text="Long", style=shape.labelup, textcolor=color.black, size=size.tiny, location=location.belowbar, color=color.lime)
plotshape(short, title="Short Signal", text="Short", style=shape.labeldown, textcolor=color.white, size=size.tiny, location=location.abovebar, color=color.red)
// === Background Color ===
bgcolor(long ? color.new(color.green, 85) : short ? color.new(color.red, 85) : na)
// === Alerts ===
alertcondition(long, title="Long Signal", message="📈 Long Signal – Twin Range Filter")
alertcondition(short, title="Short Signal", message="📉 Short Signal – Twin Range Filter")