In TradingView we can colour price bars to highlight certain situations like buy and sell signals. But how do we move those coloured price bars to the left or right of their original location?

In this article:

Colouring TradingView price bars and offsetting them

When we use colours programmatically in TradingView Pine, we can choose from the basic TradingView colours as well as the hexadecimal colour values that make countless colour variations possible. And once we’ve used a colour in our script, we can also change the script’s colours by hand.

One way to use those colour values is with the color argument of barcolor(), a function that colours a price bar (Pine Script Language Tutorial, n.d.). Another argument of that function is offset, which makes shifting the coloured price bars possible: with a positive offset value, the coloured price bars move to the right (that is, to future price bars) while a negative offset relocates the coloured price bars to the left (to past historical bars).

That feature makes it possible to display the coloured price bar on a different bar than the one on which the barcolor() function executed. For instance, when the script calculates on the 50th bar but the offset argument is set to 1 the coloured price bar appears on bar number 51. And an offset of -2 makes the coloured price bar appear on the 48th bar. Let’s look at the programming example for a more practical view of this offsetting feature.

Programmatically shifting coloured price bars in TradingView Pine

In the example indicator below we create a momentum histogram that shows the price difference between two bars. The chart’s price bars are coloured whenever that momentum crosses 0. Where that bar appears, however, depends on the script’s “Bar offset” input option that we use to manually configure if, and how, the coloured bars are shifted. We’ll colour the original bar location with gold to see how the shifted price bar relates to its original location. After discussing the code we’ll look at how the script looks on charts.

study(title="Offsetting coloured price bars", overlay=false)

// Inputs
momLength  = input(title="Momentum length", type=integer, defval=12)
colourBars = input(title="Colour bars?", type=bool, defval=true)
barOffset  = input(title="Bar offset", type=integer, defval=0)

// Compute and plot momentum
momValue = close - close[momLength]

plot(series=momValue, style=histogram, linewidth=4, color=navy)
hline(price=0, linestyle=solid)

// Colour price bars
barColour = crossover(momValue, 0) ? fuchsia : 
     crossunder(momValue, 0) ? yellow :
     na

barcolor(color=colourBars ? barColour : na, 
     offset=barOffset, title="Offset bar")

barcolor(color=barOffset != 0 and 
     (crossover(momValue, 0) or crossunder(momValue, 0)) and 
     colourBars ? 
        #FFD700 : na,
     title="Signal bar", offset=0)

We begin with study() to specify the indicator’s properties. We use that function’s title argument to set the script’s name while the overlay argument, when set to false, makes the indicator appear in a separate chart pane (TradingView, n.d.).

Then we create several input options:

momLength  = input(title="Momentum length", type=integer, defval=12)
colourBars = input(title="Colour bars?", type=bool, defval=true)
barOffset  = input(title="Bar offset", type=integer, defval=0)

Input options are made with input() and this function also returns the input’s current value (TradingView, n.d.). Here each of those returned values is stored in a variable with the assignment operator (=); that way we can refer to the input’s current value later on in the script by using the variable.

The first input is a numerical integer input and we make that by setting the input() function’s type argument to integer (Pine Script Language Tutorial, n.d.). This input is given a default value (defval) of 12 and named “Momentum length” with the title argument. That name is what’s placed before the input option in the script’s settings (see image further down below). We store this input’s value in the momLength variable to use it later on when calculating the price momentum.

The next input has its type argument set to bool to create a Boolean true/false input option. We name this one “Colour bars?” and set it to true by default. The current value of that true/false checkbox is stored in the colourBars variable. We use this input later on to turn the coloured price bars on or off.

The third and last input is another integer input, this time with a default value of zero (defval=0). We name it “Bar offset” and store its value in the barOffset variable so we can use it when offsetting the coloured price bars.

The example’s next part calculates and plots the momentum:

momValue = close - close[momLength]

