求助:在TradingView RR线旁添加对应价格标签
Pine Script v5 实现RR线旁显示价格标签方案
核心解决逻辑
之前设置yloc=yloc.price未成功,问题出在没有给标签绑定准确的坐标位置,同时未通过样式参数让标签贴合价格线。核心思路是:先计算出各RR目标价,再用label.new()指定对应价格位置,配合样式参数让标签显示在RR线旁。
完整代码示例
//@version=5 indicator("RR 价格标签示例", overlay=true) // 定义多单入场位(前一根K线高点)和止损位(前一根K线低点) entry_price = high[1] stop_loss_price = low[1] // 计算风险差值 risk = entry_price - stop_loss_price // 计算各RR比例的目标价格 target_15 = entry_price + risk * 1.5 target_20 = entry_price + risk * 2 target_30 = entry_price + risk * 3 // 绘制RR水平线 plot(target_15, color=color.blue, title="1.5:1 RR", style=plot.style_linebr) plot(target_20, color=color.green, title="2:1 RR", style=plot.style_linebr) plot(target_30, color=color.red, title="3:1 RR", style=plot.style_linebr) // 保留原有高低点标签 label.new(bar_index[1], entry_price, "入场: " + str.tostring(entry_price, "0.00"), yloc=yloc.price, style=label.style_label_down, color=color.orange, textcolor=color.white) label.new(bar_index[1], stop_loss_price, "止损: " + str.tostring(stop_loss_price, "0.00"), yloc=yloc.price, style=label.style_label_up, color=color.red, textcolor=color.white) // 给RR线添加价格标签(关键实现) // 1.5:1目标价标签 label.new(bar_index, target_15, "1.5:1\n" + str.tostring(target_15, "0.00"), yloc=yloc.price, style=label.style_label_left, color=color.blue, textcolor=color.white) // 2:1目标价标签 label.new(bar_index, target_20, "2:1\n" + str.tostring(target_20, "0.00"), yloc=yloc.price, style=label.style_label_left, color=color.green, textcolor=color.white) // 3:1目标价标签 label.new(bar_index, target_30, "3:1\n" + str.tostring(target_30, "0.00"), yloc=yloc.price, style=label.style_label_left, color=color.red, textcolor=color.white)
关键调整说明
- 标签的
x坐标用bar_index(当前K线)或bar_index[1](入场K线),可根据需求调整显示位置 - 用
style=label.style_label_left或label.style_label_right避免标签被K线实体遮挡 - 通过
str.tostring()格式化价格,指定小数位数提升可读性 - 若需限制标签显示数量,可添加
show_last=N参数(如show_last=3只显示最近3组标签)
内容的提问来源于stack exchange,提问作者euam23
相关产品推荐
相关产品推荐

