-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTime.pine
72 lines (65 loc) · 3.14 KB
/
Time.pine
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Electrified
// @version=5
// @description Utilities for measuring time.
library("Time")
///////////////////////////////////////////////////
// @function Gets the number of milliseconds per bar.
export bar() =>
var int msPerDay = 1000 * 60 * 60 * 24
var int m = if timeframe.isdaily
msPerDay
else if timeframe.isweekly
msPerDay * 7
else if timeframe.ismonthly
msPerDay * 30
else if timeframe.isseconds
1000
else if timeframe.isminutes
1000 * 60
var value = m * timeframe.multiplier
value
///////////////////////////////////////////////////
///////////////////////////////////////////////////
// @function Gets the start of the day for the provided time value.
// @param datetime The date/time to acquire the date from.
// @returns The number of milliseconds representing the date for the given value.
export date(int datetime = time) =>
timestamp(year(datetime), month(datetime), dayofmonth(datetime))
///////////////////////////////////////////////////
///////////////////////////////////////////////////
// @function Gets milliseconds from the start of the day.
// @param datetime The date/time to acquire the time of day from.
// @returns The number of milliseconds representing time of day.
export timeOfDay(int datetime = time) =>
datetime - date(datetime)
///////////////////////////////////////////////////
///////////////////////////////////////////////////
// @function Returns the number (float) bars that represents the timespan provided.
// @param timespan The number of units to convert to bar count.
// @param type The type of units to measure. ('Bar', 'Minute' or 'Day')
// @param sessionMinutes Optional override for the number of minutes per session. (Default: Regular = 390, Extended = 1440)
// @returns The number bars that represents the timespan provided.
export spanToLen(simple float timespan, simple string type, simple int sessionMinutes = na) =>
var minutesPerBar = timeframe.isminutes ? timeframe.multiplier : timeframe.isseconds ? timeframe.multiplier / 60 : na
var sess_minutes = na(sessionMinutes) ? syminfo.session == session.regular ? 390 : 1440 : sessionMinutes
switch type
'Bar' => timespan
'Minute' => timespan / minutesPerBar
'Day' => timeframe.isintraday ?
(sess_minutes * timespan / minutesPerBar) :
timeframe.isdaily ? timespan :
timeframe.isweekly ? timespan / 7 :
timeframe.ismonthly ? timespan / 30 :
na
=> na
///////////////////////////////////////////////////
///////////////////////////////////////////////////
// @function Returns the number (int) bars that represents the timespan provided.
// @param timespan The number of units to convert to bar count.
// @param type The type of units to measure. ('Bar', 'Minute' or 'Day')
// @returns The number bars that represents the timespan provided.
export spanToIntLen(simple float timespan, simple string type) =>
math.ceil(spanToLen(timespan, type))
///////////////////////////////////////////////////
plot(bar())