Skip to content

math ​

How do you get the same numbers TradingView prints? Use the math.* functions: math.exp, math.log, math.log10, math.pow, math.sin, math.cos and math.tan follow TradingView's own routines step by step, and math.round rounds as TradingView does, so a script's figures match to the last bit on every engine and in every browser.

pine
//@version=6
indicator("Log return, rounded")
r = math.log(close / close[1]) // TradingView's own log, bit for bit
plot(math.round(r * 100, 2), "Log return, %")

A na argument gives na throughout this page, and so does an argument outside a function's domain, such as the log of 0 or the square root of a negative number.

math.abs ​

pine
math.abs(number) → series float/int

The absolute value. An int stays an int.

Parameter
number
int/float

math.acos ​

pine
math.acos(angle) → series float

The arc cosine, in radians. Outside -1 to 1 it reads na.

Parameter
angle
int/float

math.asin ​

pine
math.asin(angle) → series float

The arc sine, in radians. Outside -1 to 1 it reads na.

Parameter
angle
int/float

math.atan ​

pine
math.atan(angle) → series float

The arc tangent, in radians.

Parameter
angle
int/float

math.atan2 ​

AmenoScript only
ameno
math.atan2(y, x) → series float

The angle from the x axis to the point (x, y), in radians, from -π to π. See AmenoScript's built-ins.

ParameterDescription
y
int/float
The point's vertical coordinate.
x
int/float
The point's horizontal coordinate.

math.avg ​

pine
math.avg(number0, number1, ...) → series float

The mean of its arguments, or na if any of them is na.

Parameter
number0
int/float
number1
int/float
...
int/float

math.ceil ​

pine
math.ceil(number) → series int

The smallest whole number at or above the argument, as an int.

Parameter
number
int/float

math.cos ​

pine
math.cos(angle) → series float

The cosine of an angle in radians.

Parameter
angle
int/float

math.exp ​

pine
math.exp(number) → series float

e raised to the argument.

It follows TradingView's routine step by step, so it matches TradingView where a browser's own Math.exp can differ in the last digit.

Parameter
number
int/float

math.floor ​

pine
math.floor(number) → series int

The largest whole number at or below the argument, as an int.

Parameter
number
int/float

math.hypot ​

AmenoScript only
ameno
math.hypot(x, y) → series float

The length of the hypotenuse, sqrt(x * x + y * y), without overflow on large values. See AmenoScript's built-ins.

ParameterDescription
x
int/float
One side.
y
int/float
The other side.

math.log ​

pine
math.log(number) → series float

The natural logarithm. At 0 or below it reads na.

It follows TradingView's routine step by step, so it matches TradingView to the bit.

Parameter
number
int/float

math.log10 ​

pine
math.log10(number) → series float

The base-10 logarithm. At 0 or below it reads na.

Parameter
number
int/float

math.max ​

pine
math.max(number0, number1, ...) → series float/int

The greatest of its arguments, or na if any of them is na. All ints give an int.

Parameter
number0
int/float
number1
int/float
...
int/float

math.min ​

pine
math.min(number0, number1, ...) → series float/int

The smallest of its arguments, or na if any of them is na. All ints give an int.

Parameter
number0
int/float
number1
int/float
...
int/float

math.pow ​

pine
math.pow(base, exponent) → series float

base raised to exponent.

ParameterDescription
base
int/float
The number to raise.
exponent
int/float
The power.

math.random ​

pine
math.random(min, max, seed) → series float

A pseudo-random number from min up to max.

Without a seed, each run gives different numbers. With one, each call site gives the same sequence on every run, but not TradingView's sequence, since the generator differs.

ParameterDefaultDescription
min
series int/float
0The lowest value it can return.
max
series int/float
1The value it stays below.
seed
simple int
Any int, to repeat the same sequence on every run.

math.round ​

pine
math.round(number, precision) → series float/int

The argument rounded half away from zero: to an int with no precision, else to precision decimal places.

With precision it rounds the number as it is written in decimal, so math.round(2.675, 2) is 2.68 though the stored double is a little under 2.675. An infinity reads na.

ParameterDescription
number
int/float
The value to round.
precision
simple int
How many decimal places to keep. Leave it out for a whole number.

math.round_to_mintick ​

pine
math.round_to_mintick(number) → series float

The argument rounded to the symbol's tick size, syminfo.mintick, half away from zero.

A remainder within 1e-10 of a tick short of the half counts as the half, as TradingView rounds it. An infinity reads na.

ParameterDescription
number
int/float
The value to round.

math.sign ​

pine
math.sign(number) → series float

1 for a positive argument, -1 for a negative one and 0 for 0, as a float.

Parameter
number
int/float

math.sin ​

pine
math.sin(angle) → series float

The sine of an angle in radians.

Parameter
angle
int/float

math.sqrt ​

pine
math.sqrt(number) → series float

The square root. Below 0 it reads na.

Parameter
number
int/float

math.sum ​

pine
math.sum(source, length) → series float

The sum of the last length values.

A na leaves the window as it was, as in ta.sma. A length that changes from bar to bar sums the window of the new length, and a length below 1 sums one bar.

ParameterDescription
source
series int/float
The values to add up.
length
series int
How many bars.

math.tan ​

pine
math.tan(angle) → series float

The tangent of an angle in radians.

Parameter
angle
int/float

math.todegrees ​

pine
math.todegrees(radians) → series float

An angle in radians, in degrees.

Parameter
radians
series int/float

math.toradians ​

pine
math.toradians(degrees) → series float

An angle in degrees, in radians.

Parameter
degrees
series int/float

math.* ​

The number constants.

Each is a const float.

NameValueWhat it means
math.e2.718281828459045Euler's number, the base of the natural logarithm.
math.phi1.618033988749895The golden ratio.
math.pi3.141592653589793The ratio of a circle's circumference to its diameter.
math.rphi0.618033988749895One over the golden ratio.

AmenoScript is compatible with Pine Script v6. Pine Script is a trademark of TradingView, Inc., which has no part in AmenoScript.