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
| Name | Type | Default | Description |
|---|---|---|---|
n | int | The integer value. |
Returns: int
acos
math.acos(float value) → 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].
asin
math.asin(float value) → 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].
atan
math.atan(float value) → 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).
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
ceil
math.ceil(float n) → 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
cos
math.cos(float angle) → floatReturns the cosine of angle.
Note: Pine Script 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.
exp
math.exp(float n) → 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
floor
math.floor(float n) → 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
log
math.log(float n) → 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
log10
math.log10(float n) → 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
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
math.pow(float base, float exponent) → 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
random
math.random(
series float min = 0,
series float max = 1,
series int seed = 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
math.round(float n, int precision = 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
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
| Name | Type | Default | Description |
|---|---|---|---|
n | float | The price value to round. |
Returns: float
sign
math.sign(float n) → 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.
sin
math.sin(float angle) → floatReturns the sine of angle.
Note: Pine Script 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.
sqrt
math.sqrt(float n) → 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
math.sum(series float source, series int length) → 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
math.tan(float angle) → floatReturns the tangent of angle.
Note: Pine Script 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
todegrees
math.todegrees(float rad) → floatConverts an angle from radians to degrees.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
rad | float | The angle in radians. |
Returns: float
toradians
math.toradians(float deg) → floatConverts an angle from degrees to radians.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
deg | float | The angle in degrees. |
Returns: float