-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathcalendar.py
324 lines (298 loc) · 11.7 KB
/
calendar.py
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import datetime
from zoneinfo import ZoneInfo
NY_TZ = ZoneInfo("America/New_York")
UTC_TZ = ZoneInfo("UTC")
EQUITY_OPEN = datetime.time(9, 30, 0, tzinfo=NY_TZ)
EQUITY_CLOSE = datetime.time(16, 0, 0, tzinfo=NY_TZ)
NYSE_EARLY_CLOSE = datetime.time(13, 0, 0, tzinfo=NY_TZ)
# NYSE_HOLIDAYS and NYSE_EARLY_HOLIDAYS will need to be updated each year
# From https://www.nyse.com/markets/hours-calendars
NYSE_HOLIDAYS = [
datetime.datetime(2023, 1, 2, tzinfo=NY_TZ).date(),
datetime.datetime(2023, 1, 16, tzinfo=NY_TZ).date(),
datetime.datetime(2023, 2, 20, tzinfo=NY_TZ).date(),
datetime.datetime(2023, 4, 7, tzinfo=NY_TZ).date(),
datetime.datetime(2023, 5, 29, tzinfo=NY_TZ).date(),
datetime.datetime(2023, 6, 19, tzinfo=NY_TZ).date(),
datetime.datetime(2023, 7, 4, tzinfo=NY_TZ).date(),
datetime.datetime(2022, 9, 4, tzinfo=NY_TZ).date(),
datetime.datetime(2023, 11, 23, tzinfo=NY_TZ).date(),
datetime.datetime(2023, 12, 25, tzinfo=NY_TZ).date(),
datetime.datetime(2024, 1, 1, tzinfo=NY_TZ).date(),
datetime.datetime(2024, 1, 15, tzinfo=NY_TZ).date(),
datetime.datetime(2024, 2, 19, tzinfo=NY_TZ).date(),
datetime.datetime(2024, 3, 29, tzinfo=NY_TZ).date(),
datetime.datetime(2024, 5, 27, tzinfo=NY_TZ).date(),
datetime.datetime(2024, 6, 19, tzinfo=NY_TZ).date(),
datetime.datetime(2024, 7, 4, tzinfo=NY_TZ).date(),
datetime.datetime(2024, 9, 2, tzinfo=NY_TZ).date(),
datetime.datetime(2024, 11, 28, tzinfo=NY_TZ).date(),
datetime.datetime(2024, 12, 25, tzinfo=NY_TZ).date(),
]
NYSE_EARLY_HOLIDAYS = [
datetime.datetime(2023, 7, 3, tzinfo=NY_TZ).date(),
datetime.datetime(2023, 11, 24, tzinfo=NY_TZ).date(),
datetime.datetime(2024, 7, 3, tzinfo=NY_TZ).date(),
datetime.datetime(2024, 11, 29, tzinfo=NY_TZ).date(),
datetime.datetime(2024, 12, 24, tzinfo=NY_TZ).date(),
]
FX_METAL_OPEN_CLOSE_TIME = datetime.time(17, 0, 0, tzinfo=NY_TZ)
# FX_METAL_HOLIDAYS will need to be updated each year
# From https://www.cboe.com/about/hours/fx/
FX_METAL_HOLIDAYS = [
datetime.datetime(2023, 1, 1, tzinfo=NY_TZ).date(),
datetime.datetime(2023, 12, 25, tzinfo=NY_TZ).date(),
datetime.datetime(2024, 1, 1, tzinfo=NY_TZ).date(),
datetime.datetime(2024, 12, 25, tzinfo=NY_TZ).date(),
]
RATES_OPEN = datetime.time(8, 0, 0, tzinfo=NY_TZ)
RATES_CLOSE = datetime.time(17, 0, 0, tzinfo=NY_TZ)
def is_market_open(asset_type: str, dt: datetime.datetime) -> bool:
# make sure time is in NY timezone
dt = dt.astimezone(NY_TZ)
day, date, time = dt.weekday(), dt.date(), dt.time()
if asset_type == "equity":
if date in NYSE_HOLIDAYS or date in NYSE_EARLY_HOLIDAYS:
if (
date in NYSE_EARLY_HOLIDAYS
and time >= EQUITY_OPEN
and time < NYSE_EARLY_CLOSE
):
return True
return False
if day < 5 and time >= EQUITY_OPEN and time < EQUITY_CLOSE:
return True
return False
if asset_type in ["fx", "metal"]:
if date in FX_METAL_HOLIDAYS and time < FX_METAL_OPEN_CLOSE_TIME:
return False
# If the next day is a holiday, the market is closed at 5pm ET
if (
date + datetime.timedelta(days=1) in FX_METAL_HOLIDAYS
) and time >= FX_METAL_OPEN_CLOSE_TIME:
return False
# On Friday the market is closed after 5pm
if day == 4 and time >= FX_METAL_OPEN_CLOSE_TIME:
return False
# On Saturday the market is closed all the time
if day == 5:
return False
# On Sunday the market is closed before 5pm
if day == 6 and time < FX_METAL_OPEN_CLOSE_TIME:
return False
return True
if asset_type == "rates":
if date in NYSE_HOLIDAYS or date in NYSE_EARLY_HOLIDAYS:
if (
date in NYSE_EARLY_HOLIDAYS
and time >= RATES_OPEN
and time < NYSE_EARLY_CLOSE
):
return True
return False
if day < 5 and time >= RATES_OPEN and time < RATES_CLOSE:
return True
return False
# all other markets (crypto)
return True
def get_next_market_open(asset_type: str, dt: datetime.datetime) -> int:
# make sure time is in NY timezone
dt = dt.astimezone(NY_TZ)
time = dt.time()
if asset_type == "equity":
if time < EQUITY_OPEN:
next_market_open = dt.replace(
hour=EQUITY_OPEN.hour,
minute=EQUITY_OPEN.minute,
second=0,
microsecond=0,
)
else:
next_market_open = dt.replace(
hour=EQUITY_OPEN.hour,
minute=EQUITY_OPEN.minute,
second=0,
microsecond=0,
)
next_market_open += datetime.timedelta(days=1)
elif asset_type in ["fx", "metal"]:
if (dt.weekday() == 6 and time < FX_METAL_OPEN_CLOSE_TIME) or (
dt.date() in FX_METAL_HOLIDAYS and time < FX_METAL_OPEN_CLOSE_TIME
):
next_market_open = dt.replace(
hour=FX_METAL_OPEN_CLOSE_TIME.hour,
minute=FX_METAL_OPEN_CLOSE_TIME.minute,
second=0,
microsecond=0,
)
else:
next_market_open = dt.replace(
hour=FX_METAL_OPEN_CLOSE_TIME.hour,
minute=FX_METAL_OPEN_CLOSE_TIME.minute,
second=0,
microsecond=0,
)
while is_market_open(asset_type, next_market_open):
next_market_open += datetime.timedelta(days=1)
elif asset_type == "rates":
if time < RATES_OPEN:
next_market_open = dt.replace(
hour=RATES_OPEN.hour,
minute=RATES_OPEN.minute,
second=0,
microsecond=0,
)
else:
next_market_open = dt.replace(
hour=RATES_OPEN.hour,
minute=RATES_OPEN.minute,
second=0,
microsecond=0,
)
next_market_open += datetime.timedelta(days=1)
else:
return None
while not is_market_open(asset_type, next_market_open):
next_market_open += datetime.timedelta(days=1)
return int(next_market_open.timestamp())
def get_next_market_close(asset_type: str, dt: datetime.datetime) -> int:
# make sure time is in NY timezone
dt = dt.astimezone(NY_TZ)
time = dt.time()
if asset_type == "equity":
if dt.date() in NYSE_EARLY_HOLIDAYS:
if time < NYSE_EARLY_CLOSE:
next_market_close = dt.replace(
hour=NYSE_EARLY_CLOSE.hour,
minute=NYSE_EARLY_CLOSE.minute,
second=0,
microsecond=0,
)
else:
next_market_close = dt.replace(
hour=EQUITY_CLOSE.hour,
minute=EQUITY_CLOSE.minute,
second=0,
microsecond=0,
)
next_market_close += datetime.timedelta(days=1)
elif dt.date() in NYSE_HOLIDAYS:
next_market_open = get_next_market_open(asset_type, dt)
next_market_open_date = (
datetime.datetime.fromtimestamp(next_market_open)
.astimezone(NY_TZ)
.date()
)
if next_market_open_date in NYSE_EARLY_HOLIDAYS:
next_market_close = (
datetime.datetime.fromtimestamp(next_market_open)
.astimezone(NY_TZ)
.replace(
hour=NYSE_EARLY_CLOSE.hour,
minute=NYSE_EARLY_CLOSE.minute,
second=0,
microsecond=0,
)
)
else:
next_market_close = (
datetime.datetime.fromtimestamp(next_market_open)
.astimezone(NY_TZ)
.replace(
hour=EQUITY_CLOSE.hour,
minute=EQUITY_CLOSE.minute,
second=0,
microsecond=0,
)
)
else:
next_market_close = dt.replace(
hour=EQUITY_CLOSE.hour,
minute=EQUITY_CLOSE.minute,
second=0,
microsecond=0,
)
if time >= EQUITY_CLOSE:
next_market_close += datetime.timedelta(days=1)
# while next_market_close.date() is in NYSE_HOLIDAYS or weekend, add 1 day
while (
next_market_close.date() in NYSE_HOLIDAYS
or next_market_close.weekday() >= 5
):
next_market_close += datetime.timedelta(days=1)
elif asset_type in ["fx", "metal"]:
next_market_close = dt.replace(
hour=FX_METAL_OPEN_CLOSE_TIME.hour,
minute=FX_METAL_OPEN_CLOSE_TIME.minute,
second=0,
microsecond=0,
)
if dt.weekday() != 4:
while not is_market_open(asset_type, next_market_close):
next_market_close += datetime.timedelta(days=1)
while is_market_open(asset_type, next_market_close):
next_market_close += datetime.timedelta(days=1)
elif asset_type == "rates":
if dt.date() in NYSE_EARLY_HOLIDAYS:
if time < NYSE_EARLY_CLOSE:
next_market_close = dt.replace(
hour=NYSE_EARLY_CLOSE.hour,
minute=NYSE_EARLY_CLOSE.minute,
second=0,
microsecond=0,
)
else:
next_market_close = dt.replace(
hour=RATES_CLOSE.hour,
minute=RATES_CLOSE.minute,
second=0,
microsecond=0,
)
next_market_close += datetime.timedelta(days=1)
elif dt.date() in NYSE_HOLIDAYS:
next_market_open = get_next_market_open(asset_type, dt)
next_market_open_date = (
datetime.datetime.fromtimestamp(next_market_open)
.astimezone(NY_TZ)
.date()
)
if next_market_open_date in NYSE_EARLY_HOLIDAYS:
next_market_close = (
datetime.datetime.fromtimestamp(next_market_open)
.astimezone(NY_TZ)
.replace(
hour=NYSE_EARLY_CLOSE.hour,
minute=NYSE_EARLY_CLOSE.minute,
second=0,
microsecond=0,
)
)
else:
next_market_close = (
datetime.datetime.fromtimestamp(next_market_open)
.astimezone(NY_TZ)
.replace(
hour=RATES_CLOSE.hour,
minute=RATES_CLOSE.minute,
second=0,
microsecond=0,
)
)
else:
next_market_close = dt.replace(
hour=RATES_CLOSE.hour,
minute=RATES_CLOSE.minute,
second=0,
microsecond=0,
)
if time >= RATES_CLOSE:
next_market_close += datetime.timedelta(days=1)
# while next_market_close.date() is in NYSE_HOLIDAYS or weekend, add 1 day
while (
next_market_close.date() in NYSE_HOLIDAYS
or next_market_close.weekday() >= 5
):
next_market_close += datetime.timedelta(days=1)
else: # crypto markets never close
return None
return int(next_market_close.timestamp())