Mentis course
Lesson 2. Bars and series
30 min, foundation level
A chart is a row of bars. Zscript runs your script once for every bar, oldest to newest. On each run, close means the close of the bar being worked on.
A value that has one number per bar is a series. To see an older value, use square brackets: close[1] is the close one bar ago.
//@zscript 1
indicator("Bar change")
change1 = close - close[1]
plot(change1, "Change", style=plot.style_histogram)
On the first bar there is nothing before it, so close[1] is na: not available. Math with na gives na, and plots skip it. Use nz(x) to swap na for zero, or na(x) to test for it.
You can only look back. Zscript has no way to read a bar that has not happened yet.
Exercises
- Two-bar change. Plot close minus close[2] as a histogram. Which bars are missing at the left edge, and why?
- Safe change. Use nz() so the first bar shows 0 instead of nothing.
Quick quiz
What is close[3]?
Square brackets look back by that many bars.
What does na mean?
na means not available. Plots skip it.
Education only. Not financial advice. Examples are teaching samples, not trading results.
