We can programmatically work with TradingView colours in a lot of different ways including applying colours conditionally. How do we use the conditional operator for that?

In this article:

Conditional colours with TradingView’s conditional operator

We can use a range of colours in TradingView Pine, from the basic TradingView colours to any of the hexadecimal colour values. And besides using those colours programmatically we can also change the script’s colours manually.

The basic use of those colours is to apply them straightforwardly, like when plotting a blue moving average line that has the same colour on every bar. However, it’s also possible to use colours conditionally. That way we can programmatically define certain situations that, when they happen, trigger the use of a specific colour. That allows us to, for example, colour the chart’s background with different colours and even ‘turn off’ TradingView colours programmatically.

Note: A conditional colour value cannot be used with every TradingView function. The functions that allow conditional colours are barcolor(), bgcolor(), plot(), plotarrow(), plotbar(), plotcandle(), plotchar(), and plotshape(). The functions that do not allow conditional colours are fill() and hline() (TradingView, n.d.).

Coding conditional colours into a script can be done in several ways. One approach is using the conditional (ternary) operator (?:). That operator works on three values, with the first being a true/false condition. When that condition evaluates to true, the operator’s second value is returned. Is the condition false, then the third value of the conditional operator is returned (Pine Script Language Tutorial, n.d.).

A quick example of this if/else-like behaviour is:

study(title="Conditional colours with ?:", overlay=true)

plot(series=close,
     color=close > sma(close, 5) ? green : red)

Here we display closing prices on the chart with plot(). The color argument of this function is set to the value returned by the conditional operator. That operator evaluates here whether the current bar’s close (close) is greater than (>) a 5-bar Simple Moving Average of closing prices (sma(close, 5)). When that condition is true, the operator returns the green basic TradingView colour which is then used to colour the plotted line. If that condition is false, the conditional operator returns red and the line displays in that colour.

Coding alternating colours with TradingView’s conditional operator

Now, let’s look at a more complete example that uses the conditional operator for applying colours conditionally. In the example indicator below we plot three EMAs (Exponential Moving Averages) on the chart. The colour of each of these is set conditionally, depending on the market’s trend: the moving averages are made green when they align above each other, red when they are below each other, and yellow when the EMAs don’t align. We’ll also look at how the indicator displays on charts, but first the code itself:

study(title="Colouring conditionally with ?:", overlay=true)

// Inputs
priceData = input(title="Price data", type=source, defval=close)
fastMA    = input(title="Fast MA", type=integer, defval=10)
medMA     = input(title="Medium MA", type=integer, defval=23)
slowMA    = input(title="Slow MA", type=integer, defval=48)

// Calculate values
emaFast = ema(priceData, fastMA)
emaMed  = ema(priceData, medMA)
emaSlow = ema(priceData, slowMA)

// Determine colour
lineColour = (emaFast > emaMed) and (emaMed > emaSlow) ? green :
     (emaFast < emaMed) and (emaMed < emaSlow) ? red :
     yellow

// Plot moving averages
plot(series=emaFast, linewidth=2, style=line, color=lineColour)
plot(series=emaMed, linewidth=2, style=line, color=lineColour)
plot(series=emaSlow, linewidth=2, style=line, color=lineColour)

We begin with the study() function that’s mandatory for every indicator (TradingView, n.d.). We set this function’s title argument to name the indicator and with the overlay argument set to true the script overlays on the chart’s instrument (TradingView, n.d.).

Then we make several input options:

priceData = input(title="Price data", type=source, defval=close)
fastMA    = input(title="Fast MA", type=integer, defval=10)
medMA     = input(title="Medium MA", type=integer, defval=23)
slowMA    = input(title="Slow MA", type=integer, defval=48)

These manual input options are made with the input() function and appear in the script’s ‘Inputs’ window. Besides creating the input option, input() also returns the input’s current value (TradingView, n.d.). With the assignment operator (=) we store each of those values in a variable here: that way we can use the input’s value later on in the script by using the variable.

These four input() function calls have several arguments in common. The first is title, which specifies the name placed before the input option in the script’s settings (Pine Script Language Tutorial, n.d.). The other two are type (which specifies the kind of input option to make) and defval that sets the begin value of the input option (TradingView, n.d.).

The first input is a ‘source’ price data input (type=source). That input option creates a pull-down menu with values related to the chart’s price data, like its high, low, or close prices (Pine Script Language Tutorial, n.d.). We store the values returned by input() in the priceData variable which, with the default value being set to close, initially holds the instrument’s closing prices.

