Skip to main content
In this tutorial we read one script from top to bottom and name the job of every line. There is nothing to build. What we want at the end is that you can open an unfamiliar script and say what each line is for. Here is the script. Read it once, then we take it in groups of lines.
Paste it into the editor and check it. Three lines are drawn over the price bars, the script has one setting, and the last bar carries a label.

Lines 1 and 2: the header and the declaration

//@lipi=2 selects the language version, and indicator("Range band", "band", overlay=true) declares the script. The arguments are the title, the short title, and overlay, written by name: overlay=true puts the output over the price bars instead of in a pane of its own. A script has exactly one declaration, and a script that is a strategy declares itself with strategy(...).

Line 3: the script’s one setting

An input. The chart user can change it, so 20 is where the control starts, not a fixed value; 1 and 200 are the bounds. No type is written before length: a declaration may state its type or leave it out, and this one leaves it out.

Lines 4 to 6: the calculations

Two calls to the same function, each averaging something different over the same length. hl2 is the midpoint of the bar, a name the language supplies. high - low is calculated, which shows that an argument can be an expression. These lines state their type where line 3 did not; that is the author’s style, not a difference in meaning. Line 6 asks, on every bar, whether this bar’s range is wider than the previous bar’s. high[1] is the high one bar back. The name wide is used again on lines 8 and 11: look for where a name is used before deciding what it means.

Lines 7 to 10: a value that persists

A plain variable is created again on every bar. static keeps wideBars from one bar to the next: its starting value is set once, and += 1 adds to what the previous bar left. So wideBars counts the bars so far whose range was wider than the one before. Whenever you see static, ask what the script is accumulating.

Lines 11 to 13: the plots

Three plots, all at the top level, which is where the plot family has to be. Line 11 uses wide for the color: the conditional expression picks one of two colors per bar, so the basis line is green on the bars whose range widened. The condition sits in an argument because the call itself cannot be put inside an if. Lines 12 and 13 plot expressions, so nothing is stored for them.

Lines 14 to 16: a drawing

barstate.islast is true only on the last bar of the chart. A drawing call, unlike a plot, may sit inside an if. The label is placed at the last bar’s index and at the basis value, and its text joins a string, the count, turned into text by str.tostring.

What to do with a name you do not recognise

Look it up in the reference before assuming it is a mistake. If it is a mistake, it is likely one of these: The full list is What Lipi does not do.

Check it, then trust it

A check returns more than errors: it returns the variables, functions, types and enums the script declared, with their types. That answers “what does this script actually declare” without reading all of it (see The Lipi editor).

Next

Reading someone else's script

the general order to read a script in.

Values that persist across bars

static, intra, and why var fails.