math
Mathematics.
#Functions
#math.absfunction
The absolute value.
math.abs(number: num): num| Parameter | Type | Description |
|---|---|---|
number | num | The number. |
Returns num.
Example
const a = math.abs(close - open)Since ChartScript 1
#math.acosfunction
Trigonometric acos.
math.acos(angle: float): float| Parameter | Type | Description |
|---|---|---|
angle | float | An angle in radians. |
Returns float.
Example
const v = math.acos(0.5)Since ChartScript 1
#math.asinfunction
Trigonometric asin.
math.asin(angle: float): float| Parameter | Type | Description |
|---|---|---|
angle | float | An angle in radians. |
Returns float.
Example
const v = math.asin(0.5)Since ChartScript 1
#math.atanfunction
Trigonometric atan.
math.atan(angle: float): float| Parameter | Type | Description |
|---|---|---|
angle | float | An angle in radians. |
Returns float.
Example
const v = math.atan(0.5)Since ChartScript 1
#math.avgfunction
The average of the arguments.
math.avg(number0: float, number1: float, …): float| Parameter | Type | Description |
|---|---|---|
number0 | float | The first number. |
number1 | float | The second number. |
Returns float.
Example
const a = math.avg(open, high, low, close)Since ChartScript 1
#math.ceilfunction
Rounds up.
math.ceil(number: num): int| Parameter | Type | Description |
|---|---|---|
number | num | The number. |
Returns int.
Example
const c = math.ceil(close)Since ChartScript 1
#math.cosfunction
Trigonometric cos.
math.cos(angle: float): float| Parameter | Type | Description |
|---|---|---|
angle | float | An angle in radians. |
Returns float.
Example
const v = math.cos(0.5)Since ChartScript 1
#math.expfunction
e to the power number.
math.exp(number: float): float| Parameter | Type | Description |
|---|---|---|
number | float | The number. |
Returns float.
Example
const e = math.exp(1)Since ChartScript 1
#math.floorfunction
Rounds down.
math.floor(number: num): int| Parameter | Type | Description |
|---|---|---|
number | num | The number. |
Returns int.
Example
const f = math.floor(close)Since ChartScript 1
#math.logfunction
Natural logarithm.
math.log(number: float): float| Parameter | Type | Description |
|---|---|---|
number | float | The number. |
Returns float.
Example
const l = math.log(close)Since ChartScript 1
#math.log10function
Base-10 logarithm.
math.log10(number: float): float| Parameter | Type | Description |
|---|---|---|
number | float | The number. |
Returns float.
Example
const l = math.log10(volume)Since ChartScript 1
#math.maxfunction
The largest argument (na when all are na).
math.max(value1: num, value2: num, …): num| Parameter | Type | Description |
|---|---|---|
value1 | num | The first value. |
value2 | num | The second value. |
Returns num.
Example
const m = math.max(open, close)Since ChartScript 1
#math.minfunction
The smallest argument (na when all are na).
math.min(value1: num, value2: num, …): num| Parameter | Type | Description |
|---|---|---|
value1 | num | The first value. |
value2 | num | The second value. |
Returns num.
Example
const m = math.min(open, close)Since ChartScript 1
#math.powfunction
base to the power exponent.
math.pow(base: float, exponent: float): float| Parameter | Type | Description |
|---|---|---|
base | float | The base. |
exponent | float | The exponent. |
Returns float.
Example
const p = math.pow(close, 2)Since ChartScript 1
#math.randomfunction
A pseudo-random number in [min, max); the default seed is fixed per script, so charts and alerts agree.
math.random({ min?: float = 0, max?: float = 1, seed?: simple int = na }): float| Parameter | Type | Description |
|---|---|---|
min | floatdefault 0 | The smallest value. |
max | floatdefault 1 | The largest value. |
seed | simple intdefault na | Fixed seed; the default seed is fixed per script. |
Returns float.
Example
const r = math.random(0, 1)Since ChartScript 1
#math.roundfunction
Rounds half away from zero; with precision, to that many decimals.
math.round(number: num): intmath.round(number: num, precision: int): float| Parameter | Type | Description |
|---|---|---|
number | num | The number. |
precision | int | Decimals shown. |
Returns int.
Example
const r = math.round(close, 2)Since ChartScript 1
#math.round_to_mintickfunction
Rounds to the market tick size.
math.round_to_mintick(number: float): float| Parameter | Type | Description |
|---|---|---|
number | float | The number. |
Returns float.
Example
const r = math.round_to_mintick(hl2)Since ChartScript 1
#math.signfunction
1, -1 or 0.
math.sign(number: num): num| Parameter | Type | Description |
|---|---|---|
number | num | The number. |
Returns num.
Example
const s = math.sign(close - open)Since ChartScript 1
#math.sinfunction
Trigonometric sin.
math.sin(angle: float): float| Parameter | Type | Description |
|---|---|---|
angle | float | An angle in radians. |
Returns float.
Example
const v = math.sin(0.5)Since ChartScript 1
#math.sqrtfunction
Square root.
math.sqrt(number: float): float| Parameter | Type | Description |
|---|---|---|
number | float | The number. |
Returns float.
Example
const s = math.sqrt(volume)Since ChartScript 1
#math.sumfunction
Rolling sum over length bars.
math.sum(source: float, length: simple int): float| Parameter | Type | Description |
|---|---|---|
source | float | The series to read. |
length | simple int | Number of bars. |
Returns float.
Example
const s = math.sum(volume, 20)Since ChartScript 1
#math.tanfunction
Trigonometric tan.
math.tan(angle: float): float| Parameter | Type | Description |
|---|---|---|
angle | float | An angle in radians. |
Returns float.
Example
const v = math.tan(0.5)Since ChartScript 1
#math.todegreesfunction
Radians to degrees.
math.todegrees(radians: float): float| Parameter | Type | Description |
|---|---|---|
radians | float | An angle in radians. |
Returns float.
Example
const d = math.todegrees(math.pi)Since ChartScript 1
#math.toradiansfunction
Degrees to radians.
math.toradians(degrees: float): float| Parameter | Type | Description |
|---|---|---|
degrees | float | An angle in degrees. |
Returns float.
Example
const r = math.toradians(180)Since ChartScript 1
#Constants
| Name | Type | Description |
|---|---|---|
math.e | float | Euler's number (2.71828...). |
math.phi | float | The golden ratio (1.61803...). |
math.pi | float | Pi (3.14159...). |
math.rphi | float | The reciprocal golden ratio (0.61803...). |