Math
Rounding, aggregation, clamping, logarithms, and numeric utilities.
All math functions are available under the math.* namespace.
Rounding
math.round
math.round(value, precision?) -> numberRound to a number of decimal places. Supports negative precision (e.g. -1 rounds to nearest 10).
| Param | Type | Default | Description |
|---|---|---|---|
value | number | — | Value to round |
precision | number | 0 | Decimal places |
math.roundToTick
math.roundToTick(value, tickSize?) -> numberRound to the nearest tick increment. Uses context.tickSize when tickSize is omitted.
| Param | Type | Default | Description |
|---|---|---|---|
value | number | — | Value to round |
tickSize | number | context.tickSize | Tick increment |
math.roundToTickUp
math.roundToTickUp(value, tickSize?) -> numberRound up to the nearest tick increment.
| Param | Type | Default | Description |
|---|---|---|---|
value | number | — | Value to round |
tickSize | number | context.tickSize | Tick increment |
math.roundToTickDown
math.roundToTickDown(value, tickSize?) -> numberRound down to the nearest tick increment.
| Param | Type | Default | Description |
|---|---|---|---|
value | number | — | Value to round |
tickSize | number | context.tickSize | Tick increment |
math.roundToStep
math.roundToStep(value, stepSize?) -> numberRound to the nearest step increment. Uses context.stepSize when stepSize is omitted.
| Param | Type | Default | Description |
|---|---|---|---|
value | number | — | Value to round |
stepSize | number | context.stepSize | Step increment |
math.roundToStepUp
math.roundToStepUp(value, stepSize?) -> numberRound up to the nearest step increment.
| Param | Type | Default | Description |
|---|---|---|---|
value | number | — | Value to round |
stepSize | number | context.stepSize | Step increment |
math.roundToStepDown
math.roundToStepDown(value, stepSize?) -> numberRound down to the nearest step increment.
| Param | Type | Default | Description |
|---|---|---|---|
value | number | — | Value to round |
stepSize | number | context.stepSize | Step increment |
Aggregation
math.sum
math.sum(source, length) -> numberRolling sum over the last length bars. NaN/na source values are ignored rather than pushed into the retained window, and empty windows produce NaN.
//@version=2
indicator("math.sum Example", false)
const candles = subscribe(data.OHLCV)
function onBar(index) {
plot("10-Bar Sum", math.sum(candles.close(), 10), { color: color.blue })
}math.avg
math.avg(...values) -> number
math.avg(array) -> numberArithmetic mean. Accepts variadic numbers or a single array.
math.avg(10, 20, 30) // 20math.min
math.min(...values) -> number
math.min(array) -> numberMinimum value. Accepts variadic numbers or a single array.
math.max
math.max(...values) -> number
math.max(array) -> numberMaximum value. Accepts variadic numbers or a single array.
math.clamp
math.clamp(value, min, max) -> numberConstrains a value to a range.
| Param | Type | Description |
|---|---|---|
value | number | Value to clamp |
min | number | Lower bound |
max | number | Upper bound |
Basic Operations
math.abs
math.abs(value) -> numberAbsolute value.
math.floor
math.floor(value) -> numberRound down to nearest integer.
math.ceil
math.ceil(value) -> numberRound up to nearest integer.
math.trunc
math.trunc(value) -> numberRemove fractional part (truncate toward zero).
math.pow
math.pow(base, exponent) -> numberRaise base to exponent power.
math.sqrt
math.sqrt(value) -> numberSquare root.
math.exp
math.exp(value) -> numberEuler's number raised to value power (e^x).
math.sign
math.sign(value) -> numberReturns -1, 0, or 1 indicating the sign of the value.
Logarithms
math.log10
math.log10(value) -> numberBase-10 logarithm.
math.log2
math.log2(value) -> numberBase-2 logarithm.
math.logN
math.logN(value, base) -> numberLogarithm with arbitrary base.
| Param | Type | Description |
|---|---|---|
value | number | Input value |
base | number | Log base |
Validity Checks
math.isFinite
math.isFinite(value) -> booleanReturns true if value is a finite number (not NaN, not Infinity).
Utilities
math.safeDiv
math.safeDiv(numerator, denominator, fallback?) -> numberSafe division — returns fallback when denominator is zero or NaN.
| Param | Type | Default | Description |
|---|---|---|---|
numerator | number | — | Numerator |
denominator | number | — | Denominator |
fallback | number | NaN | Value to return on division by zero |
//@version=2
indicator("math.safeDiv Example", false)
const candles = subscribe(data.OHLCV)
function onBar(index) {
const ratio = math.safeDiv(candles.volume(), candles.buyVolume(), 0)
plot("Volume Ratio", ratio, { color: color.orange })
}math.random
math.random() -> number
math.random(max) -> number
math.random(min, max) -> numberReturns a random number. Behavior depends on arguments:
| Call | Returns |
|---|---|
math.random() | [0, 1) |
math.random(max) | [0, max) |
math.random(min, max) | [min, max) |