plot(series=momValue, style=histogram, linewidth=4, color=navy)
hline(price=0, linestyle=solid)

Here we set the momValue variable to the difference between the current bar’s close (close) minus a previous bar’s closing price. We retrieve that latter with the close built-in variable alongside the history referencing operator ([]) with the momLength input variable placed between those square brackets. That input variable has a default value of 12. In that case, momValue has a positive value when the current bar closed higher than the close of 12 bars ago, while this variable will have a negative value when the current bar’s close is less than that historical price.

Next the momentum is plotted on the chart by setting the series argument of the plot() function to the momValue variable (TradingView, n.d.). Those momentum values are displayed as a histogram (style=histogram) with the thickest plot size possible (linewidth=4), and coloured with the navy basic TradingView colour.

Then we plot a horizontal line with the hline() function (Pine Script Language Tutorial, n.d.). We place that line at the zero level (price=0) with a solid style (linestyle=solid). This way the histogram bars seems to be placed on the zero line.

Determining the colour of price bars on which a momentum crossover happens is next:

barColour = crossover(momValue, 0) ? fuchsia : 
     crossunder(momValue, 0) ? yellow :
     na

We give the barColour variable here one of three values (fuchsia, yellow, or na) with two conditional (ternary) operators (?:). That operator evaluates a condition and, when that condition is true, returns its second value while the third value is returned when the condition is false (Pine Script Language Tutorial, n.d.).

The first conditional operator checks if the momentum value (momValue) crossed over 0 with crossover(), a function that returns true when its first value crossed above the second one (TradingView, n.d.). When the current bar’s momentum is indeed above 0 while it was below that value on the previous bar, then the conditional operator returns the fuchsia basic TradingView colour and we assign that colour to the barColour variable.

However, when such a crossover didn’t happen then the second conditional operator is processed. This one uses the crossunder() function to check if the 12-bar momentum of closing prices (momValue) crossed below 0. When that occurred, the conditional operator returns yellow and that colour is subsequently stored in barColour.

But when this condition is also false, then neither a cross over nor a cross under happened. In that case the second conditional operator returns the na value. That built-in variable returns a “not a number” value (TradingView, n.d.) which, when used with colours, holds the same colour as the chart already has (Pine Script Language Tutorial, n.d.). And so na ‘turns off’ any colouring by being a transparent colour.

After determining the colour for the current bar (fuchsia, yellow, or na) we perform the colouring:

barcolor(color=colourBars ? barColour : na, 
     offset=barOffset, title="Offset bar")

Here we define several arguments of the barcolor() functions. First we set offset, which is used to shift the coloured price bars to the left or right (TradingView, n.d.), to the barOffset input variable. That way we can relocate the coloured price bars manually by simply changing the “Bar offset” input option. The title argument of the barcolor() function is set to “Offset bar” here. This way the style of these coloured bars is given a descriptive name in the script’s ‘Style’ window (see image below).

We set the color argument of this barcolor() function call with another conditional (ternary) operator. This time the operator’s condition is the colourBars input variable, which holds true or false depending on whether we’ve enabled or disabled the “Colour bars?” input option. When that input is enabled, the conditional operator returns the barColour variable here. That way the bar is either coloured in fuchsia, yellow, or given a transparent colour.

However, when the “Colour bars?” input option is disabled (in which case colourBars returns false), the conditional operator returns na and the color argument of the barcolor() is then set to that value. Since that “not a number” is then returned on every bar, this ‘turns off’ the coloured price bars conditionally: when the “Colour bars?” input option is unchecked, the script won’t colour price bars.

We end the example by using barcolor() to colour the price bar again:

barcolor(color=barOffset != 0 and 
     (crossover(momValue, 0) or crossunder(momValue, 0)) and 
     colourBars ? 
        #FFD700 : na,
     title="Signal bar", offset=0)

