Topics like health, travel, finance, home, living, education and careers | Media Search Home

Three ways to round numbers in TradingView Pine Scripts

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:

  • Different ways to round values in TradingView
  • Round up and down to the nearest full integer with round()
    • Example: round a moving average down to full points
  • Round values down with TradingView"s floor() function
    • Example: round position sizes down with floor()
  • Round values up in TradingView with the ceil() function
    • Example: round volume up to the nearest 1,000
  • Summary
# Different ways to round values in TradingView

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:

  • The round() function rounds a number to the nearest full integer (that is, whole number), with ties (.5) rounding up.
  • The floor() function rounds down to the nearest full integer.
  • And the ceil() function rounds up to the nearest full integer.

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 exampleReturned 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:

varA = 123.4567 varB = round(varA) plot(series=varA) // Plots 123.4567, not 123

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 points

A 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:

//@version=3 study(title="Round example", overlay=true) roundSMA = round(sma(close, 30)) plot(series=roundSMA, color=orange)

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:

Example of plotting rounded moving average values in TradingViewExample of plotting rounded moving average values in TradingViewfloor() function""># Round values down with TradingView"s floor() function

We 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:

  • A positive value we use with floor() becomes less: 9.75 becomes 9.
  • A negative value we use with floor() also becomes less (that is, more negative). floor() turns -2.3 into -3. This happens since going down from -2.3 the value of -3 is the first full number we come across.

Here are some other floor() examples and the values the function returns:

Code exampleReturned 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:

//@version=3 study(title="Floor example", overlay=false) posSize = floor(10000 / close) plot(series=posSize, color=orange)

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:

Example of plotting the position size on a TradingView chartExample of plotting the position size on a TradingView chart

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() function

We 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:

  • A positive value we use with ceil() becomes more positive: 8.4 becomes 9 and 3.02 turns into 4.
  • With a negative value ceil() rounds up towards a greater (that is, positive) value: -2.3 becomes -2 and -8.65 gets us -8.

Some ceil() examples and the values the function returns are:

Code exampleReturned 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,000

Let"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:

//@version=3 study(title="Ceil example", overlay=false) volCeil = ceil(volume / 1000) plot(series=volCeil, color=orange, style=columns)

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:

Plotting values in thousands on a TradingView Pine chartPlotting values in thousands on a TradingView Pine chart# Summary

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

References

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/

Last updated on October 30, 2018 (published October 22, 2018).
# Related TradingView tutorials
  • How to truncate numbers in TradingView"s Pine Script?

    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.

  • How do we make a string of text from a TradingView number?

    We convert numbers into a string of text with TradingView"s tostring() function. That makes resolutions and formatted order comments possible.

« All TradingView types & values articles


Email: contact@about.com.vn - Phone: 818.333.007 - Website: Media Search Home
Copyright © 2007 - Noos. All rights reserved