CODESCRIPT

Math

33 entries. What each one takes, what it returns, and a working example.

abs(x)

Absolute value.

x
number
A number or series.
NOTE

Drops the sign and keeps the magnitude; works element-wise on both scalars and series.

CODESCRIPT
plot(abs(change(close, 1)))

The absolute magnitude of the per-bar price change (direction-less).

acos(x)

The arc-cosine of x (in radians, 0..π).

x
number
A number or series; the input is clamped to the [-1, 1] range.
NOTE

To avoid na from out-of-range input, the value is first clamped to [-1, 1]. The result is in radians; use todegrees to convert.

CODESCRIPT
plot(todegrees(acos(1)))

acos(1) = 0 radians = 0 degrees.

asin(x)

The arc-sine of x (in radians, -π/2..π/2).

x
number
A number or series; the input is clamped to the [-1, 1] range.
NOTE

To avoid na from out-of-range input, the value is first clamped to [-1, 1]. The result is in radians (todegrees for degrees).

CODESCRIPT
plot(todegrees(asin(1)))

asin(1) = π/2 radians = 90 degrees.

atan(x)

The arc-tangent of x (in radians, -π/2..π/2).

x
number
A number or series (a slope/ratio).
NOTE

Single-argument; gives the angle of a slope (Δy/Δx). For a sign/quadrant-aware angle use atan2.

CODESCRIPT
plot(todegrees(atan(change(close, 1))))

The slope angle (degrees) of the per-bar price change.

atan2(y, x)

The angle of the point (x, y) (in radians, -π..π; quadrant-aware).

y
number
The vertical component (numerator).
x
number
The horizontal component (denominator).
NOTE

The two-argument arc-tangent: unlike atan it uses the signs to give the correct quadrant (-π..π). The result is in radians.

CODESCRIPT
plot(todegrees(atan2(1, 1)))

atan2(1, 1) = π/4 radians = 45 degrees.

avg(a, b, ...)

The element-wise average.

a
series
First value/series.
b
series
Second value/series (and beyond).
NOTE

Can take more than two arguments.

CODESCRIPT
plot(avg(high, low, close))

The per-bar average of high, low and close (same as hlc3).

bool(x)

Zero/na → false, non-zero → true.

x
number
A number, series or boolean.
NOTE

In this language bool is NEVER na: bool(na) = false. A scalar input returns a real boolean, a series returns 1/0 (na→0). No collapse to the last value.

CODESCRIPT
plot(bool(volume) ? 1 : 0)

0 when volume is zero/na, otherwise 1.

ceil(x)

The value rounded up to an integer.

x
series
Value/series.
NOTE

Rounds up (toward plus infinity): ceil(2.1) = 3. Operates element-wise.

CODESCRIPT
plot(ceil(close / 10) * 10)

Snaps the close up to the nearest higher multiple of 10.

clamp(x, lo, hi)

The value constrained to the [lo, hi] range.

x
series
Value/series.
lo
number
Lower bound.
hi
number
Upper bound.
NOTE

Constrains x to the [lo, hi] range, pulling anything outside back to the bound.

CODESCRIPT
plot(clamp(rsi(close, 14), 30, 70))

Constrains RSI to the 30-70 band, trimming the overshoots.

cos(x)

Cosine.

x
series
Radians.
NOTE

The input is in radians. The output is between -1 and 1. Operates element-wise.

CODESCRIPT
plot(cos(0))

cos(0) = 1 (constant).

exp(x)

e^x.

x
series
Exponent.
NOTE

e to the power of x; the inverse of the logarithm. Operates element-wise.

CODESCRIPT
plot(exp(log(close)))

exp undoes log and returns close (identity check).

fixnan(source)

A series of the same length: each na is filled with the last non-na value up to that point (na until the first valid value).

source
series
The series to forward-fill (may contain na).
NOTE

Do not confuse with nz: nz(x)/nz(x,y) puts a CONSTANT (0 or y) in place of na. fixnan(x) puts the LAST KNOWN value from the series (forward-fill, ffill) — not constant, varies with the data.

float(x)

Converts the value to a floating-point number (na is preserved).

x
number
A number or series.
NOTE

For type clarity; explicitly marks a numeric value as floating-point. An na input stays na. It is a no-op on already-float values.

CODESCRIPT
plot(float(3))

Casts the integer 3 to 3.0.

floor(x)

The value rounded down to an integer.

x
series
Value/series.
NOTE

Rounds down (toward minus infinity): floor(-2.1) = -3. Differs from int, which truncates toward zero.

CODESCRIPT
plot(floor(close / 10) * 10)

Snaps the close down to the nearest lower multiple of 10 (price step).

int(x)

Truncates the fraction toward zero to an integer (na is preserved).

x
number
A number or series.
NOTE

Truncates toward zero: int(10.9)=10, int(-10.9)=-10. floor differs (rounds down, differs for negatives). An na input stays na.

CODESCRIPT
plot(int(close))

The integer part of the close (truncated toward zero).

log(x)

Natural logarithm.

x
number
A number or series.
NOTE

Natural logarithm (base e). Undefined for input ≤0. Operates element-wise.

CODESCRIPT
plot(log(close / prev(close, 1)))

