简体   繁体   English

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

[英]Pine-Script Labeling - Any way to round the numbers?

Hello and thank you for looking into this!您好,感谢您对此进行调查!

I am using labels in some of my strategies, to make it easier to get the correct values for entry, exit and stop prices.我在我的一些策略中使用标签,以便更容易获得正确的入场、出场和止损价格值。 However, they always display a large number of decimals, which I do not need and clog the chart.但是,它们总是显示大量小数,我不需要这些小数并阻塞图表。 Is there any way to round those number to say 2 or 3 decimals.有没有办法将这些数字四舍五入到小数点后 2 位或 3 位。 I have only come across the round function, which rounds to integers and is not fit for the task.我只遇到了圆形 function,它四舍五入为整数,不适合该任务。

As an example:举个例子:

I get the floats for my levels using我得到了我的水平的花车使用

entry_price = valuewhen(short_entry and strategy.position_size == 0, close, 0)

I then print the labels using然后我使用打印标签

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)

Thx a lot for your input!非常感谢您的意见!

tostring() has an optional argument that allows you to format the string with as many decimals as you want. tostring()有一个可选参数,允许您使用任意数量的小数格式化字符串。 To use it, pass a string like "#.####" to it.要使用它,请将"#.####"之类的字符串传递给它。 Compare the outputs of the following labels:比较以下标签的输出:

//@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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM