FlowScript: Code Your Own Indicators · Lesson 5 of 10 · 9 min

State that carries across bars: var

Counters, trailing levels and session highs, values that remember.

Normal names start fresh each bar. Declare a name with var and it keeps its value from bar to bar until you change it. The var line runs once; later assignments update it.

indicator("Session high & low", overlay = true)

var sessionHigh = na
var sessionLow = na

if new_session
    sessionHigh = high
    sessionLow = low
else if rth
    sessionHigh = max(sessionHigh, high)
    sessionLow = min(sessionLow, low)

plot(rth ? sessionHigh : na, "Session high", color = green, style = step)
plot(rth ? sessionLow : na, "Session low", color = red, style = step)
var streak = 0
streak = close > open ? streak + 1 : 0
plot(streak, "Green streak", style = columns, color = teal)

Key takeaways

  • var keeps a value across bars; plain names reset each bar.
  • Reset state on events like new_session.
  • print() and the Console help you verify state.

Next: Your own functions, lists and maps