TradingView scripts frequently have input options that affect the script’s behaviour, like the length of a moving average or an oscillator’s overbought and oversold levels. How can we add such numerical input options to a script?

In this article:

Creating numerical input options in TradingView

Input variables are a useful TradingView feature. These variables have a value that’s set manually by an input option. That way we don’t have to edit the script’s source code to change the value of a variable. Instead, we simply change its setting in the script’s ‘Inputs’ window.

Those manual input options are created with the input() function (TradingView, n.d.). This function also returns the setting’s current value (Pine Script Language Tutorial, n.d.). By assigning that value to a variable, the input’s current setting can be used in the script’s code.

Several kind of inputs are possible in TradingView, like true/false checkboxes and symbol search boxes. Numerical inputs are possible too. They allow us to, for instance, manually change the length of an EMA or the offset of a price band. For this, we set the input() function’s type argument to integer (for an input that accepts whole numbers) or float, which creates a floating-point input that allows decimal values (Pine Script Language Tutorial, n.d.). Let’s take a closer look at how we make such numerical inputs.

Default syntax for creating a numerical input in TradingView

Creating a numerical input in TradingView basically looks like:

numericalVariable = input(title, type=integer/float, 
     minval, maxval, step, defval, confirm)

The different arguments in the parentheses of the input() function work as follows (TradingView, n.d.):

Argument Description
title Optional argument that specifies which text is placed before the input option (for instance, title="EMA Length").
type Required argument that, when set to integer or float, creates a numerical up/down option (see image below).
minval Optional argument that sets the input’s minimum value (like minval=20). When a value lower than this is entered in the input option, the input resets to the minval value.
maxval Optional argument that defines the maximum value of the input (such as maxval=1.5). When we enter a value greater than this, the numerical input automatically reduces to the maxval setting.
step Optional argument that sets the step size for incrementing and decrementing the option in the script’s ‘Inputs’ window. The value for step determines how much the input’s value changes when we click on the small up/down arrow or use the keyboard’s arrow keys. When left out of input(), step defaults to 1.
defval Required argument that sets the option’s default value, and can either be an integer (like defval=2) or a decimal (defval=1.005). With defval the script can start calculating as soon as it’s added to the chart.
confirm Optional argument that, when set to true, asks the user to confirm the input’s value before the script is added to the chart. Only one input() statement needs to have confirm enabled before the ‘Inputs’ window is brought up. confirm defaults to false, and so the input options don’t require confirmation when this argument is missing.
Note: The value of the input option is returned by the input() function. That integer or floating-point value always needs to be assigned to a variable (TradingView, n.d.).

The integer and float input types created by input() look like:

Example of numerical inputs in TradingView

Now let’s look at a couple of programming examples that use these kind of inputs.

Example: making an integer input in TradingView

Before discussing the code, let’s first look at what the example script does. It creates two options in the ‘Inputs’ tab:

Example indicator with numerical inputs

When we add this script to the chart, it looks like:

Example of TradingView indicator

If we change the ‘Offset’ integer setting to 7 and enable the ‘Blue Line?’ checkbox option, the indicator looks like:

Example of TradingView indicator with changed settings

Adding integer inputs to a TradingView script

The example’s programming code is the following:

study(title="Offset plot")

offsetPlot = input(title="Offset", type=integer, 
     defval=5, minval=1, maxval=10)

blueLine = input(title="Blue line?", type=bool, defval=false)

plotColour = blueLine ? blue : red

plot(series=offset(close, offsetPlot), color=plotColour)

We start with the study() function to set the indicator’s properties. Then we create two inputs with input(). The first is an integer named “Offset” that has a default value (defval) of 5 and an allowed range of 1 (minval=1) to 10 (maxval=10). This input’s current value is stored in the offsetPlot variable. The second input, a true/false bool input titled “Blue line?”, is unchecked by default (defval=false) and has its current value put into the blueLine input variable.

Then we use the conditional ternary operator (?:) to give the plotColour variable a value. That operator evaluates a true/false condition and returns its second value when that condition is true and its third value when it’s false (Pine Script Language Tutorial, n.d.). In our example, the evaluated condition is the blueLine input variable. When that variable is true (meaning that its checkbox is enabled), then blue is returned by the conditional operator and assigned to the plotColour variable. And when the “Blue line?” input is unchecked, red is stored in plotColour.

We end the example with plot(), a function that displays a series of data on the chart (TradingView, n.d.). That function’s series argument uses the offset() function to shift the closing price a number of bars to the right (TradingView, n.d.). The amount of bars shifted is specified by the offsetPlot input variable, which we gave a default value of 5 earlier. The colour of this plotted line is set to the plotColour variable.

Example: adding a floating-point input to a TradingView script

In a similar way we can add a floating-point input to a script. Before discussing the code, this is the input option it makes:

Example TradingView Pine script with numerical input

The example indicator added to a chart looks like the following:

TradingView script example

The indicator’s code is:

study(title="Close + percentage")

perc = input(title="Percentage", type=float, 
     defval=1.10, minval=1, maxval=2, step=0.1)

plot(series=close * perc)

We start this brief example by setting the indicator’s chart name with the study() function. Then we use the input() function to create a numerical input option that accepts decimal values (type=float). This input is named “Percentage” and has a default value (defval) of 1.10. The values that it can have lay between 1 (minval=1) and 2 (maxval=2), while each click on the numerical up/down control changes its value with one-tenth (step=0.1).

The current value of that input is stored in the perc variable. That variable is used in the example’s last statement, where we multiply the closing price with value of the “Percentage” input. The result of that calculation is then plotted on the chart with the plot() function (TradingView, n.d.).

To learn more about the input() function, see working with a session input, adding a true/false checkbox, and creating a symbol search box.

Summary

Input options are added to a script with input(). That function also returns the current value of the input option, and we need to assign that value to a variable. The type argument allows for specifying the kind of input: with integer we get an input for whole numbers while float makes a floating-point input that allows for using decimal values. The minimum and maximum values for a numerical input are set with the minval and maxval arguments.


References

Pine Script Language Tutorial (n.d.). Retrieved on August 13, 2015, from https://docs.google.com/document/d/1sCfC873xJEMV7MGzt1L70JTStTE9kcG2q-LDuWWkBeY/

TradingView (n.d.). Script Language Reference Manual. Retrieved on October 22, 2015, from https://www.tradingview.com/study-script-reference/