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

添加ADX至多时间框架EMA时脚本翻译失败,求解决方案

解决Pine Script中ADX添加至多时间框架EMA指标的报错问题

报错原因

你遇到的Script could not be translated from: |B|SmoothedTrueRange := nz(SmoothedTrueRange[1]错误,核心原因是递归变量未用var初始化:在全局作用域中使用:=进行递归赋值(引用变量自身的历史值[1])时,必须用var关键字声明变量,否则Pine Script会在每根K线重新初始化变量,导致翻译器无法解析这种递归逻辑。

修正后的完整代码

study("Multi Time Frame Exponential Moving Average", "MTF EMA - Personal", overlay=true)

// 1分钟时间框架EMA设置
ma_len1 = input(title="1min EMA Length", type=integer, defval=2)
src1 = input(title="1min EMA Source", type=source, defval=close)
ma_offset1 = input(title="1min EMA Offset", type=integer, defval=0)
res1 = input(title="1min Resolution", type=resolution, defval="1")
htf_ma1 = ema(src1, ma_len1)
out1 = security(syminfo.tickerid, res1, htf_ma1)
plot(out1, color=red, offset=ma_offset1)

ma_len2 = input(title="1min EMA Length (Long)", type=integer, defval=9)
src2 = input(title="1min EMA Source (Long)", type=source, defval=close)
ma_offset2 = input(title="1min EMA Offset (Long)", type=integer, defval=0)
res2 = input(title="1min Resolution (Long)", type=resolution, defval="1")
htf_ma2 = ema(src2, ma_len2) // 修正原代码引用src1的错误
out2 = security(syminfo.tickerid, res2, htf_ma2)
plot(out2, color=red, offset=ma_offset2)

// 5分钟时间框架EMA设置
ma_len3 = input(title="5min EMA Length", type=integer, defval=2)
src3 = input(title="5min EMA Source", type=source, defval=close)
ma_offset3 = input(title="5min EMA Offset", type=integer, defval=0)
res3 = input(title="5min Resolution", type=resolution, defval="5")
htf_ma3 = ema(src3, ma_len3)
out3 = security(syminfo.tickerid, res3, htf_ma3)
plot(out3, color=red, offset=ma_offset3)

ma_len4 = input(title="5min EMA Length (Long)", type=integer, defval=9)
src4 = input(title="5min EMA Source (Long)", type=source, defval=close)
ma_offset4 = input(title="5min EMA Offset (Long)", type=integer, defval=0)
res4 = input(title="5min Resolution (Long)", type=resolution, defval="5")
htf_ma4 = ema(src4, ma_len4)
out4 = security(syminfo.tickerid, res4, htf_ma4)
plot(out4, color=red, offset=ma_offset4)

// ADX计算(修正递归变量初始化)
len = input(14)
th = input(20)

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

// 用var初始化递归变量,保留K线间的状态
var float SmoothedTrueRange = 0.0
SmoothedTrueRange := nz(SmoothedTrueRange[1]) - (nz(SmoothedTrueRange[1])/len) + TrueRange

var float SmoothedDirectionalMovementPlus = 0.0
SmoothedDirectionalMovementPlus := nz(SmoothedDirectionalMovementPlus[1]) - (nz(SmoothedDirectionalMovementPlus[1])/len) + DirectionalMovementPlus

var float SmoothedDirectionalMovementMinus = 0.0
SmoothedDirectionalMovementMinus := nz(SmoothedDirectionalMovementMinus[1]) - (nz(SmoothedDirectionalMovementMinus[1])/len) + DirectionalMovementMinus

// 添加nz()避免除数为0的错误
DIPlus = nz(SmoothedDirectionalMovementPlus / nz(SmoothedTrueRange, 1)) * 100
DIMinus = nz(SmoothedDirectionalMovementMinus / nz(SmoothedTrueRange, 1)) * 100
DX = nz(abs(DIPlus-DIMinus) / nz(DIPlus+DIMinus, 1)) * 100
ADX = sma(DX, len)

// 信号绘制
data = (out1 > out2) and (out3 > out4) and (ADX > 25)
plotshape(data, style=shape.triangleup, location=location.abovebar, color=green)

data1 = (out1 < out2) and (out3 < out4) and (ADX > 25)
plotshape(data1, style=shape.triangledown, location=location.belowbar, color=red)

关键修正说明

  • 递归变量初始化:对需要引用自身历史值的SmoothedTrueRange等变量,用var float声明,确保变量状态在每根K线间持续保留,避免重复初始化导致的翻译错误。
  • EMA数据源修正:原代码中htf_ma2错误使用了src1,改为对应输入的src2,保证参数逻辑一致性。
  • 适配新版本API:将旧版的tickerid替换为syminfo.tickerid,符合Pine Script当前版本的规范。
  • 防除零错误:在DI、DX的计算中添加nz()函数,避免除数为0导致的运行时错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 14:45:44