NexuScript 1.5
Types and literals
Every value has one of these types. There are no hidden conversions.
true or false.StringText, written in double quotes.ColorA colour with its transparency, or no colour.ArrayAn ordered list of values of one type, counted from 1.tupleA fixed group of values, plain (a, b) or named (fast = f, slow = s).missingThe number that is not there: a gap, or a value not worked out yet.trueThe Bool value for yes.falseThe Bool value for no.IntMakes an input a whole-number setting, whatever the default is written like.FloatMakes an input a number-with-decimals setting.SymbolMakes an input a symbol setting; empty text means the chart's own symbol.TimeframeMakes an input a timeframe dropdown.structDeclares a record type: a named group of typed fields, built with the type's name like a function.A 64-bit floating-point number; whole numbers are numbers too.
Syntax
Number
14 1_000 2.5 1e-3Details
Underscores may separate digits. A number written with a . or an exponent is a decimal literal, which makes an input a number-with-decimals setting.
Missing values
missing is the number that is not there.
Example
let x::Number = 1_000
plot("x", x)See also
Syntax
BoolDetails
Conditions must be Bool; there are no truthy numbers.
Missing values
A Bool is never missing; a history read before the first bar gives false.
Example
let up::Bool = close > open
if up
candle_color(color.green)
endSee also
Text, written in double quotes.
Syntax
String
"text"Details
Missing values
Text is never missing; it may be empty ("").
Example
let name::String = "Close: " * string(close)
marker(name)See also
A colour with its transparency, or no colour.
Syntax
ColorDetails
Written with the named colours (color.blue), color.hex("#22c55e") or color.rgb(34, 197, 94).
Missing values
color.none means no colour; a colour is never missing.
Example
let c::Color = close > open ? color.green : color.red
plot("Close", close, color = c)See also
An ordered list of values of one type, counted from 1.
Syntax
Array{T}
[1.0, 2.0, 3.0]
state recent::Array{Number} = []Details
Arrays are values: assigning one copies it. At most 100,000 items. An index outside the array is an error.
Errors
- NS402an index outside the array
- NS403taking from an empty array
Example
state recent::Array{Number} = []
on bar
push!(recent, close)
if length(recent) > 10
popfirst!(recent)
end
plot("Average of last 10", sum(recent) / length(recent))
endSee also
A fixed group of values, plain (a, b) or named (fast = f, slow = s).
Syntax
(a, b)
(name = a, other = b)Details
Missing values
Each part may be missing like any value of its type.
Example
let pair = (fast = ta.ema(close, 9), slow = ta.ema(close, 21))
plot("Gap", pair.fast - pair.slow)See also
The number that is not there: a gap, or a value not worked out yet.
Syntax
missingDetails
Missing values
This is the value itself.
Errors
- NS212
x == missing; useismissing(x)
Example
state saved = missing
on bar
if bar.index == 10
saved = close
end
plot("Saved close", saved)
endSee also
The Bool value for yes.
Syntax
trueMissing values
Never missing.
Example
input show = true, title = "Show average"
on bar
let avg = ta.sma(close, 20)
plot("Average", show ? avg : missing)
endSee also
The Bool value for no.
Syntax
falseMissing values
Never missing.
Example
input fill_on = false, title = "Shade"
on bar
if fill_on
background(color.fade(color.blue, 90))
end
endSee also
Makes an input a whole-number setting, whatever the default is written like.
Syntax
input name = Int(x), ...Missing values
An input always has a value.
Example
input length = Int(20), title = "Length", min = 1
on bar
plot("Average", ta.sma(close, length))
endSee also
Makes an input a number-with-decimals setting.
Syntax
input name = Float(x), ...Details
Same as writing the default with a decimal point (2.0).
Missing values
An input always has a value.
Example
input mult = Float(2), title = "Multiplier", step = 0.1
on bar
let (basis, upper, lower) = ta.bands(close, 20, mult)
plot("Upper", upper)
endSee also
Makes an input a symbol setting; empty text means the chart's own symbol.
Syntax
input name = Symbol("ES"), ...Details
The value is text in capitals. Use it with higher(...; symbol = name). The chart loads another symbol only when your chart could show it.
Missing values
An input always has a value; "" means the chart's symbol.
Example
input other = Symbol(""), title = "Compare with"
on bar
let c = higher("D", symbol = other) do
close
end
plot("Daily close", c)
endSee also
Makes an input a timeframe dropdown.
Syntax
input name = Timeframe("D"), ...Details
The value is text such as "60" or "D".
Missing values
An input always has a value.
Example
input htf = Timeframe("D"), title = "Higher timeframe"
on bar
let c = higher(htf) do
close
end
plot("Higher close", c)
endSee also
Declares a record type: a named group of typed fields, built with the type's name like a function.
Syntax
struct Name
field::Type
field::Type = default
end
let r = Name(value, field = value)Details
A struct is a named tuple with a type name: Name(...) builds one (values by position in field order, then by name; a field with a default may be left out), r.field reads a field, and the name is a type for let, state, parameters, return types and arrays (state swings::Array{Swing} = []). A struct is a value: assigning it copies it, and a field cannot be assigned (build a new one instead). A state may hold a struct whose fields are numbers, text, Bools, colours or enum values, and arrays of those. The name starts with a capital letter.
Errors
- NS201two fields or two types with the same name
- NS211a field is missing, given twice, or not in the struct
- NS210a value has another type than its field
- NS220a field of a struct is assigned
- NS232the header names a version before 1.5
Example
nexscript 1.5
indicator "Last swing high", overlay = true
struct Swing
price::Number
bar::Number = -1
end
state last = Swing(price = missing)
on bar
let ph = ta.pivot_high(high, 2, 2)
if !ismissing(ph)
last = Swing(ph, bar.index - 2)
end
plot("Last swing high", last.price, style = "step")
end