Values that carry over: starts
A name reading its own prev, with a starts line for the first bar.
For AI tools: this page as Markdown · the whole language in one file
A value that builds up from bar to bar (a trailing stop, a counter, a state) reads its own prev. A starts line says what prev gives before there is an earlier bar. This is Pine's var.
A trailing stop
1name("ATR stop", where = chart)2 3stop starts close4stop = if close > prev(stop)5 then max(prev(stop), close - 2 * atr(14))6 else min(prev(stop), close + 2 * atr(14))7plot(stop, "Stop", style = steps)A yes/no state
1name("Long or flat", where = pane)2 3long starts false4long = if crosses_above(close, ema(close, 50)) then true5 else if crosses_below(close, ema(close, 50)) then false6 else prev(long)7plot(long ? 1 : 0, "Long", style = steps)Lines may read each other's prev in any order, so counters that reset each other work:
1name("Run counters", where = pane)2 3rises starts 04falls starts 05rises_next = if close > open then prev(rises) + 1 else prev(rises)6falls_next = if close < open then prev(falls) + 1 else prev(falls)7rises = if falls_next > 3 then 0 else rises_next8falls = if rises_next > 3 then 0 else falls_next9plot(rises, "Rises")10plot(falls, "Falls")See also: starts, Looking back: prev


