In TradingView Pine we can programmatically define how many decimals the indicator uses with its values. But how do we configure where, instead of how, the indicator displays its price axis values?

In this article:

Specifying a TradingView indicator’s price scale with code

In TradingView we set indicator properties, like its name and number of decimals, with the study() function (Pine Script Language Tutorial, n.d.). This function has to be in the code of every indicator (Pine Script Language Tutorial, n.d.), and its title argument (which sets the indicator’s name) is required too (TradingView, n.d.).

Another study() argument is scale. This argument specifies which price scale the indicator should attach to (TradingView, n.d.). This is an optional argument: when we don’t specify it, the indicator attaches to the right price scale by default.

An example of the scale argument of the study() function is:

study(title="My example script", scale=scale.left)

This scale argument can be set to one of the following values (TradingView, n.d.):

Value Meaning
scale.right Sets the indicator to use the chart’s right price scale. This is also the standard setting: when the scale argument isn’t specified, it defaults to scale.right.
scale.left Makes the indicator use the left price scale. This is helpful when plotting more than one indicator in the same chart panel, or when overlaying the indicator on the chart’s instrument.
scale.none Doesn’t attach the indicator to any of the chart’s price scales. Since that makes the indicator’s values not show up on the left or right price axis, the indicator displays on the full chart area. This also means that the indicator’s values cannot be read from the price scale and that sometimes part of the indicator isn’t visible.
Tip: The scale.none value of the scale argument only has an effect when the indicator is overlaid on the chart’s instrument (that is, overlay=true) (TradingView, n.d.). The following error message is generated otherwise:
Price scale error message in TradingView
Note: If we change the setting of the scale argument when an indicator is already added to the chart, we need to remove the script from the chart and re-add it before we see the effect of the updated code.

The difference between TradingView’s scaling options

Let’s look at how the different price scales affect how an indicator looks. On the charts below we’ve added the standard Bollinger Bands indicator and the volume histogram to a Bund (German 10-year bonds) CFD intra-day chart. The standard volume histogram uses the left axis for its values while the Bollinger Bands indicator scales to the price axis:

Example of attaching a TradingView indicator to the right price scale

Now when the Bollinger Bands scale to the left axis, they plot as follows:

Example of attaching a TradingView indicator to the left price scale

The indicator now looks like a horizontal line because the chart’s left scale, that’s already used by the volume histogram, has strongly different values than the Bollinger Bands.

When the Bollinger Bands indicator is set to a ‘none’ scaling, it doesn’t attach to any price scale but instead uses the chart’s full visible area:

Example of plotting a TradingView indicator without attaching to a price scale

While the Bollinger Bands look the same as they did with right scaling, the Euro Bund price action is more visible since the right price scale doesn’t need to accommodate the Bollinger Bands values anymore.

Programmatically setting the price scale of a TradingView indicator

In the programming example below we use the scale argument of the study() function to set the indicator’s price scale. This script plots a basic RSI (Relative Strength Index) indicator that’s overlaid on the chart’s instrument. After discussing the code, we look at how the indicator plots and how changing the scale argument alters the indicator’s appearance.

study(title="RSI Example", overlay=true, scale=scale.none)

// Inputs
rsiLen  = input(title="RSI Length", type=integer, defval=14)
obLevel = input(title="Overbought Level", type=integer, defval=70)
osLevel = input(title="Oversold Level", type=integer, defval=30)

// Compute values
rsiValue = rsi(close, rsiLen)

// Plot values
plot(series=rsiValue, color=fuchsia)

