Skip to main content
In this tutorial we build a moving-average indicator whose length and color the chart user can change from the script’s settings, without editing any code. Every step is a whole script: replace what is in the editor each time, and check it.
1

plot a moving average

talib.sma is the simple moving average. It takes the series to average and the length to average over, and gives back one value per bar.
A line is drawn across the price bars, one point per bar. overlay=true puts the line on the price bars instead of in a pane of its own.
2

give the length a name

Nothing changes on the chart. The 20 now has a name, and one line owns it. That is the only line we touch next.
3

let the chart user choose the length

Replace the number with input.int. Its first argument is the value the control starts at, and its second is the title shown in the settings.
The line is unchanged, because the control starts at 20. Open the script’s settings and change Length: the average is worked out at the new length. The value is fixed for a run of the script; an input cannot change from bar to bar.The default must be a constant integer expression (20, or 2 * 3). A value that changes per bar, such as close, is refused: Cannot call `input.int` with argument `defval`=`close`. An argument of `series float` type was used but a `const int` is expected. (series and const are qualifiers: see const, input, simple and series.)
4

put bounds on the control

A length of zero makes no sense. input.int takes minval and maxval.
The control now accepts only whole numbers from 1 to 200.
5

move the control in fives

step is the size of one increment. It may be written as a named argument like the others.
6

add a tooltip

tooltip is help text for the control. We also give the plot a title.
7

let the chart user choose the color

input.color gives the chart user a color control. Its value goes where a color goes.

What we built

A three-statement indicator with two settings. The numbers and the color that decide how it looks are no longer in the source.

Next

Inputs

the other input functions and the arguments they share.

Plotting

the rest of what plot accepts.