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

如何在Pine Script图表右上角/左上角放置用户输入文本?

Pine Script 实现文本固定显示在图表左上角/右上角

你可以通过指定图表相对坐标来把文本固定在左上角或右上角,完全脱离K线价格位置的限制。以下是修改后的代码,还支持让用户自主选择显示位置:

//@version=5
indicator(title="Setting Up", overlay = true)

// 新增文本位置选择输入项
display_pos = input.string("右上角", "文本显示位置", options=["左上角", "右上角"])
Trade_Update = input.string(" ","Trade Update", options=[" ","Long Setting Up", "Short Setting Up", "Long Underway", "Short Underway"])

var label l = na
if (barstate.islast)
    // 根据选择切换坐标
    x_pos = display_pos == "右上角" ? chart.right : chart.left
    y_pos = chart.top
    
    // 创建固定位置的标签
    l := label.new(x_pos, y_pos, text=Trade_Update, 
                  yloc=yloc.price, textcolor=color.white, 
                  style=label.style_none, 
                  // 调整文本与图表边缘的间距,避免贴边
                  margin=10)
    label.delete(l[1])

核心改动说明:

  • 用chart.left/chart.right绑定图表左右边缘,chart.top绑定顶部边缘,彻底摆脱K线位置的束缚
  • 将yloc从yloc.abovebar改为yloc.price,配合chart.top实现顶部固定
  • 新增margin参数调整文本与边缘的距离,优化显示效果
  • 添加位置选择输入框,让用户可以一键切换左上角/右上角

修改后文本会固定显示在图表的顶部角落,不会随当前价格变动而靠近K线。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 14:37:46