hline(price=obLevel, color=#F4A460)
hline(price=osLevel, color=#F4A460)

We start by defining the indicator’s properties with the study() function. The indicator’s name is set to “RSI Example” with the title argument. With the overlay argument set to true the indicator displays on the chart’s instrument (TradingView, n.d.). And the scale argument set to scale.none the script doesn’t attach to the left nor right price scale.

Then we add several input options to the script:

rsiLen  = input(title="RSI Length", type=integer, defval=14)
obLevel = input(title="Overbought Level", type=integer, defval=70)
osLevel = input(title="Oversold Level", type=integer, defval=30)

Manual input options are added to a script with input(), a function that also returns the input’s value. Here each of those values is stored in a variable with the assignment operator (=). That way we can access the input’s current value later on in the script by using the variable.

All three input() functions have their type argument set to integer. That creates a numerical input option that accepts whole numbers only (Pine Script Language Tutorial, n.d.). The first of these is named “RSI Length” with the title argument and is given a default value (defval) of 14. We store this input’s value in the rsiLen variable, and use that variable later on when computing the RSI.

The other two inputs are named “Overbought Level” and “Oversold Level”, and these start with values of 70 and 30. We store the current setting of these inputs in the obLevel and osLevel variables, and use those later when plotting the horizontal overbought and oversold lines. The benefit of creating inputs for these lines is that we can easily change where the lines plot by changing the inputs.

Next we calculate the RSI:

rsiValue = rsi(close, rsiLen)

We retrieve the value of the Relative Strength Index with the rsi() function. This function works with two arguments: a series of values to process and an integer that sets the RSI length in number of bars (TradingView, n.d.). Here we have the function compute on the instrument’s closing prices (close) with the length set by rsiLen, our input variable that has a default value of 14. We assign the value returned by rsi() to the rsiValue variable for use later on in the script.

Then we plot the indicator’s values:

plot(series=rsiValue, color=fuchsia)

hline(price=obLevel, color=#F4A460)
hline(price=osLevel, color=#F4A460)

The plot() function displays the data of its series argument on the chart (TradingView, n.d.). Here that argument is set to rsiValue, the variable that we’ve just set to the 14-bar RSI values. plot() makes lines by default (TradingView, n.d.), and here that line is coloured with the fuchsia basic TradingView colour.

The last two statements in our example use hline(), a function that renders a horizontal line at a fixed price level (TradingView, n.d.). With the price argument we set those lines to the obLevel and osLevel input variables, which we gave standard values of 70 and 30 earlier. Both lines are coloured with #F4A460, the hexadecimal colour value of sandy brown.

Example: plotting a TradingView indicator without a price scale

Our above example indicator creates the following input options:

Input options of our TradingView example indicator

When we add the script to a chart, like a GBP/USD forex chart, the RSI looks like:

Example of our TradingView indicator

Here we see that, by setting the scale argument to scale.none, the indicator makes full use of the chart area to plot its values. But reading the indicator’s values is hard now since the indicator isn’t attached to a price scale.

We can see that more clearly when we enable the chart’s left axis. For that we right-click on the right price axis and select ‘Left Axis’:

Enabling the left price axis in TradingView

That displays the left axis, making in our case two price scales showing the EUR/GBP values:

Plotting a TradingView indicator with both price scales enabled

When the indicator’s values should show up on the price axis, our example indicator needs to attach to the left price scale instead of using the scale=scale.none argument of the study() function. Let’s see how.

Setting an indicator to left scaling programmatically

To have our indicator use the left price axis, we change the script’s first line to:

study(title="RSI Example", overlay=true, scale=scale.left)

We don’t make other changes but instead just save the indicator. To see the effect of the updated code, we first need to remove the indicator from the chart and then re-add it. Its values then appear on the chart’s left axis:

Attaching a TradingView indicator to the left price scale

For specifying other indicator properties with code, see setting an indicator’s overlaid settings and specifying an indicator’s precision with code. For more about the study() function and its arguments, see setting indicator properties programmatically.

Summary

The study() function sets several indicator properties, and this function needs to be added to every indicator’s code. It’s required title argument sets the indicator’s name whereas the optional scale argument specifies the price scale that the indicator should attach to. We can set this scale argument to scale.right (to use the chart’s right price axis; default), scale.left (to use the left price scale), or scale.none. This latter, which only works when the overlay argument of the study() function is set to true, makes the indicator independent from the price scales and makes it use the full chart area to plot its values.


References

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

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