math
Properties
E
Type: const float
The mathematical constant e (Euler's number), approximately 2.71828.
The base of natural logarithms and the limit of (1 + 1/n)^n as n approaches infinity.
PHI
Type: const float
The golden ratio φ (phi), approximately 1.61803.
Two quantities are in the golden ratio if their ratio equals the ratio of their sum to the larger quantity. Common in natural patterns and technical analysis (Fibonacci retracements).
PI
Type: const float
The mathematical constant π (pi), approximately 3.14159.
Represents the ratio of a circle's circumference to its diameter.
RECIPROCAL_PHI
Type: const float
The reciprocal of the golden ratio (1/φ), approximately 0.61803.
Equal to φ - 1. Used in Fibonacci analysis where 61.8% is a key retracement level.
Functions
abs
Returns the absolute value of n.
For positive numbers, returns n unchanged. For negative numbers, returns -n. For zero, returns zero.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
n | int | The integer value. |
Returns: int
acos
acos(value: float): floatReturns the arccosine (inverse cosine) of value in degrees.
Given a cosine value, returns the angle that produces it.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
value | float | The cosine value (must be between -1 and 1). |
Returns: float — An angle in degrees in the range [0, 180].
See Also: math.cos
asin
asin(value: float): floatReturns the arcsine (inverse sine) of value in degrees.
Given a sine value, returns the angle that produces it.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
value | float | The sine value (must be between -1 and 1). |
Returns: float — An angle in degrees in the range [-90, 90].
See Also: math.sin
atan
atan(value: float): floatReturns the arctangent (inverse tangent) of value in degrees.
Given a tangent value, returns the angle that produces it.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
value | float | The tangent value (any real number). |
Returns: float — An angle in degrees in the range (-90, 90).
See Also: math.tan, math.atan2
atan2
atan2(y: float, x: float): floatReturns the four-quadrant arctangent of y/x in degrees.
Unlike atan, this function takes two arguments and determines the correct quadrant using the signs of both. Useful for computing angles between vectors or points.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
y | float | The Y component (numerator). | |
x | float | The X component (denominator). |
Returns: float — An angle in degrees in the range (-180, 180].
See Also: math.atan
avg
Returns the average (arithmetic mean) of all provided arguments.
Accepts a variable number of integer arguments.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
numbers | int | Two or more integer values to average. |
Returns: float
cbrt
cbrt(n: float): floatReturns the cube root of n.
Correctly handles negative inputs (e.g., cbrt(-8.0) = -2.0), unlike pow(n, 1.0/3.0) which returns na for negatives.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
n | float | The value to take the cube root of. |
Returns: float
ceil
ceil(n: float): intReturns the ceiling of n: the smallest integer greater than or equal to n.
E.g., ceil(2.3) = 3, ceil(-2.3) = -2.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
n | float | The value to round up. |
Returns: int
See Also: math.floor, math.trunc
clamp
clamp(value: float, min: float, max: float): floatClamps value to the closed interval [min, max].
Returns min if value < min, max if value > max, otherwise value. Avoids the verbose math.max(min, math.min(max, x)) pattern.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
value | float | The value to clamp. | |
min | float | The lower bound (inclusive). | |
max | float | The upper bound (inclusive). |
Returns: float
cos
cos(angle: float): floatReturns the cosine of angle.
Note: Navi uses degrees, not radians (unlike most programming languages).
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
angle | float | The angle in degrees. |
Returns: float — A value between -1 and 1.
See Also: math.acos
exp
exp(n: float): floatReturns e (Euler's number) raised to the power of n.
The inverse of log. Useful for exponential growth/decay calculations.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
n | float | The exponent value. |
Returns: float
See Also: math.log
floor
floor(n: float): intReturns the floor of n: the largest integer less than or equal to n.
E.g., floor(2.7) = 2, floor(-2.3) = -3.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
n | float | The value to round down. |
Returns: int
See Also: math.ceil, math.trunc
hypot
hypot(x: float, y: float): floatReturns the hypotenuse of a right triangle with legs x and y.
Equivalent to sqrt(x*x + y*y) but numerically stable (avoids overflow for large inputs).
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
x | float | The length of the first leg. | |
y | float | The length of the second leg. |
Returns: float
log
log(n: float): floatReturns the natural logarithm (base e) of n.
The inverse of exp. Returns na for non-positive values.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
n | float | The value (must be positive). |
Returns: float
See Also: math.exp, math.log10
log10
log10(n: float): floatReturns the base-10 (common) logarithm of n.
Useful for calculating orders of magnitude. Returns na for non-positive values.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
n | float | The value (must be positive). |
Returns: float
See Also: math.log
max
Returns the maximum value among all provided arguments.
Accepts a variable number of integer arguments.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
numbers | int | Two or more integer values to compare. |
Returns: int
min
Returns the minimum value among all provided arguments.
Accepts a variable number of integer arguments.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
numbers | int | Two or more integer values to compare. |
Returns: int
pow
pow(base: float, exponent: float): floatReturns base raised to the power of exponent.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
base | float | The base value. | |
exponent | float | The power to raise the base to. |
Returns: float
See Also: math.sqrt, math.cbrt
random
random(
min: series float = 0,
max: series float = 1,
seed: series int = na
): series floatGenerates a pseudo-random number in the range [min, max).
The same seed produces the same sequence of numbers, useful for reproducible results.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
min | series float | 0 | Lower bound of the range (inclusive). Defaults to 0. |
max | series float | 1 | Upper bound of the range (exclusive). Defaults to 1. |
seed | series int | na | Optional seed for reproducibility. If na, uses system randomness. |
Returns: series float — A float in the half-open interval [min, max).
round
round(n: float, precision: int = 0): intRounds n to the nearest integer, or to precision decimal places.
Uses round-half-up: 0.5 rounds to 1.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
n | float | The value to round. | |
precision | int | 0 | Number of decimal places (0 for integer rounding). |
Returns: int
round_to_mintick
round_to_mintick(n: float): floatRounds n to the nearest tick value for the current symbol.
The result is always a valid price that can be used for orders. Uses symbol_info.min_tick as the rounding increment.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
n | float | The price value to round. |
Returns: float
sign
sign(n: float): floatReturns the sign of n: 1.0 for positive, -1.0 for negative, 0.0 for zero.
Useful for determining direction without magnitude.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
n | float | The value to check. |
Returns: float — 1.0, -1.0, or 0.0.
See Also: math.abs
sin
sin(angle: float): floatReturns the sine of angle.
Note: Navi uses degrees, not radians (unlike most programming languages).
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
angle | float | The angle in degrees. |
Returns: float — A value between -1 and 1.
See Also: math.asin
sqrt
sqrt(n: float): floatReturns the square root of n.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
n | float | The value (must be non-negative). |
Returns: float — The square root, or na for negative values.
sum
sum(source: series float, length: series int): series floatCalculates the rolling sum of source over the last length bars.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
source | series float | The series to sum. | |
length | series int | Number of bars to include (must be >= 1). |
Returns: series float — The total of the most recent length values in the series.
tan
tan(angle: float): floatReturns the tangent of angle.
Note: Navi uses degrees, not radians. Undefined (returns very large values) at ±90°, ±270°, etc.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
angle | float | The angle in degrees. |
Returns: float
See Also: math.atan, math.atan2
to_degrees
to_degrees(rad: float): floatConverts an angle from radians to degrees.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
rad | float | The angle in radians. |
Returns: float
See Also: math.to_radians
to_radians
to_radians(deg: float): floatConverts an angle from degrees to radians.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
deg | float | The angle in degrees. |
Returns: float
See Also: math.to_degrees
trunc
trunc(n: float): intTruncates n toward zero (removes the fractional part).
Unlike floor, truncation toward zero is symmetric: trunc(-1.9) = -1, whereas floor(-1.9) = -2.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
n | float | The value to truncate. |
Returns: int
See Also: math.floor, math.ceil

