Skip to content
Nexus Prop LLC

NexuScript 1.5

Math

Numbers. Every function returns missing when an input is missing, unless it says otherwise.

pi

valueNexuScript 1.0

The number pi, half a turn in radians (3.141592653589793).

Syntax

pi::Number

Missing values

Never missing.

Example

plot("Wave", sin(2 * pi * bar.index / 20))

See also

sincos

abs

functionNexuScript 1.0

The size of a number without its sign.

Syntax

abs(x::Number) -> Number

Parameters

NameTypeDefaultDescription
xNumberrequiredThe number to work on.

Returns

Number x when it is 0 or more, -x otherwise.

Missing values

A missing x gives missing.

Example

plot("Body size", abs(close - open))

See also

signmax

sign

functionNexuScript 1.0

Whether a number is below, at or above zero.

Syntax

sign(x::Number) -> Number

Parameters

NameTypeDefaultDescription
xNumberrequiredThe number to work on.

Returns

Number -1, 0 or 1.

Missing values

A missing x gives missing.

Example

plot("Direction", sign(close - open), style = "columns")

See also

abs

sqrt

functionNexuScript 1.0

The square root of a number.

Syntax

sqrt(x::Number) -> Number

Parameters

NameTypeDefaultDescription
xNumberrequiredThe number to work on.

Returns

Number The non-negative root; missing for a negative x.

Missing values

A missing or negative x gives missing.

Example

plot("Root of range", sqrt(high - low))

See also

^exp

floor

functionNexuScript 1.0

Rounds down to a whole number.

Syntax

floor(x::Number) -> Number

Parameters

NameTypeDefaultDescription
xNumberrequiredThe number to work on.

Returns

Number The largest whole number at or below x.

Missing values

A missing x gives missing.

Example

plot("Whole points", floor(close))

See also

ceiltruncround

ceil

functionNexuScript 1.0

Rounds up to a whole number.

Syntax

ceil(x::Number) -> Number

Parameters

NameTypeDefaultDescription
xNumberrequiredThe number to work on.

Returns

Number The smallest whole number at or above x.

Missing values

A missing x gives missing.

Example

plot("Next whole point", ceil(close))

See also

floortruncround

trunc

functionNexuScript 1.0

Drops the fraction of a number, toward zero.

Syntax

trunc(x::Number) -> Number

Parameters

NameTypeDefaultDescription
xNumberrequiredThe number to work on.

Returns

Number x without its fraction: trunc(-2.7) is -2.

Missing values

A missing x gives missing.

Example

plot("Whole part", trunc(close - open))

See also

floorceilround

exp

functionNexuScript 1.0

Raises e to a power.

Syntax

exp(x::Number) -> Number

Parameters

NameTypeDefaultDescription
xNumberrequiredThe number to work on.

Returns

Number e to the power x.

Details

Computed by NexuScript itself, so every computer gives the same last digit.

Missing values

A missing x gives missing.

Example

plot("Growth", exp(ta.change(log(close)) * 100))

See also

log^

log

functionNexuScript 1.0

The natural logarithm (base e).

Syntax

log(x::Number) -> Number

Parameters

NameTypeDefaultDescription
xNumberrequiredThe number to work on.

Returns

Number The natural log of x; missing for 0 or less.

Missing values

A missing, zero or negative x gives missing.

Example

plot("Log return", log(close / close[1]))

See also

log10log2exp

log10

functionNexuScript 1.0

The base-10 logarithm.

Syntax

log10(x::Number) -> Number

Parameters

NameTypeDefaultDescription
xNumberrequiredThe number to work on.

Returns

Number The base-10 log of x; missing for 0 or less.

Missing values

A missing, zero or negative x gives missing.

Example

plot("Digits", log10(close))

See also

loglog2

log2

functionNexuScript 1.0

The base-2 logarithm.

Syntax

log2(x::Number) -> Number

Parameters

NameTypeDefaultDescription
xNumberrequiredThe number to work on.

Returns

Number The base-2 log of x; missing for 0 or less.

Missing values

A missing, zero or negative x gives missing.

Example

plot("Doublings", log2(close))

See also

loglog10

sin

functionNexuScript 1.0

