SuperTrend + Range Filter [Whipsaw-Reduced]

YUGU-Trade · study · 93 行 · 点赞 1,219 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=5
indicator("SuperTrend + Range Filter [Whipsaw-Reduced]", overlay=true, max_labels_count=500)
 
// ———————— INPUTS ————————
st_atr_period = input.int(10, "SuperTrend ATR Period")
st_factor     = input.float(3.0, "SuperTrend Multiplier", step=0.1)  // 🔸 Increased from 2 → 3
 
rf_period = input.int(100, "Range Filter Period")
rf_smooth = input.int(3, "Range Filter Smoothing")
 
adx_len       = input.int(14, "ADX Length")
adx_threshold = input.float(25, "Min ADX to Trade")  // 🔸 Increased from 20 → 25
 
show_labels = input.bool(true, "Show Long/Short/Exit Labels")
 
// ———————— CORE CALCULATIONS ————————
[supertrend, direction] = ta.supertrend(st_factor, st_atr_period)
st_up = direction < 0
st_dn = direction > 0
 
src = close
range_median = ta.sma(ta.median(src, rf_period), rf_smooth)
rf_up = src > range_median
rf_dn = src < range_median
 
[_, _, adx] = ta.dmi(adx_len, adx_len)
strong_trend = adx >= adx_threshold
 
// ———————— VIX PROXY ————————
tr = math.max(math.max(high - low, math.abs(high - close[1])), math.abs(low - close[1]))
vix_proxy = ta.sma(tr, 14) / ta.sma(close, 14) * 100
vix_high = vix_proxy >= 15
 
// ———————— MARKET IS TRADEABLE ————————
in_trade_zone = vix_high or strong_trend  // 🔸 Still OR (flexible)
 
// ———————— STATE MANAGEMENT ————————
var bool in_long = false
var bool in_short = false
 
// Core signal (must persist for 2 bars)
core_long = st_up and rf_up
core_short = st_dn and rf_dn
 
// 🔸 2-BAR CONFIRMATION FOR ENTRIES
confirmed_long = core_long and core_long[1]
confirmed_short = core_short and core_short[1]
 
// Only enter if confirmed AND in trade zone AND not already in position
new_long = confirmed_long and in_trade_zone and not in_long[1]
new_short = confirmed_short and in_trade_zone and not in_short[1]
 
// Exit only on price-based signal flip (unchanged)
exit_long = in_long and not core_long
exit_short = in_short and not core_short
 
// ———————— CANDLE COLORING ————————
candle_color = 
     in_long ? color.green :
     in_short ? color.red :
     color.yellow  // Out of market = yellow
 
plotcandle(open, high, low, close, color=candle_color, wickcolor=candle_color, bordercolor=candle_color)
 
// ———————— LABELS ————————
if show_labels
    combined_short_entry = exit_long and new_short
    combined_long_entry = exit_short and new_long
 
    if new_long and not combined_long_entry
        label.new(bar_index, low * 0.997, "Long", style=label.style_label_up, color=color.green, textcolor=color.white)
    if new_short and not combined_short_entry
        label.new(bar_index, high * 1.003, "Short", style=label.style_label_down, color=color.red, textcolor=color.white)
    if combined_short_entry
        label.new(bar_index, high * 1.005, "Exit → Short", style=label.style_label_down, color=color.new(color.red, 70), textcolor=color.white, size=size.small)
    if combined_long_entry
        label.new(bar_index, low * 0.995, "Exit → Long", style=label.style_label_up, color=color.new(color.green, 70), textcolor=color.white, size=size.small)
    if exit_long and not new_short
        label.new(bar_index, high * 1.005, "Exit", style=label.style_label_down, color=color.gray, textcolor=color.white, size=size.small)
    if exit_short and not new_long
        label.new(bar_index, low * 0.995, "Exit", style=label.style_label_up, color=color.gray, textcolor=color.white, size=size.small)
 
// ———————— UPDATE STATE ————————
if new_long
    in_long := true, in_short := false
if new_short
    in_short := true, in_long := false
if exit_long or exit_short
    in_long := false, in_short := false
 
// ———————— PLOTS ————————
plot(supertrend, "SuperTrend", color=direction < 0 ? color.green : color.red, linewidth=1)
plot(range_median, "Range Filter", color=color.orange, linewidth=1)

← 返回列表