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.


rphi

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
math.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].


asin

navi
math.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].


atan

navi
math.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).


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


ceil

navi
math.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


cos

navi
math.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.


exp

navi
math.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


floor

navi
math.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


log

navi
math.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


log10

navi
math.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


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
math.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


random

navi
math.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
math.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
math.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 syminfo.min_tick as the rounding increment.

Parameters

NameTypeDefaultDescription
nfloatThe price value to round.

Returns: float


sign

navi
math.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.


sin

navi
math.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.


sqrt

navi
math.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.


sum

navi
math.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
math.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


todegrees

navi
math.todegrees(rad: float): float

Converts an angle from radians to degrees.

Parameters

NameTypeDefaultDescription
radfloatThe angle in radians.

Returns: float


toradians

navi
math.toradians(deg: float): float

Converts an angle from degrees to radians.

Parameters

NameTypeDefaultDescription
degfloatThe angle in degrees.

Returns: float

Released under the MIT License.