The sine of an angle.

Syntax

sin(x::Number) -> Number

Parameters

NameTypeDefaultDescription
xNumberrequiredThe number to work on.

Returns

Number A number from -1 to 1.

Units and time zones

The angle is in radians (pi is half a turn).

Missing values

A missing x gives missing.

Example

plot("Wave", sin(bar.index / 10))

See also

costanasin

cos

functionNexuScript 1.0

The cosine of an angle.

Syntax

cos(x::Number) -> Number

Parameters

NameTypeDefaultDescription
xNumberrequiredThe number to work on.

Returns

Number A number from -1 to 1.

Units and time zones

The angle is in radians.

Missing values

A missing x gives missing.

Example

plot("Wave", cos(bar.index / 10))

See also

sintanacos

tan

functionNexuScript 1.0

The tangent of an angle.

Syntax

tan(x::Number) -> Number

Parameters

NameTypeDefaultDescription
xNumberrequiredThe number to work on.

Returns

Number The tangent of x.

Units and time zones

The angle is in radians.

Missing values

A missing x gives missing.

Example

plot("Slope", tan(pi / 8))

See also

sincosatan

asin

functionNexuScript 1.0

The angle whose sine is a number.

Syntax

asin(x::Number) -> Number

Parameters

NameTypeDefaultDescription
xNumberrequiredThe number to work on.

Returns

Number An angle from -pi/2 to pi/2; missing outside -1 to 1.

Units and time zones

The result is in radians.

Missing values

A missing x, or one outside -1 to 1, gives missing.

Example

plot("Angle", asin(0.5))

See also

sinacos

acos

functionNexuScript 1.0

The angle whose cosine is a number.

Syntax

acos(x::Number) -> Number

Parameters

NameTypeDefaultDescription
xNumberrequiredThe number to work on.

Returns

Number An angle from 0 to pi; missing outside -1 to 1.

Units and time zones

The result is in radians.

Missing values

A missing x, or one outside -1 to 1, gives missing.

Example

plot("Angle", acos(0.5))

See also

cosasin

atan

functionNexuScript 1.0

The angle whose tangent is a number, or the angle of a point when given two numbers.

Syntax

atan(x::Number) -> Number

Parameters

NameTypeDefaultDescription
xNumberrequiredThe tangent. Give two numbers, atan(y, x), for the angle of the point (x, y).

Returns

Number An angle from -pi/2 to pi/2; with two numbers, from -pi to pi.

Details

atan(y, x) looks at the signs of both numbers, so it knows which quarter the point is in.

Units and time zones

The result is in radians.

Missing values

Any missing argument gives missing.

Examples

plot("Slope angle", atan(ta.change(close) / symbol.tick))
plot("Point angle", atan(close - open, 1))

See also

tanatan2

atan2

functionNexuScript 1.0

The angle of the point (x, y); write it as atan(y, x).

Syntax

atan2(y::Number, x::Number) -> Number

Parameters

NameTypeDefaultDescription
yNumberrequiredThe vertical part of the point.
xNumberrequiredThe horizontal part of the point.

Returns

Number An angle from -pi to pi.

Details

There is no atan2 name in a script: call atan with two numbers.

Units and time zones

The result is in radians.

Missing values

Any missing argument gives missing.

Example

plot("Point angle", atan(high - close, close - low))

See also

atan

round

functionNexuScript 1.0

Rounds a number to a whole number or to a number of decimals.

Syntax

round(x::Number, digits::Number = 0) -> Number

Parameters

NameTypeDefaultDescription
xNumberrequiredThe number to round.
digitsNumber0How many decimals to keep. 0 rounds to a whole number; a negative number rounds to tens, hundreds and so on.

Returns

Number x rounded; halves round away from zero, so round(2.5) is 3 and round(-2.5) is -3.

Missing values

A missing x gives missing.

Example

let mid = round(hl2, digits = 2)
plot("Mid", mid)

See also

round_to_tickfloorceil

round_to_tick

functionNexuScript 1.0

Rounds a price to the nearest price the symbol can trade at.

Syntax

round_to_tick(price::Number) -> Number

Parameters

NameTypeDefaultDescription
priceNumberrequiredThe price to round.

