In this article we look at the most important points of working with input options in TradingView Pine. Follow the links to the full articles to learn more.

In this article:

Creating an input option in TradingView Pine

Adding a manual input option to a script’s ‘Inputs’ window is done with the input() function (Pine Script Language Tutorial, n.d.; TradingView, n.d.). This function also returns the input’s current value, and that value needs to be assigned to a variable (TradingView, n.d.). We can then use that variable to access the input’s current setting. This also shows the advantage of manual input options: being able to change a value that’s used in the script without having to edit the script’s source code.

The input() function has several arguments, and its type argument defines which kind of input to make. Let’s look at the different input options that are available in TradingView.

Making a numerical input in TradingView

We started this chapter with creating numerical inputs in TradingView: whole numbers and decimal values. For the first we set the input() function’s type argument to integer, while the second requires type=float (TradingView, n.d.). More specifically, the syntax for creating a numerical input in TradingView is:

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

These different arguments mean the following (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.

The numerical inputs that we make with input() look like this:

Example of numerical inputs in TradingView

Creating a true/false checkbox in TradingView Pine

Another input option is a true/false checkbox, which we make by setting the input() function’s type argument to bool (Pine Script Language Tutorial, n.d.). The default pattern of creating such an input looks like:

boolVariable = input(title, type=bool, defval, confirm)

This shows that the input() function can use several arguments when used with a bool type. These arguments have the following meaning (TradingView, n.d.):

Argument Description
title This optional argument sets the title that’s placed before the input. It accepts a string value (like title="Plot arrows").
type A required argument that needs to be set to bool to create a true/false checkbox.
defval This required argument sets the input’s default value. For Boolean inputs, we set defval either to true (defval=true) or false (defval=false). A default value allows the script to always start calculating.
confirm An optional argument that, when set to true, asks the user to confirm the input setting(s) before the script is added to the chart. confirm defaults to false, so when omitted we won’t be asked to confirm the input options.

The bool input option creates checkboxes that look like this:

Example of the true/false checkboxes in TradingView

Working with a string input option in TradingView

In the chapter’s next article we examine creating a text box input option. For that we set the input() function’s type argument to string (Pine Script Language Tutorial, n.d.). The syntax for this looks like:

stringVariable = input(title, type=string, defval, confirm)

These arguments work as follows (TradingView, n.d.):

Argument Description
title This optional argument sets the text that’s placed before the input option in the script’s settings (for instance, title="Time frame").
type To create a text box input option, this required argument needs to be set to string.
defval This required argument sets the default value of the input option. That way the script always has a value to start calculating with. For text boxes, defval needs to be set to a literal string value (like defval="360" or defval="My message").
confirm This optional argument, when set to true, asks the user to confirm the script’s input setting(s). When a script has multiple input() statements, only one needs the confirm=true argument before the script’s inputs need to be confirmed. The confirm argument defaults to false, so when it’s missing the input options won’t need to be confirmed.

Those text box inputs look like this in a script’s ‘Inputs’ window:

Example of the string input in TradingView

Creating a time range session input in TradingView

Another input option is a session input, and with that kind of input we specify a time range in the script’s settings. To create that option, we set the type argument of the input() function to session (Pine Script Language Tutorial, n.d.). The default pattern for such an input is:

sessionString = input(title, type=session, defval, confirm)

These arguments have the following meaning (TradingView, n.d.):

Argument Description
title Optional string argument that specifies the title that’s placed before the input option (like title="RTH session").
type Required argument that, when set to session, lets the input() function create a time range option in the script’s input settings. With session, input() returns the input option’s current value as a string, which is then stored in the sessionString variable.
defval Required argument that sets the default value of the input. With a session input, defval needs to be set to a time string in 24-hour HHmm format like "0930-1600" (9:30 a.m. till 4 p.m.) or "2200-0800" (10 p.m. till 8 a.m.). With a default value the script can start calculating as soon as it is added to the chart.
confirm An optional argument that, when set to true, asks the user to confirm the input option(s) before the script starts calculating. This argument defaults to false, meaning that when confirm is missing the script settings don’t need to be confirmed. Only one of the script’s inputs needs to have this argument set too true before the inputs are displayed before the script is added to the chart.

The session inputs look as follows:

Example of the session input in TradingView

Adding a symbol search box to a TradingView script

Then we examined a symbol search box input option, which we make by setting the input() function’s type argument to symbol (Pine Script Language Tutorial, n.d.). This creates a symbol search box in the script’s ‘Inputs’ window that’s similar to the search box that’s above each TradingView chart and that we use to switch the chart to another instrument.

The syntax for creating a symbol search box is:

stringVariable = input(title, type=symbol, defval, confirm)

These arguments have the following meaning (TradingView, n.d.):

Argument Description
title This optional argument sets the input’s title (that is, the text that’s placed before the input option). This argument requires a literal string like title="Additional symbol".
type Required argument that needs to be set to symbol in order to create a symbol search box. The input() function then returns a string value with the specified symbol.
defval Required argument that specifies the default value of the symbol search box with a literal string value (like defval="GOOG"). With a default value the script can start calculating immediately when added to a chart.
confirm This optional argument, when set to true, asks the user to confirm the input settings before the script is added to the chart. This argument defaults to false, meaning that no manual confirmation of the inputs is needed when confirm is missing. Only one input() statement needs to have confirm set to true before we need to manually confirm the input settings.

Once created with the input() function, the symbol inputs looks like this:

Example of the symbol input option in TradingView

Creating a resolution pull-down menu in the script’s inputs

Another input is TradingView’s resolution input option. This input creates a pull-down menu that lists several common resolutions, and that allows us to quickly choose a certain time frame. Creating this input is done by setting the type argument of the input() function to resolution (Pine Script Language Tutorial, n.d.). The default syntax for this looks like:

resolutionString = input(title, type=resolution, defval, confirm)

As we can see here, the following arguments are available with the resolution input option (TradingView, n.d.):

Argument Description
title Optional argument that specifies which text is placed before the input (like title="Resolution setting").
type Required argument that, when set to resolution, makes input() create a resolution pull-down menu. The input() function then returns the current resolution value as a string.
defval Required argument that sets the input’s default value. With resolution, defval needs to be set to a string with a minute, day, or week value (see table below). Thanks to a default value, the script can start calculating as soon as it’s added to a chart.
confirm Optional argument that, when set to true, asks the user to confirm the input options before the script is added to the chart. Only one input() statement needs to have confirm set to true before the ‘Inputs’ window is displayed forcibly. This argument defaults to false, meaning that the input options don’t require confirmation when confirm isn’t there.

Those resolution input options look like this in the ‘Inputs’ window:

Example of the resolution input in TradingView

And the resolution pull-down menu contains these values:

Pull-down menu of the resolution input in TradingView

The default value of an input option is set with the defval argument (TradingView, n.d.). For a resolution input, this argument needs to be set to minute, day, or week values. That means we can set defval to one of the following:

Value in the pull-down menu Required defval value
1 defval="1"
2 defval="2"
3 defval="3"
5 defval="5"
15 defval="15"
30 defval="30"
45 defval="45"
1h defval="60"
2h defval="120"
3h defval="180"
1d defval="D"
1w defval="W"
Note: We can only set a resolution input’s default value to one of these values. Should we set defval to values like 10 minutes (defval="10") or 4 hours (defval="240"), the resolution pull-down menu simply defaults to its first value of ‘1’.

Adding a source input type to a TradingView script

The last input option that we discussed in this chapter is TradingView’s ‘source’ input. That inputs allows us to select different data series from the instrument that the script is applied to, such as its close, high, or low prices (see Pine Script Language Tutorial, n.d.; TradingView, n.d.). This way we can use a source input to easily change the values that a script calculates on. This input’s default syntax is:

variableSeries = input(title, type=source, defval)

These three arguments work as follows (TradingView, n.d.):

Argument Description
title Optional argument that sets the title of the input (like title="Data type"). This text is placed before the input option in the ‘Inputs’ window.
type Required argument that we need to set to source in order to create the data type pull-down menu. The value that we choose in that menu is returned by input() and then stored in the variableSeries variable.
defval Required argument that sets the input’s default value (see the next table for the allowed values). With this default value the script can start calculating as soon as it’s added to a chart.

We can set the default value (defval) of a source input option to one of these:

Default value Meaning
defval=open Sets the default value to open prices of the instrument that the script is added to.
defval=high Sets the default value to the instrument’s high prices.
defval=low Sets the source type to low prices of the data series.
defval=close Specifies a series of closing prices as the default value.
defval=hl2 Sets the default value to a bar’s midpoint (that is, [high + low] / 2).
defval=hlc3 Defines the average of the high, low, and close prices to be the default value (meaning, [high + low + close] / 3).
defval=ohlc4 Sets the default value to the average of a price bar’s four components (that is, [open + high + low + close] / 4).

These source inputs look like:

Example of source inputs in TradingView

This ends our overview of the different input options in TradingView. See all TradingView Pine programming tutorials to learn more about other subjects.


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 27, 2015, from https://www.tradingview.com/study-script-reference/