You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TradingView Pine Script报错:未声明标识符'DIPlus_Smoothed'

问题排查与解决

错误的核心原因是变量名大小写不匹配:

  • 你在代码中定义平滑后的+DI变量时,写的是DIPLus_Smoothed(注意是大写的L,即DI-PL-us)
  • 但在第70行的bearsig条件里,你调用的是DIPlus_Smoothed(小写的l,即DI-Pl-us)

Pine Script是严格区分大小写的编程语言,这两个会被识别为完全不同的变量,因此系统提示“未声明标识符'DIPlus_Smoothed'”。

修正后的完整代码

//@version=5
indicator(title='WSTF ADX + EMA Angle', shorttitle='WSTF ADX + EMA Angle', overlay=false)

//*************** Moving Average *****************

ma_trend_slow = ta.ema(close, 200)
ma_trend_fast = ta.ema(close, 150)

//*************** Moving Average *****************

lookback1 = input(5, 'Lookback 1')
malen1 = input(100, 'MA Length')
type1 = input.string('EMA', '1st MA Type', options=['SMA', 'EMA', 'HMA', 'VWMA', 'LSMA'])
f_hma(_src, _length) =>
    _return = ta.wma(2 * ta.wma(_src, _length / 2) - ta.wma(_src, _length), math.round(math.sqrt(_length)))
    _return
price = close
price1 = if type1 == 'SMA'
    ta.sma(price, malen1)
else
    if type1 == 'EMA'
        ta.ema(price, malen1)
    else
        if type1 == 'VWMA'
            ta.vwma(price, malen1)
        else
            if type1 == 'LSMA'
                ta.linreg(price, malen1, 0)
            else
                f_hma(price, malen1)


roc1 = ta.roc(price1, lookback1)

macolor = roc1 > 0 ? color.new(color.green, 80) : roc1 < 0 ? color.new(color.red, 80) : color.new(color.black, 80)

//************************************** ADX ***************************************
len = input(title='Length', defval=14)
len_LT = input(title='Length LT', defval=29)
th_b = input(title='threshold bottom', defval=15)
th_t = input(title='threshold top', defval=40)
lensma = input(title='Len SMA', defval=10)


TrueRange = math.max(math.max(high - low, math.abs(high - nz(close[1]))), math.abs(low - nz(close[1])))
DirectionalMovementPlus = high - nz(high[1]) > nz(low[1]) - low ? math.max(high - nz(high[1]), 0) : 0
DirectionalMovementMinus = nz(low[1]) - low > high - nz(high[1]) ? math.max(nz(low[1]) - low, 0) : 0

var SmoothedTrueRange=TrueRange
var SmoothedDirectionalMovementPlus=DirectionalMovementPlus
var SmoothedDirectionalMovementMinus=DirectionalMovementMinus

SmoothedTrueRange := nz(SmoothedTrueRange[1]) - (nz(SmoothedTrueRange[1])/len) + TrueRange
SmoothedDirectionalMovementPlus:= nz(SmoothedDirectionalMovementPlus[1]) - (nz(SmoothedDirectionalMovementPlus[1])/len) + DirectionalMovementPlus
SmoothedDirectionalMovementMinus:= nz(SmoothedDirectionalMovementMinus[1]) - (nz(SmoothedDirectionalMovementMinus[1])/len) + DirectionalMovementMinus

DIPlus = SmoothedDirectionalMovementPlus / SmoothedTrueRange * 100
DIMinus = SmoothedDirectionalMovementMinus / SmoothedTrueRange * 100

DX = math.abs(DIPlus - DIMinus) / (DIPlus + DIMinus) * 100
ADX = ta.sma(DX, len)

DIMinus_Smoothed = ta.sma(DIMinus, lensma)
DIPlus_Smoothed = ta.sma(DIPlus, lensma) // 修正变量名,统一为小写l

plot(DIMinus_Smoothed, color=color.red)
plot(DIPlus_Smoothed, color=color.green) // 同步修正plot中的变量名

bullsig = ta.crossover(DIPlus_Smoothed, DIMinus_Smoothed) and roc1 > 0 // 同步修正
bearsig = ta.crossover(DIMinus_Smoothed, DIPlus_Smoothed) and roc1 < 0 // 变量名匹配

//plotshape(bullsig, style=shape.labelup, location=location.belowbar, color=color.new(color.green, 0), size=size.small)

bgcolor(macolor)

//*************************

内容的提问来源于stack exchange,提问作者marketwizard

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 18:39:41