Warm tip: This article is reproduced from serverfault.com, please click

Pine-Script Labeling

发布于 2020-12-02 15:27:22

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!

Questioner
ViennaXBT
Viewed
0
beeholder 2020-12-02 23:42:44

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)