When we work with TradingView indicators, the script’s settings always need to be specified by hand or with code. In this chapter we look at the mandatory study() function and how we can configure the different properties of a TradingView indicator programmatically.

In this article:

Configuring TradingView indicators programmatically

We start the chapter by discussing the study() function and its arguments. This function defines several indicator properties and needs to be added to every TradingView indicator (Pine Script Language Tutorial, n.d.). The study() function is also important because it can set several indicator properties that cannot be set manually (Pine Script Language Tutorial, n.d.; TradingView, n.d.).

The study() function has the advantage that it can hard-code several settings. That way we don’t need to configure the indicator by hand every time, and that also prevents forgetting to configure an option. However, we can still override some of the settings set by study() if we want to. That way we can configure several instances of the same indicator differently.

Setting the name of a TradingView indicator programmatically

After discussing the study() function, we look at the different arguments of this function in depth. That begins by discussing setting an indicator name programmatically, which is something that needs to be done in every indicator (TradingView, n.d.). The name given to an indicator is displayed in three locations. First, it sets the name that’s shown on the chart:

Setting an indicator name on the TradingView price chart

That name also shows up in the chart’s ‘Data Window’:

Specifying an indicator name in the 'Data Window'

And that name is also used by TradingView in the ‘Create Alert’ window:

A TradingView indicator name in the 'Create Alert' window

Overlaying an indicator on the chart’s instrument – or not

Another setting that we can specify is displaying the TradingView indicator on the chart’s instrument or not. With this setting we can have our indicator plot in the same chart area as the chart’s instrument:

Plotting a TradingView indicator on the chart's instrument

It’s also possible to programmatically define that the indicator should display in separate chart panel:

Plotting an indicator in a separate TradingView chart panel

Setting the number of decimals used by an indicator programmatically

TradingView also makes it possible to set the precision of indicator values. With this feature we specify how many decimals there should be after the floating point of the indicator’s values. For instance, we can have an indicator display with 5 decimals:

Plotting with 5 decimals in TradingView Pine

An indicator can also plot with no decimals at all. Then it also uses the ‘K’ and ’M’ suffixes for values in the thousands and millions:

Plotting large numbers in TradingView with thousands ('K') and millions ('M')

When an indicator is overlaid on the chart’s instrument, then it uses the precision of that instrument. This means TradingView won’t always plot an indicator with the number of decimals that we specified programmatically. For more on this, see why has my indicator another precision than specified in the code?.

Specifying the price scale of an indicator with code

Another feature in TradingView is programmatically setting an indicator’s price scale. With this we define in the code which of the three price scales an indicator attaches to. For instance, the indicator can plot its values on the chart’s right price axis:

Attaching a TradingView indicator to the right price scale

An indicator can also use the left price scale. That’s helpful when the indicator’s values differ from those of the instrument since it prevents distorting the instrument’s price scale. It also plot the indicator with the number of decimals that we set programmatically, instead of inheriting this from the chart’s instrument.

Attaching an indicator to the left price scale looks like:

Attaching an indicator to the left price scale in TradingView

No scaling at all is also possible. In that case an indicator doesn’t attach to any of the price scales but instead uses the full chart area to plot. This means the indicator needs to be overlaid on the chart’s instrument and we cannot read its values from the price axis then.

An example of plotting an indicator that doesn’t use a price scale is:

Plotting a TradingView indicator that isn't attached to any price scale

Setting an indicator’s number of historical bars programmatically

The last feature of the study() function is specifying an indicator’s max bars back programmatically, which sets the number of price bars that are available for the indicator to reference (TradingView, n.d.). What this means is that we can specify how many price bars an indicator needs for its historical calculations.

For example, a 30-period moving average needs at least 30 historical price bars before it can start computing its values. To explain this behaviour visually, we can create a basic indicator and then scroll all the way back to the chart’s first bars. There we see that, when the indicator references 10 historical bars, it waits until the 11th bar before plotting its values:

Example of referencing historical price bars in TradingView

And when the indicator uses price data from 15 bars ago, then it waits until the 16th bar before plotting:

Historical data needs of a TradingView indicator

How many historical bars an indicator needs is almost always determined automatically by TradingView. In the rare cases that this amount of bars cannot be determined automatically, we get the ‘out of depth at index’ error message:

Error message caused by a too low number of historical bars in TradingView

When that error happens, we’ll need to set the indicator’s number of historical bars programmatically.

We conclude the chapter with a summary of using code to specify indicator settings, in which we recap the most important features of the study() function.


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