NexuScript 1.5
Math
Numbers. Every function returns missing when an input is missing, unless it says otherwise.
atan(y, x).roundRounds a number to a whole number or to a number of decimals.round_to_tickRounds a price to the nearest price the symbol can trade at.minThe smallest of two or more numbers.maxThe largest of two or more numbers.clampKeeps a number between a lower and an upper limit.modThe remainder after dividing, always with the sign of the divisor.ismissingTests whether a number is missing.isfiniteTests whether a number is an ordinary number: not missing and not infinite.coalesceThe first of its arguments that is not missing.The number pi, half a turn in radians (3.141592653589793).
Syntax
pi::NumberMissing values
Never missing.
Example
plot("Wave", sin(2 * pi * bar.index / 20))See also
The size of a number without its sign.
Syntax
abs(x::Number) -> NumberParameters
| Name | Type | Default | Description |
|---|---|---|---|
x | Number | required | The 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
Whether a number is below, at or above zero.
Syntax
sign(x::Number) -> NumberParameters
| Name | Type | Default | Description |
|---|---|---|---|
x | Number | required | The 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
The square root of a number.
Syntax
sqrt(x::Number) -> NumberParameters
| Name | Type | Default | Description |
|---|---|---|---|
x | Number | required | The 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
Rounds down to a whole number.
Syntax
floor(x::Number) -> NumberParameters
| Name | Type | Default | Description |
|---|---|---|---|
x | Number | required | The 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
Rounds up to a whole number.
Syntax
ceil(x::Number) -> NumberParameters
| Name | Type | Default | Description |
|---|---|---|---|
x | Number | required | The 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
Drops the fraction of a number, toward zero.
Syntax
trunc(x::Number) -> NumberParameters
| Name | Type | Default | Description |
|---|---|---|---|
x | Number | required | The 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
Raises e to a power.
Syntax
exp(x::Number) -> NumberParameters
| Name | Type | Default | Description |
|---|---|---|---|
x | Number | required | The 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
The natural logarithm (base e).
Syntax
log(x::Number) -> NumberParameters
| Name | Type | Default | Description |
|---|---|---|---|
x | Number | required | The 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
The base-10 logarithm.
Syntax
log10(x::Number) -> NumberParameters
| Name | Type | Default | Description |
|---|---|---|---|
x | Number | required | The 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
The base-2 logarithm.
Syntax
log2(x::Number) -> NumberParameters
| Name | Type | Default | Description |
|---|---|---|---|
x | Number | required | The 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
The sine of an angle.
Syntax
sin(x::Number) -> NumberParameters
| Name | Type | Default | Description |
|---|---|---|---|
x | Number | required | The 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
The cosine of an angle.
Syntax
cos(x::Number) -> NumberParameters
| Name | Type | Default | Description |
|---|---|---|---|
x | Number | required | The 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
The tangent of an angle.
Syntax
tan(x::Number) -> NumberParameters
| Name | Type | Default | Description |
|---|---|---|---|
x | Number | required | The 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
The angle whose sine is a number.
Syntax
asin(x::Number) -> NumberParameters
| Name | Type | Default | Description |
|---|---|---|---|
x | Number | required | The 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
The angle whose cosine is a number.
Syntax
acos(x::Number) -> NumberParameters
| Name | Type | Default | Description |
|---|---|---|---|
x | Number | required | The number to work on. |
Returns
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
The angle whose tangent is a number, or the angle of a point when given two numbers.
Syntax
atan(x::Number) -> NumberParameters
| Name | Type | Default | Description |
|---|---|---|---|
x | Number | required | The 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
The angle of the point (x, y); write it as atan(y, x).
Syntax
atan2(y::Number, x::Number) -> NumberParameters
| Name | Type | Default | Description |
|---|---|---|---|
y | Number | required | The vertical part of the point. |
x | Number | required | The 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
Rounds a number to a whole number or to a number of decimals.
Syntax
round(x::Number, digits::Number = 0) -> NumberParameters
| Name | Type | Default | Description |
|---|---|---|---|
x | Number | required | The number to round. |
digits | Number | 0 | How 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
Rounds a price to the nearest price the symbol can trade at.
Syntax
round_to_tick(price::Number) -> NumberParameters
| Name | Type | Default | Description |
|---|---|---|---|
price | Number | required | The 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
The smallest of two or more numbers.
Syntax
min(a::Number, b::Number, more::Number...) -> NumberParameters
| Name | Type | Default | Description |
|---|---|---|---|
a | Number | required | The first number. |
b | Number | required | The second number. |
more | Numberany number | required | Any further numbers to compare. |
Returns
Number The smallest argument.
Example
plot("Lower of open and close", min(open, close))See also
The largest of two or more numbers.
Syntax
max(a::Number, b::Number, more::Number...) -> NumberParameters
| Name | Type | Default | Description |
|---|---|---|---|
a | Number | required | The first number. |
b | Number | required | The second number. |
more | Numberany number | required | Any 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
Keeps a number between a lower and an upper limit.
Syntax
clamp(x::Number, lower::Number, upper::Number) -> NumberParameters
| Name | Type | Default | Description |
|---|---|---|---|
x | Number | required | The number to keep inside the limits. |
lower | Number | required | The smallest value allowed. |
upper | Number | required | The 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
The remainder after dividing, always with the sign of the divisor.
Syntax
mod(a::Number, b::Number) -> NumberParameters
| Name | Type | Default | Description |
|---|---|---|---|
a | Number | required | The number divided. |
b | Number | required | The 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")
endSee also
Tests whether a number is missing.
Syntax
ismissing(x::Number) -> BoolParameters
| Name | Type | Default | Description |
|---|---|---|---|
x | Number | required | The number to test. |
Returns
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); useismissing(x)
Example
let avg = ta.sma(close, 50)
if ismissing(avg)
background(color.fade(color.gray, 90))
endSee also
Tests whether a number is an ordinary number: not missing and not infinite.
Syntax
isfinite(x::Number) -> BoolParameters
| Name | Type | Default | Description |
|---|---|---|---|
x | Number | required | The number to test. |
Returns
Missing values
A missing x gives false.
Example
let ratio = volume / volume[1]
plot("Volume ratio", isfinite(ratio) ? ratio : missing)See also
The first of its arguments that is not missing.
Syntax
coalesce(x::Number, fallback::Number, more::Number...) -> NumberParameters
| Name | Type | Default | Description |
|---|---|---|---|
x | Number | required | The value you want. |
fallback | Number | required | Used when x is missing. |
more | Numberany number | required | Further 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")