Math

This section documents the mathematical (math.*) functions.


math.abs

Returns the absolute value of a number.

Syntax:

math.abs(value)

Examples:

// Basic absolute value
math.abs(-50)  // Returns 50

// Price difference absolute value
math.abs($BTC.close - $BTC.open)

// Indicator deviation
math.abs(ta.rsi($BTC, 14) - 50)

// Portfolio calculations
math.abs($BTC.close - ta.sma($BTC, 20))

Returns: Positive numerical value


math.max

Returns the largest of the given numbers.

Syntax:

Examples:

Returns: Largest numerical value from the arguments


math.min

Returns the smallest of the given numbers.

Syntax:

Examples:

Returns: Smallest numerical value from the arguments


math.round

Rounds a number to the nearest integer, or to a specified number of decimal places.

Syntax:

Examples:

Returns: Rounded value (integer if no precision, float if precision specified)


math.floor

Rounds a number down to the nearest integer.

Syntax:

Examples:

Returns: Rounded down integer value


math.ceil

Rounds a number up to the nearest integer.

Syntax:

Examples:

Returns: Rounded up integer value


math.sqrt

Returns the square root of a number.

Syntax:

Examples:

Returns: Square root of the input value


math.pow

Returns the value of a number raised to a power.

Syntax:

Examples:

Returns: Base raised to the power of exponent


math.sin

Returns the trigonometric sine of an angle (in radians).

Syntax:

Examples:

Returns: Sine of the angle in radians


math.cos

Returns the trigonometric cosine of an angle (in radians).

Syntax:

Examples:

Returns: Cosine of the angle in radians


math.tan

Returns the trigonometric tangent of an angle (in radians).

Syntax:

Examples:

Returns: Tangent of the angle in radians


math.asin

Returns the arcsine (inverse sine) of a number, result in radians.

Syntax:

Examples:

Returns: Arcsine in radians (range: -π/2 to π/2)


math.acos

Returns the arccosine (inverse cosine) of a number, result in radians.

Syntax:

Examples:

Returns: Arccosine in radians (range: 0 to π)


math.atan

Returns the arctangent (inverse tangent) of a number, result in radians.

Syntax:

Examples:

Returns: Arctangent in radians (range: -π/2 to π/2)


math.log

Returns the natural logarithm (base e) of a number.

Syntax:

Examples:

Returns: Natural logarithm of the input value


math.log10

Returns the base-10 logarithm of a number.

Syntax:

Examples:

Returns: Base-10 logarithm of the input value


math.exp

Returns e raised to the power of the given number (e^x).

Syntax:

Examples:

Returns: e raised to the power of the input value


math.sign

Returns the sign of a number: 1 for positive, -1 for negative, 0 for zero.

Syntax:

Examples:

Returns: -1, 0, or 1 depending on the sign of the input


math.avg

Calculates the average of multiple values.

Syntax:

Examples:

Returns: Average of all provided values


math.sum

Calculates the sum of values, either from an array with optional lookback or multiple arguments.

Syntax:

Examples:

Returns: Sum of all values


math.random

Returns a pseudo-random number between min and max.

Syntax:

Examples:

Returns: Random number between min and max (exclusive)


math.todegrees

Converts an angle from radians to degrees.

Syntax:

Examples:

Returns: Angle converted to degrees


math.toradians

Converts an angle from degrees to radians.

Syntax:

Examples:

Returns: Angle converted to radians

Last updated

Was this helpful?