Files
tv-indicators/adxatr.pine

77 lines
3.5 KiB
Plaintext
Raw Permalink Normal View History

2025-09-13 17:43:27 +02:00
//@version=5
indicator("ADX + ATR (on chart)", shorttitle="ADX·ATR", overlay=true)
// inputs
adx_len = input.int(14, "ADX Length", minval=1)
atr_len = input.int(14, "ATR Length", minval=1)
adx_threshold = input.int(25, "ADX Threshold", minval=0)
show_adx = input.bool(true, "Show ADX")
show_di = input.bool(false, "Show +DI / -DI")
show_atr_on_price = input.bool(true, "Plot ATR on chart (scaled)")
atr_scale = input.float(1.0, "ATR Scale Factor", step=0.1)
map_range = input.int(20, "ADX map range (price units)", minval=1)
adx_up_color = input.color(color.green, "ADX ≥ Threshold Color")
adx_dn_color = input.color(color.red, "ADX < Threshold Color")
atr_color = input.color(color.blue, "ATR Color")
// ATR
atr = ta.atr(atr_len)
// Manual DMI/ADX (Wilder smoothing) - compatible without ta.adx
up = high - high[1]
down = low[1] - low
plusDM = (up > down and up > 0) ? up : 0.0
minusDM = (down > up and down > 0) ? down : 0.0
tr1 = high - low
tr2 = math.abs(high - close[1])
tr3 = math.abs(low - close[1])
tr = math.max(math.max(tr1, tr2), tr3)
smTR = ta.rma(tr, adx_len)
smPlus = ta.rma(plusDM, adx_len)
smMinus = ta.rma(minusDM, adx_len)
plus_di = smTR != 0.0 ? 100.0 * smPlus / smTR : 0.0
minus_di = smTR != 0.0 ? 100.0 * smMinus / smTR : 0.0
dx = (plus_di + minus_di) != 0.0 ? 100.0 * math.abs(plus_di - minus_di) / (plus_di + minus_di) : 0.0
adx = ta.rma(dx, adx_len)
// mapping ADX (0..100) to a visible price range around close
adx_mapped = close - map_range + (adx / 100.0) * (2 * map_range)
// plotting (must be global scope; use ternary to hide)
adx_color = adx >= adx_threshold ? adx_up_color : adx_dn_color
// 只保留 ADX 背景(竖条)
bgcolor(show_adx and adx >= adx_threshold ? color.new(adx_up_color, 90) : na)
// DI 显示选项:三角表示当前主导方向;交叉显示为上下标签
show_di_markers = input.bool(true, "DI markers (+DI > -DI shows up triangle)")
show_di_cross = input.bool(true, "Show DI cross arrows")
dominant_up = plus_di > minus_di
dominant_dn = plus_di < minus_di
plotshape(show_di and show_di_markers ? dominant_up : na, title="+DI dominant", location=location.belowbar, style=shape.triangleup, size=size.tiny, color=color.new(color.green,0))
plotshape(show_di and show_di_markers ? dominant_dn : na, title="-DI dominant", location=location.abovebar, style=shape.triangledown, size=size.tiny, color=color.new(color.red,0))
cross_up = ta.cross(plus_di, minus_di) and plus_di > minus_di
cross_dn = ta.cross(minus_di, plus_di) and minus_di > plus_di
plotshape(show_di and show_di_cross ? cross_up : na, title="DI cross up", location=location.belowbar, style=shape.labelup, text="+", color=color.new(color.green,0), size=size.tiny)
plotshape(show_di and show_di_cross ? cross_dn : na, title="DI cross down", location=location.abovebar, style=shape.labeldown, text="-", color=color.new(color.red,0), size=size.tiny)
// DX 显示(可选):顶部小点,鼠标悬停可见数值
show_dx = input.bool(false, "Show DX value (dot at top)")
plotchar(show_dx ? dx : na, title="DX (0-100)", char='•', location=location.top, color=color.new(color.orange,0), size=size.tiny)
// ATR on price (scaled) -> show as band around price
band_scale = atr_scale
upper = close + atr * band_scale
lower = close - atr * band_scale
p_upper = plot(show_atr_on_price ? upper : na, title="ATR Upper", color=color.new(atr_color, 0), linewidth=1)
p_lower = plot(show_atr_on_price ? lower : na, title="ATR Lower", color=color.new(atr_color, 0), linewidth=1)
fill(p_upper, p_lower, color=color.new(atr_color, 85))