-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathoptions_dates.py
283 lines (240 loc) · 7.34 KB
/
options_dates.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
"""
Option Date Utilities
=====================
These are a collection of functions for determining
when the current options cycle expires (3rd Friday of most months)
and for calculating historical option expiration dates.
.. tip:: If you need to automate looking up the current option
cycle expiration, then please checkout using the script:
::
/opt/sa/analysis_engine/scripts/print_next_expiration_date.py
2018-10-19
"""
import datetime
import pandas.tseries.offsets as pd_bday
import analysis_engine.holidays as hdays
import spylunking.log.setup_logging as log_utils
log = log_utils.build_colorized_logger(name=__name__)
def get_options_for_years(
years=[
'2014',
'2015',
'2016',
'2016',
'2017',
'2018',
'2019',
'2020',
'2021',
'2022',
]):
"""get_options_for_years
:param years: number of years back
:param months: number of months to build year
"""
entities = []
months = [
'01',
'02',
'03',
'04',
'05',
'06',
'07',
'08',
'09',
'10',
'11',
'12'
]
option_expirations = {
}
opts = []
for year in years:
for month in months:
target_date_str = str(month + '-01-' + year)
target_date = datetime.datetime.strptime(
target_date_str,
'%m-%d-%Y')
option_expiration_date = option_expiration(
target_date)
option_expiration_str = option_expiration_date.strftime(
'%m-%d-%Y')
option_expirations[option_expiration_str] = option_expiration_date
opts.append(option_expiration_date.strftime(
'%m-%d-%Y'))
# end of for all months
# end of building option expiration dates
now = datetime.datetime.now() + datetime.timedelta(days=0)
num_legs = 20
num_done = 1
last_exp_date = 0
date_format = '%m-%d-%Y'
str_output = now.strftime(date_format)
log.info(f'current date={str_output}')
entities.append(str_output)
for option_exp in opts:
option_exp_date = datetime.datetime.strptime(
option_exp,
date_format)
if option_exp_date >= now:
if num_legs > 0:
if (last_exp_date == 0):
delta = (option_exp_date - now).days
else:
delta = (option_exp_date - last_exp_date).days
entities.append(str(delta))
entities.append(option_exp_date.strftime('%m-%d-%Y'))
entities.append('Leg ' + str(num_done))
else:
break
num_legs -= 1
num_done += 1
last_exp_date = option_exp_date
# end of processing
return entities
# end of get_options_for_years
def historical_options(
years=[
'2014',
'2015',
'2016',
'2017',
'2018',
'2019',
'2020',
'2021',
'2022',
'2023',
'2024',
'2025',
'2026',
'2027',
'2028'
]):
"""historical_options
:param years: years to build
"""
entities = []
months = [
'01',
'02',
'03',
'04',
'05',
'06',
'07',
'08',
'09',
'10',
'11',
'12'
]
option_expirations = {
}
opts = []
for year in years:
for month in months:
target_date_str = str(month + '-01-' + year)
target_date = datetime.datetime.strptime(
target_date_str,
'%m-%d-%Y')
option_expiration_date = option_expiration(
target_date)
option_expiration_str = option_expiration_date.strftime(
'%m-%d-%Y')
option_expirations[option_expiration_str] = option_expiration_date
opts.append(option_expiration_date.strftime(
'%m-%d-%Y'))
# end of for all months
# end of building option expiration dates
now = datetime.datetime.strptime('01-01-2009', '%m-%d-%Y')
num_legs = 400
num_done = 1
date_format = '%m-%d-%Y'
for option_exp in opts:
option_exp_date = datetime.datetime.strptime(option_exp, date_format)
if option_exp_date >= now:
if num_legs > 0:
entities.append(option_exp_date.strftime('%m-%d-%Y'))
else:
break
num_legs -= 1
num_done += 1
# end of processing
return entities
# end of historical_options
def get_options_between_dates(
start_date,
end_date):
"""get_options_between_dates
:param start_date: start date
:param end_date: end date
"""
valid_options = []
for rec in historical_options():
opt_date = datetime.datetime.strptime(
str(rec),
'%m-%d-%Y').date()
if start_date <= opt_date <= end_date:
valid_options.append(opt_date.strftime('%Y-%m-%d'))
return valid_options
# end of get_options_between_dates
def option_expiration(
date=None):
"""option_expiration
:param date: date to find the current expiration
"""
cur_date = date
if not cur_date:
cur_date = datetime.datetime.now()
while (not (cur_date.weekday() == 4 and 14 < cur_date.day < 22)):
cur_date = cur_date + datetime.timedelta(days=1)
if hdays.is_holiday(
date=cur_date):
test_date = cur_date - datetime.timedelta(days=1)
if cur_date.weekday() == 0:
test_date = cur_date - datetime.timedelta(days=3)
if hdays.is_holiday(
date=test_date):
test_date = cur_date - datetime.timedelta(days=4)
if hdays.is_holiday(
date=test_date):
test_date = cur_date - datetime.timedelta(days=5)
cur_date = test_date
# end of if this date is a holiday and go back
return cur_date
# end of option_expiration
def get_options_for_today():
"""get_options_for_today
Get a list of option expiration nodes where the last cell
has the current option cycle's expiration date.
"""
cur_date = datetime.datetime.now()
cycle_exps = historical_options()
previous_exp = None
valid_option_exps = []
for idx, org_exp_date_str in enumerate(cycle_exps):
log.debug(f'cycle={idx} expiration={org_exp_date_str}')
exp_date = datetime.datetime.strptime(
org_exp_date_str,
'%m-%d-%Y')
exp_date_str = exp_date.strftime(
'%Y-%m-%d')
cycle_start_date = exp_date - pd_bday.BDay(19)
if previous_exp:
cycle_start_date = previous_exp + pd_bday.BDay(1)
cycle_start_date_str = cycle_start_date.strftime(
'%m-%d-%Y')
valid_option_exps.append({
'exp_date': exp_date,
'exp_date_str': exp_date_str,
'cycle_start': cycle_start_date,
'cycle_start_str': cycle_start_date_str
})
if cur_date < exp_date:
break
previous_exp = exp_date
# end of for all historical options
return valid_option_exps
# end of get_options_for_today