Last active
August 29, 2015 14:16
-
-
Save QuantTraderEd/19f4616bddae20cc2361 to your computer and use it in GitHub Desktop.
OptionViewer_InpliedVolatility.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Created on Fri Feb 27 14:51:01 2015 | |
@author: assa | |
""" | |
import pdb | |
import time | |
import os | |
import sqlite3 as lite | |
import pandas as pd | |
import BlackSholesPricer as pricer | |
def convert_strike(strike): | |
if type(strike).__name__ == 'unicode': | |
if strike[2] == '2' or strike[2] == '7': | |
return '%s.5'%strike | |
else: | |
return strike | |
pass | |
print os.path.dirname(os.path.realpath(__file__)) + '\\' | |
filedbname = 'TAQ_20150226.db' | |
conn = lite.connect('./SQLite/Data/' + filedbname) | |
sql_text = """ | |
SELECT Time, ShortCD, Ask1, Bid1 | |
FROM FutOptTickData | |
WHERE TAQ = 'Q' | |
and Time between '09:00:01.000' and '09:30:00.000' | |
and substr(ShortCD,4,2) = 'K3' | |
and (substr(ShortCD,1,1) = '2' or substr(ShortCD,1,1) = '3') | |
""" | |
df = pd.read_sql(sql_text,conn) | |
ShortCDList = df['ShortCD'].unique() | |
ShortCDList.sort() | |
ShortCDList = list(ShortCDList) | |
CallShortCDList = [shortcd for shortcd in ShortCDList if shortcd[:3] == '201'] | |
PutShortCDList = [shortcd for shortcd in ShortCDList if shortcd[:3] == '301'] | |
df_call = df[df['ShortCD'].str[:3] == '201'] | |
df_put = df[df['ShortCD'].str[:3] == '301'] | |
df['ShortCDStrike'] = df['ShortCD'].str[-3:] | |
StrikeLst = list(df.drop_duplicates(cols=['ShortCDStrike'])['ShortCDStrike']) | |
StrikeLst.sort() | |
df_call_mid = pd.DataFrame() | |
df_put_mid = pd.DataFrame() | |
for strike in StrikeLst: | |
shortcd = '201K3%s'%strike | |
df_tmp = df[df['ShortCD'] == shortcd][-1:] | |
df_tmp['Strike'] = convert_strike(strike) | |
if len(df_call_mid) == 0: df_call_mid = df_tmp | |
else: df_call_mid = df_call_mid.append(df_tmp) | |
#print shortcd | |
shortcd = '301K3%s'%strike | |
df_tmp = df[df['ShortCD'] == shortcd][-1:] | |
df_tmp['Strike'] = convert_strike(strike) | |
if len(df_put_mid) == 0: df_put_mid = df_tmp | |
else: df_put_mid = df_put_mid.append(df_tmp) | |
#print shortcd | |
df_call_mid['Mid'] = (df_call_mid['Bid1'].astype(float) + df_call_mid['Ask1'].astype(float)) * 0.5 | |
df_put_mid['Mid'] = (df_put_mid['Bid1'].astype(float) + df_put_mid['Ask1'].astype(float)) * 0.5 | |
df_syth = df_call_mid.merge(df_put_mid,left_on='Strike',right_on='Strike',how='outer') | |
df_syth['SythPrice'] = df_syth['Mid_x'].astype(float) - df_syth['Mid_y'].astype(float) + df_syth['Strike'].astype(float) | |
df_syth['Differ'] = abs(df_syth['Mid_x'].astype(float) - df_syth['Mid_y'].astype(float)) | |
df_syth = df_syth.sort('Differ') | |
call_atm_price = df_syth.iloc[0]['Mid_x'] | |
put_atm_price = df_syth.iloc[0]['Mid_y'] | |
r = 0.00000612875 | |
T = 6.25 * 10 + 11 * 9 - 0.5 | |
#S0 = 252.48 * np.exp(-r*T) | |
S0 = 252.19 | |
K = float(df_syth['Strike'].iloc[0]) | |
print '-' * 20 | |
t = time.time() | |
r_high = 0.000009 | |
r_low = 0.000005 | |
r = pricer.CalcImpliedInterestRate(S0, K, r_low, r_high, T, call_atm_price, put_atm_price,0.000000001,0.0017) | |
elapsed = time.time()-t | |
print "Option\tBlack-Scholes ImR:", r | |
print "Elapsed:", elapsed | |
# df_call_mid['ImVol'] = df_call_mid.apply(lambda r: pricer.CalcImpliedVolatility('C', | |
# S0, | |
# float(r['Strike']), | |
# r, | |
# T, | |
# float(r['Mid']), | |
# 0.0001, | |
# 0.0017),axis=1) | |
callImVolLst = [] | |
putImVolLst = [] | |
df_call_mid = df_call_mid[df_call_mid['Strike'] >= '252.5'] | |
df_put_mid = df_put_mid[df_put_mid['Strike'] <= '252.5'] | |
print df_call_mid | |
print df_put_mid | |
callimvol = 0.0017 | |
for i in xrange(len(df_call_mid)): | |
K = float(df_call_mid['Strike'].iloc[i]) | |
callprice = float(df_call_mid['Mid'].iloc[i]) | |
callimvol = pricer.CalcImpliedVolatility('C', S0, K, r, T, callprice, 0.0001, callimvol) | |
callImVolLst.append(callimvol) | |
putimvol = 0.0017 | |
for i in xrange(len(df_put_mid)-1,-1,-1): | |
K = float(df_put_mid['Strike'].iloc[i]) | |
putprice = float(df_put_mid['Mid'].iloc[i]) | |
putimvol = pricer.CalcImpliedVolatility('P', S0, K, r, T, putprice, 0.0001, putimvol) | |
putImVolLst.insert(0,putimvol) | |
pdb.set_trace() | |
df_call_mid['ImVol'] = pd.Series(callImVolLst, index=df_call_mid.index) | |
df_put_mid['ImVol'] = pd.Series(putImVolLst, index=df_put_mid.index) | |
df = df_put_mid.append(df_call_mid[0:]) | |
df.index = df['Strike'] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment