-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcboe_data.py
More file actions
533 lines (456 loc) · 17.7 KB
/
Copy pathcboe_data.py
File metadata and controls
533 lines (456 loc) · 17.7 KB
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
"""
Retrieves delayed quotes for option chain data from CBOE's API
It reuses the same code from OpenBB
(https://github.com/OpenBB-finance/OpenBBTerminal)
"""
from datetime import datetime
import random
import requests
from typing import Tuple
import pandas as pd
TICKER_EXCEPTIONS: list[str] = ["NDX", "RUT"]
def get_user_agent() -> str:
"""Get a not very random user agent."""
user_agent_strings = [
"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.10; rv:86.1) Gecko/20100101 Firefox/86.1",
"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:86.1) Gecko/20100101 Firefox/86.1",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:82.1) Gecko/20100101 Firefox/82.1",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:86.0) Gecko/20100101 Firefox/86.0",
"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:86.0) Gecko/20100101 Firefox/86.0",
"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.10; rv:83.0) Gecko/20100101 Firefox/83.0",
"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:84.0) Gecko/20100101 Firefox/84.0",
]
return random.choice(user_agent_strings) # nosec # noqa: S311
# Write an abstract helper to make requests from a url with potential headers and params
def request(
url: str, method: str = "get", timeout: int = 5, **kwargs
) -> requests.Response:
"""Abstract helper to make requests from a url with potential headers and params.
Parameters
----------
url : str
Url to make the request to
method : str
HTTP method to use. Choose from:
delete, get, head, patch, post, put, by default "get"
timeout : int
How many seconds to wait for the server to send data
Returns
-------
requests.Response
Request response object
Raises
------
ValueError
If invalid method is passed
"""
method = method.lower()
if method not in ["delete", "get", "head", "patch", "post", "put"]:
raise ValueError(f"Invalid method: {method}")
# We want to add a user agent to the request, so check if there are any headers
# If there are headers, check if there is a user agent, if not add one.
# Some requests seem to work only with a specific user agent, so we want to be able to override it.
headers = kwargs.pop("headers", {})
timeout = timeout
if "User-Agent" not in headers:
headers["User-Agent"] = get_user_agent()
func = getattr(requests, method)
return func(
url,
headers=headers,
timeout=timeout,
**kwargs,
)
def get_cboe_directory() -> pd.DataFrame:
"""Gets the US Listings Directory for the CBOE.
Returns
-------
pd.DataFrame: CBOE_DIRECTORY
DataFrame of the CBOE listings directory
Examples
-------
>>> from openbb_terminal.stocks.options import cboe_model
>>> CBOE_DIRECTORY = cboe_model.get_cboe_directory()
"""
try:
CBOE_DIRECTORY: pd.DataFrame = pd.read_csv(
"https://www.cboe.com/us/options/symboldir/equity_index_options/?download=csv"
)
CBOE_DIRECTORY = CBOE_DIRECTORY.rename(
columns={
" Stock Symbol": "Symbol",
" DPM Name": "DPM Name",
" Post/Station": "Post/Station",
}
).set_index("Symbol")
return CBOE_DIRECTORY
except requests.exceptions.HTTPError:
return pd.DataFrame()
def get_cboe_index_directory() -> pd.DataFrame:
"""Gets the US Listings Directory for the CBOE
Returns
-------
pd.DataFrame: CBOE_INDEXES
Examples
-------
>>> from openb_terminal.stocks.options import cboe_model
>>> CBOE_INDEXES = cboe_model.get_cboe_index_directory()
"""
try:
CBOE_INDEXES: pd.DataFrame = pd.DataFrame(
pd.read_json(
"https://cdn.cboe.com/api/global/us_indices/definitions/all_indices.json"
)
)
CBOE_INDEXES = CBOE_INDEXES.rename(
columns={
"calc_end_time": "Close Time",
"calc_start_time": "Open Time",
"currency": "Currency",
"description": "Description",
"display": "Display",
"featured": "Featured",
"featured_order": "Featured Order",
"index_symbol": "Ticker",
"mkt_data_delay": "Data Delay",
"name": "Name",
"tick_days": "Tick Days",
"tick_frequency": "Frequency",
"tick_period": "Period",
"time_zone": "Time Zone",
},
)
indices_order: list[str] = [
"Ticker",
"Name",
"Description",
"Currency",
"Tick Days",
"Frequency",
"Period",
"Time Zone",
]
CBOE_INDEXES = pd.DataFrame(CBOE_INDEXES, columns=indices_order).set_index(
"Ticker"
)
return CBOE_INDEXES
except requests.exceptions.HTTPError:
return pd.DataFrame()
# Gets the list of indexes for parsing the ticker symbol properly.
INDEXES = get_cboe_index_directory().index.tolist()
SYMBOLS = get_cboe_directory()
def get_ticker_info(symbol: str) -> Tuple[pd.DataFrame, list[str]]:
"""Gets basic info for the symbol and expiration dates
Parameters
----------
symbol: str
The ticker to lookup
Returns
-------
Tuple: [pd.DataFrame, pd.Series]
ticker_details
ticker_expirations
Examples
--------
>>> from openbb_terminal.stocks.options import cboe_model
>>> ticker_details,ticker_expirations = cboe_model.get_ticker_info('AAPL')
>>> vix_details,vix_expirations = cboe_model.get_ticker_info('VIX')
"""
stock = "stock"
index = "index"
symbol = symbol.upper()
new_ticker: str = ""
ticker_details = pd.DataFrame()
ticker_expirations: list = []
try:
if symbol in TICKER_EXCEPTIONS:
new_ticker = "^" + symbol
elif symbol not in INDEXES:
new_ticker = symbol
elif symbol in INDEXES:
new_ticker = "^" + symbol
# Gets the data to return, and if none returns empty Tuple #
symbol_info_url = (
"https://www.cboe.com/education/tools/trade-optimizer/symbol-info/?symbol="
f"{new_ticker}"
)
symbol_info = request(symbol_info_url)
symbol_info_json = symbol_info.json()
symbol_info_json = pd.Series(symbol_info.json())
if symbol_info_json.success is False:
ticker_details = pd.DataFrame()
ticker_expirations = []
print("No data found for the symbol: " f"{symbol}" "")
else:
symbol_details = pd.Series(symbol_info_json["details"])
symbol_details = pd.DataFrame(symbol_details).transpose()
symbol_details = symbol_details.reset_index()
ticker_expirations = symbol_info_json["expirations"]
# Cleans columns depending on if the security type is a stock or an index
type_ = symbol_details.security_type
if stock[0] in type_[0]:
stock_details = symbol_details
ticker_details = pd.DataFrame(stock_details).rename(
columns={
"current_price": "price",
"bid_size": "bidSize",
"ask_size": "askSize",
"iv30": "ivThirty",
"prev_day_close": "previousClose",
"price_change": "change",
"price_change_percent": "changePercent",
"iv30_change": "ivThirtyChange",
"iv30_percent_change": "ivThirtyChangePercent",
"last_trade_time": "lastTradeTimestamp",
"exchange_id": "exchangeID",
"tick": "tick",
"security_type": "type",
}
)
details_columns = [
"symbol",
"type",
"tick",
"bid",
"bidSize",
"askSize",
"ask",
"price",
"open",
"high",
"low",
"close",
"volume",
"previousClose",
"change",
"changePercent",
"ivThirty",
"ivThirtyChange",
"ivThirtyChangePercent",
"lastTradeTimestamp",
]
ticker_details = (
pd.DataFrame(ticker_details, columns=details_columns)
.set_index(keys="symbol")
.dropna(axis=1)
.transpose()
)
if index[0] in type_[0]:
index_details = symbol_details
ticker_details = pd.DataFrame(index_details).rename(
columns={
"symbol": "symbol",
"security_type": "type",
"current_price": "price",
"price_change": "change",
"price_change_percent": "changePercent",
"prev_day_close": "previousClose",
"iv30": "ivThirty",
"iv30_change": "ivThirtyChange",
"iv30_change_percent": "ivThirtyChangePercent",
"last_trade_time": "lastTradeTimestamp",
}
)
index_columns = [
"symbol",
"type",
"tick",
"price",
"open",
"high",
"low",
"close",
"previousClose",
"change",
"changePercent",
"ivThirty",
"ivThirtyChange",
"ivThirtyChangePercent",
"lastTradeTimestamp",
]
ticker_details = (
pd.DataFrame(ticker_details, columns=index_columns)
.set_index(keys="symbol")
.dropna(axis=1)
.transpose()
).rename(columns={f"{new_ticker}": f"{symbol}"})
except requests.exceptions.HTTPError:
print("There was an error with the request'\n")
ticker_details = pd.DataFrame()
ticker_expirations = list()
return ticker_details, ticker_expirations
return ticker_details, ticker_expirations
def get_ticker_iv(symbol: str) -> pd.DataFrame:
"""Gets annualized high/low historical and implied volatility over 30/60/90 day windows.
Parameters
----------
symbol: str
The loaded ticker
Returns
-------
pd.DataFrame: ticker_iv
Examples
--------
>>> from openbb_terminal.stocks.options import cboe_model
>>> ticker_iv = cboe_model.get_ticker_iv('AAPL')
>>> ndx_iv = cboe_model.get_ticker_iv('NDX')
"""
# Checks ticker to determine if ticker is an index or an exception that requires modifying the request's URLs
try:
if symbol in TICKER_EXCEPTIONS:
quotes_iv_url = (
"https://cdn.cboe.com/api/global/delayed_quotes/historical_data/_"
f"{symbol}.json"
)
elif symbol not in INDEXES:
quotes_iv_url = (
"https://cdn.cboe.com/api/global/delayed_quotes/historical_data/"
f"{symbol}.json"
)
elif symbol in INDEXES:
quotes_iv_url = (
"https://cdn.cboe.com/api/global/delayed_quotes/historical_data/_"
f"{symbol}.json"
)
h_iv = request(quotes_iv_url)
if h_iv.status_code != 200:
print("No data found for the symbol: " f"{symbol}" "")
return pd.DataFrame()
data = h_iv.json()
h_data = pd.DataFrame(data)[2:-1]["data"].rename(f"{symbol}")
h_data.rename(
{
"hv30_annual_high": "hvThirtyOneYearHigh",
"hv30_annual_low": "hvThirtyOneYearLow",
"hv60_annual_high": "hvSixtyOneYearHigh",
"hv60_annual_low": "hvsixtyOneYearLow",
"hv90_annual_high": "hvNinetyOneYearHigh",
"hv90_annual_low": "hvNinetyOneYearLow",
"iv30_annual_high": "ivThirtyOneYearHigh",
"iv30_annual_low": "ivThirtyOneYearLow",
"iv60_annual_high": "ivSixtyOneYearHigh",
"iv60_annual_low": "ivSixtyOneYearLow",
"iv90_annual_high": "ivNinetyOneYearHigh",
"iv90_annual_low": "ivNinetyOneYearLow",
},
inplace=True,
)
iv_order = [
"ivThirtyOneYearHigh",
"hvThirtyOneYearHigh",
"ivThirtyOneYearLow",
"hvThirtyOneYearLow",
"ivSixtyOneYearHigh",
"hvSixtyOneYearHigh",
"ivSixtyOneYearLow",
"hvsixtyOneYearLow",
"ivNinetyOneYearHigh",
"hvNinetyOneYearHigh",
"ivNinetyOneYearLow",
"hvNinetyOneYearLow",
]
ticker_iv = pd.DataFrame(h_data).transpose()
except requests.exceptions.HTTPError:
print("There was an error with the request'\n")
return pd.DataFrame(ticker_iv, columns=iv_order).transpose()
def get_quotes(symbol: str) -> pd.DataFrame:
"""Gets the complete options chains for a ticker.
Parameters
----------
symbol: str
The ticker get options data for
Returns
-------
pd.DataFrame
DataFrame with all active options contracts for the underlying symbol.
Examples
--------
>>> from openbb_terminal.stocks.options import cboe_model
>>> xsp = cboe_model.OptionsChains().get_chains('XSP')
>>> xsp_chains = xsp.chains
"""
# Checks ticker to determine if ticker is an index or an exception that requires modifying the request's URLs.
try:
if symbol in TICKER_EXCEPTIONS:
quotes_url = (
"https://cdn.cboe.com/api/global/delayed_quotes/options/_"
f"{symbol}"
".json"
)
else:
if symbol not in INDEXES:
quotes_url = (
"https://cdn.cboe.com/api/global/delayed_quotes/options/"
f"{symbol}"
".json"
)
if symbol in INDEXES:
quotes_url = (
"https://cdn.cboe.com/api/global/delayed_quotes/options/_"
f"{symbol}"
".json"
)
r = request(quotes_url)
if r.status_code != 200:
print("No data found for the symbol: " f"{symbol}" "")
return pd.DataFrame()
r_json = r.json()
data = pd.DataFrame(r_json["data"])
options = pd.Series(data.options, index=data.index)
options_columns = list(options[0])
options_data = list(options[:])
options_df = pd.DataFrame(options_data, columns=options_columns)
options_df = options_df.rename(
columns={
"option": "contractSymbol",
"bid_size": "bidSize",
"ask_size": "askSize",
"iv": "impliedVolatility",
"open_interest": "openInterest",
"theo": "theoretical",
"last_trade_price": "lastTradePrice",
"last_trade_time": "lastTradeTimestamp",
"percent_change": "changePercent",
"prev_day_close": "previousClose",
}
)
# Pareses the option symbols into columns for expiration, strike, and optionType
option_df_index = options_df["contractSymbol"].str.extractall(
r"^(?P<Ticker>\D*)(?P<expiration>\d*)(?P<optionType>\D*)(?P<strike>\d*)"
)
option_df_index = option_df_index.reset_index().drop(
columns=["match", "level_0"]
)
option_df_index.optionType = option_df_index.optionType.str.replace(
"C", "call"
).str.replace("P", "put")
option_df_index.strike = [ele.lstrip("0") for ele in option_df_index.strike]
option_df_index.strike = pd.Series(option_df_index.strike).astype(float)
option_df_index.strike = option_df_index.strike * (1 / 1000)
option_df_index.strike = option_df_index.strike.to_list()
option_df_index.expiration = [
ele.lstrip("1") for ele in option_df_index.expiration
]
option_df_index.expiration = pd.DatetimeIndex(
option_df_index.expiration, yearfirst=True
).astype(str)
option_df_index = option_df_index.drop(columns=["Ticker"])
# Joins the parsed symbol into the dataframe.
quotes = option_df_index.join(options_df)
now = datetime.now()
temp = pd.DatetimeIndex(quotes.expiration)
temp_ = (temp - now).days + 1
quotes["dte"] = temp_
quotes = quotes.set_index(
keys=["expiration", "strike", "optionType"]
).sort_index()
quotes["openInterest"] = quotes["openInterest"].astype(int)
quotes["volume"] = quotes["volume"].astype(int)
quotes["bidSize"] = quotes["bidSize"].astype(int)
quotes["askSize"] = quotes["askSize"].astype(int)
quotes["previousClose"] = round(quotes["previousClose"], 2)
quotes["changePercent"] = round(quotes["changePercent"], 2)
except requests.exceptions.HTTPError:
print("There was an error with the request'\n")
return pd.DataFrame()
return quotes.reset_index()