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:
math.max(value1, value2, ...)Examples:
// Find maximum of two prices
math.max($BTC.open, $BTC.close)
// Maximum of multiple indicators
math.max(ta.rsi($BTC, 14), ta.rsi($ETH, 14), ta.rsi($SOL, 14))
// Dynamic stop loss
math.max($BTC.low, ta.sma($BTC, 20) * 0.95)
// Risk management
math.max($BTC.close * 0.02, 100) // Minimum position sizeReturns: Largest numerical value from the arguments
math.min
Returns the smallest of the given numbers.
Syntax:
math.min(value1, value2, ...)Examples:
// Find minimum of two prices
math.min($BTC.open, $BTC.close)
// Minimum RSI across assets
math.min(ta.rsi($BTC, 14), ta.rsi($ETH, 14))
// Position sizing with cap
math.min($BTC.close * 0.1, 5000) // Maximum position size
// Dynamic support level
math.min($BTC.low, ta.bb($BTC, 20, 2).lower)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:
math.round(value)
math.round(value, precision)Examples:
// Round price to nearest dollar
math.round($BTC.close)
// Round to 2 decimal places
math.round($BTC.close, 2) // e.g., 45123.456 -> 45123.46
// Round indicator values
math.round(ta.rsi($BTC, 14))
// Position sizing with precision
math.round($BTC.close * 0.01, 4) // Round to 4 decimal places
// Volume normalization
math.round($BTC.volume / 1000000, 1) // Round to 1 decimal placeReturns: Rounded value (integer if no precision, float if precision specified)
math.floor
Rounds a number down to the nearest integer.
Syntax:
math.floor(value)Examples:
// Floor price value
math.floor($BTC.close)
// Conservative position sizing
math.floor($BTC.close * 0.01)
// RSI level grouping
math.floor(ta.rsi($BTC, 14) / 10) * 10 // Round to nearest 10
// Time-based calculations
math.floor($BTC.volume / 1000) * 1000 // Floor to thousandsReturns: Rounded down integer value
math.ceil
Rounds a number up to the nearest integer.
Syntax:
math.ceil(value)Examples:
// Ceiling price value
math.ceil($BTC.close)
// Conservative stop loss
math.ceil($BTC.close * 1.05)
// Minimum lot sizing
math.ceil($BTC.close * 0.001)
// Volume ceiling
math.ceil($BTC.volume / 1000000) // Round up to millionsReturns: Rounded up integer value
math.sqrt
Returns the square root of a number.
Syntax:
math.sqrt(value)Examples:
// Volatility calculations
math.sqrt(ta.atr($BTC, 14))
// Standard deviation approximation
math.sqrt(math.abs($BTC.close - ta.sma($BTC, 20)))
// Risk metrics
math.sqrt($BTC.volume / ta.sma($BTC.volume, 20))
// Price normalization
math.sqrt($BTC.close / 1000)Returns: Square root of the input value
math.pow
Returns the value of a number raised to a power.
Syntax:
math.pow(base, exponent)Examples:
// Exponential calculations
math.pow($BTC.close / $BTC.close[1], 2) // Squared returns
// Compound growth
math.pow(1.01, 30) // 1% growth over 30 periods
// Volatility weighting
math.pow(ta.atr($BTC, 14) / $BTC.close, 2)
// Risk scaling
math.pow(ta.rsi($BTC, 14) / 50, 3) // Cubic scalingReturns: Base raised to the power of exponent
math.sin
Returns the trigonometric sine of an angle (in radians).
Syntax:
math.sin(angle)Examples:
// Sine wave calculations
math.sin(math.toradians(45)) // Sine of 45 degrees
// Cyclical analysis
math.sin($BTC.close / 10000)
// Wave pattern detection
math.sin(ta.rsi($BTC, 14) * math.toradians(3.6)) // Map RSI to sine wave
// Market cycle modeling
math.sin(2 * Math.PI * $BTC.close / ta.sma($BTC, 50))Returns: Sine of the angle in radians
math.cos
Returns the trigonometric cosine of an angle (in radians).
Syntax:
math.cos(angle)Examples:
// Cosine wave calculations
math.cos(math.toradians(45)) // Cosine of 45 degrees
// Phase-shifted analysis
math.cos($BTC.close / 10000)
// Complementary wave patterns
math.cos(ta.rsi($BTC, 14) * math.toradians(3.6))
// Market momentum cycles
math.cos(2 * Math.PI * $BTC.close / ta.sma($BTC, 20))Returns: Cosine of the angle in radians
math.tan
Returns the trigonometric tangent of an angle (in radians).
Syntax:
math.tan(angle)Examples:
// Tangent calculations
math.tan(math.toradians(45)) // Returns 1.0
// Slope calculations
math.tan(math.atan(($BTC.close - $BTC.close[10]) / 10))
// Trend angle analysis
math.tan(math.atan((ta.sma($BTC, 20) - ta.sma($BTC, 20)[5]) / 5))Returns: Tangent of the angle in radians
math.asin
Returns the arcsine (inverse sine) of a number, result in radians.
Syntax:
math.asin(value)Examples:
// Inverse sine calculations (value must be between -1 and 1)
math.asin(0.5) // Returns π/6 radians (30 degrees)
// Normalized RSI to angle
math.asin((ta.rsi($BTC, 14) - 50) / 50) // Map RSI to -π/2 to π/2
// Price oscillation analysis
math.asin(($BTC.close - ta.sma($BTC, 20)) / ta.atr($BTC, 14))Returns: Arcsine in radians (range: -π/2 to π/2)
math.acos
Returns the arccosine (inverse cosine) of a number, result in radians.
Syntax:
math.acos(value)Examples:
// Inverse cosine calculations (value must be between -1 and 1)
math.acos(0.5) // Returns π/3 radians (60 degrees)
// Phase analysis
math.acos(($BTC.close - $BTC.low) / ($BTC.high - $BTC.low))
// Correlation angle
math.acos(ta.rsi($BTC, 14) / 100) // Map RSI to angleReturns: Arccosine in radians (range: 0 to π)
math.atan
Returns the arctangent (inverse tangent) of a number, result in radians.
Syntax:
math.atan(value)Examples:
// Inverse tangent calculations
math.atan(1) // Returns π/4 radians (45 degrees)
// Trend angle calculation
math.atan(($BTC.close - $BTC.close[10]) / 10)
// Price slope analysis
math.atan((ta.ema($BTC, 20) - ta.ema($BTC, 20)[5]) / 5)
// Momentum angle
math.atan(ta.rsi($BTC, 14) - 50)Returns: Arctangent in radians (range: -π/2 to π/2)
math.log
Returns the natural logarithm (base e) of a number.
Syntax:
math.log(value)Examples:
// Natural logarithm
math.log($BTC.close) // Log of current price
// Price ratio analysis
math.log($BTC.close / $BTC.close[30]) // 30-period log return
// Volatility calculations
math.log(ta.atr($BTC, 14) / ta.atr($BTC, 50))
// Growth rate analysis
math.log($BTC.volume / ta.sma($BTC.volume, 20))Returns: Natural logarithm of the input value
math.log10
Returns the base-10 logarithm of a number.
Syntax:
math.log10(value)Examples:
// Base-10 logarithm
math.log10($BTC.close) // Log base 10 of price
// Order of magnitude analysis
math.log10($BTC.volume)
// Scale normalization
math.log10($BTC.close / 1000)
// Exponential scaling
math.log10(ta.rsi($BTC, 14))Returns: Base-10 logarithm of the input value
math.exp
Returns e raised to the power of the given number (e^x).
Syntax:
math.exp(value)Examples:
// Exponential function
math.exp(1) // Returns e (≈2.718)
// Exponential growth modeling
math.exp(ta.rsi($BTC, 14) / 100)
// Volatility expansion
math.exp(ta.atr($BTC, 14) / $BTC.close)
// Compound growth calculation
math.exp(math.log($BTC.close / $BTC.close[30]) * 12) // AnnualizedReturns: 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:
math.sign(value)Examples:
// Sign of price change
math.sign($BTC.close - $BTC.close[1])
// Trend direction
math.sign(ta.sma($BTC, 20) - ta.sma($BTC, 50))
// Momentum direction
math.sign(ta.rsi($BTC, 14) - 50)
// Volume flow direction
math.sign($BTC.volume - ta.sma($BTC.volume, 20))Returns: -1, 0, or 1 depending on the sign of the input
math.avg
Calculates the average of multiple values.
Syntax:
math.avg(value1, value2, ...)Examples:
// Average of multiple prices
math.avg($BTC.open, $BTC.close, $BTC.high, $BTC.low)
// Average RSI across timeframes
math.avg(ta.rsi($BTC, 14), ta.rsi($BTC, 21), ta.rsi($BTC, 28))
// Portfolio average
math.avg($BTC.close, $ETH.close, $SOL.close)
// Multi-indicator average
math.avg(ta.sma($BTC, 20), ta.ema($BTC, 20), ta.wma($BTC, 20))Returns: Average of all provided values
math.sum
Calculates the sum of values, either from an array with optional lookback or multiple arguments.
Syntax:
math.sum(array, length)
math.sum(value1, value2, ...)Examples:
// Sum of recent volumes (if you had volume array)
// math.sum(volumeArray, 10) // Sum of last 10 values
// Sum of multiple values
math.sum($BTC.open, $BTC.close, $BTC.high, $BTC.low)
// Sum of indicators
math.sum(ta.rsi($BTC, 14), ta.rsi($ETH, 14), ta.rsi($SOL, 14))
// Portfolio value sum
math.sum($BTC.close * 0.5, $ETH.close * 0.3, $SOL.close * 0.2)Returns: Sum of all values
math.random
Returns a pseudo-random number between min and max.
Syntax:
math.random(min, max, seed)Examples:
// Random number between 0 and 1
math.random()
// Random price adjustment
$BTC.close + math.random(-100, 100)
// Random position sizing
math.random(0.01, 0.05) * $BTC.close
// Random threshold for dynamic strategies
ta.rsi($BTC, 14) > math.random(30, 70)Returns: Random number between min and max (exclusive)
math.todegrees
Converts an angle from radians to degrees.
Syntax:
math.todegrees(radians)Examples:
// Convert radians to degrees
math.todegrees(Math.PI) // Returns 180
// Convert trend angle to degrees
math.todegrees(math.atan(($BTC.close - $BTC.close[10]) / 10))
// Phase angle in degrees
math.todegrees(math.asin((ta.rsi($BTC, 14) - 50) / 50))Returns: Angle converted to degrees
math.toradians
Converts an angle from degrees to radians.
Syntax:
math.toradians(degrees)Examples:
// Convert degrees to radians
math.toradians(180) // Returns π
// 45-degree angle in radians
math.toradians(45)
// Use with trigonometric functions
math.sin(math.toradians(30)) // Sine of 30 degreesReturns: Angle converted to radians
Last updated
Was this helpful?