Pine-Script Labeling - 有什么方法可以四舍五入吗?

Pine-Script Labeling - Any way to round the numbers?

您好,感谢您的关注!

我在我的一些策略中使用标签,以便更容易获得正确的入场价、出场价和止损价。但是,它们总是显示大量的小数,我不需要这些小数并且会阻塞图表。有什么办法可以将这些数字四舍五入为 2 位或 3 位小数。我只遇到过 round 函数,它四舍五入为整数,不适合这个任务。

举个例子:

我使用

为我的关卡获取花车
entry_price = valuewhen(short_entry and strategy.position_size == 0, close, 0)

然后我使用

打印标签
label.new(x=bar_index, y=high, text = "Entry = " + tostring(entry_price), color=color.black, textcolor=color.black, style=label.style_arrowdown, yloc = yloc.abovebar)

非常感谢您的意见!

tostring() 有一个可选参数,允许您使用任意多的小数来格式化字符串。要使用它,请将 "#.####" 之类的字符串传递给它。比较以下标签的输出:

//@version=4
study("My Script", overlay=true)
a = 0.12345678
l1 = label.new(bar_index, high, tostring(a))
l2 = label.new(bar_index, low, tostring(a, "#.##"), style=label.style_label_up)