简体   繁体   English

松树脚本:RSI 指标为线着色

[英]Pine-script : RSI Indicator color the line

I use the basic RSI Indicator我使用基本的 RSI 指标

Here the code这里的代码

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

I need to have the line in green when it's up 50% and in red when it's down 50% I have no find solution so if you can help me: it will be cool.我需要在上升 50% 时将线设为绿色,在下降 50% 时将线设为红色我没有找到解决方案,所以如果你能帮助我:它会很酷。

Thanks by advance for your help.提前感谢您的帮助。

Have a good day祝你有美好的一天

Create a color variable for the color and use ternary operator to set its value.为颜色创建一个color变量并使用三元运算符设置其值。

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

Your full code:您的完整代码:

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

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM