Session and time information
The “time” function and variable
Pine provides means to work with trade session, time and date information. On this IBM chart at 30 minutes, two scripts are running: “Bar date/time” and “Session bars”.
This is the “Bar date/time” script:
The
time
variable returns the date/time (timestamp) of each bar’s opening time
in UNIX format1 and in the
exchange’s timezone. As can be seen from the screenshot, the time
value on the last bar is equal to 1397593800000. This value is the
number of milliseconds that have passed since 00:00:00 UTC, 1 January,
1970 and corresponds to Tuesday, 15th of April, 2014 at 20:30:00 UTC.
The chart’s time gauge in the screenshot shows the time of the last bar
as 2014-04-15 16:30 because it has a 4-hour difference between the
exchange’s timezone, which is the default time returned by the time
function.
The second script is “Session bars”:
This shows how the user can distinguish between regular session and
extended hours bars by using the built-in
time
function rather than the time
variable. Note that the background
behind these bars is colored because of the chart’s settings; not
because of the script. The time
function returns the time of the
bar’s start in milliseconds UNIX time, or na
if the bar is located
outside the given trade session (09:30—16:00 in our example). The
time
function accepts two arguments: the resolution
used to
determine the timestamp of bars and session
, the session specification
in the form of a string containing the beginning and end of the trade
session in the exchange’s timezone. The string “0930-1600”
corresponds to the trade session of the IBM symbol. These are examples
of trade session specifications:
0000-0000
0900-1600,1700-2000
2000-1630:1234567
0930-1700:146
24x7
0000-0000:1234567
0000-0000:23456
1700-1700:23456
1000-1001:26
Session specification used for the time
function’s second argument
does not need to correspond to the symbol’s real trade session.
Hypothetical session specifications can be used to highlight other bars
of a data series.
Pine provides an overloaded version of the time
function which does
not require custom session specification. This version of the function
uses the regular session of a symbol. For example, it is possible to
highlight the beginning of each half-hour bar on a minute chart in the
following way:
The previous example’s is_newbar
custom function can be used in many
situations. Here, we use it to display the market’s opening high and
low on an intraday chart:
Pay attention to the variables highTimeFrame
and sessSpec
. They are
defined using the
input
function and its type
parameter to make their type explicit.
Built-in variables for working with time
Pine’s standard library has an assortment of built-in variables and functions which make it possible to use time in the script’s logic.
The most basic variables:
- time - UNIX time of the current bar start in milliseconds, UTC timezone.
- timenow - Current UNIX time in milliseconds, UTC timezone.
- syminfo.timezone - Exchange timezone of the chart main symbol series.
Variables that give information about the current bar start time:
- year - Current bar year.
- month - Current bar month.
- weekofyear - Week number of current bar.
- dayofmonth - Date of current bar.
- dayofweek - Day of week for current bar. You can use
sunday
,monday
,tuesday
,wednesday
,thursday
,friday
andsaturday
variables for comparisons. - hour - Hour of the current bar start time (in exchange timezone).
- minute - Minute of the current bar start time (in exchange timezone).
- second - Second of the current bar start time (in exchange timezone).
Functions for UNIX time “construction”:
- year(t) - Returns year for provided UTC time
t
. - month(t) - Returns month for provided UTC time
t
. - weekofyear(t) - Returns week of year for provided UTC time
t
. - dayofmonth(t) - Returns day of month for provided UTC time
t
. - dayofweek(t) - Returns day of week for provided UTC time
t
. - hour(t) - Returns hour for provided UTC time
t
. - minute(t) - Returns minute for provided UTC time
t
. - second(t) - Returns second for provided UTC time
t
. - timestamp(year, month, day, hour, minute)
-Returns UNIX time of specified date and time. Note, there is
also an overloaded version with an additional
timezone
parameter.
All these variables and functions return time in the exchange time
zone, except for the time
and timenow
variables which return time
in UTC timezone.
Footnotes
-
UNIX time is measured in seconds. Pine Script uses UNIX time multiplied by 1000, so it’s in millisecods. ↩