In TradingView indicator and strategy scripts we use plenty of numerical values. But sometimes we want to round those values before we perform further work on them. How do we do that? Let"s find out what the three ways are to round values in TradingView Pine.
IN THIS ARTICLE:
Virtually every TradingView script calculates with numbers. Whether it"s a moving average or the highest high, numbers are everywhere. But sometimes we want to round our values. We might for instance want to show less decimals on the chart to make an indicator easier to read. Or round the outcome of a money management formula down so we don"t trade a position that"s too big.
There are three ways in which we can round values in TradingView Pine:
An integer is any number that does not have a decimal or fraction. That means 0, -3, 8, and 28,024 are integers. But 3.87 and 1 2/3 are not. We often use the above three functions to get an integer from a real number.
A real number is any number that can be expressed as a decimal (Kelley, 2007). Examples of that are 3.1415926535 and 23,434.95. So what the round(), floor(), and ceil() functions do is turn a real number into an integer one.
Let"s explore these functions more closely and see how they work.
round()""># Round up and down to the nearest full integer with round()The round() function returns the value of its argument rounded to the nearest integer, with .5 rounding up (TradingView, n.d.). round(close), for instance, rounds the instrument"s closing price to an integer.
Some round() examples and the values the function returns are:
Code example | Returned value |
---|---|
round(126.8) | 127 |
round(13.30) | 13 |
round(1.31667) | 1 |
round(-23.492) | -23 |
round(-1.34) | -1 |
round(-4.5) | -4 |
Note the latter entry in this table: round(-4.5) returns -4, not -5. This is because round() rounds ties (.5) up (so towards a more positive number), not down.
To round values to a certain number of decimals after the floating point, first multiply the value you pass into the round() function with a factor of 10. Then divide the outcome of round() with the same amount.
For instance, round(sma(close, 30) * 10) / 10 shows us a SMA rounded to tens of a point (281.1, 77.2, and 23.9).
round(close * 10000) / 10000, on the other hand, rounds values to the whole pip (the fourth decimal after the floating point). That means a closing price of 1.16606 becomes 1.1661 and 1.16826 becomes 1.1683.
By the way, round() returns a rounded value of the argument we give it. But it does not change value we pass into the function:
This same feature also applies to floor() and ceil(). Those functions also don"t change the value we give them. Instead they simply return the rounded value of the argument.
# Example: round a moving average down to full pointsA quick example script that uses round() is the one below, which plots a 30-bar Simple Moving Average (SMA) in full points (without decimal values). The script"s code is:
In this mini-indicator we first define the script"s name and overlay setting with the study() function. Then we calculate a 30-bar SMA with sma(). That function runs here on closing prices (close) for 30 bars.
We place the sma() function inside the round() function. This way whatever value sma() computes gets rounded up or down to the nearest full integer. Then we use the plot() function to show the rounded 30-bar SMA as a regular line plot on the chart.
Here"s how that SMA that only plots full points shows on the chart:
floor() function""># Round values down with TradingView"s floor() functionWe round values down with TradingView"s floor() function. That function accepts one numerical argument. It returns the largest integer that"s less than or equal to the provided argument (TradingView, n.d.). So with 12.89 floor() gives us 12, not 13.
With floor() we always truncate fractional remainders to their floor. That means:
Here are some other floor() examples and the values the function returns:
Code example | Returned value |
---|---|
floor(126.8) | 126 |
floor(13.30) | 13 |
floor(1.31667) | 1 |
floor(-23.492) | -24 |
floor(-1.34) | -2 |
floor(-4.5) | -5 |
As these examples show, the floor() function rounds down for negative values just as it rounds down for positive values.
floor()""># Example: round position sizes down with floor()We typically use TradingView"s floor() function when we calculate a strategy"s position size. That has us always round down an order size to the nearest full contract, share, unit, or lot. With that the trading strategy doesn"t trade a position that"s too big.
Let"s say we want to invest at most 10,000 in a position. How many shares can we purchase for that amount? Here"s a mini-indicator to find out:
First we define the indicator"s properties with the study() function. Then we make the posSize variable. We set that variable to the outcome of 10,000 divided by the bar"s closing price (close).
We do that division inside the floor() function. That way the outcome is rounded down to the nearest full integer. After that we use the plot() function to show the position size on the chart. Here"s how that looks like:
As long as the S&P 500 index stays under 2,500 points we can buy 4 units for 10,000. But as soon as prices rise above 2,500, our 10k gets us a maximum trade size of 3.
From this example we can also tell the difference between floor() and round(). The last price on the chart is 2,566.8, which gets us 3.8959 units (10,000 / 2,566.8). The round() function will turn that 3.8959 into 4, which means we"ll trade a position size that"s a little too big. floor(), on the other hand, turns 3.8959 into 3. That has us invest less than 10k, but also risk less.
ceil() function""># Round values up in TradingView with the ceil() functionWe round values up with TradingView"s ceil() function. That function accepts one numerical argument. It returns the smallest integer that"s greater than or equal to the function"s argument (TradingView, n.d.). So with 12.34 ceil() gives us 13, not 12.
With ceil() we always truncate a fractional numbers to their ceiling. That means:
Some ceil() examples and the values the function returns are:
Code example | Returned value |
---|---|
ceil(126.8) | 127 |
ceil(13.30) | 14 |
ceil(1.31667) | 2 |
ceil(-23.492) | -23 |
ceil(-1.34) | -1 |
ceil(-4.5) | -4 |
As we can tell from the table, ceil() rounds up for both positive as well as negative values.
# Example: round volume up to the nearest 1,000Let"s say we want to plot an instrument"s volume, but don"t care about the precise volume: we rather have volume show in the thousands. Here"s how we can round volume up to the nearest 1k:
We start this quick example indicator with the study() function. Then we round the instrument"s volume up to the nearest 1,000.
For that we first divide the bar"s volume (volume) with 1,000. We do that operation inside the ceil() function. That rounds up the outcome to the nearest full integer. A volume of 2,842 shares would show as 3 and 13,402 as 14.
We then make a columns plot to show the volume readings on the chart:
# SummaryThere are three ways to round values in TradingView. With the round() function we round values to the nearest full integer either up or down, with .5 rounding up.
We can also round down the nearest full integer. We do that with floor(). That way 2.34 and 2.87 become 2, and -8.6 and -8.01 both turn into -9.
The third way to round is with ceil(). This function rounds up to the nearest full integer. That has the function return 5 for 4.6 and 4.12. For -7.31 and -7.75 the function returns -7.
And so round() rounds values up and down. With floor() we get a value less than or equal to the argument passed into the function. And ceil() gives us values that are greater than or equal to its argument value.
Kelley, W.M. (2007). The Complete Idiot"s Guide to Algebra (2nd edition). New York, NY: Penguin Group.
TradingView (n.d.). Pine Script Language Reference Manual. Retrieved on September 26, 2018, from https://www.tradingview.com/study-script-reference/
Truncation throws away a number"s fractional value. TradingView does so with the int() function. With a custom function we truncate a number of decimals.
We convert numbers into a string of text with TradingView"s tostring() function. That makes resolutions and formatted order comments possible.