MMTMMT Docs

Math

Rounding, aggregation, clamping, logarithms, and numeric utilities.

All math functions are available under the math.* namespace.

Rounding

math.round

math.round(value, precision?) -> number

Round to a number of decimal places. Supports negative precision (e.g. -1 rounds to nearest 10).

ParamTypeDefaultDescription
valuenumberValue to round
precisionnumber0Decimal places

math.roundToTick

math.roundToTick(value, tickSize?) -> number

Round to the nearest tick increment. Uses context.tickSize when tickSize is omitted.

ParamTypeDefaultDescription
valuenumberValue to round
tickSizenumbercontext.tickSizeTick increment

math.roundToTickUp

math.roundToTickUp(value, tickSize?) -> number

Round up to the nearest tick increment.

ParamTypeDefaultDescription
valuenumberValue to round
tickSizenumbercontext.tickSizeTick increment

math.roundToTickDown

math.roundToTickDown(value, tickSize?) -> number

Round down to the nearest tick increment.

ParamTypeDefaultDescription
valuenumberValue to round
tickSizenumbercontext.tickSizeTick increment

math.roundToStep

math.roundToStep(value, stepSize?) -> number

Round to the nearest step increment. Uses context.stepSize when stepSize is omitted.

ParamTypeDefaultDescription
valuenumberValue to round
stepSizenumbercontext.stepSizeStep increment

math.roundToStepUp

math.roundToStepUp(value, stepSize?) -> number

Round up to the nearest step increment.

ParamTypeDefaultDescription
valuenumberValue to round
stepSizenumbercontext.stepSizeStep increment

math.roundToStepDown

math.roundToStepDown(value, stepSize?) -> number

Round down to the nearest step increment.

ParamTypeDefaultDescription
valuenumberValue to round
stepSizenumbercontext.stepSizeStep increment

Aggregation

math.sum

math.sum(source, length) -> number

Rolling 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) -> number

Arithmetic mean. Accepts variadic numbers or a single array.

math.avg(10, 20, 30)  // 20

math.min

math.min(...values) -> number
math.min(array) -> number

Minimum value. Accepts variadic numbers or a single array.

math.max

math.max(...values) -> number
math.max(array) -> number

Maximum value. Accepts variadic numbers or a single array.

math.clamp

math.clamp(value, min, max) -> number

Constrains a value to a range.

ParamTypeDescription
valuenumberValue to clamp
minnumberLower bound
maxnumberUpper bound

Basic Operations

math.abs

math.abs(value) -> number

Absolute value.

math.floor

math.floor(value) -> number

Round down to nearest integer.

math.ceil

math.ceil(value) -> number

Round up to nearest integer.

math.trunc

math.trunc(value) -> number

Remove fractional part (truncate toward zero).

math.pow

math.pow(base, exponent) -> number

Raise base to exponent power.

math.sqrt

math.sqrt(value) -> number

Square root.

math.exp

math.exp(value) -> number

Euler's number raised to value power (e^x).

math.sign

math.sign(value) -> number

Returns -1, 0, or 1 indicating the sign of the value.

Logarithms

math.log10

math.log10(value) -> number

Base-10 logarithm.

math.log2

math.log2(value) -> number

Base-2 logarithm.

math.logN

math.logN(value, base) -> number

Logarithm with arbitrary base.

ParamTypeDescription
valuenumberInput value
basenumberLog base

Validity Checks

math.isFinite

math.isFinite(value) -> boolean

Returns true if value is a finite number (not NaN, not Infinity).

Utilities

math.safeDiv

math.safeDiv(numerator, denominator, fallback?) -> number

Safe division — returns fallback when denominator is zero or NaN.

ParamTypeDefaultDescription
numeratornumberNumerator
denominatornumberDenominator
fallbacknumberNaNValue 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) -> number

Returns a random number. Behavior depends on arguments:

CallReturns
math.random()[0, 1)
math.random(max)[0, max)
math.random(min, max)[min, max)

On this page