In Pine Script, we often encounter situations where we need to round off numbers, whether to simplify our outputs or prepare data for specific calculations. In such cases, we resort to the math.round
function. This powerful tool helps us round numbers to the nearest integer or a given decimal precision. Let’s delve deeper into the functionality and uses of this function in Pine Script programming.
Function Signature
Understanding the function signature is the first step in learning how to use a function. The function signature of math.round
is as follows:
math.round(number, precision)
Function Arguments
number
The number
parameter can be either a series int/float
, which is the value that we want to round off.
precision
The precision
parameter is an optional argument, represented as a series int
. This determines the decimal places to which the number
will be rounded. If no argument is supplied, the function rounds the number to the nearest integer.
Return Value
The math.round
function returns the value of the input number
rounded to the nearest integer or to the specified precision
.
It’s essential to note that if the function encounters ‘na’ (not available) values, it returns ‘na’.
Syntax in Practice
Let’s examine the following simple example to illustrate the math.round
function:
//@version=5 indicator("My Script", overlay = true) rounded_close = math.round(close, 2) plot(rounded_close , linewidth = 3)
let’s dissect the provided Pine Script line by line.
//@version=5
The //@version=5 declaration specifies the version of Pine Script that is used in your script. It is a mandatory line that must be placed at the very top of your script. Each version comes with its own features, changes, and improvements, and the language is backward compatible, meaning a script written in an older version will work in a newer one. In this case, we’re using version 5 of Pine Script.
indicator("My Script", overlay = true)
The indicator function call initializes your script, providing it a title (“My Script”), and setting whether or not it overlays the main price chart. If overlay = true, the output of the script will be displayed on the price chart itself. If overlay = false, the output will be shown in a separate pane below the main price chart.
rounded_close = math.round(close, 2)
This line introduces a new variable rounded_close and assigns it a value obtained by rounding the close price to two decimal places. The close price is a built-in variable in Pine Script that refers to the closing price of the current bar (or candlestick). The math.round function is used to round this close price to the nearest hundredth (since the precision is set to 2).
plot(rounded_close, linewidth = 3)
Finally, the plot
function is used to plot the rounded_close
variable on the chart. The linewidth = 3
part is an argument that sets the thickness of the plotted line to 3 units.
In conclusion, this script rounds off the closing price to two decimal places and plots it onto the chart with a line thickness of 3 units.
Key Takeaway
The math.round
function is a fundamental tool in Pine Script that enables us to round numbers to the nearest integer or a specific decimal precision. Its two parameters – number
and precision
– provide flexibility in how we manipulate numerical data in our scripts. Always remember that for ‘na’ values, the function will return ‘na’.
Conclusion
Mastering the math.round
function opens up new possibilities in Pine Script programming. It offers a simple solution to adjust numerical precision, helping you control how you present and process your data. Now you can confidently implement