Skip to content

Instantly share code, notes, and snippets.

@axs
Last active August 29, 2015 14:14
Show Gist options
  • Save axs/c25b230159fe7f1c2e8b to your computer and use it in GitHub Desktop.
Save axs/c25b230159fe7f1c2e8b to your computer and use it in GitHub Desktop.
import pandas as pd
import numpy as np
option_data_frame = pd.read_csv('C:/code/python/pyql/examples/data/df_SPX_24jan2011.csv',parse_dates=[0,2])
grouped = option_data_frame.groupby('dtExpiry')
for spec, group in grouped:
print('processing group %s' % spec)
indx = group.index
dtTrade = group['dtTrade'][indx[0]]
dtExpiry = group['dtExpiry'][indx[0]]
spot = group['Spot'][indx[0]]
daysToExpiry = (dtExpiry-dtTrade).days
timeToMaturity = daysToExpiry/365.0
df_call = group[group['Type'] == 'C']
df_put = group[group['Type'] == 'P']
df_C = pd.DataFrame((df_call['PBid']+df_call['PAsk'])/2,columns=['PremiumC'])
df_C.index = df_call['Strike']
df_P = pd.DataFrame((df_put['PBid']+df_put['PAsk'])/2,columns=['PremiumP'])
df_P.index = df_put['Strike']
df_all = df_C.join(df_P, how='inner')
df_all['Strike'] = df_all.index
df_all['C-P'] = df_all['PremiumC'] - df_all['PremiumP']
y = np.array(df_all['C-P'])
x = np.array(df_all['Strike'])
A = np.vstack((x, np.ones(x.shape))).T
b = np.linalg.lstsq(A, y)[0]
iRate = -np.log(-b[0])/timeToMaturity
dRate = np.log(spot/b[1])/timeToMaturity
discountFactor = np.exp(-iRate*timeToMaturity)
Fwd = spot * np.exp((iRate-dRate)*timeToMaturity)
print('Fwd: %f int rate: %f div yield: %f' % (Fwd, iRate, dRate))
#based off combos
def impliedCarry(callP , putP , strike , stockP):
return callP - putP + strike - stockP
def impliedBorrow (callP , putP , strike , stockP, daysToExpiry):
return impliedCarry(callP , putP , strike , stockP) / strike * 360 / daysToExpiry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment