A script with manual options is also a script that’s easy to configure. How do we add text box options to a script?

In this article:

Adding a text box option to a TradingView script

Script inputs are a useful TradingView feature. They allow us to quickly alter script values by manually changing an input option (like the RSI overbought level). That way we can change the values used in the script without having to edit its source code.

Inputs are added to a script with the input() function (Pine Script Language Tutorial, n.d.). That function not only creates the input option in the script’s settings, it also returns the option’s current value which we need to store in a variable (TradingView, n.d.). We can then use that variable in the script’s code to refer to the input’s current value.

The kind of option that input() makes depends on its type argument (Pine Script Language Tutorial, n.d.). With that argument we can create numerical up/down inputs, true/false checkboxes, and text box inputs. For that latter we set type to string. A string is, in programming languages, a string of text with one or several characters (Stellman & Greene, 2010).

Syntax for creating a string text box input in TradingView

Creating a text box input option in TradingView has the following default pattern:

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

When used to create a text box, the input() function has the following arguments (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.
Note: The input() function returns the current value of the input option. This value always needs to be assigned to a variable (TradingView, n.d.).

The text boxes that we create with the input() function look as follows:

Example of string inputs in TradingView

Now let’s examine a few examples in which we create text boxes with the input() function.

Plotting another time frame’s high and low with a string input

One way to use a string input type is to create a text box that sets a time frame, and then plot that time frame’s high and low on the chart. Before discussing the code for that, this is what that input looks like:

Script example's input options in TradingView

The example script is:

study(title="String input - time frame's high and low", overlay=true)

tf = input(title="Higher time frame", type=string, defval="360")

tfHigh = security(tickerid, tf, high)
tfLow = security(tickerid, tf, low)

plot(series=tfHigh, color=green, linewidth=2)
plot(series=tfLow, color=red, linewidth=2)

We first set the indicator’s properties with study(). Then we use input() to create an input option that we name "Higher time frame" and set to a string. The default value (defval) of this input is set to "360" which, in this context, means 360 minutes or 6 hours. We put the current value of this time frame input into the tf variable.

Then we make two other variables that hold the time frame’s high and low: tfHigh and tfLow. Both are set to a data series returned by security(). That function allows us to request data from other symbols and/or resolutions (Pine Script Language Tutorial, n.d.). We use security() here with three arguments: the first two are the symbol and resolution, the third is the value that the function should return (TradingView, n.d.).

We specify the symbol with tickerid, a built-in variable that returns the symbol and exchange of the instrument that the script is added to (TradingView, n.d.). The resolution is set to the tf input variable. security() returns, for the tfHigh variable, the high prices (high) while it puts the low prices (low) for the tickerid symbol and tf time frame in the tfLow variable.

Then we use the plot() function to display a series of data on the chart (TradingView, n.d.). We first plot the tfHigh values on the chart. Since plot() draws lines by default (Pine Script Language Tutorial, n.d.), those different time frame high prices are displayed as a green line. With the linewidth of 2 we get a slightly thicker line than the default line. We end the example with plot() that displays the tfLow values as a red line on the chart.

If we add this example to a 30-minute chart, it looks like:

Example of TradingView script

Changing the “Higher Time Frame” input option to “1D” (to get a daily time frame) makes the indicator look as follows:

Example of TradingView script

Plotting an EMA based on a different time frame in TradingView

Surprisingly, string inputs in TradingView have currently very limited uses; they cannot, for example, be used with plotshape() or plotchar() to display the text or character on the chart. So in our second example we use security() again. But before discussing the code, let’s look at the type of inputs that the script makes:

Example of string inputs in TradingView

The programming code for this is:

study(title="String input", overlay=true)

timeFrame = input(title="Time frame", type=string, defval="30")
emaLength = input(title="EMA length", type=integer, defval=10)

data = security(tickerid, timeFrame, close)
emaValue  = ema(data, emaLength)

plot(series=emaValue, color=fuchsia, linewidth=2)

We first use the study() function to set the indicator’s settings. Then we create two inputs. The first, whose current value we store in the timeFrame variable, is titled "Time frame" and set to a string with a default value (defval) of "30". The second input is a numerical integer input that’s assigned to the emaLength variable and that has a default value of 10.

With security() we then request data from an additional resolution other than the one that the script is applied to (Pine Script Language Tutorial, n.d.). Its first argument is set to the tickerid variable which holds the current symbol and exchange (TradingView, n.d.). That way the data loaded by security() is for the exact same instrument that the script is added to. The second argument is the timeFrame input variable and the third argument is close. That latter sets the data that’s returned by security() (TradingView, n.d.). We store the values returned by that function in the data variable series.

Then we calculate an Exponential Moving Average (EMA) with that data variable. We use the ema() function for that, which requires a series of values and the EMA period as a number of bars (TradingView, n.d.). That period length is set to the emaLength input variable that we gave a default value of 10 earlier. The values computed by ema() are put in the ema variable.

The example ends with plot() that, by setting its series argument to the ema variable, plots the EMA values on the chart.

When the indicator is added with its default settings to a chart, it plots a 10-period EMA based on closing prices from a 30-minute time frame like this:

Chart example of a TradingView Pine script

If we set the “Time Frame” input to 120 (that is, 120 minutes or 2 hours) the EMA changes to:

Example chart of a TradingView script

And setting the “Time Frame” input to 1D (one day) changes the fuchsia line to:

Example of a TradingView Pine script

To learn more about adding inputs to TradingView scripts, see creating a true/false checkbox, working with a resolution input, and adding symbol search to a script’s options.

Summary

We add input options to TradingView scripts with the input() function. Text boxes are one kind of input, and we make those by setting the function’s type argument to string. The value that the input option is currently set to is returned by input(), and that value needs to be assigned to a variable. That way we can use the input’s value in the script.


References

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

Stellman, A. & Greene, J. (2010). Head First C#: A Brain-Friendly Guide (2nd edition). Sebastopol, CA: O’Reilly Media.

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