-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsmileWeights.py
63 lines (49 loc) · 2.18 KB
/
smileWeights.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
"""
smileWeights.py
Created by Luca Camerani at 04/01/2021, University of Milano-Bicocca.
All rights reserved.
This file is part of the EcoFin-Library (https://github.com/LucaCamerani/EcoFin-Library),
and is released under the "BSD Open Source License".
"""
import matplotlib.pyplot as plt
import numpy as np
from EcoFin.dataDownload.optionsManager import OptionManager
from EcoFin.dataDownload.ticker import Ticker
from EcoFin.options.chainWeights import ChainWeights
from EcoFin.options.impliedVolatility import ImpliedVolatility
from EcoFin.options.volatilitySmile import VolatilitySmile
# -------------------------[Set-up]-------------------------
ticker = Ticker('MSFT')
optionManager = OptionManager(ticker, now=None)
i = 0 # <- Time To Maturity curve
exp = optionManager.getExpirations()[i]
optionChain = optionManager.getOptionChain(exp=exp)
# ----------------------------------------------------------
fig, axs = plt.subplots(2, figsize=(15, 8), sharex=True)
fig.suptitle('Weigh implied volatility ({})'.format(ticker.getInfo().ticker), fontsize=16)
forwardPrice = optionChain.getForwardPrice()
impliedVolatility = ImpliedVolatility(optionChain)
volatilitySmile = VolatilitySmile(impliedVolatility)
smile = volatilitySmile.getVolatilitySmile()
chainWeights = ChainWeights(optionChain)
weights = chainWeights.computeMoneynessWeights()
# chart 1
axs[0].set_title('Volatility smile (VS)')
axs[0].plot(smile.strike, smile.smile, label='Smile')
axs[0].plot(forwardPrice, np.amin(smile.smile) * 0.7, markersize=8, marker="^", color='violet')
axs[0].vlines(forwardPrice, np.nanmin(smile.smile) * 0.7, np.nanmax(smile.smile),
linestyle="dashed", color='violet', alpha=.6, label='Forward Price')
axs[0].set(ylabel='Volatility',
title='Option volatility smile')
axs[0].legend()
axs[0].grid()
# chart 2
axs[1].set_title('Weights')
axs[1].plot(weights, label='Weights ($w$)', color='green', alpha=.5, linewidth=4)
axs[1].vlines(forwardPrice, np.nanmin(weights) * 0.7, np.nanmax(weights) * 1.5,
linestyle="dashed", color='violet', alpha=.6, label='Forward Price')
axs[1].set(xlabel='Strike', ylabel='Weights')
axs[1].legend()
axs[1].grid()
plt.show()