ScriptingTutorialsAdd an input to your script

Add an input to your script

In this tutorial we will build a moving-average indicator whose length the reader of the script can change from the chart, without editing any code.

Open a chart with the Lipi editor beside it. Every step below is a whole script: replace what is in the editor each time, and check it.

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

indicator("Moving average", "MA", true)
plot(talib.sma(close, 20))

Check the script. A line is drawn across the price bars, one point per bar. The third argument to indicator, true, is what puts the line on the price bars rather than in a pane of its own.

Step 2: give the length a name

indicator("Moving average", "MA", true)
length = 20
plot(talib.sma(close, length))

Nothing on the chart changes. That is the result we want at this step: the 20 now has a name, and one line of the script owns it. That line is the only one we touch next.

Step 3: let the reader choose the length

Replace the number with a call to input.int. Its first argument is the value the control starts at, and its second is the title.

indicator("Moving average", "MA", true)
length = input.int(20, "Length")
plot(talib.sma(close, length))

The line is unchanged, because the control starts at 20. Now open the script’s settings from the chart and change Length. The call gives back the value you chose, the average is worked out at that length, and the line is redrawn.

The value you choose is fixed for the run of the script. An input cannot change from bar to bar.

Step 4: put bounds on the control

A length of zero is not a sensible request. input.int takes a minimum and a maximum, and they go after the title.

indicator("Moving average", "MA", true)
length = input.int(20, "Length", 1, 200)
plot(talib.sma(close, length))

Values below 1 and above 200 are no longer accepted. Try the same change to Length as before: the line still follows the control between those two bounds.

Step 5: move the control in fives

The argument after the maximum is the step, the size of one increment.

Write it as a named argument and the script does not parse:

length = input.int(20, "Length", step = 5)

step is a word the grammar keeps for counted loops, so it cannot be used as an argument name. This one mistake accounts for most of the compile failures in the published script library, so it is worth meeting now. Pass everything up to the step positionally instead:

indicator("Moving average", "MA", true)
length = input.int(20, "Length", 1, 200, 5)
plot(talib.sma(close, length))

Length now moves in fives.

Step 6: finish the script

Everything after the step can be passed positionally too, so a tooltip fits on the same call. We also give the plot a title and a colour of its own.

indicator("Moving average", "MA", true)
length = input.int(20, "Length", 1, 200, 5, "Bars in the average")
plot(talib.sma(close, length), title = "Average", color = color.blue)

Check it. The line is drawn in blue, the plot is named Average, and Length carries help text.

What we built

A three-line indicator with one setting. The number that decides the calculation is no longer in the source: it comes from the chart, and the call hands it to us.

Next

  • Inputs — the other ten input functions and the arguments they share.
  • Plotting — the rest of what plot accepts.
Ready to put this into practice?
Try GoCharting Premium
Unlock advanced orderflow, market profile, options desk, and real-time data — everything you just read about, live in your charts.
Upgrade