In TradingView we can colour the chart’s background and colour a background section. But how do we use that latter feature to highlight potential support and resistance areas?

In this article:

Colouring a part of the background in TradingView

The colours that a TradingView indicator or strategy uses can either be set manually or programmatically, with the latter allowing for things like conditional colours. When we code with colours we can choose from the basic TradingView colours as well as numerous hexadecimal colour values.

fill() can use such colour values, and this function colours part of the background that’s between two plots or two horizontal lines (Pine Script Language Tutorial, n.d.). One way to use fill() is to colour support and resistance areas with it. The reasoning behind this approach is the following. While typical support and resistance indicators (like pivots or highs and lows from previous sessions) plot at a specific price level, it’s likely that those levels instead highlight a support and resistance area. To see how we can shade such areas in TradingView, let’s look at a Camarilla pivots indicator.

Calculating Camarilla pivots in TradingView

Camarilla pivots work just like regular pivots, with the most important Camarilla pivot levels being R3 and S4 (where short positions are initiated) and R4 and S3 that trigger long signals (Bhandari, 2012). The Camarilla pivots are calculated as follows (Bhandari, 2012):

R4 = Close + (High - Low) * 1.1/2
R3 = Close + (High - Low) * 1.1/4
R2 = Close + (High - Low) * 1.1/6
R1 = Close + (High - Low) * 1.1/12
S1 = Close - (High - Low) * 1.1/12
S2 = Close - (High - Low) * 1.1/6
S3 = Close - (High - Low) * 1.1/4
S4 = Close - (High - Low) * 1.1/2

Before colouring a background area with fill(), let’s first create a basic Camarilla pivots indicator. The code for that is the following:

study(title="Camarilla Pivots - example", overlay=true)

// Get daily data
dayClose = security(tickerid, "D", close[1])
dayHigh = security(tickerid, "D", high[1])
dayLow = security(tickerid, "D", low[1])

// Calculate pivots
r4 = dayClose + (dayHigh - dayLow) * 1.1 / 2
r3 = dayClose + (dayHigh - dayLow) * 1.1 / 4
r2 = dayClose + (dayHigh - dayLow) * 1.1 / 6
r1 = dayClose + (dayHigh - dayLow) * 1.1 / 12
s1 = dayClose - (dayHigh - dayLow) * 1.1 / 12
s2 = dayClose - (dayHigh - dayLow) * 1.1 / 6
s3 = dayClose - (dayHigh - dayLow) * 1.1 / 4
s4 = dayClose - (dayHigh - dayLow) * 1.1 / 2

// Plot Camarilla pivot points
plot(series=r4, color=green, style=circles, linewidth=2)
plot(series=r3, color=red, style=circles, linewidth=2)
plot(series=r2, color=gray, style=circles, linewidth=2)
plot(series=r1, color=gray, style=circles)
plot(series=s1, color=gray, style=circles)
plot(series=s2, color=gray, style=circles, linewidth=2)
plot(series=s3, color=green, style=circles, linewidth=2)
plot(series=s4, color=red, style=circles, linewidth=2)

This indicator starts with the mandatory study() function. Then we retrieve the instrument’s daily data:

dayClose = security(tickerid, "D", close[1])
dayHigh = security(tickerid, "D", high[1])
dayLow = security(tickerid, "D", low[1])

Each of these three variables (dayClose, dayHigh, and dayLow) are assigned the values returned by security(). That function requests data from a symbol and resolution other than the one that the script is applied to (Pine Script Language Tutorial, n.d.). security() then loads that data in the background, after which it can be used in the script’s code.

We use security() with three arguments here: the first two specify the symbol and resolution to load, the third sets the kind of data to return (TradingView, n.d.). The security() function statements have their symbol set to tickerid, a built-in variable that returns the chart’s current symbol with its exchange prefix (TradingView, n.d.). The second argument is "D" in all cases, and this causes security() to load daily data. The last argument is set to close[1], high[1], or low[1]. This use of the history referencing operator ([]) makes security() return prices from yesterday (and not from the current session).

Then we calculate the pivots:

