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:
//@version=4
study("Bar date/time")
plot(time)
The time
variable returns the date/time (timestamp) of each bar’s opening time in UNIX
format [1] 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”:
//@version=4
study("Session bars")
t = time(timeframe.period, "0930-1600")
plot(na(t) ? 0 : 1)
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:
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:
//@version=4
study("new 30 min bar")
is_newbar(res) =>
t = time(res)
not na(t) and (na(t[1]) or t > t[1])
plot(is_newbar("30") ? 1 : 0)
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:
//@version=4
study("Opening high/low", overlay=true)
highTimeFrame = input("D", type=input.resolution)
sessSpec = input("0930-1600", type=input.session)
is_newbar(res, sess) =>
t = time(res, sess)
na(t[1]) and not na(t) or t[1] < t
newbar = is_newbar("1440", sessSpec)
var float s1 = na
var float s2 = na
if newbar
s1 := low
s2 := high
plot(s1, style=plot.style_circles, linewidth=3, color=color.red)
plot(s2, style=plot.style_circles, linewidth=3, color=color.lime)
Pay attention to the variables highTimeFrame
and sessSpec
. They
are defined using the input function
and its type
parameter to make their type explicit.
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:
Variables that give information about the current bar start time:
sunday
, monday
, tuesday
, wednesday
, thursday
, friday
and saturday
variables for comparisons.Functions for UNIX time “construction”:
t
.t
.t
.t
.t
.t
.t
.t
.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
[1] | UNIX time is measured in seconds. Pine Script uses UNIX time multiplied by 1000, so it’s in millisecods. |