You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Pine Script中设置标签位置使其显示在绘制线上而非K线上下方

解决方法

你现在标签固定在K线上下是因为使用了yloc.abovebar/yloc.belowbar参数,该参数会强制将标签绑定到K线的上下位置,不需要绑定K线的话直接去掉yloc参数,手动指定标签的y坐标为线段的y值即可。


具体修改点

  • 删除label.new()函数中的yloc参数
  • 把y参数值设置为对应线段的Y轴坐标(你代码里已经用line.get_y1()取到了线段价格,直接用这个值即可)
  • 若需要微调标签和线段的间距,可对y值做小幅偏移,避免文字和线段重叠

修改后代码示例

if isBullEngulf
    bullEngulfOpen := line.new(bar_index - 1, open[1], bar_index, open[1], extend=extend.right, color=color.black)
    bullEngulfLow := line.new(bar_index - 1, low < low[1] ? low : low[1], bar_index, low < low[1] ? low : low[1], extend=extend.right, color=color.green)
    bullEngulfLowPrice = line.get_y1(bullEngulfLow)
    offset = input(15, maxval=900, title="Label Offset",step=5)
    // 低位标签放到线段上,加小幅偏移避免和线段重叠
    bullEngulfLowLabel := label.new(x = bar_index + offset, y = bullEngulfLowPrice + syminfo.mintick * 2, text="\n"+tostring(bullEngulfLowPrice), style=label.style_none,size=size.normal,textcolor=color.white)
    
    bullEngulfHigh := line.new(bar_index - 1, high < high[1] ? high : high[1], bar_index, high < high[1] ? high : high[1], extend=extend.right, color=color.green)
    bullEngulfHighPrice = line.get_y1(bullEngulfHigh)
    // 高位标签放到线段上
    bullEngulfHighLabel := label.new(bar_index - 1, y = bullEngulfHighPrice, text="\n"+tostring(bullEngulfHighPrice), style=label.style_none,size=size.normal,textcolor=color.white)

效果参考

效果图示例

注:用syminfo.mintick做偏移适配所有品种的报价精度,不会出现低价品种偏移过大、高价品种偏移不足的问题。

内容的提问来源于stack exchange,提问作者mgoblue33

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 17:09:03