Skip to content

Instantly share code, notes, and snippets.

@raddy
Last active December 24, 2015 21:59
Show Gist options
  • Save raddy/6869869 to your computer and use it in GitHub Desktop.
Save raddy/6869869 to your computer and use it in GitHub Desktop.
Example of working with those h5 files
import pandas as pd
import numpy as np
from bsm_implieds import implied_fut,implied_futs
def is_kospi(symbol_list):
s = pd.Series(symbol_list)
is_call = s.str[2:6]=='4201'
is_put = s.str[2:6]=='4301'
is_fut = s.str[2:6]=='4101'
return np.logical_or(is_call,np.logical_or(is_put,is_fut))
def kospi_types_from_symbols(symbol_list):
res = np.zeros(len(symbol_list))
is_call = pd.Series(symbol_list).str[2:4]=='42'
is_put = pd.Series(symbol_list).str[2:4]=='43'
res[is_call] = 1
res[is_put] = -1
return res
def kospi_strikes_from_symbols(symbol_list):
strikes = pd.Series(symbol_list).str[8:11].astype(float)
strikes[strikes%5 != 0 ] += .5
return (strikes * 100).values
fn = '/Users/green/2013-09-25T08:00:00_2013-09-25T15:15:00.h5'
string_date = fn.split('/')[-1].split('T')[0]
store = pd.HDFStore(fn)
start_time = pd.Timestamp(string_date+'T09:00:00').value
end_time = pd.Timestamp(string_date+'T15:05:0').value
dat = store.select('pcap_data',[pd.Term('index','>=',start_time),pd.Term('index','<=',end_time)])
supp = store.select('supplementary',[pd.Term('index','>=',start_time),pd.Term('index','<=',end_time)])
recombined_dat = (dat.join(supp,how='outer'))
recombined_dat = recombined_dat[is_kospi(dat.symbol).values]
expiries = recombined_dat.symbol.str[6:8]
recombined_dat = recombined_dat[expiries.isin(store['dtes'].index).values]
recombined_dat['tte'] = recombined_dat.symbol.str[6:8].replace(store['dtes'].to_dict()[0]).astype(float) / 260.
recombined_dat['strike'] = kospi_strikes_from_symbols(recombined_dat.symbol.values)
recombined_dat['type'] = kospi_types_from_symbols(recombined_dat.symbol.values)
recombined_dat['implied_bid'] = implied_futs(recombined_dat.bid1.astype(double),recombined_dat.strike.astype(double),recombined_dat.vols,recombined_dat.tte,recombined_dat.type.astype(long),.03,26410) - recombined_dat.basis
recombined_dat['implied_ask'] = implied_futs(recombined_dat.ask1.astype(double),recombined_dat.strike.astype(double),recombined_dat.vols,recombined_dat.tte,recombined_dat.type.astype(long),.03,26410) - recombined_dat.basis
brief = ['symbol','bid1','ask1','vols','basis','fut_bid','fut_ask','tte','strike','type','implied_bid','implied_ask']
print 'Some random (semi-illustrative) slice: '
print recombined_dat.ix[1380099784890515000:1380099784900515000,brief]
print 'Note that put/call stuff needs to be reversed...too lazy righ now'
del dat
del supp
store.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment