-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathchain.py
250 lines (185 loc) · 7.38 KB
/
chain.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
import pandas as pd
import yfinance as yf
import datetime as dt
import numpy as np
import click
from scipy.stats import norm
from typing import List, cast, Tuple, Any, Dict
import logging
import coloredlogs
from uniplot.uniplot import plot
def monte_carlo_binary(S, K, T, r, sigma, Q,
type_='call', Ndraws=10_000_000, seed=0):
np.random.seed(seed)
dS = np.random.normal((r - sigma**2 / 2) * T, sigma * np.sqrt(T), size=Ndraws)
ST = S * np.exp(dS)
if type_ == 'call':
return len(ST[ST > K]) / Ndraws * Q * np.exp(-r * T)
elif type_ == 'put':
return len(ST[ST < K]) / Ndraws * Q * np.exp(-r * T)
else:
raise ValueError('Type must be put or call')
def d2(S, K, T, r, sigma):
return (np.log(S / K) + (r - sigma**2 / 2) * T) / (sigma * np.sqrt(T))
def binary_call(S, K, T, r, sigma, Q=1):
N = norm.cdf
return np.exp(-r * T) * N(d2(S, K, T, r, sigma)) * Q
def binary_put(S, K, T, r, sigma, Q=1):
N = norm.cdf
return np.exp(-r * T) * N(-d2(S, K, T, r, sigma)) * Q
def get_option_dates(symbol) -> List[str]:
logging.info('getting option dates for symbol {}'.format(symbol))
return cast(List[str], yf.Ticker(symbol).options)
def get_chains(symbol: str, date: str) -> Tuple[pd.DataFrame, pd.DataFrame]:
def augment(bid: float, df: pd.DataFrame) -> pd.DataFrame:
df['IV'] = df.impliedVolatility
df['T'] = (dt.datetime.strptime(date, '%Y-%m-%d') - dt.datetime.now()).days / 255.0
df['S'] = bid
df['K'] = df.strike
return df
logging.info('getting option chain data for {}'.format(symbol))
ticker = yf.Ticker(symbol) # type: ignore
bid = ticker.get_info()['bid'] # type: ignore
ask = ticker.get_info()['ask'] # type: ignore
calls = augment(bid, pd.DataFrame(ticker.option_chain(date=date).calls))
puts = augment(bid, pd.DataFrame(ticker.option_chain(date=date).puts))
return (calls, puts)
def get_call_chain(symbol: str, date: str) -> pd.DataFrame:
return get_chains(symbol, date)[0]
def get_put_chain(symbol: str, date: str) -> pd.DataFrame:
return get_chains(symbol, date)[1]
def vol_by_strike(polymdl, K):
return np.poly1d(polymdl)(K)
def new_K(chain: pd.DataFrame):
# newK = np.arange(chain.K.iloc[0], chain.K.iloc[-1], 0.0001) # new higher resolution strikes
# newK = np.arange(1.0, chain.K.iloc[-1], 0.0001)
newK = np.arange(1.0, chain.K.iloc[-1], 0.1)
return newK
def plot_implied_vol(chain: pd.DataFrame):
import matplotlib.pyplot as plt
vols = chain.IV.values # vol
Ks = chain.K.values # strikes
newK = new_K(chain)
poly = np.polyfit(Ks, vols, 5) # create implied vol fit
newVols = np.poly1d(poly)(newK) # fit model to new higher resolution strikes
plt.plot(newK, newVols)
plt.title('Implied Volatility Function')
plt.xlabel('$K$')
plt.ylabel('Implied Vol')
plt.show()
def plot_expiration_cdf(chain: pd.DataFrame, risk_free_rate: float = 0.001):
import matplotlib.pyplot as plt
df = chain
S = df.S[0] # extract S_0
T = df['T'][0] # extract T
r = risk_free_rate
vols = chain.IV.values # volatility values
Ks = chain.K.values # strikes
poly = np.polyfit(Ks, vols, 5) # create implied vol fit
newK = new_K(chain)
newVols = np.poly1d(poly)(newK) # fit model to new higher resolution strikes
binaries = binary_put(S, newK, T, r, newVols) # calculating the binaries
plt.plot(newK, binaries, label='cdf')
plt.axvline(S, color='black', linestyle='--', label='$S_0$')
plt.xlabel('$S_T$')
plt.ylabel('cdf')
plt.title('Cdf of option chain')
plt.legend()
plt.show()
def implied_constant_helper(chain: pd.DataFrame, risk_free_rate: float = 0.001):
df = chain
S = df.S[0]
T = df['T'][0]
r = risk_free_rate
logging.info('calculating implied and constant distributions')
vols = chain.IV.values # volatility values
Ks = chain.K.values # strikes
poly = np.polyfit(Ks, vols, 5) # create implied vol fit
newK = new_K(chain)
newVols = np.poly1d(poly)(newK) # fit model to new higher resolution strikes
binaries = binary_put(S, newK, T, r, newVols)
PsT = [] # market implied probabilities
for i in range(1, len(binaries)):
p = binaries[i] - binaries[i - 1]
PsT.append(p)
constant_vol = vol_by_strike(poly, S)
binaries_const = binary_put(S, newK, T, r, constant_vol)
const_p = []
for i in range(1, len(binaries_const)):
p = binaries_const[i] - binaries_const[i - 1]
const_p.append(p)
logging.debug('finished calculating')
return {
'x': newK,
'market_implied': PsT,
'constant': const_p,
}
def plot_market_implied_vs_constant_helper(x, market_implied, constant):
import matplotlib.pyplot as plt
plt.plot(x[1:], market_implied, color='red', label='Market Implied')
plt.plot(x[1:], constant, color='black', label='Constant Vol')
plt.ylabel('$\mathbb{P}$')
plt.xlabel('$S_T$')
plt.legend()
plt.title('Market implied vs Constant')
plt.show()
def plot_market_implied_vs_constant_console(x, market_implied, constant, title):
plot(
[x[1:], x[1:]],
[constant, market_implied],
lines=True,
color=True,
interactive=True,
height=55,
width=100,
legend_labels=['constant, market_implied'],
title=title
)
def implied_constant(symbol: str, date: str, risk_free_rate: float = 0.001) -> Dict[str, Any]:
calls, puts = get_chains(symbol, date)
return implied_constant_helper(calls, risk_free_rate)
def plot_implied_constant(symbol, date: str, risk_free_rate: float = 0.001, console = False):
data = implied_constant(symbol, date, risk_free_rate)
if console:
plot_market_implied_vs_constant_console(
data['x'], data['market_implied'],
data['constant'],
'{} for {}, constant vs market implied'.format(symbol, date)
)
else:
plot_market_implied_vs_constant_helper(data['x'], data['market_implied'], data['constant'])
def plot_chain(
symbol: str,
list_dates: bool,
date: str,
console: bool,
risk_free_rate: float = 0.001
):
if list_dates:
dates = get_option_dates(symbol)
for d in dates:
option_date = dt.datetime.strptime(d, '%Y-%m-%d')
print('{} [{}] {} days from today'.format(symbol, d, (option_date - dt.datetime.now()).days))
return
if not date:
dates = get_option_dates(symbol)
date = dates[0]
option_date = dt.datetime.strptime(date, '%Y-%m-%d')
plot_implied_constant(symbol, date, risk_free_rate, console)
@click.command()
@click.option('--symbol', required=True, help='ticker symbol e.g. FB')
@click.option('--list_dates', required=False, is_flag=True, default=False, help='get the list of expirary dates')
@click.option('--date', required=False, help='option expiry date, format YYYY-MM-DD')
@click.option('--console', required=False, default=False, is_flag=True, help='print histogram to console [default: false]')
@click.option('--risk_free_rate', required=False, default=0.001, help='risk free rate [default 0.001]')
def main(
symbol: str,
list_dates: bool,
date: str,
console: bool,
risk_free_rate: float,
):
plot_chain(symbol, list_dates, date, console, risk_free_rate)
if __name__ == '__main__':
coloredlogs.install('INFO')
main()