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

TradingView合并Stochastic RSI、DMI、RSI提示src已定义错误如何解决

报错原因

你代码存在两个核心语法错误:

  1. 单脚本重复调用indicator()函数:Pine Script v5规则要求单个脚本仅能声明一次indicator(),你直接拼接三个独立指标的代码,保留了Stochastic RSI和独立RSI两个indicator()定义,属于非法写法。
  2. 变量重复定义:第8行已经定义了Stochastic RSI的数据源变量src,第39行定义普通RSI数据源时再次使用了src作为变量名,同一作用域下变量名不能重复。

修复后的完整可运行代码

//@version=5
indicator(title="Stoch RSI + DMI + RSI 组合指标", shorttitle="SR+DMI+RSI", format=format.price, precision=2, timeframe="", timeframe_gaps=true)

// Stochastic RSI 参数
smoothK = input.int(3, "K", minval=1, group="Stochastic RSI设置")
smoothD = input.int(3, "D", minval=1, group="Stochastic RSI设置")
lengthRSI = input.int(14, "RSI Length", minval=1, group="Stochastic RSI设置")
lengthStoch = input.int(14, "Stochastic Length", minval=1, group="Stochastic RSI设置")
src_stoch_rsi = input(close, title="RSI Source", group="Stochastic RSI设置")
rsi1 = ta.rsi(src_stoch_rsi, lengthRSI)
k = ta.sma(ta.stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
d = ta.sma(k, smoothD)
plot(k, "K", color=#2962FF)
plot(d, "D", color=#FF6D00)
h0 = hline(80, "Stoch RSI 超买线80", color=#787B86)
h2 = hline(70, "Stoch RSI 超买线70", color=#787B86)
h3 = hline(60, "Stoch RSI 区间线60", color=#787B86)
h4 = hline(50, "Stoch RSI 中线50", color=#787B86)
h5 = hline(40, "Stoch RSI 区间线40", color=#787B86)
h6 = hline(30, "Stoch RSI 超卖线30", color=#787B86)
h1 = hline(20, "Stoch RSI 超卖线20", color=#787B86)
fill(h0, h1, color=color.rgb(33, 150, 243, 90), title="Stoch RSI背景")

// DMI(ADX) 参数
lensig_adx = input.int(14, title="ADX Smoothing", minval=1, maxval=50,group="ADX设置")
len_adx = input.int(14, minval=1, title="DI Length",group="ADX设置")
up_adx = ta.change(high)
down_adx = -ta.change(low)
plusDM_adx = na(up_adx) ? na : (up_adx > down_adx and up_adx > 0 ? up_adx : 0)
minusDM_adx = na(down_adx) ? na : (down_adx > up_adx and down_adx > 0 ? down_adx : 0)
trur_adx = ta.rma(ta.tr, len_adx)
plus_adx = fixnan(100 * ta.rma(plusDM_adx, len_adx) / trur_adx)
minus_adx = fixnan(100 * ta.rma(minusDM_adx, len_adx) / trur_adx)
sum_adx = plus_adx + minus_adx
adx_adx = 100 * ta.rma(math.abs(plus_adx - minus_adx) / (sum_adx == 0 ? 1 : sum_adx), lensig_adx)
plot(adx_adx, color=#F50057, title="ADX")
plot(plus_adx, color=#2962FF, title="+DI")
plot(minus_adx, color=#FF6D00, title="-DI")

// 普通RSI 参数
len_rsi = input.int(14, minval=1, title="Length",group="RSI设置")
src_rsi = input(close, "Source",group="RSI设置")
up = ta.rma(math.max(ta.change(src_rsi), 0), len_rsi)
down = ta.rma(-math.min(ta.change(src_rsi), 0), len_rsi)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
plot(rsi, "RSI", color=#7E57C2)
band1 = hline(70, "RSI 超买线70", color=#787B86)
bandm = hline(50, "RSI 中线50", color=color.new(#787B86, 50))
band0 = hline(30, "RSI 超卖线30", color=#787B86)
fill(band1, band0, color=color.rgb(126, 87, 194, 90), title="RSI背景")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 04:06:04