Rules
## Rules
- A script starts with `indicator(title, shorttitle, overlay, precision)`. `overlay=true` draws on the price
pane; otherwise the script gets its own pane below the chart. A backtest starts with `strategy(title, ...)`
instead (see Strategies).
- The script body runs once per bar, oldest bar first. Names hold their value on the current bar; `x[n]` is
the value n bars ago (`na` before the first bar). Scripts can never read future bars.
- `var x = 0` initialises once and keeps its value between bars. Plain variables are recomputed every bar.
- While the last bar is forming it is re-run on every update; `var` variables roll back first, so live and
history give the same result.
- Declare with `=`, reassign with `:=` (or `+= -= *= /= %=`). Declaring a name twice is an error; inside
if/for bodies use `:=` on outer variables. A variable that is `na` at first needs a type: `float x = na`.
- Blocks are indented (4 spaces recommended). A line continues when inside ( ) or [ ], when it ends with an
operator or comma, or when the next line is indented deeper and starts with an operator.
- Types: int, float, bool, string, color, the drawing handles line, box, label, table, and arrays
(`array<float>` etc., see below). bool is never na. `/` always gives float; use int() or math.floor()
for integer division. Arithmetic with na gives na; comparisons with na are false, test with na(x).
- Qualifiers, lowest to highest: const (literals) < input (input.* values) < simple (known before the first
bar, e.g. syminfo.mintick) < series (per bar). Lengths of ta.* functions must be simple or lower (literals
and inputs are fine, bar_index is not). Plot titles and most style arguments must be const.
- Control flow: `if / else if / else`, `for i = 0 to 9 [by step]` (inclusive), `for v in arr` and
`for [i, v] in arr` (arrays), `while`, `break`, `continue`,
`switch` with or without a subject (`=>` per case, a bare `=>` is the default). if and switch can be the
right side of `=` or `:=`; their value is the last line of the branch taken (na when none).
- Functions: `f(a, b) => expr` or an indented body whose last line is the result; `[x, y]` returns several
values, unpacked with `[a, b] = f(...)`. Declared at the top level only, no recursion. Each call site has its
own ta.* state, so a function with ta.ema called twice keeps two averages.
- plot() returns a handle for fill(); hline() too. plot(offset=n) shifts a plot n bars (positive = right, drawn
past the last bar; n may be an input). Colors: color.new(c, transp) with transparency 0..100.
- Keep ta.* calls at the top level, not inside if or loops (warning W001): their state only advances on bars
where the call runs.
- Errors have a code and position, e.g. "T010 unknown name 'clse' (did you mean 'close'?)". Fix the reported
line; all independent errors are reported at once.