Tradingview: Create an Indicator

This post also forms part of the getting started series for Tradingview. In this tutorial, we shall follow similar steps to the Tradingview: First Script the difference this time is that we shall create an indicator instead of a strategy.

If you do not know how to open the pine script editor, I suggest reading the start of the first script post mentioned above. It will guide you through opening a chart and accessing the editor.

The Indicator

Before we start getting our hands dirty, talk a little about the indicator we are going to create. In this post we will be creating a dual timeframe RSI indicator. The indicator will plot RSI values from a 2 timeframes. The first plotted line will be the current timeframe, the second will be a user selected timeframe. The theory is that if the instrument is overbought / oversold on two timeframes at the same time, it might be more likely to reverse or at least revert to the mean. This indicator would be most suitable for ranging market conditions.

The basic topics covered in this tutorial are:

  • Understanding the study function
  • Adding indicator inputs
  • Importing data from another timeframe
  • Calling an RSI function
  • Plotting both horizontal and RSI lines

Ok, so lets create an indicator.

The Code

First of all the complete code is below. Code commentary then follows to give further information on what each section is doing.

Code Commentary

Lets start from the top with the study() function.

The study function must be included in every script. Tradingview refers to this type of function as an “annotation function“. As the name suggests, annotation functions annotate things that appear on the chart. For example, assigning a title to the indicator on the chart. The example above just gives the indicator a long and short title.

Inputs

Next we get to the inputs section. Inputs give your indicator parameters that can be changed after adding the indicator to the chart. They give other users the ability to change the indicator settings if they do not like the defaults. In addition to this, users can also save their own configuration for any indicator you make. There is nothing you need to code. The functionality is baked into the platform!

  • defval = default value for the parameter
  • title = The text that appears on the input box
  • type = the type of value that shall be input. Notice that one of the types is set to resolution. This is a special variable that will provide a list of all supported timeframes.

Below we can see an example of what these inputs look like below.

Create an indicator with inputs

Import other data

Now we move onto importing data. This line uses a function called security() to bring data into the script. The data imported could be from a different timeframe on the same instrument, the same timeframe on a different instrument or a different timeframe and instrument altogether.

  • tickerid = Is another special variable that points back to the same instrument. This allows us to get another timeframe from the same instrument easily. More importantly, since we do not write a specific instrument name (E.g LON:VOD), this indicator can now be applied to any instrument without changing the code.
  • otf = Is the variable returned from the input function we added above.
  • rsi(close, otf_period) = Returns an RSI series for the time frame selected. Here you could simply write close and you would have access to the close value of each bar for that timeframe.

Plotting

Finally we plot all the lines on the chart. The function hline() plots the horizontal lines. The function plot() is plotting the RSI series values. I think the keyword arguments are fairly self explanatory.

The Result

To see the resulting indicator

  1. Open Tradingview (Use this affiliate link if you want to be kind – http://tradingview.go2cloud.org/SHpB)
  2. Past the script into the pine editor.
  3. Press the “Add to Chart” button

Example showing where to click

You should then see something that looks like this:

Create An Indicator: Dual RSI Result

And that is all there is to it! Now we now how to create an indicator.

Moving on from here, try to have a play around with importing data from different sources, try mixing indicators, play around with the plotting functions and see what interesting combinations you can come up with.