简体   繁体   中英

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. I have only come across the round function, which rounds to integers and is not fit for the task.

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. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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