Skip to main content
Mathematical functions for calculations, statistics, and data transformation in your indicators.

Arithmetic Functions

abs()

Returns the absolute value of a number.
var result = abs(-10);  // Returns 10
var result = abs(10);   // Returns 10

round()

Rounds a number to the nearest integer or specified decimal places.
var result = round(3.7);      // Returns 4
var result = round(3.14159, 2);  // Returns 3.14

floor()

Rounds down to the nearest integer.
var result = floor(3.9);  // Returns 3
var result = floor(-3.1); // Returns -4

ceil()

Rounds up to the nearest integer.
var result = ceil(3.1);   // Returns 4
var result = ceil(-3.9);  // Returns -3

Comparison Functions

max()

Returns the maximum of two or more values.
var result = max(10, 20);       // Returns 20
var result = max(5, 10, 15);    // Returns 15

min()

Returns the minimum of two or more values.
var result = min(10, 20);       // Returns 10
var result = min(5, 10, 15);    // Returns 5

Mathematical Operations

sqrt()

Returns the square root of a number.
var result = sqrt(16);  // Returns 4
var result = sqrt(2);   // Returns 1.414...

pow()

Raises a number to a power.
var result = pow(2, 3);   // Returns 8 (2^3)
var result = pow(10, 2);  // Returns 100

log()

Returns the natural logarithm (base e).
var result = log(Math.E);  // Returns 1
var result = log(10);      // Returns 2.302...

log10()

Returns the base-10 logarithm.
var result = log10(100);  // Returns 2
var result = log10(1000); // Returns 3

exp()

Returns e raised to a power.
var result = exp(1);  // Returns 2.718... (e^1)
var result = exp(0);  // Returns 1

Statistical Functions

sum()

Calculates the sum of values over a period.
timeseries ohlcvData = ohlcv(symbol=currentSymbol, exchange=currentExchange);
var totalVolume = sum(source=ohlcvData.volume, period=20);

avg()

Calculates the average of values.
var average = avg(10, 20, 30);  // Returns 20

stdev()

Calculates standard deviation over a period.
timeseries ohlcvData = ohlcv(symbol=currentSymbol, exchange=currentExchange);
var volatility = stdev(source=ohlcvData.close, period=20);

variance()

Calculates variance over a period.
timeseries ohlcvData = ohlcv(symbol=currentSymbol, exchange=currentExchange);
var priceVariance = variance(source=ohlcvData.close, period=20);

Practical Examples

Volatility Calculation

//@version=2

define(title="Volatility Bands", position="onchart", axis=false);

timeseries ohlcvData = ohlcv(symbol=currentSymbol, exchange=currentExchange);

var smaValue = sma(source=ohlcvData.close, period=20);
var stdDev = stdev(source=ohlcvData.close, period=20);

var upperBand = smaValue + (2 * stdDev);
var lowerBand = smaValue - (2 * stdDev);

plotLine(value=upperBand, width=1, colors=["red"], label=["Upper"], desc=["Upper Band"]);
plotLine(value=smaValue, width=2, colors=["blue"], label=["SMA"], desc=["Middle Band"]);
plotLine(value=lowerBand, width=1, colors=["green"], label=["Lower"], desc=["Lower Band"]);

Percentage Change

//@version=2

define(title="Percent Change", position="offchart", axis=true);

timeseries ohlcvData = ohlcv(symbol=currentSymbol, exchange=currentExchange);

var currentClose = ohlcvData.close[0];
var previousClose = ohlcvData.close[1];
var percentChange = ((currentClose - previousClose) / previousClose) * 100;

var colorIndex = percentChange > 0 ? 0 : 1;

plotBar(value=percentChange, width=1, colors=["green", "red"], colorIndex=colorIndex, label=["% Change"], desc=["Percentage Change"]);

ATR Calculation

//@version=2

define(title="ATR Indicator", position="offchart", axis=true);

timeseries ohlcvData = ohlcv(symbol=currentSymbol, exchange=currentExchange);

// True Range calculation
var highLow = ohlcvData.high[0] - ohlcvData.low[0];
var highClose = abs(ohlcvData.high[0] - ohlcvData.close[1]);
var lowClose = abs(ohlcvData.low[0] - ohlcvData.close[1]);
var trueRange = max(highLow, max(highClose, lowClose));

// ATR as RMA of True Range
var atrValue = rma(source=trueRange, period=14);

plotLine(value=atrValue, width=2, colors=["orange"], label=["ATR"], desc=["Average True Range"]);

Tips

Precision

Be aware of floating-point precision issues. Use round() when displaying values to users.

Division by Zero

Always check for division by zero:
var result = denominator != 0 ? numerator / denominator : 0;

Performance

Math functions are optimized for performance. However, avoid excessive calculations in loops.