过滤ADX

xiaoVinter · study · 69 行 · 点赞 28 · TradingView 原页

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

Pine Script

//@version=6
indicator("过滤ADX", shorttitle="ADX filter", format=format.price, precision=2, timeframe="", timeframe_gaps=true)
 
TT_ADX_LEN = "The time period to be used in calculating the ADX which has a smoothing component."
TT_DI_LEN  = "The time period to be used in calculating the DI (Directional Indicator)."
 
adxlen = input(14, title = "ADX Smoothing", tooltip = TT_ADX_LEN)
dilen  = input(14, title = "DI Length",     tooltip = TT_DI_LEN)
 
// ── 区间设置 ──────────────────────────────────────────────
lower_band = input.float(20.0, title = "震荡上限 (Lower Band)", minval = 1,  maxval = 99, step = 0.5,
     tooltip = "ADX 低于此值视为震荡行情(矩形下轨)")
upper_band = input.float(40.0, title = "趋势确认 (Upper Band)", minval = 1,  maxval = 100, step = 0.5,
     tooltip = "ADX 高于此值视为强趋势行情(矩形上轨)")
 
range_color = input.color(color.new(color.orange, 80), title = "震荡区间颜色")
trend_color = input.color(color.new(color.teal,   80), title = "趋势区间颜色")
 
// ── 核心计算 ──────────────────────────────────────────────
dirmov(len) =>
    up      = ta.change(high)
    down    = -ta.change(low)
    plusDM  = na(up)   ? na : (up   > down and up   > 0 ? up   : 0)
    minusDM = na(down) ? na : (down > up   and down > 0 ? down : 0)
    truerange = ta.rma(ta.tr, len)
    plus  = fixnan(100 * ta.rma(plusDM,  len) / truerange)
    minus = fixnan(100 * ta.rma(minusDM, len) / truerange)
    [plus, minus]
 
adx(dilen, adxlen) =>
    [plus, minus] = dirmov(dilen)
    sum = plus + minus
    adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)
 
sig = adx(dilen, adxlen)
 
// ── 状态判断 ──────────────────────────────────────────────
is_ranging  = sig < lower_band
is_trending = sig > upper_band
// lower_band ~ upper_band 之间为过渡区
 
// ── ADX 曲线(根据状态变色)────────────────────────────────
line_color = is_trending ? color.new(color.teal, 0) :
             is_ranging  ? color.new(color.orange, 0) :
             color.new(color.gray, 0)
 
plot(sig, color = line_color, linewidth = 2, title = "ADX")
 
// ── 水平分界线 ────────────────────────────────────────────
hline(lower_band, "震荡上限", color = color.new(color.orange, 30), linestyle = hline.style_dashed, linewidth = 1)
hline(upper_band, "趋势确认", color = color.new(color.teal,   30), linestyle = hline.style_dashed, linewidth = 1)
 
// ── 背景矩形色带 ──────────────────────────────────────────
// 震荡区间:0 ~ lower_band
bgcolor(is_ranging  ? color.new(color.orange, 90) : na, title = "震荡背景")
// 趋势区间:upper_band 以上
bgcolor(is_trending ? color.new(color.teal,   90) : na, title = "趋势背景")
 
// ── 填充色带(固定区间,始终可见)────────────────────────────
lower_plot = plot(lower_band, color = na, editable = false, display = display.none)
upper_plot = plot(upper_band, color = na, editable = false, display = display.none)
zero_plot  = plot(0,          color = na, editable = false, display = display.none)
 
// 0 ~ lower_band 震荡带
fill(zero_plot,  lower_plot, color = range_color, title = "震荡区间填充")
// lower_band ~ upper_band 过渡带(留白,不填色,区分明显)
// upper_band 以上趋势带
fill(upper_plot, plot(100, color = na, editable = false, display = display.none),
     color = trend_color, title = "趋势区间填充")

← 返回列表