To configure TradingView indicators with code we use the indicator()
function. This function has several arguments to set various settings. But what indicator settings are there?
We can categorise the indicator settings in two groups:
- Settings unique to indicators.
- Settings that are also available with strategies.
The shared settings we explored earlier in the script settings chapter. So let’s look at the unique settings first.
Unique indicator settings
The following indicator settings aren’t available to other scripts [1] :
timeframe
- Defines the time frame (resolution) that the script runs on. This calculates the entire script on a different time frame than what the chart currently uses. If we want an indicator to process daily data while we chart 1-hour bars, this setting is how we do that.
timeframe_gaps
- Says how data from the higher time frame is merged with the current chart. We can either show gaps or repeat the previous values to get a continuous series of data.
Those gaps are a normal consequence of using another time frame’s data, because not every bar on the chart will match a higher time frame bar. (If we plot 1-hour data on a 15-minute chart, there’s only one hourly data point for every four bars.)
Settings shared with strategies
Indicators also share several settings with strategy scripts.
The common settings work the same for indicators as they do for strategies. If you know how a setting works with strategies, you’ll also know how that setting works with indicators.
Those shared settings are [1] :
title
(gives the script’s its display name).shorttitle
(specifies the script’s alternative, abbreviated name).overlay
(makes the script overlay on the chart’s instrument).format
(sets how plotted numbers appear on the chart’s price scale).precision
(specifies the number of decimals of the script’s plotted values).scale
(says which price scale the script uses for its plotted values).max_bars_back
(defines the minimum number of bars the script needs for its calculations).explicit_plot_zorder
(makes the script control how elements are overlaid on top of each other).max_lines_count
(specifies the maximum number of trend lines the script can make).max_labels_count
(sets the maximum number of labels the script can draw).max_boxes_count
(defines the maximum number of boxes the script can place on the chart).linktoseries
(makes the script always appear on the chart’s instrument).
Summary
- Indicators have two kind of settings: unique settings and those shared with strategies.
- If we know how a particular strategy setting works, then we also know how the same indicator setting works.
- We don’t need to remember all indicator settings. There are recommended indicator settings and some we’ll almost never use.
TradingView’s
indicator()
function configures an indicator’s settings through code.With
indicator()
we override the default settings of TradingView indicators. And we use the function to configure features that don’t have a manual option in TradingView’s user interface.Code examples of
indicator()
The
indicator()
function has several arguments. Each one configures a certain setting or behaviour. But there’s only one argument required:title
. That option sets the name of the script.With only that required argument, the minimal use of
indicator()
looks like this:indicator(title="My Bollinger Bands")
We can configure a lot more options than just the script’s name. With the other optional settings we configure our indicator in detail.
We might, for instance, want to overlay the script on the chart’s instrument. And plot indicator values with 2 decimals. In that case we use
indicator()
like so:indicator(title="My Bollinger Bands", overlay=true, precision=2)
Note that keyword arguments aren’t necessary with
indicator()
. It’s just more convenient to use them. But we can also pass arguments by position toindicator()
. For example:indicator("My Bollinger Bands", "MBB", true)
All options of the
indicator()
functionThese are all the arguments we can use with TradingView’s
indicator()
function:indicator(title, shorttitle, overlay, format, precision, scale, max_bars_back, linktoseries, max_lines_count, max_labels_count, resolution, resolution_gaps)
There are quite some indicator settings! But we don’t need to remember all of them. In fact, some we’ll practically never use. See recommended indicator settings for the most important settings to remember.
This is what the different
indicator()
arguments mean:Argument Description title
(required)Specifies the name of the indicator. shorttitle
Provides an abbreviated script name. A shorter name prevents clutter on the chart. overlay
Defines where the script appears: overlaid on top of the chart’s instrument or in a separate chart panel. format
Sets the format of plotted values. The indicator’s values can show as prices, volume figures, percentages, or with the same style as the chart’s instrument. precision
Says how many decimal digits the indicator’s plotted values use. scale
Specifies which price scale the script uses: the right price axis, the left one, or no price scale. max_bars_back
Defines the required number of historical bars. TradingView auto-detects this, but in very rare situations we’ll need to help our indicator. linktoseries
Makes the indicator connected to the chart’s instrument. That has the script use the same chart area and price scale settings as the instrument does. max_lines_count
Defines the maximum number of trend lines our indicator can draw. max_labels_count
Says how many text labels the indicator can place on the chart. resolution
Sets the custom resolution that the indicator calculates on. This way a script calculates on a different time frame than the chart uses. resolution_gaps
Specifies how data from the higher time frame gets combined with bars on the current chart. It’s a good idea to configure as many options with code as possible. This saves time; else we’ll have to configure the indicator by hand each time we add it to the chart.
There’s also another benefit. Some options have no manual setting. So using code is the only way to change those features.
Features of
indicator()
- Every indicator (study) needs to use the
indicator()
function. It’s not possible to make an indicator without usingindicator()
. - There’s only one required argument with
indicator()
:title
to name the indicator. All other arguments are optional. - An indicator can only use the
indicator()
function once. When we have severalindicator()
calls in our code, we have to combine their arguments into one function call. - When we include
indicator()
in our script, we tell TradingView that our source code is an indicator (study) script. If we would include thestrategy()
function instead, then TradingView treats our code as a trading strategy. - An implicit Pine Script rule is to place the
indicator()
function directly below the script’s version statement (//@version=n
). For example:
//@version=4 indicator(title="My Bollinger Bands", overlay=true)
- Every indicator (study) needs to use the