The per-bar log return.

log10(x)

Base-10 logarithm; na if x<=0.

x
series
Positive value/series.
NOTE

Base-10 logarithm. Undefined for input ≤0. Operates element-wise.

CODESCRIPT
plot(log10(volume))

The base-10 logarithm of volume (its order of magnitude).

math.pi / e / phi / rphi / sqrt2 / sqrt3

Math constants (dot access): pi=3.14159..., e=2.71828..., phi=golden ratio 1.61803..., rphi=inverse golden ratio 0.61803..., sqrt2=1.41421..., sqrt3=1.73205...

max(a, b, ...)

The larger of the two (element-wise).

a
number
First value.
b
number
Second value.
NOTE

Picks the larger of two values element-wise; a series and a constant can be mixed.

CODESCRIPT
plot(max(close - open, 0))

The size of up-bodies only; 0 on down bars.

min(a, b, ...)

The smaller of the two (element-wise).

a
number
First value.
b
number
Second value.
NOTE

Picks the smaller of two values element-wise; a series and a constant can be mixed.

CODESCRIPT
plot(min(high - close, close - low))

The shorter of the upper and lower wick.

mod(a, b)

a mod b; na if b=0.

a
series
Dividend.
b
series
Divisor.
NOTE

The remainder of a divided by b. Undefined when the divisor is 0. Operates element-wise.

CODESCRIPT
plotshape(mod(barIndex, 10) == 0, style="cross", location="bottom")

A mark in the bottom band once every 10 bars.

nz(source, replacement=0)

The series without na.

source
series
Source series.
replacement
number
Value to substitute for na (default 0).
NOTE

Replaces na values with replacement (default 0), leaving the rest untouched.

CODESCRIPT
plot(nz(change(close, 1), 0))

0 instead of na on the first bar; the normal change afterwards.

pow(x, y)

x to the power of y.

x
number
Base.
y
number
Exponent.
NOTE

x to the power of y. Operates element-wise; base and exponent may be series.

CODESCRIPT
plot(pow(2, 10))

2 to the power of 10 = 1024 (constant).

random(min, max, seed?)

A series holding a deterministic random value in [min, max) for each bar.

min
number
Lower bound (inclusive).
max
number
Upper bound.
seed
number
Optional. Seed (default 0); the same seed reproduces the same sequence.
NOTE

It is deterministic: with the same seed the backtest and live results match exactly (seed → reproducibility). It is not a truly unpredictable number.

CODESCRIPT
plot(random(0, 1, 42))

A reproducible random series in 0..1 per bar, seeded with 42.

round(x, decimals=0)

The rounded value.

x
series
Value/series to round.
decimals
number
Number of decimal places (default 0).
NOTE

Operates element-wise.

CODESCRIPT
plot(round(close, 2))

The close rounded to two decimals.

round_to_mintick(deger, tick?)

The value rounded to the nearest multiple of tick.

deger
number
The value or series to round.
tick
number
Optional. The price step (default 0.01; falls back to 0.01 if ≤0).
NOTE

To snap a price to the symbol's trading step. If you want a fixed number of decimals, round is more appropriate.

CODESCRIPT
plot(round_to_mintick(12.347, 0.05))

12.347 to the nearest 0.05 multiple → 12.35.

sign(x)

Sign: +1 when positive, 0 at zero, −1 when negative.

x
number
A number or series.
NOTE

Returns only -1, 0 or +1 (positive→1, negative→-1, zero→0). Operates element-wise.

CODESCRIPT
plot(sign(change(close, 1)))

+1 if price rose, -1 if it fell, 0 if unchanged.

sin(x)

Sine.

x
series
Radians.
NOTE

The input is in radians. The output is between -1 and 1. Operates element-wise.

CODESCRIPT
plot(sin(toradians(barIndex * 10)))

A smooth oscillating (-1..1) wave driven by the bar index.

sqrt(x)

Square root.

x
number
A number or series.
NOTE

A negative input returns na. Operates element-wise.

CODESCRIPT
plot(sqrt(pow(high - low, 2)))

The square root of the squared bar range = the range itself (identity check).

sum(source, length)

The sum of the last length bars.

source
series
Series to sum.
length
number
Window length.
NOTE

The first length-1 bars are None.

CODESCRIPT
plot(sum(volume, 5))

The total volume of the last 5 bars (rolling window).

tan(x)

Tangent.

x
series
Radians.
NOTE

The input is in radians. It diverges near odd multiples of 90°. Operates element-wise.

CODESCRIPT
plot(tan(0))

tan(0) = 0 (constant).

todegrees(rad)

The same angle in degrees (rad × 180 / π).

rad
number
An angle in radians (a number or series).
NOTE

Trigonometric/arc functions output radians; use this for readable degrees. The inverse is toradians.

CODESCRIPT
plot(todegrees(3.141592653589793))

π radians = 180 degrees.

toradians(derece)

The same angle in radians (degrees × π / 180).

derece
number
An angle in degrees (a number or series).
NOTE

sin/cos/tan expect radians; convert a degree value with this before passing it in. The inverse is todegrees.

CODESCRIPT
plot(sin(toradians(90)))

Converts 90 degrees to radians and takes its sine → 1.