Pine-script RSI指标线高于50显绿低于50显红功能实现咨询
RSI指标线动态变色实现方案
只需修改plot函数的颜色参数,通过三元运算符判断RSI数值与50的关系动态赋值即可,完整可运行代码如下:
//@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)) // 动态设置RSI线颜色:高于50绿色,低于等于50红色 plot(rsi, "RSI", color=rsi > 50 ? color.green : color.red) 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")
可选调整项:
- 若要自定义颜色透明度,可使用
color.new(颜色, 透明度)格式,透明度取值0-100,数值越大越透明,例如color.new(color.green, 20)就是浅绿色 - 若要为RSI等于50的情况单独设置颜色,可扩展判断逻辑:
color=rsi > 50 ? color.green : rsi < 50 ? color.red : color.gray
内容的提问来源于stack exchange,提问作者Wags
相关产品推荐
相关产品推荐

