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

如何在Pine Script v5的plotchar()中添加描述性文本?

在Pine Script v5中为图表顶部的数值添加描述性文本

你的需求是将原代码输出的Sample 2.48%改为Sample Percentage 2.48%,但plotchar()的char参数仅支持单个字符,无法直接输出完整的描述性文本。推荐使用标签(label)组件实现自定义文本+数值的显示,以下是修改后的代码:

//@version=5
indicator('Sample', overlay=true, timeframe="", timeframe_gaps=true, format=format.percent)
pct := 2.48

// 仅初始化一次标签,避免重复创建
var sample_label = label.new(
     bar_index, 
     high, 
     "", 
     yloc=yloc.abovebar, 
     color=#ffffff, 
     textcolor=color.black,
     style=label.style_label_down
)

// 动态拼接描述文本与百分比数值
label.set_text(sample_label, str.format("Sample Percentage {0}%", pct))

代码说明

  • var关键字确保标签仅在脚本启动时创建一次,不会随K线循环重复生成
  • str.format()用于将描述性文本和变量pct拼接成目标字符串
  • yloc.abovebar让标签显示在K线顶部,和原plotchar()的location.top效果一致

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 02:25:28