r4 = dayClose + (dayHigh - dayLow) * 1.1 / 2
r3 = dayClose + (dayHigh - dayLow) * 1.1 / 4
r2 = dayClose + (dayHigh - dayLow) * 1.1 / 6
r1 = dayClose + (dayHigh - dayLow) * 1.1 / 12
s1 = dayClose - (dayHigh - dayLow) * 1.1 / 12
s2 = dayClose - (dayHigh - dayLow) * 1.1 / 6
s3 = dayClose - (dayHigh - dayLow) * 1.1 / 4
s4 = dayClose - (dayHigh - dayLow) * 1.1 / 2

Each of these variables is assigned a Camarilla pivot value, and they are calculated with the formulas shown earlier. With the dayClose, dayHigh, and dayLow variables we get access to the daily data we retrieved earlier with security().

After that the pivot levels are plotted on the chart:

plot(series=r4, color=green, style=circles, linewidth=2)
plot(series=r3, color=red, style=circles, linewidth=2)
plot(series=r2, color=gray, style=circles, linewidth=2)
plot(series=r1, color=gray, style=circles)
plot(series=s1, color=gray, style=circles)
plot(series=s2, color=gray, style=circles, linewidth=2)
plot(series=s3, color=green, style=circles, linewidth=2)
plot(series=s4, color=red, style=circles, linewidth=2)

We graph the pivot levels on the chart with plot(), a function that displays whichever data is specified by its series argument (TradingView, n.d.). Each series argument is set to a variable that holds a pivot level, and with the color argument we set them to the green, red, or gray basic TradingView colours. These colours reflect the typical Camarilla pivots interpretation, where the R3 and S4 price levels trigger short positions and R4 and S3 are for longs (Bhandari, 2012).

The plot() function’s style argument specifies the kind of plot (TradingView, n.d.). We set this argument to circles to create small, non-connecting circles. Lastly, several plots have their linewidth argument set to 2, which makes the plot slightly thicker than normal.

Plotting the Camarilla pivots in TradingView

When we add the above example to the chart, it looks like this on a S&P 500 CFD:

Example of Camarilla Pivots in TradingView Pine

And on a GBP/USD chart the pivots plot as follows:

Plotting Camarilla Pivots on a TradingView forex chart

But as these images show, pivot price levels aren’t always touched before their area act as support or resistance. So instead of plotting exact price levels, let’s look at displaying support and resistance areas.

Shading support and resistance areas in TradingView

We can highlight support and resistance areas with fill() like this:

study(title="Camarilla Pivots - example", overlay=true)

// Input
range = input(title="Shaded area size (in ticks)", 
     type=integer, defval=10) * syminfo.mintick

// Get daily data
dayClose = security(tickerid, "D", close[1])
dayHigh = security(tickerid, "D", high[1])
dayLow = security(tickerid, "D", low[1])

// Calculate pivots
r4 = dayClose + (dayHigh - dayLow) * 1.1 / 2
r3 = dayClose + (dayHigh - dayLow) * 1.1 / 4
r2 = dayClose + (dayHigh - dayLow) * 1.1 / 6
r1 = dayClose + (dayHigh - dayLow) * 1.1 / 12
s1 = dayClose - (dayHigh - dayLow) * 1.1 / 12
s2 = dayClose - (dayHigh - dayLow) * 1.1 / 6
s3 = dayClose - (dayHigh - dayLow) * 1.1 / 4
s4 = dayClose - (dayHigh - dayLow) * 1.1 / 2

// Plot Camarilla pivot points
plot(series=r2, color=gray, style=circles)
plot(series=r1, color=gray, style=circles)
plot(series=s1, color=gray, style=circles)
plot(series=s2, color=gray, style=circles)

// Shade the R4, R3, S3, and S4 levels
fill(plot1=plot(series=r4 + range, color=green), 
     plot2=plot(series=r4 - range, color=green), 
     color=green, transp=90)
     
fill(plot1=plot(series=r3 + range, color=red),
     plot2=plot(series=r3 - range, color=red),
     color=red, transp=90)

fill(plot1=plot(series=s3 + range, color=green),
     plot2=plot(series=s3 - range, color=green),
     color=green, transp=90)
     
fill(plot1=plot(series=s4 + range, color=red),
     plot2=plot(series=s4 - range, color=red),
     color=red, transp=90)

