Functions

Math Functions

The math namespace, covering abs, rounding, powers, logs, trig, and hyperbolics for numeric work in kScript.

The math namespace gives you JavaScript-style numeric helpers: absolute value, rounding, powers and roots, logarithms, and the full trig and hyperbolic families. Call them on plain numbers or on any series value, and they evaluate per bar.

Every method lives under the math. prefix. There is no global abs() or round(); it is always math.abs(...), math.round(...). Use them to normalize a signal, scale a value into a range, or build a custom indicator from primitives.

//@version=2
define(title="Distance from average", position="offchart", axis=true)

timeseries trade = ohlcv(symbol=currentSymbol, exchange=currentExchange)
var spread = trade.close - sma(source=trade.close, period=20)

// How far is price from its average, regardless of direction?
plotLine(value=math.abs(spread), colors=["#2563eb"], width=2, label=["Distance"], desc=["Absolute distance from the 20-bar SMA"])

What's available

GroupMethods
Basicabs, sign
Roundinground, floor, ceil, trunc
Powers & rootspow, sqrt, cbrt, hypot
Exp & logexp, expm1, log, log1p, log2, log10
Comparisonmax, min
Trig (radians)sin, cos, tan, asin, acos, atan, atan2
Hyperbolicsinh, cosh, tanh, asinh, acosh, atanh

Constants are available too: math.PI, math.E, math.SQRT2, math.LN2, math.LN10, math.LOG2E, math.LOG10E, math.SQRT1_2.

Each behaves like its JavaScript counterpart. math.max and math.min take two arguments and return the larger or smaller. math.round rounds to the nearest integer. Trig functions work in radians, so divide an index or angle accordingly (math.sin(barIndex / 10)).

Every method in one script

This indicator runs the full namespace at once. It builds a few helper series first (a centered series of price minus its average, a bounded value safe for asin/acos, and a positive series safe for log and sqrt), then plots each math.* result.

math_namespace.ks
//@version=2
define(title="Math Namespace", position="offchart", axis=true)

timeseries trade = ohlcv(symbol=currentSymbol, exchange=currentExchange)
timeseries closeSeries = trade.close

var centered = closeSeries - sma(source=closeSeries, period=5)
var bounded = math.sin(barIndex / 10)
var positive = math.abs(centered) + 1
var small = centered / 10

plotLine(value=math.abs(centered), colors=["#2563eb"], label=["abs"], desc=["math.abs result"])
plotLine(value=math.round(centered), colors=["#dc2626"], label=["round"], desc=["math.round result"])
plotLine(value=math.max(trade.high, trade.close), colors=["#16a34a"], label=["max"], desc=["math.max result"])
plotLine(value=math.min(trade.low, trade.close), colors=["#ea580c"], label=["min"], desc=["math.min result"])
plotLine(value=math.pow(small, 2), colors=["#7c3aed"], label=["pow"], desc=["math.pow result"])
plotLine(value=math.sqrt(positive), colors=["#0891b2"], label=["sqrt"], desc=["math.sqrt result"])
plotLine(value=math.log(positive), colors=["#4b5563"], label=["log"], desc=["math.log result"])
plotLine(value=math.floor(centered), colors=["#be123c"], label=["floor"], desc=["math.floor result"])
plotLine(value=math.ceil(centered), colors=["#0f766e"], label=["ceil"], desc=["math.ceil result"])
plotLine(value=math.sign(centered), colors=["#9333ea"], label=["sign"], desc=["math.sign result"])
plotLine(value=math.sin(barIndex / 8), colors=["#1d4ed8"], label=["sin"], desc=["math.sin result"])
plotLine(value=math.cos(barIndex / 8), colors=["#b45309"], label=["cos"], desc=["math.cos result"])
plotLine(value=math.tan(barIndex / 40), colors=["#15803d"], label=["tan"], desc=["math.tan result"])
plotLine(value=math.asin(bounded), colors=["#6d28d9"], label=["asin"], desc=["math.asin result"])
plotLine(value=math.acos(bounded), colors=["#0e7490"], label=["acos"], desc=["math.acos result"])
plotLine(value=math.atan(centered), colors=["#991b1b"], label=["atan"], desc=["math.atan result"])
plotLine(value=math.atan2(centered, positive), colors=["#374151"], label=["atan2"], desc=["math.atan2 result"])
plotLine(value=math.sinh(small), colors=["#3b82f6"], label=["sinh"], desc=["math.sinh result"])
plotLine(value=math.cosh(small), colors=["#ef4444"], label=["cosh"], desc=["math.cosh result"])
plotLine(value=math.tanh(small), colors=["#22c55e"], label=["tanh"], desc=["math.tanh result"])
plotLine(value=math.asinh(small), colors=["#f97316"], label=["asinh"], desc=["math.asinh result"])
plotLine(value=math.acosh(positive), colors=["#8b5cf6"], label=["acosh"], desc=["math.acosh result"])
plotLine(value=math.atanh(bounded / 2), colors=["#06b6d4"], label=["atanh"], desc=["math.atanh result"])
plotLine(value=math.exp(small), colors=["#64748b"], label=["exp"], desc=["math.exp result"])
plotLine(value=math.expm1(small), colors=["#e11d48"], label=["expm1"], desc=["math.expm1 result"])
plotLine(value=math.log1p(math.abs(small)), colors=["#10b981"], label=["log1p"], desc=["math.log1p result"])
plotLine(value=math.log2(positive), colors=["#6366f1"], label=["log2"], desc=["math.log2 result"])
plotLine(value=math.log10(positive), colors=["#14b8a6"], label=["log10"], desc=["math.log10 result"])
plotLine(value=math.cbrt(centered), colors=["#f59e0b"], label=["cbrt"], desc=["math.cbrt result"])
plotLine(value=math.hypot(centered, small), colors=["#84cc16"], label=["hypot"], desc=["math.hypot result"])
plotLine(value=math.trunc(centered), colors=["#a855f7"], label=["trunc"], desc=["math.trunc result"])

Domain notes

A few methods are only defined for part of the number line, exactly as in standard math:

  • math.sqrt and math.log expect non-negative input. Guard with math.abs(x) + 1 or a max floor if your series can go negative or hit zero.
  • math.asin and math.acos only accept values in [-1, 1]. Feed them something already bounded (the example uses math.sin(...)).
  • math.acosh expects input >= 1.

Out-of-domain input produces na rather than throwing, so a plot may simply show gaps where the input left the valid range.

Calling a method that does not exist is a hard error: math.unknownFn(...) fails at runtime with Unknown math method: unknownFn. If you hit that, check the spelling against the table above.