Skip to main content
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) 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).
1

the smallest script

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

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.
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):
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).

What we built

An indicator with one calculation and two lines.

Next

Add an input to your script

let the chart user change the 14.

The Lipi editor

what a check reports.

Plotting

the rest of what plot accepts.