With this segment of code we colour price bars on which a momentum cross over or cross under occurred, provided that the original bars (which we coloured fuchsia or yellow) are shifted to the left or right. The benefit of highlighting those original cross over and cross under bars is that we can visually see how the shifted price bar relates to its original location.

To do that, we again use the conditional (ternary) operator (?:) to set the value for the color argument of this barcolor() function call. The condition that’s evaluated here by that operator consist out of three expressions. The first checks whether the barOffset input variable is unequal to (!=) 0, which happens when we’ve set that input option to a positive value (to shift the bars to the right) or a negative value (to relocate the bars to the left).

The second expression checks whether the crossover() or crossunder() functions return true when they are calculated on the momentum (momValue) and a value of 0. Since we combine those functions with the logical or operator, only one of them needs to be true before this expression is true. And so we’re either looking for a cross over or a cross under here.

The third and last expression is the colourBars input variable, which returns true when the “Colour bars?” input is enabled and returns false when that checkbox is disabled. Since all three expressions are combined with the and logical operator, all of them need to be true before the entire condition is true (Pine Script Language Tutorial, n.d.). Should one or several of them be false, then the entire condition is false too.

What we’re doing here in normal English is the following. We only colour the price bar for the second time when the “Bar offset” input has a positive or negative value, and when the momentum either crossed above 0 or crossed below that value, and when the “Colour bars?” input option is enabled. When all three situations happened the conditional operator returns the #FFD700 hexadecimal colour value of gold to the color argument of the barcolor() function. However, when one, two, or even three of these situations didn’t occur on the current bar, then the conditional operator returns na to that argument and the bar isn’t coloured.

The other arguments of barcolor() defined here are title (which we set to “Signal bar”) and offset. We hard-code that latter argument to 0 so that these golden price bars aren’t shifted.

Offsetting price bars in TradingView programmatically

When we add our above programming example to a GPB/USD chart, it looks like:

Example of colouring price bars in TradingView
Note: Even when a script displays in a subchart, the barcolor() function only colours price bars of the chart’s instrument. When we want to create coloured bars in a separate chart panel, we’ll need to use plotbar() to make coloured price bars or plotcandle() to create custom candlesticks in TradingView.

Our example script has the following input options:

Input options of the TradingView script

Now when we change that “Bar Offset” input to 1, the previous chart changes to:

Colouring price bars in TradingView Pine

Here we see that the yellow and fuchsia bars move one step to the right while on their original location a golden bar now appears. This way we can see how the shifted price bar relates to its original location.

When we set the offset input to 2, the yellow and fuchsia bars move one bar to the right while the golden bars remain in place:

Offsetting coloured price bars in TradingView

Should we change the offset to -1, the coloured price bars move one step to the left from their original location:

Example that offsets price bars in TradingView

And a bar offset of -2 changes the chart to:

Example of coloured, offsetted price bars in TradingView

We can change the colours used by the barcolor() function by editing the script’s source code, but they can also be adjusted in the script’s ‘Style’ window. That window looks like:

Changing the colours of a TradingView script manually

Now when we change the script’s colours manually to:

Changing the TradingView script's colours by hand

Then the chart looks like:

Colouring TradingView price bars with manually picked colours

Note that we also made a “Colour Bars?” input option so that we could turn the coloured price bars off. When we disable that checkbox, the previous chart changes to:

Turn off bar colouring in TradingView with an input option

See changing the colour of price bars in TradingView to learn more about the barcolor() function. Other ways to make coloured price bars is with the plotbar() function that plots coloured price bars and the plotcandle() function that creates coloured price candles. All of these functions can work with the basic TradingView colours and hexadecimal colour values.

Summary

The barcolor() function colours the price bar of the chart’s instrument. This function has several arguments, including color to set the price bar’s colour and offset that relocates the coloured bar to the left or right. Whenever we set that argument to a positive integer value, the coloured bars shift to the right (that is, to future bar locations). A negative offset value, on the other hand, moves the coloured bars to historical bars to the left of their original location.


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