PineScript v5:如何让plotchar()显示4位小数而非整数
解决PineScript v5显示USDEUR收盘价4位小数的问题
方法1:修改plotchar()实现
plotchar()默认仅展示整数,你可以通过str.tostring()函数将收盘价格式化为4位小数的字符串,再通过text参数传递给plotchar()实现精准显示:
//@version=5 indicator("USDEUR 4-Decimal Display", overlay=true) USDEUR = close // 替换为你实际使用的USDEUR数据源变量 // 将数值格式化为带4位小数的字符串 formattedPrice = str.tostring(USDEUR, "#.####") // 传递格式化后的文本到plotchar的text参数 plotchar(USDEUR, title="USDEUR", char="", location=location.top, color=color.white, text=formattedPrice)
方法2:使用label.new()实现
如果需要更灵活的显示样式,label.new()是更合适的选择,它支持自定义标签位置、颜色和文本样式:
//@version=5 indicator("USDEUR 4-Decimal Label", overlay=true) USDEUR = close // 替换为你实际使用的USDEUR数据源变量 // 仅在最新K线更新标签 var label priceLabel = na if barstate.islast formattedPrice = str.tostring(USDEUR, "#.####") // 创建或更新标签到K线顶部位置 priceLabel := label.new(bar_index, high, formattedPrice, color=color.white, textcolor=color.black, style=label.style_label_down)
关键说明
str.tostring(USDEUR, "#.####")中的格式化模板#.####确保输出固定保留4位小数,即使末尾为0也会显示。- 如果你的
USDEUR不是直接取当前K线收盘价,请替换为实际的数据源变量。
内容的提问来源于stack exchange,提问作者user3848207
相关产品推荐
相关产品推荐