The other three inputs are all numerical integer inputs, made by setting the type argument to integer (Pine Script Language Tutorial, n.d.). The first of these is named “Fast MA” with a default value of 10 and its current value stored in the fastMA variable. The second and third integer inputs are named “Medium MA” and “Slow MA”. These have default values of 23 and 48 respectively, and we track their values with the medMA and slowMA variables.

Next we calculate the moving averages:

emaFast = ema(priceData, fastMA)
emaMed  = ema(priceData, medMA)
emaSlow = ema(priceData, slowMA)

The EMAs are calculated with the ema() function. That function requires two arguments: a series of values to process and an integer that specifies the number of bars to calculate on (TradingView, n.d.). We set the first argument of each of these ema() function calls to the priceData input variable. That way, whenever we change that input option, the EMA values change with it.

The first calculated EMA has its length set with the fastMA input variable that holds a value of 10 by default. We assign this EMA value to the emaFast variable. The other two EMAs are by default calculated on 23 and 48 bars by using the medMA and slowMA input variables. We store the values of those moving averages in the emaMed and emaSlow variables.

Then we determine in which colour to plot the EMAs:

lineColour = (emaFast > emaMed) and (emaMed > emaSlow) ? green :
     (emaFast < emaMed) and (emaMed < emaSlow) ? red :
     yellow

We use two conditional (ternary) operators (?:) here to figure out which colour to store in the lineColour variable. For that we evaluate up to two conditions, each consisting out of two expressions that are joined together with the and logical operator. That operator only returns true when both the expression on its left and the expression on its right are true also. And and returns false when one or both of the expressions that it combines are false too (Pine Script Language Tutorial, n.d.).

The condition of the first conditional operator is whether the fast EMA (emaFast) is greater than (>) the middle moving average (emaMed) and whether that middle EMA is above the slow one (emaMed > emaSlow). This condition is true whenever the moving averages align above each other. In that case the conditional operator returns the green basic TradingView colour which is then assigned to the lineColour variable.

But when that condition is false, then TradingView Pine moves on to the second conditional operator. That one checks whether the fast EMA is less than the middle moving average (emaFast < emaMed) and if the middle moving average is aligned below the slowest one (emaMed < emaSlow). When both expressions are true, then this second conditional operator returns red. But when this condition also isn’t true, then the moving averages aren’t aligned above nor aligned below. In that case yellow is returned by the conditional operator.

Now that the colour of the EMA lines is determined, we plot the moving averages on the chart:

plot(series=emaFast, linewidth=2, style=line, color=lineColour)
plot(series=emaMed, linewidth=2, style=line, color=lineColour)
plot(series=emaSlow, linewidth=2, style=line, color=lineColour)

That plotting is done with plot(), a function that displays a series of data on the chart (TradingView, n.d.). With this function’s series argument we specify the values to plot, and here we set that argument to the emaFast, emaMed, and emaSlow variables which hold the EMAs with a length of 10, 23, and 48 bars.

The other arguments of these three plot() statements are identical. Each has its style argument set to line to plot a line on the chart. Their color argument is set to the lineColour variable that we’ve just assigned green, red, or yellow. And the linewidth argument, which sets the plot size ranging from 1 (default size) to 4 (TradingView, n.d.), is set to 2 here so that each plotted line is a bit bigger than default.

Conditionally colouring moving averages in TradingView

When we add the above example to the German DAX CFD, it looks like:

TradingView example script added to the chart

The input options of the script are:

Example of the TradingView script's input options

When we change these inputs to, for example, ‘Price Data’ based on HL2 values (a bar’s midpoint) and the moving averages’ length to 5, 15, and 30; then the previous chart changes to:

Settings of the TradingView example script changed

Other ways in which we can use colours conditionally are with specifying colours with the iff() function and using if/else statements to set TradingView colours. The colours that we can use with this are the basic TradingView colours and the hexadecimal colour values, and it’s also possible to ‘turn off’ TradingView colours conditionally.

Summary

TradingView colours can be applied straight-forwardly by using the same colour on every bar. But colours can also be set programmatically. One way to do that is with the conditional (ternary) operator (?:). That operator requires three values: a true/false condition and two values that are returned depending on that condition. Should the condition evaluate to true, then the second value is returned; when the condition is false, the third value is returned. By acting as a concise if/else statement, the conditional operator can evaluate several conditions and return a colour value that accompanies that condition.


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