Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.darvas.app/llms.txt

Use this file to discover all available pages before exploring further.

Overview

The math namespace mirrors the JavaScript Math object with Pine Script-compatible names. All functions are available globally as math.*.

Arithmetic

FunctionReturnsExample
math.abs(x)numbermath.abs(-5)5
math.sqrt(x)numbermath.sqrt(9)3
math.pow(x, y)numbermath.pow(2, 10)1024
math.log(x)numberNatural log
math.log10(x)numberBase-10 log
onBar(() => {
  const logReturn = math.log(ctx.close() / ctx.close(1));
  plot("Log Return", logReturn);
});

Min / max

FunctionReturnsExample
math.min(...values)numbermath.min(3, 1, 2)1
math.max(...values)numbermath.max(3, 1, 2)3
onBar(() => {
  // Clamp RSI between 10 and 90
  const rsi = ta.rsi(ctx.close, 14);
  const clamped = math.max(10, math.min(90, nz(rsi, 50)));
  plot("Clamped RSI", clamped);
});

Rounding

FunctionReturnsNotes
math.round(x, decimals?)numberRounds to decimals places (default 0)
math.floor(x)numberRound down to nearest integer
math.ceil(x)numberRound up to nearest integer
const price = ctx.close();
const rounded = math.round(price, 2);  // e.g. 42567.123 -> 42567.12
const floored = math.floor(price);     // e.g. 42567.123 -> 42567

Trigonometric

FunctionNotes
math.sin(x)Input in radians
math.cos(x)Input in radians
math.tan(x)Input in radians
// Sine-wave overlay (purely decorative example)
const PI = 3.14159265358979;
onBar(() => {
  const t = ctx.i() / 20;
  const wave = math.sin(2 * PI * t) * 100;
  plot("Sine wave", wave);
});

Constants

The math namespace does not expose math.PI directly - use the literal 3.14159265358979 or derive it from math.asin(1) * 2 if needed.

Random

FunctionReturns
math.random()number in [0, 1) - different each evaluation
math.random() is available but its output changes on every indicator re-evaluation. Avoid using it for anything that should produce stable chart output.

str.*

String utilities namespace.

NaN handling

math.* functions propagate NaN - guard inputs with nz().