NexuScript 1.5
Script structure
The header, the declaration, settings and the block that runs on every bar.
The first line of every script: the language version it is written for.
Syntax
nexscript 1
nexscript 1.NDetails
Blank lines and comments may come before it, nothing else. nexscript 1 is NexuScript 1.0; nexscript 1.2 also lets the script use what 1.1 and 1.2 added. A script written for an older version keeps working the same on every newer 1.x.
Errors
- NS110the first line is not a
nexscriptheader, or names a version this chart does not have - NS232the script uses a name added after the version in its header; raise the header
Example
nexscript 1.2
indicator "Close"
on bar
plot("Close", close)
endSee also
Names the script and says where it draws.
Syntax
indicator "Title", overlay = false, short = "Title"Details
Comes right after the header, once. overlay = true draws on the price chart; false (the default) gives the script its own pane below, with its own price scale. short is the name in the legend.
Example
nexscript 1
indicator "Fast and slow", overlay = true, short = "F/S"
on bar
plot("Fast", ta.ema(close, 9))
plot("Slow", ta.ema(close, 21))
endSee also
Names a script that may also ask for orders and read its position.
Syntax
strategy "Title", overlay = true, short = "Title"Details
A strategy is an indicator plus the order calls and reads of the Strategies chapter. Its order calls are recorded, never sent: a backtest simulates them. Needs a nexscript 1.1 header or later.
Errors
- NS231
strategyunder anexscript 1header, or an order name used in an indicator
Example
nexscript 1.1
strategy "Cross", overlay = true
on bar
let fast = ta.ema(close, 9)
let slow = ta.ema(close, 21)
if ta.crossover(fast, slow)
order.buy("Long")
end
endSee also
A setting the trader can change in the indicator's settings window.
Syntax
input name = default, title = "Label", min = ..., max = ..., step = ..., options = [...], group = "...", inline = "...", tab = "...", tooltip = "...", when = other => value, key = "..."Details
The kind of setting comes from the default: a whole number, a number with decimals (2.0 or Float(2)), true/false (a switch), text (a text box, or a dropdown with options), an enum member (a dropdown), a colour (a colour picker), a price source such as close (a source dropdown, and the input is a series), Symbol("ES") or Timeframe("D"). The variable name is the key the value is saved under, so changing the title keeps the trader's setting. when shows the row only while another input has a given value. At most 200 inputs; changing one runs the script again from the first bar.
Errors
- NS302a limit or default that is not fixed
Example
input length = 20, title = "Length", min = 1, max = 500
input src = close, title = "Source"
input kind = "EMA", title = "Average", options = ["SMA", "EMA"]
on bar
let e = ta.ema(src, length)
let s = ta.sma(src, length)
plot("Average", kind == "EMA" ? e : s)
endSee also
A name for a value that never changes.
Syntax
const name = expression
const name::Type = expressionDetails
At the top of a script a constant is worked out once, before the first bar, from literals, inputs, other constants, symbol and timeframe facts and plain functions. Inside a block it is a local that cannot be assigned again.
Example
const zone = "America/New_York"
const half_tick = symbol.tick / 2
on bar
plot("Hour in New York", hour(zone))
endSee also
A variable that keeps its value from one bar to the next.
Syntax
state name = initial
state name::Array{Number} = []Details
The initial value is set at the start of every full run. On the forming bar the chart puts every state variable back to its value after the last closed bar before running again, so a live update never counts twice. State may hold numbers, bools, text, colours, enum values and arrays of those, at most 100,000 values in total.
Missing values
A state variable may hold missing; state saved = missing is a common start.
Live and historical bars
Rolled back before each update of the forming bar, so it ends each update as if the bar had run once.
Example
state up_bars = 0
on bar
if close > open
up_bars += 1
end
plot("Up bars so far", up_bars)
endSee also
A type with a fixed list of named choices.
Syntax
enum Name
member = "Label"
...
endDetails
Example
enum Average
sma = "Simple"
ema = "Exponential"
end
input kind = Average.ema, title = "Average"
on bar
let e = ta.ema(close, 20)
let s = ta.sma(close, 20)
plot("Average", kind == Average.ema ? e : s)
endSee also
Defines your own function.
Syntax
function name(param, param = default, param::Type)::ReturnType
...
return value
end
name(param) = expressionDetails
Parameters with defaults come last. Arguments are copies: a function cannot change its caller's variables, except state variables. A function that calls itself needs a return type (::Number) and may nest 200 calls deep. A function that calls a ta function keeps history, follows the call-site rule, and cannot call itself. Each place it is called from keeps its own memory.
Errors
- NS300a function that keeps history is called where it might not run on every bar
Example
function body_share()
let range = high - low
return range > 0 ? abs(close - open) / range : missing
end
typical(h, l, c) = (h + l + c) / 3
on bar
plot("Body share", body_share())
plot("Typical", typical(high, low, close))
endSee also
The block that runs once for every bar, oldest first, and again each time the newest bar changes.
Syntax
on bar
...
endDetails
Everything before it is setup and runs once per full run. Exactly one on bar block. return inside it ends this bar's run early.
Live and historical bars
Closed bars run once each. The forming bar runs again on every price update, with state rolled back first, and everything it drew the time before is cleared.
Example
on bar
plot("Range", high - low, style = "columns")
end