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.

Parameters

NameTypeDefaultDescription
nfloatThe float value.

Returns: float


acos

pine
math.acos(float value) → 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

pine
math.asin(float value) → 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

pine
math.atan(float value) → 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

pine
math.ceil(float n) → 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

pine
math.cos(float angle) → float

Returns the cosine of angle.

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

Parameters

NameTypeDefaultDescription
anglefloatThe angle in degrees.

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


exp

pine
math.exp(float n) → 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

pine
math.floor(float n) → 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

pine
math.log(float n) → 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

pine
math.log10(float n) → 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

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

Returns base raised to the power of exponent.

Parameters

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

Returns: float


random

pine
math.random(
    series float min = 0,
    series float max = 1,
    series int seed = 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

pine
math.round(float n, int precision = 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

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.mintick as the rounding increment.

Parameters

NameTypeDefaultDescription
nfloatThe price value to round.

Returns: float

Rounds n to the nearest multiple of the symbol's minimum tick size.

Ensures prices are valid for the instrument being traded.

Parameters

NameTypeDefaultDescription
nfloatThe value to round.

Returns: float


sign

pine
math.sign(float n) → 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

pine
math.sin(float angle) → float

Returns the sine of angle.

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

Parameters

NameTypeDefaultDescription
anglefloatThe angle in degrees.

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


sqrt

pine
math.sqrt(float n) → 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

pine
math.sum(series float source, series int length) → 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

pine
math.tan(float angle) → float

Returns the tangent of angle.

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

Parameters

NameTypeDefaultDescription
anglefloatThe angle in degrees.

Returns: float


todegrees

pine
math.todegrees(float rad) → float

Converts an angle from radians to degrees.

Parameters

NameTypeDefaultDescription
radfloatThe angle in radians.

Returns: float


toradians

pine
math.toradians(float deg) → float

Converts an angle from degrees to radians.

Parameters

NameTypeDefaultDescription
degfloatThe angle in degrees.

Returns: float

Released under the MIT License.