Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
2846 views
Kernel: Python 3 (system-wide)
import numpy as np import scipy as sp import matplotlib.pyplot as plt import matplotlib.dates as md from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm import pandas as pd %matplotlib inline
options = pd.read_csv("../../../data/L3_optionstats_20131101.part.csv")
options.head()
options.loc[1]
UnderlyingSymbol AAPL UnderlyingPrice 520.03 Flags W OptionSymbol AAPL131101P00355000 Type put Expiration 11/01/2013 DataDate 11/01/2013 Strike 355 Last 0.01 Bid 0 Ask 0.01 Volume 0 OpenInterest 195 T1OpenInterest 0 IVMean 0 IVBid 0 IVAsk 0 Delta 0 Gamma 0 Theta 0 Vega 0 AKA AAPL131101P00355000 Name: 1, dtype: object
pd.unique( options['UnderlyingSymbol'] )
array(['AAPL', 'AIG', 'AMZN', 'FB', 'GE', 'GOOGL', 'JNJ', 'JPM', 'MSFT', 'XOM'], dtype=object)
today = pd.datetime(2013,11,1) # options['Expiration'] = pd.to_datetime( options['Expiration'] ) options['T'] = (options['Expiration'] - today) / np.timedelta64(1,'Y') #
subset = options[['UnderlyingSymbol','Type','Expiration','T', 'Strike','IVMean','UnderlyingPrice',' DataDate']]\ [ (options['UnderlyingSymbol'] == 'JNJ') & (options['Type'] == 'call') & (options['Expiration'] > '2013-11-01') ] subset.head()
[d.strftime("%Y-%m-%d") for d in sorted(set(subset['Expiration']))]
['2013-11-08', '2013-11-16', '2013-11-22', '2013-11-29', '2013-12-06', '2013-12-21', '2014-01-18', '2014-04-19', '2015-01-17', '2016-01-15']
sorted(set(subset['T']))
[0.019165349048919554, 0.04106860510482761, 0.05749604714675866, 0.07666139619567822, 0.09582674524459776, 0.13689535034942538, 0.2135567465451036, 0.4627062841810578, 1.2101548970889204, 2.2040151406257484]
def draw1(symbol, type, drawPoints=True): subset = options[['Expiration','Strike','IVMean','UnderlyingPrice']]\ [ (options['UnderlyingSymbol'] == symbol) & (options['Type'] == type)] # & (options['ExpirationDT'] > '2013-11-01') ] expiration_dates = sorted(set(subset['Expiration'])) price = subset.loc[subset.index[1]]['UnderlyingPrice'] plt.figure(figsize=(12, 8)) lines = [] for date in expiration_dates: data = subset[subset.Expiration == date] l, = plt.plot(data['Strike'], data['IVMean'], label=date.strftime("%Y-%m-%d"), lw=1.5) lines.append(l) if drawPoints: plt.plot(data['Strike'], data['IVMean'], 'r.') plt.axvline(x=price, linewidth=1, color='k') plt.grid(True) plt.xlabel('strike') plt.ylabel('implied volatility') plt.legend(handles=lines) plt.show()
def draw2(symbol, type, drawPoints=True): subset = options[['T', 'Strike','IVMean','UnderlyingPrice']]\ [ (options['UnderlyingSymbol'] == symbol) & (options['Type'] == type) & (options['Expiration'] > '2013-11-01') ] fig = plt.figure(figsize=(12, 8)) ax = fig.gca(projection='3d') Y = subset['T'] X = subset['Strike'] Z = subset['IVMean'] ax.plot_trisurf(X, Y, Z, cmap=cm.jet, linewidth=0.2) ax.set_ylabel("Expiration") ax.set_xlabel("Strike") ax.set_zlabel("Implied vol") plt.show()
draw1('JNJ', 'call')
Image in a Jupyter notebook
draw2('JNJ', 'call')
Image in a Jupyter notebook
draw1('JNJ', 'put')
Image in a Jupyter notebook
draw2('JNJ', 'put')
Image in a Jupyter notebook
draw1('GOOGL', 'call', False)
Image in a Jupyter notebook
draw2('GOOGL', 'call', False)
Image in a Jupyter notebook
draw1('AIG', 'call')
Image in a Jupyter notebook
draw1('AAPL', 'call',False)
Image in a Jupyter notebook
draw1('AMZN', 'call')
Image in a Jupyter notebook
draw2('AMZN', 'call')
Image in a Jupyter notebook
draw1('FB', 'call')
Image in a Jupyter notebook
draw2('FB', 'call')
Image in a Jupyter notebook
draw1('GE', 'call')
Image in a Jupyter notebook
draw2('GE', 'call')
Image in a Jupyter notebook