-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSessionInfo.pine
309 lines (278 loc) · 10.9 KB
/
SessionInfo.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Electrified (electrifiedtrading)
// @version=5
// @description Utility functions for session specific information like the bar index of the session.
library('SessionInfo', false)
// Note: The following are already provided by PineScript 5.
// session.ismarket https://www.tradingview.com/pine-script-reference/v5/#var_session{dot}ismarket
// session.ispremarket https://www.tradingview.com/pine-script-reference/v5/#var_session{dot}ispremarket
// session.ispostmarket https://www.tradingview.com/pine-script-reference/v5/#var_session{dot}ispostmarket
///////////////////////////////////////////////////
// Session Info ///////////////////////////////////
///////////////////////////////////////////////////
///////////////////////////////////////////////////
// @function Returns true if the current bar is in the session specification.
// @param spec session.regular (default), session.extended or other time spec.
// @returns True if the current is in session; otherwise false.
export isIn(
simple string spec = session.regular,
simple string res = timeframe.period) =>
not na(time(res, spec))
///////////////////////////////////////////////////
///////////////////////////////////////////////////
// @function Returns true if the bar is before before the session (default = regular) opens.
// @param spec session.regular (default), session.extended or other time spec.
// @param res The resolution (default = timeframe.period).
// @returns True if before the session.
export isBefore(
simple string spec = session.regular,
simple string res = timeframe.period) =>
if not timeframe.isintraday
false
else
var y = year
var m = month
var d = dayofmonth
var bool start = false
if y != year or m != month or d != dayofmonth
y := year
m := month
d := dayofmonth
start := true
if isIn(spec, res)
start := false
false
else
start
///////////////////////////////////////////////////
///////////////////////////////////////////////////
// @function Returns true if the bar is before before the session (default = regular) opens.
// @param spec session.regular (default), session.extended or other time spec.
// @param res The resolution (default = timeframe.period).
// @returns True if before the session.
export isAfter(
simple string spec = session.regular,
simple string res = timeframe.period) =>
if not timeframe.isintraday
false
else
var y = year
var m = month
var d = dayofmonth
var int phase = na
if y != year or m != month or d != dayofmonth
y := year
m := month
d := dayofmonth
phase := 0
if isIn(spec, res)
phase := 1
false
else
phase == 1
///////////////////////////////////////////////////
///////////////////////////////////////////////////
// @function Converts the number of minutes to a length to be used with indicators.
// @param minutes The number of minutes.
// @param multiple The length multiplier.
// @returns math.ceil(minutes * multiple / timeframe.multiplier)
export minutesToLen(
simple int minutes,
simple float multiple = 1) =>
var minutesPerBar = timeframe.isminutes ? timeframe.multiplier : timeframe.isseconds ? timeframe.multiplier / 60 : na
math.ceil(minutes * multiple / minutesPerBar)
///////////////////////////////////////////////////
///////////////////////////////////////////////////
// @function Returns the intraday bar index. May not always map directly to time as a bars can be skipped.
// @param spec session.regular (default), session.extended or other time spec.
// @param res The resolution (default = '1440').
// @returns The integer index of the bar of the session.
export bar(
simple string spec = session.regular,
simple string res = '1440') =>
var int notYet = na
if timeframe.isdaily
0
else if not timeframe.isintraday
notYet
else
var barCount = -1
barCount += 1
var y = year
var m = month
var d = dayofmonth
var int firstBar = na
t = time(res, spec)
if na(t) or y != year or m != month or d != dayofmonth
y := year
m := month
d := dayofmonth
firstBar := na(t) ? na : barCount
else if na(firstBar) and (na(t[1]) and not na(t) or t[1] < t)
firstBar := barCount
barCount - firstBar
///////////////////////////////////////////////////
///////////////////////////////////////////////////
// @function Returns the number bars in the past to find the first bar of the session of a given day.
// @param daysPrior The number of days in the past to count the bars.
// @param spec session.regular (default), session.extended or other time spec.
// @param res The resolution (default = '1440').
// @returns The number bars in the past to find the first bar of the session of a given day.
export firstBarOfDay(
simple int daysPrior = 0,
simple string spec = session.regular,
simple string res = '1440') =>
var int notYet = na
if daysPrior < 0
runtime.error('firstBarOfDay(daysPrior) must be (>=0) at least zero.')
notYet
else if timeframe.isdaily
0
else if not timeframe.isintraday
notYet
else
var days = daysPrior + 1
var dayRecord = array.new_int()
var dayCount = 0
var barCount = -1
barCount += 1
var y = year
var m = month
var d = dayofmonth
var int firstBar = na
t = time(res, spec)
if y != year or m != month or d != dayofmonth
y := year
m := month
d := dayofmonth
if not na(t)
array.push(dayRecord, barCount)
dayCount += 1
if dayCount > days
array.shift(dayRecord)
dayCount -= 1
else if na(t[1]) and not na(t) or t[1] < t
array.push(dayRecord, barCount)
dayCount += 1
if dayCount > days
array.shift(dayRecord)
dayCount -= 1
dayCount == 0 ? notYet : barCount - array.get(dayRecord, 0)
///////////////////////////////////////////////////
///////////////////////////////////////////////////
// @function Returns true if the current bar is the first one of the session.
// @param spec session.regular (default), session.extended or other time spec.
// @param res The resolution (default = '1440').
// @returns True if the current bar is the first one of the session.
export isFirstBar(
simple string spec = session.regular,
simple string res = '1440') => bar(spec, res) == 0
///////////////////////////////////////////////////
///////////////////////////////////////////////////
// @function Returns Returns true if the previous bar was the last of the session.
// @param spec session.regular (default), session.extended or other time spec.
// @param res The resolution (default = '1440').
// @returns True if was the last bar of the session.
export wasLastBar(
simple string spec = session.regular,
simple string res = '1440') =>
var bool notYet = na
if timeframe.isdaily
true
else if not timeframe.isintraday
notYet
else
var int b = na
bb = bar(spec, res)
wasLast = bb < b or na(bb) and not na(b)
b := bb
wasLast
///////////////////////////////////////////////////
//////////////////////////////////////////////////
// @function Returns maximum (usual) number of bars per day.
// @param spec session.regular (default), session.extended or other time spec.
// @param res The resolution (default = '1440').
// @returns The max (usual) number of bars per day.
export maxBars(
simple string spec = session.regular,
simple string res = '1440') =>
var int notYet = na
if timeframe.isdaily
1
else if not timeframe.isintraday
notYet
else
var int max = na
var int b = 0
var int count = 0
b += 1
if wasLastBar(spec, res)
count += 1
if na(max) or b > max
max := b
if isFirstBar(spec, res)
b := 0
count > 3 ? max : na
///////////////////////////////////////////////////
//////////////////////////////////////////////////
// @function Returns maximum (usual) number of minutes per day.
// @param spec session.regular (default), session.extended or other time spec.
// @param res The resolution (default = '1440').
// @returns The max (usual) number of minutes per day.
export maxMinutes(
simple string spec = session.regular,
simple string res = '1440') =>
maxBars(spec, res) * timeframe.multiplier
///////////////////////////////////////////////////
//////////////////////////////////////////////////
// @function Returns the number of bars equal to the number of days based upon usual session length.
// @param spec session.regular (default), session.extended or other time spec.
// @param res The resolution (default = '1440').
// @returns The number of bars representing the number of days.
export daysToBars(
simple int days,
simple string spec = session.regular,
simple string res = '1440') =>
var int notYet = na
if timeframe.isdaily
days
else if not timeframe.isintraday
notYet
else
days * maxBars(spec, res)
///////////////////////////////////////////////////
///////////////////////////////////////////////////
// @function Returns the number bars in the past to find the last bar of the session of a given day.
// @param daysPrior The number of days in the past to count the bars.
// @param spec session.regular (default), session.extended or other time spec.
// @param res The resolution (default = '1440').
// @returns The number bars in the past to find the last bar of the session of a given day.
export lastBarOfDay(
simple int daysPrior = 1,
simple string spec = session.regular,
simple string res = '1440') =>
var int notYet = na
if daysPrior < 1
runtime.error('lastBarOfDay(daysPrior) must be (>=1) at least one.')
notYet
else if timeframe.isdaily
daysPrior
else if not timeframe.isintraday
notYet
else
var dayRecord = array.new_int()
var dayCount = 0
var barCount = -1
barCount += 1
if wasLastBar(spec, res)
array.push(dayRecord, barCount - 1)
dayCount += 1
if dayCount > daysPrior
array.shift(dayRecord)
dayCount -= 1
dayCount == 0 ? notYet : barCount - array.get(dayRecord, 0)
///////////////////////////////////////////////////
bos = bar()
plot(bos, 'Bar of the Standard Session', color=color.green, style=plot.style_stepline)
bose = bar(session.extended)
plot(bose, 'Bar of the Extended Session', style=plot.style_stepline)