本页源码来自 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
//@version=6
indicator("Range Filter + EMA Gate Indicator — Confirmed Signals",
shorttitle="RF + EMA Confirmed",
overlay=true,
max_labels_count=500)
//────────────────────────────────────────────────────────────────────
// Colors
//────────────────────────────────────────────────────────────────────
color BULL_COLOR = #089981
color BEAR_COLOR = #f23645
color NEUTRAL_COLOR = #5b9cf6
//────────────────────────────────────────────────────────────────────
// Inputs
//────────────────────────────────────────────────────────────────────
srcChoice = input.string("Close", "Source", options=["Close","Open","High","Low","HL2","HLC3","OHLC4"], group="Source Settings")
useHA = input.bool(false, "Use Heikin-Ashi Candles", group="Source Settings")
per = input.int(50, "Sampling Period", minval=1, group="Range Filter")
mult = input.float(3.0, "Range Multiplier", minval=0.1, step=0.1, group="Range Filter")
ema50Len = input.int(50, "Fast EMA Gate", group="EMA Filters")
ema150Len = input.int(150, "Slow EMA Gate", group="EMA Filters")
useTrend = input.bool(true, "Use EMA 200 Trend Filter", group="EMA Filters")
ema200Len = input.int(200, "EMA 200 Length", group="EMA Filters")
useBBFilter = input.bool(false, "Enable Bollinger Filter?", group="Bollinger Filter")
bbLength = input.int(20, "Bollinger Length", group="Bollinger Filter")
bbMult = input.float(2.0, "Bollinger Multiplier", group="Bollinger Filter")
maxSignals = input.int(2, "Max Signals per EMA Gate", minval=1, group="Signal Rules")
showEMAs = input.bool(true, "Show EMAs", group="Visuals")
showDebug = input.bool(false, "Show Debug Markers", group="Visuals")
//────────────────────────────────────────────────────────────────────
// Heikin-Ashi Source
//────────────────────────────────────────────────────────────────────
ha_close = (open + high + low + close) / 4.0
var float ha_open = na
ha_open := na(ha_open[1]) ? (open + close) / 2.0 : (ha_open[1] + ha_close[1]) / 2.0
ha_high = math.max(high, math.max(ha_open, ha_close))
ha_low = math.min(low, math.min(ha_open, ha_close))
src_ha = switch srcChoice
"Close" => ha_close
"Open" => ha_open
"High" => ha_high
"Low" => ha_low
"HL2" => (ha_high + ha_low) / 2.0
"HLC3" => (ha_high + ha_low + ha_close) / 3.0
"OHLC4" => (ha_open + ha_high + ha_low + ha_close) / 4.0
src_raw = switch srcChoice
"Close" => close
"Open" => open
"High" => high
"Low" => low
"HL2" => hl2
"HLC3" => hlc3
"OHLC4" => ohlc4
src = useHA ? src_ha : src_raw
//────────────────────────────────────────────────────────────────────
// Range Filter
//────────────────────────────────────────────────────────────────────
smoothrng(src_, t, m) =>
wper = (t * 2) - 1
avrng = ta.ema(math.abs(src_ - src_[1]), t)
ta.ema(avrng, wper) * m
smrng = smoothrng(src, per, mult)
rngfilt(x, r) =>
var float rf = na
rf := na(rf[1]) ? x : x > rf[1] ? math.max(rf[1], x - r) : math.min(rf[1], x + r)
rf
filt = rngfilt(src, smrng)
var int upcount = 0
var int downcount = 0
upcount := filt > filt[1] ? nz(upcount[1]) + 1 : filt < filt[1] ? 0 : nz(upcount[1])
downcount := filt < filt[1] ? nz(downcount[1]) + 1 : filt > filt[1] ? 0 : nz(downcount[1])
longCond = src > filt and upcount > 0
shortCond = src < filt and downcount > 0
var int CondIni = 0
CondIni := longCond ? 1 : shortCond ? -1 : nz(CondIni[1])
//────────────────────────────────────────────────────────────────────
// EMA Filters
//────────────────────────────────────────────────────────────────────
ema50 = ta.ema(src, ema50Len)
ema150 = ta.ema(src, ema150Len)
ema200 = ta.ema(src, ema200Len)
trendLong = not useTrend or src > ema200
trendShort = not useTrend or src < ema200
crossUp = ta.crossover(ema50, ema150)
crossDn = ta.crossunder(ema50, ema150)
// EMA gate direction
var int gate_dir = na
var int signals_taken = 0
if na(gate_dir)
gate_dir := ema50 > ema150 ? 1 : -1
if crossUp
gate_dir := 1
signals_taken := 0
if crossDn
gate_dir := -1
signals_taken := 0
allowLongByGate = gate_dir == 1
allowShortByGate = gate_dir == -1
//────────────────────────────────────────────────────────────────────
// Bollinger Filter
//────────────────────────────────────────────────────────────────────
basis = ta.sma(src, bbLength)
dev = bbMult * ta.stdev(src, bbLength)
bbLongFilter = not useBBFilter or src > basis
bbShortFilter = not useBBFilter or src < basis
//────────────────────────────────────────────────────────────────────
// Signal Logic — confirmed candle only
//────────────────────────────────────────────────────────────────────
longEntrySignal = longCond and CondIni[1] != 1 and trendLong and bbLongFilter
shortEntrySignal = shortCond and CondIni[1] != -1 and trendShort and bbShortFilter
rawBuySignal = longEntrySignal and allowLongByGate and signals_taken < maxSignals
rawSellSignal = shortEntrySignal and allowShortByGate and signals_taken < maxSignals
buySignal = barstate.isconfirmed and rawBuySignal
sellSignal = barstate.isconfirmed and rawSellSignal
if buySignal
signals_taken += 1
if sellSignal
signals_taken += 1
//────────────────────────────────────────────────────────────────────
// Visuals
//────────────────────────────────────────────────────────────────────
filterColor = upcount > 0 ? BULL_COLOR : downcount > 0 ? BEAR_COLOR : NEUTRAL_COLOR
plot(filt, title="Range Filter", color=filterColor, linewidth=2)
plot(showEMAs ? ema50 : na, title="Fast EMA", color=color.orange)
plot(showEMAs ? ema150 : na, title="Slow EMA", color=color.blue)
plot(showEMAs and useTrend ? ema200 : na, title="EMA 200 Trend Filter", color=color.gray)
plotshape(
buySignal,
title="BUY Signal",
text="BUY",
style=shape.labelup,
location=location.belowbar,
color=BULL_COLOR,
textcolor=color.white,
size=size.small)
plotshape(
sellSignal,
title="SELL Signal",
text="SELL",
style=shape.labeldown,
location=location.abovebar,
color=BEAR_COLOR,
textcolor=color.white,
size=size.small)
//────────────────────────────────────────────────────────────────────
// Debug Markers
//────────────────────────────────────────────────────────────────────
plotshape(showDebug and longCond,
title="Range Filter Long",
color=BULL_COLOR,
location=location.bottom,
style=shape.circle,
size=size.tiny)
plotshape(showDebug and shortCond,
title="Range Filter Short",
color=BEAR_COLOR,
location=location.bottom,
style=shape.circle,
size=size.tiny)
plotshape(showDebug and (trendLong or trendShort),
title="Trend OK",
color=color.gray,
location=location.bottom,
style=shape.circle,
size=size.tiny)
plotshape(showDebug and (bbLongFilter or bbShortFilter),
title="BB OK",
color=color.blue,
location=location.bottom,
style=shape.circle,
size=size.tiny)
plotshape(showDebug and (allowLongByGate or allowShortByGate),
title="Gate Open",
color=color.orange,
location=location.bottom,
style=shape.circle,
size=size.tiny)
//────────────────────────────────────────────────────────────────────
// Alerts — confirmed candle only
//────────────────────────────────────────────────────────────────────
alertcondition(buySignal,
title="BUY Signal Confirmed",
message="Confirmed BUY signal: Range Filter + EMA Gate conditions confirmed after candle close.")
alertcondition(sellSignal,
title="SELL Signal Confirmed",
message="Confirmed SELL signal: Range Filter + EMA Gate conditions confirmed after candle close.")
alertcondition(barstate.isconfirmed and crossUp,
title="EMA Gate Bullish Confirmed",
message="Confirmed EMA Gate bullish: Fast EMA crossed above Slow EMA after candle close.")
alertcondition(barstate.isconfirmed and crossDn,
title="EMA Gate Bearish Confirmed",
message="Confirmed EMA Gate bearish: Fast EMA crossed below Slow EMA after candle close.")