-
Notifications
You must be signed in to change notification settings - Fork 3
/
US Federal Holidays.py
140 lines (97 loc) · 4.77 KB
/
US Federal Holidays.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: filetype=python
# In[1]:
#this script computes cme holidays
#based upon federal holidays for the next two years
#the trading calendar is crucial to the vix calculator
# https://github.com/je-suis-tm/quant-trading/blob/master/VIX%20Calculator.py
#if you just want the current year holiday calendar
# https://www.cmegroup.com/tools-information/holiday-calendar.html
import datetime as dt
import pandas as pd
import random as rd
import time
import requests
import os
os.chdir('d:/')
# In[2]:
#scraping function
def scrape(url):
session=requests.Session()
session.headers.update(
{'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'})
time.sleep(rd.randint(0,10))
response=session.get(url,verify=False)
return response
# In[3]:
#get exchange holidays
def get_cme_holidays():
weekdays=['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
federal_holidays=["New Year's Day",
'M L King Day',
"Presidents' Day",
'Good Friday',
'Memorial Day',
'Thanksgiving Day',
'Christmas']
currentyear=dt.datetime.now().year
allholidays=pd.DataFrame(columns=['DAY', 'DATE', 'HOLIDAY'])
#get this year plus the next two years
for year in range(currentyear,currentyear+3):
url=f'https://www.calendarlabs.com/holidays/us/{year}'
response=scrape(url)
response.raise_for_status()
#get tables from html
dataframes=pd.read_html(response.text)
#cleansing
holidays=dataframes[1]
holidays['DATE']=holidays['DATE'].apply(lambda x:x[:-6])
holidays['DAY']=holidays['DAY'].apply(lambda x:x[-3:])
#datetime conversion
holidays['DATE']=pd.to_datetime(holidays['DATE'])
#only select federal holiday + good friday
#cuz july the 4th and labor day is at the beginning of the month
#all monthly options expire at the end of the month
cme_holidays=holidays[holidays['HOLIDAY'].isin(federal_holidays)]
cme_holidays.reset_index(inplace=True,drop=True)
#create cme holidays based upon +-1 day on the official holiday
for i in cme_holidays.index:
temp=pd.DataFrame(columns=cme_holidays.columns)
if cme_holidays.at[i,'DAY']=='Mon':
temp['DAY']=['Fri','Tue']
temp['DATE']=[cme_holidays.at[i,'DATE']-dt.timedelta(days=3),
cme_holidays.at[i,'DATE']+dt.timedelta(days=1)]
temp['HOLIDAY']=[cme_holidays.at[i,'HOLIDAY']]*2
elif cme_holidays.at[i,'DAY']=='Fri':
temp['DAY']=['Thu','Mon']
temp['DATE']=[cme_holidays.at[i,'DATE']-dt.timedelta(days=1),
cme_holidays.at[i,'DATE']+dt.timedelta(days=3)]
temp['HOLIDAY']=[cme_holidays.at[i,'HOLIDAY']]*2
elif cme_holidays.at[i,'DAY']=='Sat':
temp['DAY']=['Fri','Mon']
temp['DATE']=[cme_holidays.at[i,'DATE']-dt.timedelta(days=1),
cme_holidays.at[i,'DATE']+dt.timedelta(days=2)]
temp['HOLIDAY']=[cme_holidays.at[i,'HOLIDAY']]*2
elif cme_holidays.at[i,'DAY']=='Sun':
temp['DAY']=['Fri','Mon']
temp['DATE']=[cme_holidays.at[i,'DATE']-dt.timedelta(days=2),
cme_holidays.at[i,'DATE']+dt.timedelta(days=1)]
temp['HOLIDAY']=[cme_holidays.at[i,'HOLIDAY']]*2
else:
temp['DAY']=[weekdays[dt.datetime.weekday(cme_holidays.at[i,'DATE'])-1],
weekdays[dt.datetime.weekday(cme_holidays.at[i,'DATE'])+1]]
temp['DATE']=[cme_holidays.at[i,'DATE']-dt.timedelta(days=1),
cme_holidays.at[i,'DATE']+dt.timedelta(days=1)]
temp['HOLIDAY']=[cme_holidays.at[i,'HOLIDAY']]*2
cme_holidays=cme_holidays.append(temp)
cme_holidays.reset_index(inplace=True,drop=True)
allholidays=allholidays.append(cme_holidays)
allholidays.reset_index(inplace=True,drop=True)
return allholidays
# In[4]:
def main():
allholidays=get_cme_holidays()
allholidays.to_csv('cme holidays.csv',index=False)
if __name__ == "__main__":
main()