NexuScript 1.5
Statements and control flow
Variables, decisions, loops and functions.
elseif and else branches.forRepeats a block over a range of numbers or over the items of an array.breakLeaves the innermost loop at once.continueSkips to the next turn of the innermost loop.returnLeaves a function with a value, or ends this bar's run early in on bar.=Assigns a new value to a variable or an array item.switchRuns the block of the first case that matches a value, or the else block when none does.Declares a local variable that may be changed later in its block.
Syntax
let name = expression
let name::Type = expression
let (a, b, c) = tupleDetails
A local lives until the end of its block. Assigning to a name that was never declared is an error, so a typo is caught. A let written directly in on bar keeps history: fast[1] is its value on the bar before.
Errors
- NS200assigning to or reading a name that was never declared
Example
let fast = ta.ema(close, 9)
let (macd_line, signal, hist) = ta.macd(close, 12, 26, 9)
plot("Rising", fast > fast[1] ? 1 : 0)
plot("Histogram", hist, style = "histogram")See also
Runs a block only when a condition is true, with optional elseif and else branches.
Syntax
if condition
...
elseif other
...
else
...
endDetails
Conditions must be Bool; a number is not true or false by itself (write x != 0 or !ismissing(x)). A ta call inside an if whose condition changes from bar to bar is refused, because it would miss bars: work it out before the if.
Errors
- NS210the condition is not a Bool
- NS300a function that keeps history inside a branch whose condition changes from bar to bar
Example
let avg = ta.sma(close, 20)
if close > avg
background(color.fade(color.green, 90))
elseif close < avg
background(color.fade(color.red, 90))
else
background(color.none)
endSee also
Repeats a block over a range of numbers or over the items of an array.
Syntax
for i in first:last
for i in first:step:last
for item in array
...
endDetails
Ranges include both ends; step may be negative but not 0. Looping over an array works on a copy taken when the loop starts. There is no while: every loop knows how many times it runs. Each step counts toward the step budget of 1,000,000 per bar.
Errors
- NS300a function that keeps history inside a loop
Example
let total = 0.0
for i in 0:9
total += coalesce(close[i], close)
end
plot("Average of 10", total / 10)See also
Leaves the innermost loop at once.
Syntax
breakExample
let found = 0
for i in 1:20
if high[i] > high
found = i
break
end
end
plot("Bars since a higher high", found)See also
Skips to the next turn of the innermost loop.
Syntax
continueExample
let ups = 0
for i in 0:9
if close[i] <= open[i]
continue
end
ups += 1
end
plot("Up bars in the last 10", ups)See also
Leaves a function with a value, or ends this bar's run early in on bar.
Syntax
return
return valueDetails
A ta call written after a return that only some bars reach is refused, because those bars would skip it.
Example
on bar
plot("Close", close)
if bar.index < 20
return
end
marker("20+", position = "below")
endSee also
Assigns a new value to a variable or an array item.
Syntax
name = value
name += value name -= value name *= value name /= valueDetails
Works on let locals, state variables and array items (levels[2] = close). Inputs, constants and parameters cannot be assigned. += adds to the variable, and so on.
Missing values
Assigning missing is allowed for numbers.
Example
state count = 0
on bar
count += 1
plot("Bars", count)
endSee also
Runs the block of the first case that matches a value, or the else block when none does.
Syntax
switch value
case a
...
case b, c
...
else
...
endDetails
The value is a number, text, Bool, colour or enum value, worked out once. Each case lists one or more fixed values of the same type; the first match runs and the others are skipped. When the value and the cases are fixed (an input, a constant), the chosen block is the same on every bar, so functions that keep history may be called inside it, as in an if on an input. In NexuScript 1.4 and earlier switch and case are ordinary names.
Errors
- NS201a case value appears twice
- NS210a case value has another type than the switch value
- NS302a case value changes from bar to bar
Example
nexscript 1.5
indicator "Pick an average", overlay = true
input kind = "EMA", title = "Average", options = ["EMA", "SMA", "WMA"]
on bar
let avg = missing
switch kind
case "EMA"
avg = ta.ema(close, 20)
case "SMA"
avg = ta.sma(close, 20)
else
avg = ta.wma(close, 20)
end
plot("Average", avg)
end