在一个脚本中添加三个指标
Adding three indicators in one script
我正在尝试在一个脚本中添加 DMI、RSI 和 RVI。但是遇到如下信息——
添加到图表操作失败,原因:第 16 行:'len' 已定义。
添加到图表操作失败,原因:第 22 行:'len' 已定义。
我的代码如下-
//@version=5
indicator(title="Relative Strength Index", shorttitle="RSI", format=format.price, precision=2, timeframe="", timeframe_gaps=true)
len = input.int(14, minval=1, title="Length")
src = input(close, "Source")
up = ta.rma(math.max(ta.change(src), 0), len)
down = ta.rma(-math.min(ta.change(src), 0), len)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
plot(rsi, "RSI", color=#7E57C2)
band1 = hline(70, "Upper Band", color=#787B86)
bandm = hline(50, "Middle Band", color=color.new(#787B86, 50))
band0 = hline(30, "Lower Band", color=#787B86)
fill(band1, band0, color=color.rgb(126, 87, 194, 90), title="Background")
//@version=5
indicator(title="Directional Movement Index", shorttitle="DMI", format=format.price, precision=4, timeframe="", timeframe_gaps=true)
lensig = input.int(14, title="ADX Smoothing", minval=1, maxval=50)
len = input.int(14, minval=1, title="DI Length")
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)
trur = ta.rma(ta.tr, len)
plus = fixnan(100 * ta.rma(plusDM, len) / trur)
minus = fixnan(100 * ta.rma(minusDM, len) / trur)
sum = plus + minus
adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), lensig)
plot(adx, color=#F50057, title="ADX")
plot(plus, color=#2962FF, title="+DI")
plot(minus, color=#FF6D00, title="-DI")
//@version=5
indicator(title="Relative Volatility Index", shorttitle="RVI", format=format.price, precision=2, timeframe="", timeframe_gaps=true)
length = input.int(10, minval=1), src = close
len = 14
stddev = ta.stdev(src, length)
upper = ta.ema(ta.change(src) <= 0 ? 0 : stddev, len)
lower = ta.ema(ta.change(src) > 0 ? 0 : stddev, len)
rvi = upper / (upper + lower) * 100
h0 = hline(80, "Upper Band", color=#787B86)
h1 = hline(20, "Lower Band", color=#787B86)
fill(h0, h1, color=color.rgb(126, 87, 194, 90), title="Background")
offset = input.int(0, "Offset", minval = -500, maxval = 500)
plot(rvi, title="RVI", color=#7E57C2, offset = offset)
请帮我解决这个问题。
谢谢和问候。
在 Pine 脚本中,如果您要更改现有变量的值,您需要键入
:=
因此,第一次创建变量时输入“=”,第二次输入“:=”,然后为其分配新值
示例:
len = 10
len := 30
每当您尝试组合两个指标时,您必须记住变量名称必须是唯一的。在你的案例中,source 和 length 已经定义了不止一次,所以我相应地重命名了它(确保代码中没有两个同名的变量。
我已经在下面提供了代码,没有任何错误
//@version=5
indicator(title="Relative Strength Index", shorttitle="RSI", format=format.price, precision=2, timeframe="", timeframe_gaps=true)
len = input.int(14, minval=1, title="Length",group="RSI setting")
src = input(close, "Source",group="RSI setting")
up = ta.rma(math.max(ta.change(src), 0), len)
down = ta.rma(-math.min(ta.change(src), 0), len)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
plot(rsi, "RSI", color=#7E57C2)
band1 = hline(70, "Upper Band", color=#787B86)
bandm = hline(50, "Middle Band", color=color.new(#787B86, 50))
band0 = hline(30, "Lower Band", color=#787B86)
fill(band1, band0, color=color.rgb(126, 87, 194, 90), title="Background")
lensig_adx = input.int(14, title="ADX Smoothing", minval=1, maxval=50,group="ADX setting")
len_adx = input.int(14, minval=1, title="DI Length",group="ADX setting")
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")
length_rvi = input.int(10, minval=1,group="RVI setting")
src_rvi = close
len_rvi = 14
stddev_rvi = ta.stdev(src_rvi, length_rvi)
upper_rvi = ta.ema(ta.change(src_rvi) <= 0 ? 0 : stddev_rvi, len_rvi)
lower_rvi = ta.ema(ta.change(src_rvi) > 0 ? 0 : stddev_rvi, len_rvi)
rvi_rvi = upper_rvi / (upper_rvi + lower_rvi) * 100
h0_rvi = hline(80, "Upper Band", color=#787B86)
h1_rvi = hline(20, "Lower Band", color=#787B86)
fill(h0_rvi, h1_rvi, color=color.rgb(126, 87, 194, 90), title="Background")
offset = input.int(0, "Offset", minval = -500, maxval = 500)
plot(rvi_rvi, title="RVI", color=#7E57C2, offset = offset)
我正在尝试在一个脚本中添加 DMI、RSI 和 RVI。但是遇到如下信息—— 添加到图表操作失败,原因:第 16 行:'len' 已定义。 添加到图表操作失败,原因:第 22 行:'len' 已定义。
我的代码如下-
//@version=5
indicator(title="Relative Strength Index", shorttitle="RSI", format=format.price, precision=2, timeframe="", timeframe_gaps=true)
len = input.int(14, minval=1, title="Length")
src = input(close, "Source")
up = ta.rma(math.max(ta.change(src), 0), len)
down = ta.rma(-math.min(ta.change(src), 0), len)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
plot(rsi, "RSI", color=#7E57C2)
band1 = hline(70, "Upper Band", color=#787B86)
bandm = hline(50, "Middle Band", color=color.new(#787B86, 50))
band0 = hline(30, "Lower Band", color=#787B86)
fill(band1, band0, color=color.rgb(126, 87, 194, 90), title="Background")
//@version=5
indicator(title="Directional Movement Index", shorttitle="DMI", format=format.price, precision=4, timeframe="", timeframe_gaps=true)
lensig = input.int(14, title="ADX Smoothing", minval=1, maxval=50)
len = input.int(14, minval=1, title="DI Length")
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)
trur = ta.rma(ta.tr, len)
plus = fixnan(100 * ta.rma(plusDM, len) / trur)
minus = fixnan(100 * ta.rma(minusDM, len) / trur)
sum = plus + minus
adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), lensig)
plot(adx, color=#F50057, title="ADX")
plot(plus, color=#2962FF, title="+DI")
plot(minus, color=#FF6D00, title="-DI")
//@version=5
indicator(title="Relative Volatility Index", shorttitle="RVI", format=format.price, precision=2, timeframe="", timeframe_gaps=true)
length = input.int(10, minval=1), src = close
len = 14
stddev = ta.stdev(src, length)
upper = ta.ema(ta.change(src) <= 0 ? 0 : stddev, len)
lower = ta.ema(ta.change(src) > 0 ? 0 : stddev, len)
rvi = upper / (upper + lower) * 100
h0 = hline(80, "Upper Band", color=#787B86)
h1 = hline(20, "Lower Band", color=#787B86)
fill(h0, h1, color=color.rgb(126, 87, 194, 90), title="Background")
offset = input.int(0, "Offset", minval = -500, maxval = 500)
plot(rvi, title="RVI", color=#7E57C2, offset = offset)
请帮我解决这个问题。
谢谢和问候。
在 Pine 脚本中,如果您要更改现有变量的值,您需要键入
:=
因此,第一次创建变量时输入“=”,第二次输入“:=”,然后为其分配新值
示例:
len = 10
len := 30
每当您尝试组合两个指标时,您必须记住变量名称必须是唯一的。在你的案例中,source 和 length 已经定义了不止一次,所以我相应地重命名了它(确保代码中没有两个同名的变量。
我已经在下面提供了代码,没有任何错误
//@version=5
indicator(title="Relative Strength Index", shorttitle="RSI", format=format.price, precision=2, timeframe="", timeframe_gaps=true)
len = input.int(14, minval=1, title="Length",group="RSI setting")
src = input(close, "Source",group="RSI setting")
up = ta.rma(math.max(ta.change(src), 0), len)
down = ta.rma(-math.min(ta.change(src), 0), len)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
plot(rsi, "RSI", color=#7E57C2)
band1 = hline(70, "Upper Band", color=#787B86)
bandm = hline(50, "Middle Band", color=color.new(#787B86, 50))
band0 = hline(30, "Lower Band", color=#787B86)
fill(band1, band0, color=color.rgb(126, 87, 194, 90), title="Background")
lensig_adx = input.int(14, title="ADX Smoothing", minval=1, maxval=50,group="ADX setting")
len_adx = input.int(14, minval=1, title="DI Length",group="ADX setting")
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")
length_rvi = input.int(10, minval=1,group="RVI setting")
src_rvi = close
len_rvi = 14
stddev_rvi = ta.stdev(src_rvi, length_rvi)
upper_rvi = ta.ema(ta.change(src_rvi) <= 0 ? 0 : stddev_rvi, len_rvi)
lower_rvi = ta.ema(ta.change(src_rvi) > 0 ? 0 : stddev_rvi, len_rvi)
rvi_rvi = upper_rvi / (upper_rvi + lower_rvi) * 100
h0_rvi = hline(80, "Upper Band", color=#787B86)
h1_rvi = hline(20, "Lower Band", color=#787B86)
fill(h0_rvi, h1_rvi, color=color.rgb(126, 87, 194, 90), title="Background")
offset = input.int(0, "Offset", minval = -500, maxval = 500)
plot(rvi_rvi, title="RVI", color=#7E57C2, offset = offset)