Situation: You want to get a better understanding of why setting the chart’s time zone to ‘Exchange’ is important when using the TradingView built-in variables for date and time. Let’s look at a couple of examples for that.

In this article:

Example 1: Colour the first price bar of the calendar day

The example below checks if the current price bar is the first of the day and, when it is, colours that bar’s background. When the bar isn’t the first of the day, we use the na variable to make the background transparent.

With that we can expect that only the first bar each day has a coloured background. Even though the example is just a few lines, we use the same logic in more complex scripts (like trading the opening gap or tracking the daily high and low).

The code for colouring the background of the day’s first bar is:

//@version=2
study(title="Recipe - First bar of day", overlay=true)

// When the current bar's day of month differs from
// that of the previous bar, the current bar is the
// first of the day
firstBar = (dayofmonth != dayofmonth[1])

// Determine the background colour
colourOfBg = firstBar ? teal : na

// Perform the background colouring
bgcolor(color=colourOfBg, transp=90)

When we add this example to a Crude Oil CFD chart set to ‘Exchange’ time zone, the chart looks like:

Exchange time: colouring the first bar of the day

Here the script behaves like it should: the highlighted bars coincide with the date stamp on the horizontal time axis. That is, the bars that mark the 7th, 8th, 9th, and 10th day of the month all have their background colour set to teal.

Now when we change the chart’s time zone setting to UTC+1, the chart looks like this:

Colouring the day's first bar in TradingView with a wrong time zone setting

If we only consider price action, then the same price bars are still highlighted as the first bar of the day. This happens because the dayofmonth variable works with the date in the exchange’s time zone (TradingView, n.d.), and that didn’t change when we adjusted the chart’s time zone setting.

What did change was how much the time axis shifted. Now the highlighted bars don’t correspond to the first bar of the day if we look at the chart’s time axis. Instead, the day’s first bar now happens several hours after the day started. If we didn’t knew better, we’d say the script is broken. And if the script should trade based on the first bar of the day, we’d be in for a surprise with trades happening at the ‘wrong’ times.

Example 2: Highlight price bars in a specific trading session

In the second example we programmatically determine if a price bar falls in a specific trading session. When it does, we colour the chart’s background. This way the indicator colours the background of all prices happening between 9:30 and 16:00. The code for this is:

//@version=2
study(title="Recipe - Highlight a trading session", overlay=true)

// Whenever the `time()` function returns a value different than
// `na`, the current bar falls in the session. Our `inSession`
// variable is `true` then.
inSession = not na(time(period, "0930-1600")[1])

// Set the colour to orange when the bar falls inside the
// session and `na` (meaning, no colour) when it doesn't
colourOfBg = inSession ? orange : na

// Colour the chart's background
bgcolor(color=colourOfBg, transp=85)

When this example indicator is added to a AUD/CAD 30-minute chart that has its time zone set to ‘Exchange’, it looks like:

Colouring the TradingView background between 9:30 and 16:00 hour

Here we see that the script starts colouring the background orange after the 9:30 bar up until (and including) the 16:00 bar. This way the indicator correctly identifies all price action happening in that time range.

Now if we adjust the chart’s time zone from UTC-5 (the exchange time for AUD/CAD) to UTC+3, the chart changes to:

Colouring the TradingView background with a different time zone setting

Again, if we only look at price action then the same bars are highlighted. This happens because the time() function works based on the instrument’s exchange time zone (Pine Script Language Tutorial, n.d.), and the time zone in which the instrument quotes didn’t change when we made the visual chart adjustment.

But looking at the bar time stamps, the horizontal time axis now shows that the bars between 18:00 till 00:00 have an orange background. If we didn’t take into consideration which TradingView functions and variables work with ‘Exchange’ time, it would seem we have a programming error in our script or that there’s a TradingView bug somewhere. But in fact, the script works as intended.

Tip: When we use a time zone different from ‘Exchange’, our indicator or strategy still works correctly – even though it doesn’t seem like that when looking at the bar times. To prevent confusion and a lot of time spend looking for programming errors, consider always using the ‘Exchange’ time zone setting on all of your charts.
Note: We can also take advantage of the fact that TradingView scripts calculate with the instrument’s time zone instead of the chart’s time zone setting. For instance, say we colour the chart’s background of a certain time range (like in the previous example). If we then change the chart’s time zone setting, we can easily see how the exchange time corresponds to that other time zone.

See TradingView variables and functions that use ‘Exchange’ time for a list of the code elements that use the instrument’s time zone.

Learn more:


References

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

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