This script is similar to the previous, but there are a few differences. The first change is the addition of an input option:

range = input(title="Shaded area size (in ticks)", 
     type=integer, defval=10) * syminfo.mintick

The input() function adds a manual input option to the script’s settings (Pine Script Language Tutorial, n.d.), and this function returns whatever value the option is currently set to (TradingView, n.d.). And so when we change this numerical input, the range input variable changes too without having to edit the script’s source code.

The input is named “Shaded area size (in ticks)”, its type argument is set to integer, and it has a default value (defval) of 10. We use this input later on in the code such that, the higher the input’s value, the wider the shaded area around the Camarilla pivot levels.

Before the value that’s returned by input() is assigned to the range variable, we multiply it with syminfo.mintick. That built-in variable returns the minimum tick value (that is, price change) for the current symbol (TradingView, n.d.). The minimum price change is 0.01 for most stocks and 0.25 for the E-mini S&P 500 future, for instance. By using this variable the number of ticks that the input is set to are translated into the instrument’s price scale.

Another difference is that now only the R2, R1, S1, and S2 pivot levels are plotted with circles:

plot(series=r2, color=gray, style=circles)
plot(series=r1, color=gray, style=circles)
plot(series=s1, color=gray, style=circles)
plot(series=s2, color=gray, style=circles)

The other Camarilla pivots (R4, R3, S3, and S4) are displayed as lines instead of circles. Those lines are made at the same time that we shade the background with fill():

fill(plot1=plot(series=r4 + range, color=green), 
     plot2=plot(series=r4 - range, color=green), 
     color=green, transp=90)
     
fill(plot1=plot(series=r3 + range, color=red),
     plot2=plot(series=r3 - range, color=red),
     color=red, transp=90)

fill(plot1=plot(series=s3 + range, color=green),
     plot2=plot(series=s3 - range, color=green),
     color=green, transp=90)
     
fill(plot1=plot(series=s4 + range, color=red),
     plot2=plot(series=s4 - range, color=red),
     color=red, transp=90)

We call the fill() function four times here. Each time we specify the first and second plot objects with plot1 and plot2, and use color to set the background’s colour to either red or green. The transparency of the filled background is set with transp, and this argument accepts values ranging from 0 (no transparency) to 100 (fully transparent). By setting that argument to 90 here we get a highly transparent background.

Each fill() statement is highly similar and does the following. First the plot1 and plot2 arguments are set to the plot() function. The series argument of these plots is set to one of the Camarilla pivot levels (which we stored in the r4, r3, s3, and s4 variables) plus or minus the range input variable. That way we set the upper and lower price of the area that should be filled with fill(). Since we don’t set the style argument of these plot() functions, the plots default to a line plot (TradingView, n.d.). And we colour these lines with the red or green basic TradingView colours.

This approach where we use the plot() function inside the parentheses of the fill() function works because the plot() function not only plots a series of data but also returns a plot object (TradingView, n.d.). And both the plot1 and plot2 arguments of the fill() function require such an object (TradingView, n.d.).

Highlighting support and resistance areas in TradingView

When we add this adjusted script to the chart, it looks as follows:

Example of shading support and resistance areas in TradingView

This indicator has the following input options:

Input options of a TradingView script

Should we change that input to a higher value like 30, the indicator looks like:

Colouring support and resistance areas in TradingView

And setting that input to 5 makes the shaded area quite small:

Colouring the background of support and resistance in TradingView

See basic TradingView colours and the list of hexadecimal colours for more on colours. The fill() function is also discussed in the colouring a part of the background and shading the background between a plot and horizontal line articles.

Summary

The fill() function colours part of the chart’s background. It does so by filling the area between two plot objects (which are returned by the plot() function) or two horizontal line objects, which are returned by the hline() function. One application of the fill() function is to highlight potential support and resistance levels instead of relying on the exact price level that’s plotted by the plot() or hline() functions.


References

Bhandari, B. (2012, October 24). Trading stocks with Camarilla pivots. Retrieved on November 20, 2015, from http://www.futuresmag.com/2012/10/24/trading-stocks-camarilla-pivots

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