Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to format the float to 3 decimal in pinescript

I m using this alertcondition to ask the value from a plot, but the returned value has more than 3 decimal places.

Can I format it to make the output only display precision until 3 decimals only?

alertcondition(crossover(d2,d3), title="monitor", message='🎯{{ticker}}🎯 Monitor If price above Current Price={{close}} PIVOT = {{plot("Pivot")}} SUPPORT = {{plot("S1")}} RESISTANCE = {{plot("R1")}}
like image 447
ZKTON Avatar asked Jan 26 '23 07:01

ZKTON


2 Answers

If you need to format a string to display only three decimal values, then the format string param of the tostring() function will help you.

If you need to plot the value with precision of three decimal values, then you might just set the study's param precision to 3.

An example of using those params is below:

//@version=4
study("My Script", precision=3) // precision gives three decimal values in the plot-functions

// the format '#.###' gives three decimal values in a string
val = open / close
s = tostring(val, '#.###') 
label.new(bar_index, high, s)

// this plots only three decimal values, because of the study's param 'precision=3'
plot(val)
like image 104
Michel_T. Avatar answered Jan 27 '23 21:01

Michel_T.


You will need to round your plotted value:

//@version=4
study("")
f_round( _val, _decimals) => 
    // Rounds _val to _decimals places.
    _p = pow(10,_decimals)
    round(abs(_val)*_p)/_p*sign(_val)
plot(close, "CloseP")
plot(f_round(close, 3), "CloseR")
alertcondition(true, "A", 'Unrounded: {{plot("CloseP")}}, Rounded:{{plot("CloseR")}}')
like image 29
PineCoders-LucF Avatar answered Jan 27 '23 20:01

PineCoders-LucF