We can add several inputs to a TradingView script, like symbol search boxes and true/false checkboxes. But what is a source input type, and how do we use it?

In this article:

Adding source input types to a TradingView Pine script

When we create an indicator or trading strategy in TradingView, we can use variables whose value is set manually in the script’s ‘Inputs’ window. That way we can change certain values (like the length of moving average) without having to change the script’s code.

We make such input variables by adding an input to a script with the input() function (Pine Script Language Tutorial, n.d.). That function not only creates the input option itself, it also return whatever value the input is currently set to. And we need to assign that returned value to a variable (TradingView, n.d.).

Several kind of inputs are possible in TradingView, like true/false checkboxes, session time range inputs, and symbol search boxes. A ‘source’ input is also possible, and that input allows us to select different data series from the instrument that the script is applied to, like its closing or low prices (see Pine Script Language Tutorial, n.d.; TradingView, n.d.).

We can, for instance, use a source input to manually configure which data an Exponential Moving Average (EMA) should be calculated on. Or we can use it to specify if a script should plot high, low, or average prices. But before discussing a full example that uses this input type, let’s first examine it more closely.

Default syntax for creating a source input in TradingView

The default pattern for creating a source input is:

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

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

When we add a source input type to a script, it creates an input settings like this:

Example of the source input in TradingView

The following items are in its pull-down menu:

Example of items in the pulldown menu

Default values for the source input type in TradingView

The default value of a source input type needs to be one of items displayed in the previous image. The possible defval values and their meaning are (TradingView, n.d.):

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

Let’s look at an example to get a better idea of how the source type can be used.

Example: plotting the correlation between different data series

Before looking at the example’s code, these are the inputs that the programming code makes:

Example of the script's inputs

When we add the script with its default settings to a 15-minute EUR/GBP chart, the indicator looks like:

Chart example of TradingView script

Now if we change the input options to these settings:

Changing the inputs settings of the TradingView script

With those settings the indicator plots the correlation between the EUR/GBP and USD/CAD highs like this:

Example of the TradingView script added to the chart

Working with the source input type in TradingView Pine

The example script has the following code:

study(title="Correlation with other data series", 
     shorttitle="Correlation")

// Add the inputs
otherDataSeries = input(title="Other data series", type=symbol,
     defval="EURUSD")

sourceType = input(title="Source type", type=source, defval=close)
    
corLength = input(title="Correlation length", type=integer,
     defval=20, minval=5)
    
emaLength = input(title="EMA length", type=integer,
     defval=12, minval=5)
    
// Get the additional data series
otherSymbol = security(otherDataSeries, period, sourceType)

// Calculate correlation and EMA
corr = correlation(sourceType, otherSymbol, corLength)
emaCorr = ema(corr, emaLength)

// Plot values
plot(series=corr)
plot(series=emaCorr, color=orange)

The code begins with study(), a required function that sets the indicator’s properties like the name of the indicator (TradingView, n.d.). We then use the input() function four times to add as many inputs to the script.

First we create a symbol search box by setting the input’s type to symbol. Its name is set to “Other data series” and this option’s default value (defval) is “EURUSD”. We store the input’s current value (a string with the symbol and exchange) in the otherDataSeries input variable. We’ll use that variable later on when loading price data in the background.

The second input is a source type that’s by default set to closing prices and is named “Source type”. We store that input’s value in the sourceType variable. The third input, whose value is stored in the corLength variable, is a numeric integer input named “Correlation length” with a default value of 20 and a minimum value (minval) of 5. “EMA length” is the last input, and this integer has a default value of 12 and a minimum of 5. We assign that input to the emaLength variable.

Next we use the security() function to load a data series in the background (Pine Script Language Tutorial, n.d.). We pass three arguments into this function: the symbol to load, its resolution, and the value to return from that data series (TradingView, n.d.). The symbol is specified with the otherDataSeries input variable. Its resolution is set with period, a built-in variable that returns the resolution of the data series that the script is currently added to (TradingView, n.d.). The sourceType input variable, which we set to closing prices by default, is the last argument. So, with the default values, security() returns here the EUR/USD closing prices with the same resolution as the current chart. We store that data in the otherSymbol variable series.

Then we calculate the correlation between the chart’s data series and the data that’s loaded with security(). We do that with correlation(), a function that requires three arguments: two data series (to calculate the correlation between) and an integer that specifies the correlation length (TradingView, n.d.). The data series are set to the sourceType input variable (which, with its default value of close, returns the chart’s closing prices) and otherSymbol, the variable series in which we stored the data returned by security(). The correlation length is set to the corLength input variable while the values returned by correlation() are stored in the corr variable series.

Next we calculate an EMA by passing two values in the ema() function. The first is a series of values to process (for which we use the correlation values, corr) while the second is the number of bars to calculate the EMA on (TradingView, n.d.), which we set to the emaLength input variable. This way we calculate the moving average on the correlation values and the length that’s set in the script’s input options. The values calculated by ema() are stored in the emaCorr variable.

With both the correlation and EMA calculated we can display these on the chart with plot(), a function that plots a series of data on the chart (TradingView, n.d.). In the first plot() function we plot the correlation values on the chart by setting the function’s series argument to corr. The second plot() statement displays the EMA values (emaCorr) on the chart as an orange line.

For more on working with input(), see creating a session time range input, working with the resolution input, and adding a symbol input to a TradingView script.

Summary

The input() function adds input options to a script. It also returns the setting’s current value, and by storing that value in a variable we can use the manual option’s value in the script’s source code. With the input() function’s type argument we specify the kind of input, and setting this argument to source creates a pull-down menu with different source data series (like high, close, or hl2). The input() function’s defval argument sets an input’s default value.


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