We can colour a background section in TradingView and use that feature to colour the background between circles and crosses and fill the background around histograms and columns. But how do we fill the background alongside an area plot?

In this article:

Colouring a background area along a TradingView area plot

Besides configuring colours manually, we can also code the basic TradingView colours and hexadecimal colour values directly into our scripts. When we do, things like applying colours conditionally become possible too.

While we can colour the bar’s full background with bgcolor(), the fill() function is more precise given that it colours a specific background area (Pine Script Language Tutorial, n.d.). To know which area to fill in, this function either requires two plot objects (that the plot() function returns) or two horizontal line objects, which are returned by the hline() function (TradingView, n.d.). These objects represent an instance of the plot or horizontal line, alike to how a string variable represents a certain text. For more on the fill() function, see colouring part of the chart’s background.

The plot() function can display data with different plot styles (TradingView, n.d.). But regardless of the kind of plots or how we combine them, fill() can colour the background between them. And so we can fill the background between circles and crosses and colour the background of histogram and columns plots. Another plot option is an area plot, which displays a line and fills the area between the line’s value and 0. That plot style looks like this:

Example of an area plot in TradingView

With the background between the area plot’s line and 0 already filled, we don’t need the fill() function for that. But we can fill the background section that’s above the plot’s value. Let’s look at an example of that.

Filling the background areas above a TradingView area plot

In the example below we display a double-smoothed RSI (Relative Strength Index) value with an area plot, followed by making two horizontal lines placed at a random signal and overbought level. After discussing the code, we’ll look at charts that show the indicator and its settings.

study(title="Colouring and filling an area", 
     shorttitle="Area example")

// Inputs
srcData      = input(title="Data", type=source, defval=close)
rsiLength    = input(title="RSI Length", type=integer, defval=7)
smoothLength = input(title="Smoothing Length", type=integer, 
     defval=3, minval=2)

// Calculate values
rsiValue  = rsi(srcData, rsiLength)
smoothRSI = sma(sma(rsiValue, smoothLength), smoothLength)

// Plot values
areaPlot = plot(series=smoothRSI, color=blue, 
     style=area, transp=40, title="Area plot")

// Plot the lines
lineColour = true ? na : blue

lineOB = plot(series=80, color=lineColour, 
     style=line, title="Overbought line")
lineSignal = plot(series=60, color=lineColour, 
     style=line, title="Signal line")

// Fill the background areas
fill(plot1=lineOB, plot2=lineSignal, 
     color=orange, transp=40, title="Overbought area")
fill(plot1=lineSignal, plot2=areaPlot, 
     color=yellow, transp=30, title="Signal area")

We begin with the mandatory study() function. With its title and shorttitle arguments we specify the script’s name in TradingView (TradingView, n.d.).

After that we add three input options to the script:

srcData      = input(title="Data", type=source, defval=close)
rsiLength    = input(title="RSI Length", type=integer, defval=7)
smoothLength = input(title="Smoothing Length", type=integer, 
     defval=3, minval=2)

We make these inputs with input(), a function that not only adds a manual option to the script’s settings but also returns that option’s value (TradingView, n.d.). By assigning that value to a variable, we can use the variable later on to refer to the input’s current setting. That way manually changing the input setting also changes the variable’s value in the script’s code.

The first input that we make is a ‘source’ input by setting the type argument to source (Pine Script Language Tutorial, n.d.). This input type creates a pull-down menu for selecting price data series from the instrument that the script is added to (like its high, low, or open prices). The title argument, which sets the text that’s placed before the input option, is to “Data”. We set the input’s default value (defval) to closing prices (close) and store its current value in the srcData variable. So when we change this input manually to high prices, then srcData doesn’t contain close prices but the bar’s high prices.

The two other inputs have their type argument set to integer and that makes numerical integer inputs. The first one, which is named “RSI Length”, has a default value (defval) of 7 and has its value stored in the rsiLength variable. “Smoothing Length” is the title of the third and last input, and this option has a default value of 3. We’ll use this input later on to double-smooth the RSI values. The minval argument, which defines an input’s minimum value (TradingView, n.d.), is set to 2. And so the value of the smoothLength variable is always 2 or more.

Then we compute the values that we’re going to plot later on:

rsiValue  = rsi(srcData, rsiLength)
smoothRSI = sma(sma(rsiValue, smoothLength), smoothLength)

The RSI is calculated here with rsi(), a function that can work with a series of values and an integer that specifies the RSI length as the number of bars (TradingView, n.d.). We set its data values to calculate on to srcData, our input variable that holds the instrument’s closing prices by default. The RSI length is set to the rsiLength input variable that starts off with a value of 7. The computed RSI value that’s returned by rsi() is assigned with = to the rsiValue variable.

We then double-smooth the RSI value. We do that with the sma() function, which calculates a Simple Moving Average (SMA) with two arguments: a series of values and the number of bars to calculate on (TradingView, n.d.). That sma() function is used twice here. The first, innermost sma() function call computes the SMA on the rsiValues for the smoothLength period. This computes, when the inputs are set to their default values, the 3-bar average of the 7-bar RSI value of closing prices.

