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

# Add an input to your script

> Turn a hard-coded length into an input.int the chart user can change, with bounds, a step, a tooltip, and a color input.

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.

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Moving average", "MA", overlay=true)
plot(talib.sma(close, 20))
```

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

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Moving average", "MA", overlay=true)
length = 20
plot(talib.sma(close, length))
```

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.

```lipi highlight={3} theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Moving average", "MA", overlay=true)
length = input.int(20, "Length")
plot(talib.sma(close, length))
```

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](https://gocharting.com/docs/scripting/language/qualifiers.md).)

### 4. put bounds on the control

A length of zero makes no sense. `input.int` takes `minval` and `maxval`.

```lipi highlight={3} theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Moving average", "MA", overlay=true)
length = input.int(20, "Length", minval=1, maxval=200)
plot(talib.sma(close, length))
```

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.

```lipi highlight={3} theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Moving average", "MA", overlay=true)
length = input.int(20, "Length", minval=1, maxval=200, step=5)
plot(talib.sma(close, length))
```

### 6. add a tooltip

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

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Moving average", "MA", overlay=true)
length = input.int(20, "Length", minval=1, maxval=200, step=5, tooltip="Bars in the average")
plot(talib.sma(close, length), title="Average")
```

### 7. let the chart user choose the color

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

```lipi highlight={4-5} theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Moving average", "MA", overlay=true)
length = input.int(20, "Length", minval=1, maxval=200, step=5, tooltip="Bars in the average")
lineColor = input.color(color.blue, "Line color")
plot(talib.sma(close, length), title="Average", color=lineColor, linewidth=2)
```

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

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Moving average", "MA", overlay=true)
length = input.int(20, "Length", minval=1, maxval=200, step=5, tooltip="Bars in the average")
lineColor = input.color(color.blue, "Line color")
plot(talib.sma(close, length), title="Average", color=lineColor, linewidth=2)
```

## Next

> [card] [Inputs](https://gocharting.com/docs/scripting/configuration/inputs.md)
>
> the other input functions and the arguments they share.

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