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

How to truncate numbers in TradingView"s Pine Script?

Sometimes our script has a value from which we want to remove the fractional value. We could perform rounding, but that might change the value before the decimal point (.). Let"s see how truncation helps.

IN THIS ARTICLE:

  • Truncation: throw away fractional values from a number
  • Truncate TradingView values to a whole number
    • Example: truncate and plot the closing price
  • Truncate TradingView values to a number of decimal places
    • Example: truncate closing prices to 3 and 1 decimals
  • Summary
# Truncation: throw away fractional values from a number

When we truncate a number, we throw away its fractional value. That way we end with a whole (integer) number.

TradingView has two ways to part ways with decimal values:

  • The int() function will convert a floating-point value to an integer. That removes any decimal portion: 1.83952 becomes 1.
  • When we leverage int() in a custom function, we can truncate a value to a certain number of decimal places. That would turn 1.83952 into 1.83 or 1.839.

Let"s take a closer look at both approaches.

Truncation is different from rounding. When we round values up and down, the number before the decimal point may change. That way 1.8 becomes 2 and 1.3 gets turned into 1.

When we truncate a value, the number before the dot (.) never changes. So 3.9 always becomes 3 and -5.78 is turned into -5.

# Truncate TradingView values to a whole number

To turn a floating-point value into a whole (integer) number, we simply call the int() function on that value (TradingView, n.d.). What we get from that function is an integer – and so any fractional value is lost in the process.

Using int() is simple:

int(1.33445) // Returns: 1

With int() we always throw away the fractional value of a number. That"s how we end up with the number before the decimal point (.) consistently.

Here are more examples of int():

Code exampleReturned value
int(126.8)126
int(13.30)13
int(1.31667)1
int(-23.49)-23
int(-1.34)-1
int(-4.5)-4
int(0)0
int(na)na

TradingView"s floor() function truncates positive numbers, just as int() does. So why not use floor() instead?

When we use that function on a negative number, floor() doesn"t truncate but rather rounds down (-5.7 becomes -6). And so we use int(), which truncates both positive as well as negative numbers.

# Example: truncate and plot the closing price

Here"s how you can use the int() function in a TradingView script:

//@version=4 study(title="Truncate to whole number", overlay=false) // Truncate the closing price to an integer closeTrunc = int(close) // Output both the close and truncated close plot(series=close, title="Close") plot(series=closeTrunc, title="Truncated")

Here we first set the indicator properties with the study() function. Then we truncate the instrument"s closing price to a whole number with the int() function. We store that value in the closeTrunc variable for use later.

Then the plot() function makes two regular line plots. The first displays the instrument"s closing price with the original number of decimal places. The other shows the truncated value.

Feel free to add this mini-script to the chart and see how it behaves with different instruments. That makes the above explanation of int() a lot more practical.

# Truncate TradingView values to a number of decimal places

TradingView"s int() function can only truncate to a whole number (TradingView, n.d.). So what if we want to keep a number of decimal places, but throw away the rest?

Luckily, we can do just that with a small bit of code. Here"s a custom function that truncates floating-point values to a particular number of decimal places:

// truncate() truncates a given number // to a certain number of decimals truncate(number, decimals) => factor = pow(10, decimals) int(number * factor) / factor

This function has two parameters: number for the value to truncate, and decimals for how many decimal places that value should keep.

The function"s first line calculates the factor we"ll use to temporarily adjust the value to truncate. To get that value we call the pow() function with 10 and decimals as arguments. That raises 10 to the power of the decimals parameter.

Then we do the truncation. Here the int() function operates on the value to truncate (number) multiplied with the factor we just calculated. After that we divide with factor to get the original scale of the number back.

Say we want to keep two decimals and our number is 1.3428. The factor for that is 100 (10^2). Inside int() we then multiply 1.3428 with 100, which gives 134.28. int() removes those two decimal places and returns 134. We then divide back by 100 to get the original scale back: 1.34.

To use the above custom function we simply do:

truncate(1.83495, 3) // Returns: 1.834

So what truncate() does is throw away the fractional value after a certain number of decimal places.

Here are a few more truncate() examples:

Code exampleReturned value
truncate(126.8, 0)126
truncate(13.30, 3)13.3
truncate(1.31667, 2)1.31
truncate(-23.49, 1)-23.4
truncate(-1.34, 5)-1.34
truncate(-4.5, 1)-4.5
truncate(na, 5)na
# Example: truncate closing prices to 3 and 1 decimals

One way to use truncate() is to plot closing prices with a certain number of decimal places. Here"s how that looks:

//@version=4 study(title="Truncate to decimal amount", overlay=false) // truncate() truncates a given number // to a certain number of decimals truncate(number, decimals) => factor = pow(10, decimals) int(number * factor) / factor // Output original close and truncated values plot(series=close, title="Close", color=color.orange) plot(series=truncate(close, 3), title="Truncate (3)") plot(series=truncate(close, 1), title="Truncate (1)")

First we define the indicator"s settings with the study() function. Then we copy-paste the code of the custom truncate() function.

Next we make three line plots. The first displays the instrument"s closing price (close). The next truncates that price to 3 decimals with truncate(). Then the last plot uses the same function to show the close with 1 decimal value.

LEARN MORE

  • Round values in TradingView
  • Types and values TradingView category
# Summary

When we truncate a value, we remove its fractional value. Any floating-point value we give the int() function is returned without any decimal places.

It"s also possible to truncate after a certain number of decimal places. For that we use a custom function that leverages TradingView"s int() function.

References

TradingView (n.d.). Pine Script Language Reference Manual. Retrieved on November 4, 2019, from https://www.tradingview.com/pine-script-reference/v4/

Published November 8, 2019.
# Related TradingView tutorials
  • 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.

  • Three ways to round numbers in TradingView Pine Scripts

    TradingView has three rounding functions. round() rounds to the nearest full integer, with .5 rounding up. ceil() rounds up and floor() rounds down.

« All TradingView types & values articles


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