如何在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
相关产品推荐
相关产品推荐