Returns

Number The nearest multiple of symbol.tick.

Units and time zones

Prices in the symbol's quote currency.

Missing values

A missing price gives missing. While the tick size is not known yet, the price comes back unchanged.

Example

plot("Mid on tick", round_to_tick(hl2))

See also

symbol.tickround

min

functionNexuScript 1.0

The smallest of two or more numbers.

Syntax

min(a::Number, b::Number, more::Number...) -> Number

Parameters

NameTypeDefaultDescription
aNumberrequiredThe first number.
bNumberrequiredThe second number.
moreNumberany numberrequiredAny further numbers to compare.

Returns

Number The smallest argument.

Missing values

If any argument is missing the result is missing. Use coalesce first to skip gaps.

Example

plot("Lower of open and close", min(open, close))

See also

maxminimumclamp

max

functionNexuScript 1.0

The largest of two or more numbers.

Syntax

max(a::Number, b::Number, more::Number...) -> Number

Parameters

NameTypeDefaultDescription
aNumberrequiredThe first number.
bNumberrequiredThe second number.
moreNumberany numberrequiredAny further numbers to compare.

Returns

Number The largest argument.

Missing values

If any argument is missing the result is missing.

Example

plot("Upper of open and close", max(open, close))

See also

minmaximumclamp

clamp

functionNexuScript 1.0

Keeps a number between a lower and an upper limit.

Syntax

clamp(x::Number, lower::Number, upper::Number) -> Number

Parameters

NameTypeDefaultDescription
xNumberrequiredThe number to keep inside the limits.
lowerNumberrequiredThe smallest value allowed.
upperNumberrequiredThe largest value allowed.

Returns

Number lower when x is below it, upper when x is above it, otherwise x.

Missing values

A missing argument gives missing.

Example

plot("RSI kept in 20 to 80", clamp(ta.rsi(close, 14), 20, 80))

See also

minmax

mod

functionNexuScript 1.0

The remainder after dividing, always with the sign of the divisor.

Syntax

mod(a::Number, b::Number) -> Number

Parameters

NameTypeDefaultDescription
aNumberrequiredThe number divided.
bNumberrequiredThe number to divide by.

Returns

Number a - b * floor(a / b): mod(-7, 3) is 2, where -7 % 3 is -1.

Missing values

A missing argument, or b of 0, gives missing.

Example

if mod(bar.index, 10) == 0
    marker("10")
end

See also

%floor

ismissing

functionNexuScript 1.0

Tests whether a number is missing.

Syntax

ismissing(x::Number) -> Bool

Parameters

NameTypeDefaultDescription
xNumberrequiredThe number to test.

Returns

Bool true when x is missing.

Details

This is the only way to test for a gap: x == missing is refused because any comparison with missing is false.

Missing values

This is the function that looks for it.

Errors

  • NS212comparing with the literal missing (x == missing); use ismissing(x)

Example

let avg = ta.sma(close, 50)
if ismissing(avg)
    background(color.fade(color.gray, 90))
end

See also

coalesceisfinitemissing

isfinite

functionNexuScript 1.0

Tests whether a number is an ordinary number: not missing and not infinite.

Syntax

isfinite(x::Number) -> Bool

Parameters

NameTypeDefaultDescription
xNumberrequiredThe number to test.

Returns

Bool false for missing, infinity and minus infinity, true otherwise.

Missing values

A missing x gives false.

Example

let ratio = volume / volume[1]
plot("Volume ratio", isfinite(ratio) ? ratio : missing)

See also

ismissingcoalesce

coalesce

functionNexuScript 1.0

The first of its arguments that is not missing.

Syntax

coalesce(x::Number, fallback::Number, more::Number...) -> Number

Parameters

NameTypeDefaultDescription
xNumberrequiredThe value you want.
fallbackNumberrequiredUsed when x is missing.
moreNumberany numberrequiredFurther fallbacks, tried in order.

Returns

Number The first argument that is not missing, or the last one when all are.

Missing values

This is the function that fills gaps; there is no hidden zero anywhere else.

Example

plot("Volume", coalesce(volume, 0), style = "columns")

See also

ismissingmissing