Mentis course
Lesson 6. Remembering with var
30 min, foundation level
Normal values start fresh on every bar. A var value is set once and keeps what you give it.
//@zscript 1
indicator("Count green bars")
var int greens = 0
if close > open
greens := greens + 1
plot(greens)
Use var for running totals, the last signal price, or a drawing you update. varip also keeps values between ticks of the newest bar; most scripts never need it.
Exercises
- Reset each day. Make the counter go back to 0 at the start of every day (hint: dayofmonth != dayofmonth[1]).
- Track the highest close. Keep the highest close so far in a var float and plot it.
Quick quiz
Which keyword keeps a value from bar to bar?
var is set once and then kept.
How do you change an existing value?
:= changes; = creates; == compares.
Education only. Not financial advice. Examples are teaching samples, not trading results.
