Pine Script技术咨询:无法显示当前RSI数值的问题
在Pine Script中显示当前RSI数值的解决方案
你的代码已经完成了RSI的计算与K线着色,但缺少直接展示RSI数值的逻辑,以下几种实用方法可实现需求:
方法1:在RSI指标线旁标注实时数值
通过label组件在最新的RSI数据点旁显示数值,修改后代码如下:
//{RSI col rsicol = input.bool(false, title='Show RSI colors?', group='Settings ॐ', tooltip='Show RSI Levels On Bars') srcRSI = close lenRSI = input.int(14, minval=1, title='RSI Length', group='RSI Settings') up = ta.rma(math.max(ta.change(srcRSI), 0), lenRSI) down = ta.rma(-math.min(ta.change(srcRSI), 0), lenRSI) rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - 100 / (1 + up / down) rsiPlot = plot(rsi, "RSI", color=#57c269) // 新增:在最新RSI数据点显示数值 var label rsiLabel = na if barstate.islast rsiLabel := label.new(bar_index, rsi, str.tostring(rsi, "#.##"), color=color.white, textcolor=color.black, style=label.style_label_left) //coloring method below srcRSI1 = close lenRSI1 = input.int(60, minval=1, title='Over Bought', group='RSI Settings') srcRSI2 = close lenRSI2 = input.int(40, minval=1, title='Over Sold', group='RSI Settings') isup() => rsi > lenRSI1 isdown() => rsi < lenRSI2 barcolor(rsicol and isup() ? color.green : rsicol and isdown() ? color.red : na) // }
这段代码会在最新K线对应的RSI位置生成标签,显示保留两位小数的数值,标签样式可通过参数调整。
方法2:在图表固定位置显示RSI数值
若希望数值固定在图表某区域(如顶部/底部),可使用固定坐标的标签:
// 新增:在图表右上角固定显示RSI var label fixedRsiLabel = label.new(bar_index, high * 1.05, "", color=color.blue, textcolor=color.white) if barstate.islast label.set_text(fixedRsiLabel, "当前RSI: " + str.tostring(rsi, "#.##")) label.set_x(fixedRsiLabel, bar_index)
将这段代码添加到rsiPlot = plot(...)之后即可,调整high * 1.05可改变显示位置(如low * 0.95放在底部)。
方法3:用表格规整展示RSI数值
如果需要更整洁的展示形式,可使用table组件:
// 新增:创建表格显示RSI数值 var rsiTable = table.new(position.top_right, 1, 1, bgcolor=color.gray, border_width=1) if barstate.islast table.cell(rsiTable, 0, 0, "RSI数值: " + str.tostring(rsi, "#.##"), textcolor=color.white)
代码会在图表右上角生成灰色表格,清晰展示当前RSI数值。
关键说明
- 所有方法均通过
barstate.islast判断仅在最新K线更新数值,避免重复绘制 str.tostring(rsi, "#.##")用于格式化数值,可修改为#.#保留一位小数,或#显示整数- 标签、表格的颜色、位置、大小等样式,均可通过对应参数自定义调整
内容的提问来源于stack exchange,提问作者Ankit Bh
相关产品推荐
相关产品推荐

