Pine-script:RSI 指标颜色线

Pine-script : RSI Indicator color the line

我使用基本的 RSI 指标

这里是代码

//@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")

我需要在上涨 50% 时显示绿色,在下跌 50% 时显示红色 我找不到解决方案,所以如果你能帮助我:那就太好了。

预先感谢您的帮助。

祝你有美好的一天

为颜色创建一个 color 变量并使用三元运算符设置其值。

color rsi_color = rsi >= 50 ? color.green : color.red
plot(rsi, "RSI", color=rsi_color)

您的完整代码:

//@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))
color rsi_color = rsi >= 50 ? color.green : color.red
plot(rsi, "RSI", color=rsi_color)
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")