We can add different input options to TradingView scripts, like time range inputs and true/false checkbox inputs. But what about a text box to search for another instrument’s symbol?

In this article:

Creating a symbol search box input in TradingView

When coding a TradingView strategy or indicator, we can use variables that have their values set with a manual option in the script’s ‘Inputs’ settings. That way we can change a variable’s value with a few clicks, as opposed to editing the script’s source code and changing the variable there.

We create such input options with the input() function, and we specify which kind of input to make with this function’s type argument (TradingView, n.d.). With that argument set to symbol, the input() function adds a symbol search box to the script’s settings (Pine Script Language Tutorial, n.d.).

An example of such a symbol search box is:

Example of symbol input option in TradingView

Besides creating a manual input option, the input() function also returns the option’s current value (TradingView, n.d.). With a symbol search box input, input() returns a string that holds the exchange and symbol of the specified instrument.

In the image above, the input() function would return "NASDAQ:GOOG". When we assign that returned value to a variable, that variable can be used later in the script to refer to the input’s current value. That way we can, for instance, load that instrument’s price data on the chart (as we do in the example below).

Syntax for a TradingView symbol input

Creating a symbol search box in TradingView has the following default pattern:

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

These different arguments of the input() function have the following meaning (TradingView, n.d.):

Argument Description
title 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"). Thanks to 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’re prompted to confirm the script’s inputs.
Note: The input() function returns the input’s current value (TradingView, n.d.). That value has to be assigned to a variable if we want to use the input’s value later on in the script. If we don’t store it in a variable, we cannot use the input’s value in the other parts of the script.

Symbol inputs in TradingView look as follows:

Example of symbol inputs in TradingView

Now let’s examine how we can use a symbol search input option in the context of a full programming example.

Example: using a symbol input to plot another security

The programming example below plots both the closing price and Exponential Moving Average (EMA) from another instrument. We use a symbol input to make it easy to search for and select that instrument. The image below gives a quick peek of the example script; after discussing the code we’ll take a closer look at the indicator’s behaviour.

TradingView example: plotting another instrument's price data
study(title="Symbol's close")

// Add the script's inputs
symbolName = input(title="Symbol", type=symbol, 
     defval="NASDAQ:GOOG")
    
emaLength = input(title="EMA Length", type=integer,
     defval=20, minval=10)

// Retrieve the other symbol's data
symbolClose = security(symbolName, period, close)

symbolEma = ema(symbolClose, emaLength)

// Determine the EMA colour
emaColour = (symbolClose > symbolEma) ? green : red

// Plot both lines
plot(series=symbolClose, color=orange, linewidth=2)
plot(series=symbolEma, color=emaColour, style=cross)

We first configure the indicator’s properties with study(), a function that needs to be added to every indicator (TradingView, n.d.). We use this function’s title argument to specify the script’s name.

Then we add two input options to the script:

symbolName = input(title="Symbol", type=symbol, 
     defval="NASDAQ:GOOG")
    
emaLength = input(title="EMA Length", type=integer,
     defval=20, minval=10)

We add input options to the script’s settings with input(), and this function also returns the input’s current value (Pine Script Language Tutorial, n.d.). Here we assign those values to variables for use later in the script.

The first input is a symbol input type. We name this input “Symbol”, and that name is what’s displayed before the option in the script’s settings (see image further below). We set its default value (defval) to “NASDAQ:GOOG”, the ticker of the Alphabet (previously Google) stock. We track this input’s value with the symbolName input variable.

The other input is a numerical integer input. Such an input only accepts whole numbers, and is made by setting the type argument of the input() function to integer (Pine Script Language Tutorial, n.d.). We name this input “EMA Length”, give it a default value of 20, and set its minimum value (minval) to 10. The input’s current value is what we store in the emaLength variable.

After making the inputs, we retrieve the other symbol’s price data:

symbolClose = security(symbolName, period, close)

symbolEma = ema(symbolClose, emaLength)

We load the price data with security(), a function for retrieving data from a symbol and/or resolution other than the one currently shown on the chart (Pine Script Language Tutorial, n.d.).

We use security() here with three values inside its parentheses. The first is symbolName, the input variable that we set to GOOG. The next one is period, a built-in variable that returns the chart’s current time frame (TradingView, n.d.). With that variable we have security() load data with the same resolution as the chart currently uses. The last value is close and that makes the function return closing prices. We assign those returned values to the symbolClose variable.

Then we calculate the EMA with ema(), a function that requires two arguments: the values to calculate on and a number with the moving average’s length (TradingView, n.d.). Here we set those arguments to the other symbol’s closing prices (symbolClose) and emaLength, our input variable with a standard value of 20. We store the calculated EMA in the symbolEma variable.

Then we figure out which colour to give our moving average:

emaColour = (symbolClose > symbolEma) ? green : red

Here we assign the emaColour variable one of two conditional colours. With the conditional (ternary) operator (?:) we determine which colour to store in the variable during the current script calculation.

That conditional operator works on three values. The first is a true/false condition that, when true, makes the conditional operator return its second value. When that condition is false, the operator returns its third and last value (Pine Script Language Tutorial, n.d.). This makes the operator work like an if/else statement: ‘if this is true, return A; otherwise, return B’.

The condition that we evaluate here is whether the other symbol’s closing price (stored in the symbolClose variable and which is GOOG by default) is greater than (>) its 20-bar EMA (symbolEma). When that’s the case, the conditional operator returns the green basic TradingView colour; otherwise, it returns red. That returned value is then assigned to the emaColour variable for use when plotting.

That plotting is what we do next:

plot(series=symbolClose, color=orange, linewidth=2)
plot(series=symbolEma, color=emaColour, style=cross)

The plot() function displays the data of its series argument on the chart, with a line by default (TradingView, n.d.).

Our first plot() function call plots the symbolClose values in the orange standard colour. We make this line a little thicker than default by setting linewidth to 2. That argument sets the size of the plot, starting with 1 as the default size (TradingView, n.d.).

The next plot() statement displays the moving average (symbolEma). We set this plot’s colour to the emaColour variable, which holds red or green depending on the symbol’s close in relation to its EMA. We specify the plot’s type with the style argument (TradingView, n.d.), and with a value of cross these moving average values display as small ‘+’ signs.

Charts: plotting another symbol in a TradingView script

Now let’s see what our example script does. When we add the example indicator to a SPDR S&P 500 tracker (SPY) chart, it looks like:

Example of a TradingView script added to the SPY tracker

The indicator’s input options are:

Example of the TradingView indicator's default input options

When we click in the “Symbol” input field, TradingView brings up a symbol search menu to help us find any instrument:

Example of searching for another TradingView symbol

Now let’s change the “Symbol” input option from GOOG to Microsoft (MSFT), and set the “EMA Length” input option to 50. Those updated settings change the chart to:

Example of a TradingView script plotting the Microsoft stock

A nice feature of our example indicator is that the other symbol’s time frame is based on the chart’s current resolution. And so when we change the chart to daily NASDAQ-100 price bars, the subchart also plots Microsoft with daily data:

Example chart with daily prices in TradingView

Summary

We add manual input options to a script with input(), and this function’s title argument specifies which text TradingView places before the input in the script’s settings. The input() function also returns the input’s current value, and we can store that in a variable to use later in the script. One kind of input is the symbol search box, and we make those by setting the type argument of the input() function to symbol. In that case, input() returns a string with the symbol and exchange of the instrument we’ve set the input to. The default value is set with the defval argument of input(), and with symbol inputs that argument has to be a string with the exchange and symbol (like defval="NASDAQ:GOOG").


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