> ## Documentation Index
> Fetch the complete documentation index at: https://gocharting.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Read someone else's script

> A line-by-line walk through one short script, naming the job each line does, and the names that tell you a script will not compile.

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.

```lipi lines theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Range band", "band", overlay=true)
length = input.int(20, "Length", minval=1, maxval=200)
float basis = talib.sma(hl2, length)
float span = talib.sma(high - low, length)
bool wide = (high - low) > (high[1] - low[1])
static int wideBars = 0
if wide {
    wideBars += 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)
if barstate.islast {
    label.new(bar_index, basis, text="Wide bars: " + str.tostring(wideBars))
}
```

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

```text theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Range band", "band", overlay=true)
```

`//@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

```text theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
length = input.int(20, "Length", minval=1, maxval=200)
```

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

```text theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
float basis = talib.sma(hl2, length)
float span = talib.sma(high - low, length)
bool wide = (high - low) > (high[1] - low[1])
```

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

```text theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
static int wideBars = 0
if wide {
    wideBars += 1
}
```

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

```text theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
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, 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

```text theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
if barstate.islast {
    label.new(bar_index, basis, text="Wide bars: " + str.tostring(wideBars))
}
```

`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](https://gocharting.com/docs/scripting/reference/functions/global.md) before assuming it is a mistake. If it is a mistake, it is likely one of
these:

| What you see | What it means |
| - | - |
| a declaration opening `var` or `varip` | It does not compile. `static` is the keyword that keeps a value from bar to bar. |
| `array.size(a)` | It does not compile. The function is `array.length(a)`. |
| `timeframe.period`, `request.security(...)` | They do not exist. See `interval.*`; another symbol cannot be read. |
| a statement ending in `;` | It does not compile. One statement to a line. |

The full list is [What Lipi does not do](https://gocharting.com/docs/scripting/getting-started/not-supported.md).

## 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](https://gocharting.com/docs/scripting/getting-started/editor.md)).

## Next

> [card] [Reading someone else's script](https://gocharting.com/docs/scripting/getting-started/reading-scripts.md)
>
> the general order to read a script in.

> [card] [Values that persist across bars](https://gocharting.com/docs/scripting/evaluation/persistence.md)
>
> `static`, `intra`, and why `var` fails.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.