如何在TradingView Pine Script中为RSI添加超买超卖文本标注
在RSI指标面板添加固定文本标注的实现
要在RSI的85和15水平位置添加居中的固定文本,你可以使用Pine Script的label函数,结合var关键字确保标签只初始化一次,不会随K线刷新重复创建。
修改后的完整代码
//@version=4 study(title="Relative Strength Index", shorttitle="RSI", format=format.price, precision=2, resolution="") len = input(14, minval=1, title="Length") src = input(close, "Source", type = input.source) up = rma(max(change(src), 0), len) down = rma(-min(change(src), 0), len) rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down)) plot(rsi, "RSI", color=color.rgb(255, 255, 255)) band6 = hline (85, "upper band2", color=#ffffff70,linestyle=hline.style_dotted, linewidth=1) band5 = hline (70, "upper Band3", color=#ffffff70,linestyle=hline.style_dotted, linewidth=1) band4 = hline (60, "upper band4", color=#ffffff70,linestyle=hline.style_dotted, linewidth=1) band2 = hline (30, "lower band2", color=#ffffff70,linestyle=hline.style_dotted, linewidth=1) band1 = hline (15, "lower band3", color=#ffffff70,linestyle=hline.style_dotted, linewidth=1) band0 = hline (00, "lower band4", color=#ffffff70,linestyle=hline.style_dotted, linewidth=1) // 添加超买超卖文本标注 var label overBoughtLabel = label.new( x=na, y=85, text="over bought", color=color.new(color.white, 100), // 透明背景 textcolor=color.white, style=label.style_none, // 隐藏标签边框 align=label.align_center, // 水平居中 yloc=yloc.price // 基于价格坐标定位 ) var label overSoldLabel = label.new( x=na, y=15, text="over sold", color=color.new(color.white, 100), textcolor=color.white, style=label.style_none, align=label.align_center, yloc=yloc.price )
关键代码说明
var关键字:确保标签仅在脚本初始化时创建一次,避免每次K线更新都重复生成标签x=na+align=label.align_center:让文本在RSI指标面板中水平居中显示y=85/y=15:对应RSI的超买超卖水平线位置color.new(color.white, 100):设置标签背景完全透明,避免遮挡图表style=label.style_none:隐藏标签的边框和背景,只显示文本
内容的提问来源于stack exchange,提问作者Nabeel Saeed
相关产品推荐
相关产品推荐

