In TradingView we can create several input types, like those for numerical values, session time ranges, and even symbol search inputs. But what’s a ‘resolution input’ and how do we add it to our scripts?
In this article:
Adding a resolution drop-down menu in TradingView
In TradingView scripts we can work with variables whose value is set by an input option. That makes changing a certain value used in the script’s code easy: instead of having to edit the script’s source code, we simply go to the script’s input settings and change the option’s value.
We create those manual input options with the
input() function (TradingView, n.d.). This function not only adds a setting to the script’s ‘Inputs’ window, it also returns the current value of that input option (Pine Script Language Tutorial, n.d.). When we store that value in a variable with the assignment operator (
=), then we can use the variable anywhere in the script’s code to refer to the current value of the input option.
One kind of input that we can make in TradingView is a drop-down menu with different resolution values (see image further down below). For that we set the
type argument of the
input() function to
resolution (Pine Script Language Tutorial, n.d.). Let’s take a closer look at how creating such an input with the
input() function works.
Default syntax for creating a resolution input option
The basic pattern that creates a resolution input with
resolutionString = input(title, type=resolution, defval, confirm)
type argument is set to
input() function returns a string with the number of minutes that correspond to the input’s value. That returned value can then be used later on in the code.
So with the input option set to ‘2h’, the value that’s returned by
"120". An exception are the ‘1d’ and ‘1w’ values from the resolution drop-down menu; the function returns for those
The different arguments of the
input() function that we can use when creating a resolution input are (TradingView, n.d.):
||Optional argument that specifies which text is placed before the input option in the script’s settings (like
||Required argument that, when set to
||Required argument that sets the input’s default value. With
||Optional argument that, when set to
input()function returns the current value of its input option; by assigning this value to a variable we’re able to use the input’s value later on in the script.
The pull-down menu that
resolution makes looks as follows:
Setting the default value of a resolution input in TradingView
We set the default value of a resolution input with the
defval argument of the
input() function (TradingView, n.d.). This argument accepts a string with minutes, day, or week values.
The different values in the pull-down menu correspond to the following
|Value in the pull-down menu||Required
resolutioninput to a value from the pull-down menu. In other words, specifying a default value of 10 minutes (
defval="10") or 4 hours (
defval="240") doesn’t work; the resolution pull-down menu simply defaults to ‘1’ then. With a text box input we have the flexibility to choose other resolutions also.
Example: plotting highs and lows from another time frame
In the programming example below we create a resolution input option to specify another time frame. Then, with the help of that input’s value, we retrieve the high, low, and EMA (Exponential Moving Average) from that time frame. We then plot those three values on the chart. A glance of the indicator is given below; after discussing the script’s code we examine its appearance in detail.
study(title="Data from another time frame", overlay=true, shorttitle="Higher time frame") // Add the inputs timeFrame = input(title="Other time frame", type=resolution, defval="180") emaLength = input(title="EMA length", type=integer, defval=21, minval=5, maxval=100) // Retrieve the higher time frame's data symbolHigh = security(tickerid, timeFrame, high) symbolLow = security(tickerid, timeFrame, low) emaClose = ema(security(tickerid, timeFrame, close), emaLength) // Plot the other time frame's data plot(series=symbolHigh, color=green, linewidth=3, style=circles) plot(series=symbolLow, color=red, linewidth=3, style=cross) plot(series=emaClose, color=orange, linewidth=2)
We first set the indicator properties with
study(). We use the
shorttitle arguments to name the indicator, while we display the indicator on the chart’s instrument by setting
Then we create two input options:
timeFrame = input(title="Other time frame", type=resolution, defval="180") emaLength = input(title="EMA length", type=integer, defval=21, minval=5, maxval=100)
We make these input options with the
input() function, and store the value returned by this function in a variable with the assignment operator (
The first input is named “Other time frame” with the
title argument, and we set this one to a resolution input option (
type=resolution). Its default value (
"180" (meaning, 180 minutes or 3 hours), and we track its current value with the
The other input is a numerical integer input named “EMA length”. This one has a standard value of 21 with an allowed range of 5 (
minval=5) to 100 (
maxval=100). We store this input’s value in the
emaLength variable to use later on.
Next we load the price data from the other time frame:
symbolHigh = security(tickerid, timeFrame, high) symbolLow = security(tickerid, timeFrame, low) emaClose = ema(security(tickerid, timeFrame, close), emaLength)
security() function we can retrieve data from another symbol and/or resolution other than the one that the script calculates on (Pine Script Language Tutorial, n.d.; TradingView, n.d.). We use this function here with three arguments. These are the symbol with the accompanying exchange, the resolution to load, and the expression that
security() should return (TradingView, n.d.).
There are two similarities between the three
security() function calls. First, each specifies the symbol to load with
tickerid. That built-in variable returns the symbol name and exchange prefix of the instrument that the script is currently added to (see TradingView, n.d.). The other similarity is that each uses the
timeFrame input variable to specify which resolution
security() should load. This way our “Other Time Frame” input option affects which data is loaded here.
There are differences too. The first
security() function call loads
high prices for the instrument’s other time frame, and stores the values returned by
security() in the
symbolHigh variable. The next
security() function call loads
low prices from that time frame which we then assign to the
The third use of
security() is a bit different. Now we don’t store the returned values in a variable but use these with
ema(). That function computes an Exponential Moving Average (EMA) with two arguments: a series of values to process alongside an integer with the moving average’s length (TradingView, n.d.). For the first argument we use the values returned by
security(), whereas the second is set to the
emaLength input variable. That variable of our “EMA Length” input option has a standard value of 21.
This way we don’t calculate the EMA based on the chart’s time frame, but with the time frame that we loaded with
security() and specified with the
timeFrame resolution input variable. We store those EMA values in the
emaClose variable for now.
With the values computed, we plot them on the chart:
plot(series=symbolHigh, color=green, linewidth=3, style=circles) plot(series=symbolLow, color=red, linewidth=3, style=cross) plot(series=emaClose, color=orange, linewidth=2)
plot() function displays the data of its
series argument on the chart (TradingView, n.d.). With the function’s
color argument we set the plot’s colour and
linewidth sets the plot size, starting from 1 for the default size (TradingView, n.d.).
plot() statement displays the high prices from the other time frame (
series=symbolHigh). This plot is coloured with the
green basic TradingView colour and, with
linewidth=3, made a few steps bigger than the default size. With the
style argument set to
circles we display these values as little dots on the chart.
plot() statement shows the other time frame’s low prices (
series=symbolLow). These values appear as small ‘+’ signs (
style=cross) coloured in red (
With the last
plot() function call we display the EMA values (
series=emaClose) in the
orange standard TradingView colour on the chart. Since we don’t set the style of this plot, it appears as a line by default (TradingView, n.d.).
Example: using data from a higher time frame in TradingView
The indicator shows the high and low from the ‘Other Time Frame’ input option, besides the EMA of closing prices from that time frame. So when added to a 5-minute Crude Oil chart, it looks like:
When a new time period (as set by the ‘Other Time Frame’ input option begins), those high and low prices are plotted in the right side of the chart as they develop:
If we change the script’s input options to an ‘Other Time Frame’ value of ‘1h’ (instead of the default ‘180’, or 3 hours) and set the ‘EMA Length’ option to 12, then the chart changes to:
Adding inputs to an indicator or strategy is done with
input(). This function returns the input option’s current value, and we can store that value in a variable for use later on in the script.
The kind of input that
input() makes is set with this function’s
type argument. When we set that argument to
resolution, the script adds a pull-down menu with several resolution values to the input settings. This way we can easily choose a resolution by hand (like 15 minutes, an hour, or a day). With resolution inputs, the
input() function returns a string with the resolution setting (like
"120" for 2 hours). The function’s
defval argument, which sets the option’s default value, uses a string value as well. The default value can, however, only be set to one of the values that are already available in the pull-down menu.
- Besides creating a resolution drop-down menu, we can also use the
input()function to add a numerical input option or a true/false checkbox to the settings of an indicator or trading strategy.
- Another time-related input option is a session time range input. That input makes it possible to manually configure a time interval (like 8:00 till 18:00 hour).
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 16, 2015, from https://www.tradingview.com/study-script-reference/