Skip to content

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

NameTypeDefaultDescription
nintThe integer value.

Returns: int

Returns the absolute value of n.

For positive numbers, returns n unchanged. For negative numbers, returns -n. For zero, returns zero.

Parameters

NameTypeDefaultDescription
nfloatThe float value.

Returns: float


acos

navi
acos(value: float): float

Returns the arccosine (inverse cosine) of value in degrees.

Given a cosine value, returns the angle that produces it.

Parameters

NameTypeDefaultDescription
valuefloatThe cosine value (must be between -1 and 1).

Returns: float — An angle in degrees in the range [0, 180].

See Also: math.cos


asin

navi
asin(value: float): float

Returns the arcsine (inverse sine) of value in degrees.

Given a sine value, returns the angle that produces it.

Parameters

NameTypeDefaultDescription
valuefloatThe sine value (must be between -1 and 1).

Returns: float — An angle in degrees in the range [-90, 90].

See Also: math.sin


atan

navi
atan(value: float): float

Returns the arctangent (inverse tangent) of value in degrees.

Given a tangent value, returns the angle that produces it.

Parameters

NameTypeDefaultDescription
valuefloatThe tangent value (any real number).

Returns: float — An angle in degrees in the range (-90, 90).

See Also: math.tan, math.atan2


atan2

navi
atan2(y: float, x: float): float

Returns 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

NameTypeDefaultDescription
yfloatThe Y component (numerator).
xfloatThe 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

NameTypeDefaultDescription
numbersintTwo or more integer values to average.

Returns: float

Returns the average (arithmetic mean) of all provided arguments.

Parameters

NameTypeDefaultDescription
numbersfloatTwo or more float values to average.

Returns: float


cbrt

navi
cbrt(n: float): float

Returns 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

NameTypeDefaultDescription
nfloatThe value to take the cube root of.

Returns: float

See Also: math.pow, math.sqrt


ceil

navi
ceil(n: float): int

Returns the ceiling of n: the smallest integer greater than or equal to n.

E.g., ceil(2.3) = 3, ceil(-2.3) = -2.

Parameters

NameTypeDefaultDescription
nfloatThe value to round up.

Returns: int

See Also: math.floor, math.trunc


clamp

navi
clamp(value: float, min: float, max: float): float

Clamps 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

NameTypeDefaultDescription
valuefloatThe value to clamp.
minfloatThe lower bound (inclusive).
maxfloatThe upper bound (inclusive).

Returns: float


cos

navi
cos(angle: float): float

Returns the cosine of angle.

Note: Navi uses degrees, not radians (unlike most programming languages).

Parameters

NameTypeDefaultDescription
anglefloatThe angle in degrees.

Returns: float — A value between -1 and 1.

See Also: math.acos


exp

navi
exp(n: float): float

