-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathYahoo_options.py
More file actions
133 lines (111 loc) · 4.03 KB
/
Yahoo_options.py
File metadata and controls
133 lines (111 loc) · 4.03 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
"""
Yahoo options data using pandas-datareader
Must install pandas-datareader and html5lib:
conda install pandas-datareader, html5lib
References
* http://finance.yahoo.com/q/op?s=AMZN+Options
* https://pandas-datareader.readthedocs.org/en/latest/
Prepared for Data Bootcamp course at NYU
* http://databootcamp.nyuecon.com/
* https://github.com/NYUDataBootcamp/Materials/Code/Lab
Written by Dave Backus, February 2016
Created with Python 3.5
"""
# Spencer wrote the first version of this module
import pandas as pd
import pandas_datareader.yahoo.options as yho
import matplotlib.pyplot as plt
"""
supply ticker, get option prices
"""
ticker = 'amzn'
otk = yho.Options(ticker)
exp = otk.expiry_dates # get expiration dates
# get option prices
cols = [0, 1, 2, 7]
calls = otk.get_call_data(expiry=exp[7])[cols]
puts = otk.get_put_data(expiry=exp[7])[cols]
# drop extra index levels
calls = calls.reset_index(level=[1,2,3], drop=True)
puts = puts.reset_index(level=[1,2,3], drop=True)
# cut off extremes
spot = otk.underlying_price
calls = calls[(calls.index >= 0.75*spot) & (calls.index <= 1.25*spot)]
puts = puts[(puts.index >= 0.75*spot) & (puts.index <= 1.25*spot)]
# compute mid-market
calls['Mid'] = (calls['Bid'] + calls['Ask'])/2
puts['Mid'] = (puts['Bid'] + puts['Ask'])/2
#%%
"""
plot put and call prices
* bid-ask mid
* last
* implied vols
"""
fig, ax = plt.subplots()
calls['Mid'].plot(lw=2, color='blue', alpha=0.6, ax=ax)
puts['Mid'].plot(lw=2, color='m', alpha=0.6, ax=ax)
ymin, ymax = ax.get_ylim()
ax.set_title('Prices of Amazon options (bid-ask avg)', fontsize=14, loc='left')
ax.set_ylabel('Option Prices')
ax.set_xlabel('Strike Price')
ax.vlines(x=spot, ymin=ymin, ymax=ymax, linestyle='dashed')
ax.text(1.01*spot, 0.9*ymax, 'Stock price', horizontalalignment='left')
ax.text(545, 80, 'Put prices', horizontalalignment='right', color='m')
ax.text(420, 80, 'Call prices', horizontalalignment='left', color='b')
fig.show()
#%%
fig, ax = plt.subplots()
which = 'last'
calls[which].plot(lw=2, color='blue', alpha=0.6, ax=ax)
puts[which].plot(lw=2, color='m', alpha=0.6, ax=ax)
ymin, ymax = ax.get_ylim()
ax.set_title('Prices of Amazon options (last quote)', fontsize=14, loc='left')
ax.set_ylabel('Option Prices')
ax.set_xlabel('Strike Price')
ax.vlines(x=spot, ymin=ymin, ymax=ymax, linestyle='dashed')
ax.text(1.01*spot, 0.9*ymax, 'Stock price', horizontalalignment='left')
ax.text(545, 80, 'Put prices', horizontalalignment='right', color='m')
ax.text(420, 80, 'Call prices', horizontalalignment='left', color='b')
fig.show()
#%%
# convert IV data to numbers
calls['IV'] = calls['IV'].str.replace('%', '').astype(float)
puts['IV'] = puts['IV'].str.replace('%', '').astype(float)
fig, ax = plt.subplots()
calls['IV'].plot(lw=2, color='blue', alpha=0.6, ax=ax)
puts['IV'].plot(lw=2, color='m', alpha=0.6, ax=ax)
ymin, ymax = ax.get_ylim()
ax.set_title('Implied volatilities of Amazon options', fontsize=14, loc='left')
ax.set_ylabel('Implied Volatility (percent)')
ax.set_xlabel('Strike Price')
ax.vlines(x=spot, ymin=ymin, ymax=ymax, linestyle='dashed')
ax.text(1.01*spot, 0.9*(ymax-ymin)+ymin, 'Stock price', horizontalalignment='left')
ax.text(400, 46, 'Puts', horizontalalignment='right', color='m')
ax.text(450, 46, 'Calls', horizontalalignment='left', color='b')
fig.show()
#%%
#%%
"""
we can also use read_html, but the output is a mess
"""
url = 'http://finance.yahoo.com/q/op?s=AMZN+Options'
ops = pd.read_html(url)
print('Dimensions of ops[1]', ops[1].shape)
print('Dimensions of ops[2]', ops[2].shape)
print('Column labels:', list(ops[1]))
print('Row labels:', ops[1].index.tolist())
#%%
"""
Check whether packages are installed
http://stackoverflow.com/questions/14050281/how-to-check-if-a-python-module-exists-without-importing-it
"""
import importlib
def check_for_package(package):
lib_spec = importlib.util.find_spec(package)
found = lib_spec is not None
print('Package', package, 'found?', found)
return found
foundpdr = check_for_package('pandas_datareader.yahoo.options')
foundh5 = check_for_package('html5lib')
ready = foundpdr and foundh5