Situation: You’re using a built-in variable or function for time and date in your TradingView indicator or strategy, and are wondering in which time zone the returned values are.

In this article:

Discussion: time zones used by TradingView functions and variables

When we use times and dates in our code, especially on a global platform like TradingView, we inevitably run into the issue of working with different time zones.

Besides the time zone of our chart (see how to change a chart’s time zone?), we also have to pay attention to which time zone functions and variables use. That way when our script behaves different from what we expect, we can troubleshoot if this is used by a time zone setting or not.

Now the standard TradingView variables and functions work with one of two time zones: the instrument’s exchange time zone or the UTC (Coordinated Universal Time) time zone (TradingView, n.d.).

Functions and variables that use the instrument’s time zone

To standardise TradingView script behaviour, almost all built-in variables and functions for time and date return their values in the same time zone as the instrument (Pine Script Language Tutorial, n.d.; TradingView, n.d.).

That means most built-in variables and functions don’t look at our price chart’s time zone, but instead return values in the time zone of our chart’s instrument. When that time zone differs from the one used on the chart, our script can exhibit some odd behaviour (see how does the chart’s time zone setting affect script behaviour?).

The following functions and variables work with the same time zone as the chart’s instrument (Pine Script Language Tutorial, n.d.; TradingView, n.d.; Vitvlkv, 2015):

Code element Description
dayofmonth Returns the date (as in, day of month) of the current bar time in the instrument’s exchange time zone.
dayofmonth() Returns, for the provided UNIX time, the day of the month in the instrument’s exchange time zone.
dayofweek Returns the day of week for the current bar in exchange time zone of the instrument.
dayofweek() Returns, for the provided UNIX time, the day of week in exchange time zone.
minute Returns the number of minutes of the current bar in the instrument’s exchange time zone.
minute() Returns the number of minutes for the provided UNIX time in the instrument’s exchange time zone.
month Returns the month number of the current bar in the instrument’s exchange time zone.
month() Returns the month number for the provided UNIX time in the instrument’s exchange time zone.
second Returns the number of seconds of the current bar in the instrument’s exchange time zone.
second() Returns the number of seconds for the provided UNIX time in the instrument’s exchange time zone.
time() Returns, for a provided resolution and session, the UNIX time of the current bar if that bar falls in that specified session. When the current bar doesn’t fall in that session, time() returns a NaN value.
weekofyear Returns the week number of the current bar in the instrument’s exchange time zone.
weekofyear() Returns the week number of the year for the provided UNIX time in the instrument’s exchange time zone.
year Returns the year for the current bar in the instrument’s exchange time zone.
year() Returns the year (in the instrument’s exchange time zone) for the provided UNIX time.

Since these functions and variables return time in the instrument’s ‘Exchange’ time zone, it’s often best to set the chart’s time zone to ‘Exchange’ too. That way we prevent differences between what our script calculates with and how the indicator or strategy appears on the chart.

For instance, say we use the time() function to see if a price bar falls within a specific session. When we don’t have the chart’s time zone set to ‘Exchange’, the bars of which the script says they belong to the specified session are not the same bars that fall in the time range that we see on the chart’s time axis. (See how a chart’s time zone setting affects scripts for more.)

Note: With nearly all TradingView built-in functions and variables using the instrument’s time zone, our chart’s time zone only has a visual effect: behind the scenes, our indicators and strategies use the ‘Exchange’ time zone – which isn’t necessarily the same time that we see on the chart.
Tip: By always keeping the chart’s time zone set to ‘Exchange’, we prevent confusion and the impression of programming bugs due to differences between the time zone used by the instrument and the chart itself.

Functions and variables that use UTC time zone

Currently, there are two built-in variables that don’t use the instrument’s time zone but return their values in UTC instead (Pine Script Language Tutorial, n.d.; TradingView, n.d.):

Code element Description
time Returns the current bar’s closing time in UTC time zone.
timenow Returns the current time in UTC time zone.

Because both time and timenow use a different time zone than the other TradingView functions and variables for working with time, we cannot compare them directly with each other. We can, of course, use time and timenow to see how the current time relates to a bar’s time.

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

Vitvlkv (2015, October 5). ‘time()’ only works with exchange time chart? Forum discussion. Retrieved on May 14, 2017, from https://getsatisfaction.com/tradingview/topics/-time-only-works-with-exchange-time-chart#reply_16186241