Appearance
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/intThe absolute value. An int stays an int.
| Parameter |
|---|
numberint/float |
math.acos
pine
math.acos(angle) → series floatThe arc cosine, in radians. Outside -1 to 1 it reads na.
| Parameter |
|---|
angleint/float |
math.asin
pine
math.asin(angle) → series floatThe arc sine, in radians. Outside -1 to 1 it reads na.
| Parameter |
|---|
angleint/float |
math.atan
pine
math.atan(angle) → series floatThe arc tangent, in radians.
| Parameter |
|---|
angleint/float |
math.atan2
AmenoScript only
ameno
math.atan2(y, x) → series floatThe angle from the x axis to the point (x, y), in radians, from -π to π. See AmenoScript's built-ins.
| Parameter | Description |
|---|---|
yint/float | The point's vertical coordinate. |
xint/float | The point's horizontal coordinate. |
math.avg
pine
math.avg(number0, number1, ...) → series floatThe mean of its arguments, or na if any of them is na.
| Parameter |
|---|
number0int/float |
number1int/float |
...int/float |
math.ceil
pine
math.ceil(number) → series intThe smallest whole number at or above the argument, as an int.
| Parameter |
|---|
numberint/float |
math.cos
pine
math.cos(angle) → series floatThe cosine of an angle in radians.
| Parameter |
|---|
angleint/float |
math.exp
pine
math.exp(number) → series floate 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 |
|---|
numberint/float |
math.floor
pine
math.floor(number) → series intThe largest whole number at or below the argument, as an int.
| Parameter |
|---|
numberint/float |
math.hypot
AmenoScript only
ameno
math.hypot(x, y) → series floatThe length of the hypotenuse, sqrt(x * x + y * y), without overflow on large values. See AmenoScript's built-ins.
| Parameter | Description |
|---|---|
xint/float | One side. |
yint/float | The other side. |
math.log
pine
math.log(number) → series floatThe 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 |
|---|
numberint/float |
math.log10
pine
math.log10(number) → series floatThe base-10 logarithm. At 0 or below it reads na.
| Parameter |
|---|
numberint/float |
math.max
pine
math.max(number0, number1, ...) → series float/intThe greatest of its arguments, or na if any of them is na. All ints give an int.
| Parameter |
|---|
number0int/float |
number1int/float |
...int/float |
math.min
pine
math.min(number0, number1, ...) → series float/intThe smallest of its arguments, or na if any of them is na. All ints give an int.
| Parameter |
|---|
number0int/float |
number1int/float |
...int/float |
math.pow
pine
math.pow(base, exponent) → series floatbase raised to exponent.
| Parameter | Description |
|---|---|
baseint/float | The number to raise. |
exponentint/float | The power. |
math.random
pine
math.random(min, max, seed) → series floatA 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.
| Parameter | Default | Description |
|---|---|---|
minseries int/float | 0 | The lowest value it can return. |
maxseries int/float | 1 | The value it stays below. |
seedsimple int | Any int, to repeat the same sequence on every run. |
math.round
pine
math.round(number, precision) → series float/intThe 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.
| Parameter | Description |
|---|---|
numberint/float | The value to round. |
precisionsimple 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 floatThe 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.
| Parameter | Description |
|---|---|
numberint/float | The value to round. |
math.sign
pine
math.sign(number) → series float1 for a positive argument, -1 for a negative one and 0 for 0, as a float.
| Parameter |
|---|
numberint/float |
math.sin
pine
math.sin(angle) → series floatThe sine of an angle in radians.
| Parameter |
|---|
angleint/float |
math.sqrt
pine
math.sqrt(number) → series floatThe square root. Below 0 it reads na.
| Parameter |
|---|
numberint/float |
math.sum
pine
math.sum(source, length) → series floatThe 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.
| Parameter | Description |
|---|---|
sourceseries int/float | The values to add up. |
lengthseries int | How many bars. |
math.tan
pine
math.tan(angle) → series floatThe tangent of an angle in radians.
| Parameter |
|---|
angleint/float |
math.todegrees
pine
math.todegrees(radians) → series floatAn angle in radians, in degrees.
| Parameter |
|---|
radiansseries int/float |
math.toradians
pine
math.toradians(degrees) → series floatAn angle in degrees, in radians.
| Parameter |
|---|
degreesseries int/float |
math.*
The number constants.
Each is a const float.