> ## 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.

# Write your first script

> Build an average-bar-range indicator from a declaration, one calculation and two plot calls.

A script needs a header line and a declaration, and shows something only if it has an output. `//@lipi=2` selects the language version, `indicator()`
declares the script (`strategy()` does it for a script that trades, see [Strategies and backtesting](https://gocharting.com/docs/scripting/strategies/overview.md)) and `plot()` puts a value on the chart. In this page we build an indicator that shows how wide each bar is and
its moving average. Type each step into the editor and check it (see [The Lipi editor](https://gocharting.com/docs/scripting/getting-started/editor.md)).

### 1. the smallest script

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Average bar range", "ABR")
plot(high - low)
```

`high` and `low` are the bar's high and low. `high - low` is calculated for every bar, and `plot` draws one point per bar. The
second argument of `indicator` is the short title; with no `overlay` argument the line is drawn in a pane of its own, not over the
price bars.

### 2. give the calculation a name

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Average bar range", "ABR")
float barRange = high - low
plot(barRange, title="Range")
```

The chart does not change. `float` states the type of `barRange`; it may be left out, as in `barRange = high - low`. A name is
created once with `=` and changed later with `:=`.

### 3. average it

`talib.sma` takes a series and a length and returns the simple moving average, one value per bar.

```lipi highlight={4-5} theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Average bar range", "ABR")
float barRange = high - low
plot(barRange, title="Range", color=color.gray)
plot(talib.sma(barRange, 14), title="Average (14)", color=color.orange, linewidth=2)
```

Two lines now appear: the bar range in grey and its 14-bar average, thicker, in orange. Arguments after the first may be named, as
`title=`, `color=` and `linewidth=` are here.

## Two rules to keep in mind

**Output calls sit at the top level.** `plot` and the other output calls (`plotshape`, `plotchar`, `plotarrow`, `plotbar`,
`plotcandle`, `hline`, `fill`, `bgcolor`, `barcolor`, `alertcondition`) cannot be placed inside an `if`, a loop or a function.
Drawings such as `label.new`, `alert` and strategy orders can (see [Script anatomy](https://gocharting.com/docs/scripting/language/anatomy.md)):

```lipi-invalid theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Average bar range", "ABR")
float barRange = high - low
if barRange > 0 {
    plot(barRange)
}
```

The compiler says: `plot()` must be called in the global scope. When a plot should change with a condition, put the condition in an
argument, such as the color: `plot(barRange, color = barRange > 5 ? color.red : color.gray)`.

**One statement to a line.** There is no `;` between statements, and a block's statements go on their own lines (see
[Statements and lines](https://gocharting.com/docs/scripting/language/statements-and-lines.md)).

## What we built

An indicator with one calculation and two lines.

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Average bar range", "ABR")
float barRange = high - low
plot(barRange, title="Range", color=color.gray)
plot(talib.sma(barRange, 14), title="Average (14)", color=color.orange, linewidth=2)
```

## Next

> [card] [Add an input to your script](https://gocharting.com/docs/scripting/learn/add-an-input.md)
>
> let the chart user change the 14.

> [card] [The Lipi editor](https://gocharting.com/docs/scripting/getting-started/editor.md)
>
> what a check reports.

> [card] [Plotting](https://gocharting.com/docs/scripting/chart-output/plotting.md)
>
> the rest of what `plot` accepts.


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