If I want it to work fast, would I want to add these fields to the index? I tried but it behaves quirky and, oddly enough, we don't have a python expert in house (at least none that I can talk to).this will get the type and exp
optionsDF[ (optionsDF.type == 'CALL') & (optionsDF.EXP == '20170907') ]
Pandas has wonderful features for that - in fact, I can even do something liketargetDelta = 25
D = [ abs(x - targetDelta) for x in optionsDF[ (optionsDF.type == 'CALL') & (optionsDF.EXP == '20170907') ]]
D.index(min(D)
cond = (opt['exp']=='20170907') & (opt['cp']=='C')
x = opt[cond][abs(opt['delta'] - targetDelta) == abs(opt['delta'] - targetDelta).min()]
opt = pd.read_csv('spy.csv')
opt.set_index(['date', 'exp', 'cp', 'strike'], inplace=True)
after that I can search for complete combinations very quickly:
subx = opt.loc[('20170903', '20170907', 'C', 15)]
subx = opt.loc[('20170903', '20170907', 'C', 15):('20170703', '20170907', 'C', 15)]
Pandas has wonderful features for that - in fact, I can even do something like
However, my main issue is that when I am searching through a lot of options, it works pretty slow. So instead I did something likeCode:cond = (opt['exp']=='20170907') & (opt['cp']=='C') x = opt[cond][abs(opt['delta'] - targetDelta) == abs(opt['delta'] - targetDelta).min()]
but ranges of dates misbehave badly - it seem to do an outside join on everything instead on a single field, e.g. if i do:Code:opt = pd.read_csv('spy.csv') opt.set_index(['date', 'exp', 'cp', 'strike'], inplace=True) after that I can search for complete combinations very quickly: subx = opt.loc[('20170903', '20170907', 'C', 15)]
it will return every option that has a strike of 15 etcCode:subx = opt.loc[('20170903', '20170907', 'C', 15):('20170703', '20170907', 'C', 15)]
import xarray as xr
import pandas as pd
df=pd.read_csv('http://www.deltaneutral.com/files/Sample_SPX_20151001_to_20151030.csv')
sdf=df[ (df.type=='call') & (df.expiration=='10/16/2015') & (df.quotedate == '10/01/2015') &(df.underlying=='SPX')]
sxdf=xr.Dataset.from_dataframe(sdf)
sxdf.set_index(strike='strike', inplace=True)
sxdf.sel(strike=[621],method='nearest').to_dataframe()