time
¶In Pine there are special means for working with trade sessions, time and date. We will review a simple chart, IBM,30 on which has been applied 2 scripts: “Bar date/time” and “Session bars”.
Here is the initial code of the first script “Bar date/time”:
study("Bar date/time")
plot(time)
This illustrates the meaning of the variable time
. The variable
time
returns the date/time (timestamp) of each bar start on the chart in UNIX
format [1].
As can be seen from the screenshot, the value time
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 (in the exchange timezone, from here the difference
between this time and UTC is 4 hours).
The second script, “Session bars”:
study("Session bars")
t = time(period, "0930-1600")
plot(na(t) ? 0 : 1)
This shows how the user can distinguish between session bars and bars
that get into extended hours by using the built-in function time
and
not the variable time
(the background behind these bars has been
colored over with grey). The function time
returns the time of the
bar start in milliseconds UNIX time or na
value if the bar is located outside
the given trade session (09:30-16:00 in our example). time
accepts
two arguments, the first is resolution
, the bars of which are needed
to determine their timestamp, and the second — session
(session specification),
which is a string that specifies the beginning and end of the trade
session (in the exchange timezone). The string “0930-1600” corresponds
to the trade session of IBM symbol. Examples of trade session
specifications:
Session specification, which is being passed to the function time
,
is not required to correspond with the real trade session of the symbol
on the chart. It’s possible to pass different “hypothetical” session
specifications which can be used to highlight some other bars of
a data series. It’s possible to transfer the different “hypothetical”
session specifications which can be used to highlight those or other
bars in a data series.
There is an overloaded function time
that allows the user to skip
custom session specification. In this case, internally, it will use a
regular session specification of a symbol. For example, it’s possible to
highlight the beginning of each half-hour bar on a minute-based chart in
the following way:
//version=3
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 function is_newbar
similar to the previous example can be used
in many situations. For example, it’s essential to display on an
intraday chart the highs and lows which began at the market’s opening:
//@version=3
study("Opening high/low", overlay=true)
highTimeFrame = input("D", type=resolution)
sessSpec = input("0930-1600", type=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)
s1 = na
s1 := newbar ? low : nz(s1[1])
s2 = na
s2 := newbar ? high : nz(s2[1])
plot(s1, style=circles, linewidth=3, color=red)
plot(s2, style=circles, linewidth=3, color=lime)
Pay attention to the variables highTimeFrame
and sessSpec
. They
have been declared in a special way with the help of the
input functions.
Pine’s standard library has an assortment of built-in variables and functions which make it possible to use time in various cases of the script 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 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. |