Syntax
Variables, types, operators and control flow in NeuroScript.
NeuroScript uses indentation to delimit blocks (like Python, without : or
{}). Every script starts with the version line and the indicator declaration —
see First steps for the full anatomy.
Comments
// line comment — everything after // is ignored
length = input.int(14, "Length") // also works at the end of a lineVariables
Declare with = and reassign with :=:
avg = ta.sma(close, 20) // declares
avg := ta.sma(close, 50) // reassigns (the variable already exists)The type is inferred from the value, but you can declare it explicitly:
int period = 14
float factor = 2.0
active = true // inferred bool
text = "RSI" // inferred stringLanguage types: int, float, bool, string, color. Market values like
close are series (series<float>) — a sequence with one value per candle
(see Market data).
var and varip — state across candles
By default a variable is recomputed on every candle. Use var to initialize
once and keep the value across candles:
var contador = 0
contador := contador + 1 // accumulates across the chartvarip is similar, but the value also persists across ticks within the same
candle (intrabar).
Operators
| Category | Operators |
|---|---|
| Arithmetic | + - * / % |
| Comparison | == != < > <= >= |
| Logical | and or not |
| Ternary | condition ? valueIfTrue : valueIfFalse |
| History | series[n] — value from n candles back |
variacao = close - close[1] // history: previous candle
isUp = close > open and volume > 0 // logical
col = close >= open ? color.green : color.red // ternaryControl flow
if / else
Blocks by indentation — no : (Python) and no {} (C):
tendencia = 0
if close > open
tendencia := 1
else
tendencia := -1for
Numeric loop with to (optional step with by, e.g. for i = 0 to 100 by 10):
soma = 0.0
for i = 0 to 9
soma := soma + close[i] // sum of the last 10 closesTo iterate over an array, use for ... in:
valores = array.from(1, 2, 3)
total = 0
for v in valores
total := total + vwhile
i = 0
while i < 10
i := i + 1switch
Picks one value among several cases (=>), with a default case at the end:
signal = switch
close > open => 1
close < open => -1
=> 0Indicator rules
- The first line of the script must be
//@version=6. plot()inside anifonly draws on the candles where that branch runs (on the others the value isna). For signals, keepplot()at the top level withcondition ? value : na,style=plot.style_circles(orstyle_triangleup/style_triangledown) and a conditional color, or useplotshape(condition, style=shape.triangleup, location=location.belowbar)for a marker anchored to the candle.plotchar()andplotarrow()draw nothing on the chart.offset=onplot()/plotshape()shifts the drawing by bars (positive forward, negative backward); the projection past the last candle is not drawn.linestyle=plot.linestyle_dashed|dotteddashes the line.- NeuroScript has no
returnat the top level.