Read someone else’s script
In this tutorial we will 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 line by line.
indicator("Range band", "band", true)
length = input.int(20, "Length", 1, 200)
float basis = talib.sma(hl2, length)
float span = talib.sma(high - low, length)
bool wide = (high - low) > (high[1] - low[1])
plot(basis, title = "Basis", color = wide ? color.green : color.gray)
plot(basis + span, title = "Upper", color = color.gray)
plot(basis - span, title = "Lower", color = color.gray)Paste it into the editor and check it. Three lines are drawn across the price bars, and the script has one setting.
Line 1: the script declares itself
indicator("Range band", "band", true)The declaration. Every published script starts with one, so this is the line you should expect to
see first. The three arguments are the title, the short title, and the overlay flag — true puts
the output on the price bars rather than in a pane of its own.
Line 2: the script’s one setting
length = input.int(20, "Length", 1, 200)An input. The reader of the script can change this from the chart, so 20 is where the control starts, not a fixed value. The 1 and the 200 are the bounds.
No data type is written before length. A declaration may state its type or leave it out, and this
one leaves it out.
Lines 3 and 4: the two measurements
float basis = talib.sma(hl2, length)
float span = talib.sma(high - low, length)Two calls to the same function, each averaging something different over the same length. These two lines state their type where line 2 did not — that is a matter of the author’s style, not of meaning.
hl2 is the bar’s midpoint, halfway between its high and its low: a name the language supplies so
the script need not compute it. high - low is computed, and it shows that an argument can be an expression rather than
a bare name.
The reader’s length is passed to both, which is why one control moves all three lines.
Line 5: a question about the previous bar
bool wide = (high - low) > (high[1] - low[1])A bool, and a use of the index suffix. high[1] and low[1] are the high and the low of the
previous bar, so this line asks whether this bar’s range is wider than the last one’s. It is a
question asked afresh on every bar. The brackets around each subtraction are the author being
explicit about what is compared with what.
Nothing on the chart reads wide yet. Look for where a name like this is used before deciding what
it means.
Lines 6 to 8: the output
plot(basis, title = "Basis", color = wide ? color.green : color.gray)
plot(basis + span, title = "Upper", color = color.gray)
plot(basis - span, title = "Lower", color = color.gray)Three plots, all at the top level of the script — that is where every chart-output call has to be.
Line 6 is where wide is used: the conditional expression picks one of two colours per bar, so the
basis line is green on the bars whose range widened. A condition that decides how output looks lives
in an argument like this one, because the call itself cannot be put inside a branch.
Lines 7 and 8 plot expressions rather than variables. Nothing is stored for them; the value is computed in the argument.
Each plot is titled. Untitled plots all default to the same name rather than being numbered, so a script that titles its plots is a script whose author expected them to be told apart.
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 to be one of these three:
| What you see | What it means |
|---|---|
a declaration opening var | It will not compile. static is the keyword that makes a value persist across bars. |
step passed as a named argument | It will not parse. step can only be passed positionally. |
| a gradient or transparency helper | Not present. color.new sets the opacity of a colour. |
A quarter of all saved scripts use at least one name the language does not have, and those scripts fail about twice as often as the rest. So a script that does not run is more often reaching for an absent name than doing something subtle.
Check it, then trust it
Check an unfamiliar script in the editor before reading further into it. A check returns more than the errors: it returns the symbol table the script resolved to — its variables, functions, types, enums and scopes. That is a direct answer to “what does this script actually declare”.
Next
- Reading someone else’s script — where the published scripts are and what to expect across the library.
- What Lipi does not do — the full list of names that do not resolve.
- Values that persist across bars —
static, and whyvarfails.