Returns e (Euler's number) raised to the power of n.

The inverse of log. Useful for exponential growth/decay calculations.

Parameters

NameTypeDefaultDescription
nfloatThe exponent value.

Returns: float

See Also: math.log


floor

navi
floor(n: float): int

Returns the floor of n: the largest integer less than or equal to n.

E.g., floor(2.7) = 2, floor(-2.3) = -3.

Parameters

NameTypeDefaultDescription
nfloatThe value to round down.

Returns: int

See Also: math.ceil, math.trunc


hypot

navi
hypot(x: float, y: float): float

Returns 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

NameTypeDefaultDescription
xfloatThe length of the first leg.
yfloatThe length of the second leg.

Returns: float


log

navi
log(n: float): float

Returns the natural logarithm (base e) of n.

The inverse of exp. Returns na for non-positive values.

Parameters

NameTypeDefaultDescription
nfloatThe value (must be positive).

Returns: float

See Also: math.exp, math.log10


log10

navi
log10(n: float): float

Returns the base-10 (common) logarithm of n.

Useful for calculating orders of magnitude. Returns na for non-positive values.

Parameters

NameTypeDefaultDescription
nfloatThe 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

NameTypeDefaultDescription
numbersintTwo or more integer values to compare.

Returns: int

Returns the maximum value among all provided arguments.

Parameters

NameTypeDefaultDescription
numbersfloatTwo or more float values to compare.

Returns: float


min

Returns the minimum value among all provided arguments.

Accepts a variable number of integer arguments.

Parameters

NameTypeDefaultDescription
numbersintTwo or more integer values to compare.

Returns: int

Returns the minimum value among all provided arguments.

Parameters

NameTypeDefaultDescription
numbersfloatTwo or more float values to compare.

Returns: float


pow

navi
pow(base: float, exponent: float): float

Returns base raised to the power of exponent.

Parameters

NameTypeDefaultDescription
basefloatThe base value.
exponentfloatThe power to raise the base to.

Returns: float

See Also: math.sqrt, math.cbrt


random

navi
random(
    min: series float = 0,
    max: series float = 1,
    seed: series int = na
  ): series float

Generates a pseudo-random number in the range [min, max).

The same seed produces the same sequence of numbers, useful for reproducible results.

Parameters

NameTypeDefaultDescription
minseries float0Lower bound of the range (inclusive). Defaults to 0.
maxseries float1Upper bound of the range (exclusive). Defaults to 1.
seedseries intnaOptional seed for reproducibility. If na, uses system randomness.

Returns: series float — A float in the half-open interval [min, max).


round

navi
round(n: float, precision: int = 0): int

Rounds n to the nearest integer, or to precision decimal places.

Uses round-half-up: 0.5 rounds to 1.

Parameters

NameTypeDefaultDescription
nfloatThe value to round.
precisionint0Number of decimal places (0 for integer rounding).

Returns: int


round_to_mintick

navi
round_to_mintick(n: float): float

Rounds 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

NameTypeDefaultDescription
nfloatThe price value to round.

Returns: float


sign

navi
sign(n: float): float

Returns the sign of n: 1.0 for positive, -1.0 for negative, 0.0 for zero.

Useful for determining direction without magnitude.

Parameters

NameTypeDefaultDescription
nfloatThe value to check.

Returns: float — 1.0, -1.0, or 0.0.

See Also: math.abs


sin

navi
sin(angle: float): float

Returns the sine of angle.

Note: Navi uses degrees, not radians (unlike most programming languages).

Parameters

NameTypeDefaultDescription
anglefloatThe angle in degrees.

Returns: float — A value between -1 and 1.

See Also: math.asin


sqrt

navi
sqrt(n: float): float

Returns the square root of n.

Parameters

NameTypeDefaultDescription
nfloatThe value (must be non-negative).

Returns: float — The square root, or na for negative values.

See Also: math.pow, math.cbrt


sum

navi
sum(source: series float, length: series int): series float

Calculates the rolling sum of source over the last length bars.

Parameters

NameTypeDefaultDescription
sourceseries floatThe series to sum.
lengthseries intNumber of bars to include (must be >= 1).

Returns: series float — The total of the most recent length values in the series.


tan

navi
tan(angle: float): float

Returns the tangent of angle.

Note: Navi uses degrees, not radians. Undefined (returns very large values) at ±90°, ±270°, etc.

Parameters

NameTypeDefaultDescription
anglefloatThe angle in degrees.

Returns: float

See Also: math.atan, math.atan2


to_degrees

navi
to_degrees(rad: float): float

Converts an angle from radians to degrees.

Parameters

NameTypeDefaultDescription
radfloatThe angle in radians.

Returns: float

See Also: math.to_radians


to_radians

navi
to_radians(deg: float): float

Converts an angle from degrees to radians.

Parameters

NameTypeDefaultDescription
degfloatThe angle in degrees.

Returns: float

See Also: math.to_degrees


trunc

navi
trunc(n: float): int

Truncates n toward zero (removes the fractional part).

Unlike floor, truncation toward zero is symmetric: trunc(-1.9) = -1, whereas floor(-1.9) = -2.

Parameters

NameTypeDefaultDescription
nfloatThe value to truncate.

Returns: int

See Also: math.floor, math.ceil