The second sma() function call uses the value returned by the first to perform the double smoothing. And so we compute the second SMA on the value that’s returned by sma(rsiValue, smoothLength) and set the length of this second average to the smoothLength variable. The value returned by the outer sma() function is assigned to the smoothRSI variable.

Then we display that variable’s values as an area plot:

areaPlot = plot(series=smoothRSI, color=blue, 
     style=area, transp=40, title="Area plot")

The plot() function displays whichever data is specified by its series argument (TradingView, n.d.). Here we set that argument to our smoothed RSI values and, to make the plot display as an area plot, set the style argument to area. The plot’s colour is set to the blue basic TradingView colour and its transp argument, which sets the transparency with values from 0 (no transparency) to 100 for full transparency (TradingView, n.d.), is given a value of 40 for a light-transparent plot.

We also set the plot() function’s title argument. This argument sets the plot’s name, and this name is shown in the script’s ‘Style’ window (Pine Script Language Tutorial, n.d.). We use that window to manually configure the appearance of plots (see image further down below). Giving a plot a descriptive name is helpful since that makes finding a specific plot to configure easier. After specifying the plot() arguments, we store the plot object returned by that function in the areaPlot variable with the assignment operator (=).

Then we make two horizontal lines:

lineOB = plot(series=80, color=lineColour, 
     style=line, title="Overbought line")
lineSignal = plot(series=60, color=lineColour, 
     style=line, title="Signal line")

The plotColour variable that we make here is given a value with the conditional ternary operator (?:). This operator evaluates a condition and, when that condition is found true, returns the operator’s second value. Should the condition evaluate to false, the operator’s third value is returned (Pine Script Language Tutorial, n.d.).

The condition that we evaluate here it a little bit odd: it’s simply the true Boolean value. That condition is, of course, always true. And so na is always assigned to the plotColour variable. That na value, a built-in variable that represent a “not a number” value (TradingView, n.d.), gives a plot the same colour as the chart’s background (Pine Script Language Tutorial, n.d.), which in effect makes it transparent. However, we currently cannot assign the na variable to the color argument of the plot() function directly. And so we first put it into a variable series with the conditional operator and then use that variable with the plot() function. In other words, that artificial true condition with the conditional operator is just to store na value in a variable for use with plot() later.

We then use plot() to plot a line (style=line) at the fixed value of 80 (series=80). That horizontal line has its color argument set to the plotColour variable (and this makes the line invisible). We name the plot “Overbought line” and store its plot object in the lineOB variable. The next plot() statement also draws a line and also has its colour set to lineColour, but plots at 60 (series=60) and is named “Signal line”. Its plot object, returned by plot(), is assigned to the lineSignal variable.

Note: We plot these horizontal lines with plot() and not with the hline() function (which plots horizontal lines). That’s because fill() can only colour the background that’s between two plot objects or two horizontal line objects (TradingView, n.d.). Since the smoothed RSI value is already plotted with plot(), the horizontal lines also need to be made with plot(). Otherwise we cannot fill the background between the RSI and a horizontal line. See colouring the background between a plot and horizontal line for more.

Next up is colouring a background section:

fill(plot1=lineOB, plot2=lineSignal, 
     color=orange, transp=40, title="Overbought area")
fill(plot1=lineSignal, plot2=areaPlot, 
     color=yellow, transp=30, title="Signal area")

The fill() function is used twice here, both times with the plot1 and plot2 arguments to specify the first and second plot object that the background should be coloured between (TradingView, n.d.). In the first fill() statement we fill the background between the lineOB and lineSignal plots, which are the overbought and signal horizontal lines. This filled area is set to the orange basic TradingView colour and its transparency (transp) is set to 40. We name this section “Overbought” to find it more easily in the script’s ‘Style’ window.

The second fill() function call colours the area between the horizontal signal line (plot1=lineSignal) and the smoothed RSI area plot (plot2=areaPlot). This chart area is made yellow with a slight transparency (transp=30), and we call this background section “Signal area”.

Example: filling the TradingView background around an area plot

When we add the above example indicator to a chart, it looks like this on a EUR/USD chart:

Example of colouring the area around an area plot in TradingView

And the smoothed RSI looks like this on a S&P 500 index CFD chart:

Colouring the background of a TradingView chart

Since we’ve used the title argument to name each plot and filled background, changing the colours manually in the ‘Style’ window is easy due to the descriptive names:

Example of the TradingView script's Style settings

The input options that we made with input() look like:

Example of the input options of the TradingView script

If we set the ‘Data’ option to ‘high’, change the RSI length to 10, and apply a smoothing of 7, then the previous shown S&P 500 chart changes to the following:

Programmatically colouring a background in TradingView

Other uses of the fill() function are shading the background of support and resistance areas and filling the background between circles and crosses. When we colour part of the chart’s background, the colours that we can use are the basic TradingView colours besides any of the hexadecimal colour values.

Summary

The fill() function colours a part of the chart’s background. It does so by filling the area that’s between two plot objects (which are returned by the plot() function) or the chart’s portion that’s between two horizontal line objects (that the hline() function returns). fill() can colour the background between any plot style, including an area plot. Since the fill() function cannot fill the area between a plot and a horizontal line, we need to use plot() with a fixed value to make a horizontal line. Then fill() can colour the background that’s between the plot and horizontal plot.


References

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

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