def result_dicts(some_mids,some_quotes,expiration_dict,trade_date,expiries): vol_results = dict() money_results = dict() dte_info = dict() #could be appended to expiry info but... basis_info = dict() #loop through all your expiries # -- append basis to your weighted mids table (we'll store this later) # -- store vols/moneyness info into dicts temporarily for exp in expiries: und_expiry_code = underlying_code_by_two_digit_code(exp) und_sym = underlying_code(und_expiry_code,underlyings(some_quotes)).values[0] exp_dte = dte(exp,trade_date,expiration_dict) print 'Expiry : ', exp, ' :: Corresponding Underlying: ',und_sym ,' :: DTE: ', exp_dte basis_code = exp+'-'+und_expiry_code+'-'+str(exp_dte) if und_expiry_code == exp: #quarterly spot_filtered = pd.Series(some_mids[und_sym].values,index=some_mids.index) syn_spread = pd.Series(np.zeros(len(spot_filtered)),index=some_mids.index) else: spot = synthetic_offset(exp,und_sym,some_mids) if len(spot.valid()) == 0: #we were never able to calculate a synthetic basis and thus can't calc underlying continue syn_spread = univariate_kf(spot.values,spot[spot.first_valid_index()],1,500) spot_filtered = pd.Series(syn_spread+some_mids[und_sym].values,index=spot.index) some_mids[basis_code] = spot_filtered vol_results[exp] = imp_vols_cython(some_mids.ix[:,options_expiry_mask(some_mids.columns,exp).values],spot_filtered,exp_dte) money_results[exp] = pd.DataFrame(altmoneys(spot_filtered.fillna(method='ffill').fillna(method='bfill').values, kospi_strikes_from_symbols(vol_results[exp].columns.values).values,exp_dte/260.0), index = some_mids.index, columns = vol_results[exp].columns) dte_info[exp] = exp_dte basis_info[exp] = pd.Series(syn_spread,index=spot.index) return [vol_results,money_results,dte_info,basis_info]
def save_supplementary(h5_pointer,pcap_info,vols_type): s = pd.Series(h5_pointer.keys()) vol_series_list = [] basis_series_list = [] fut_bid_series_list = [] fut_ask_series_list = [] print 'Pcap Info Length entering: ',len(pcap_info) for k in s[s.str.contains(vols_type)]: two_digit_code = k[1:3] my_und = underlying_code(underlying_code_by_two_digit_code(two_digit_code),underlyings(pcap_info)).values[0] just_that_data = pcap_info[options_expiry_mask(pcap_info.symbol,two_digit_code)] just_that_fut = pcap_info[pcap_info.symbol==my_und] strikes = np.array(kospi_strikes_from_symbols(just_that_data.symbol.values),dtype=object) just_those_vols = h5_pointer[k] basis = h5_pointer['basis'][two_digit_code] basis.index = basis.index.astype(np.int64) just_that_data['vols'] = cross(strikes,just_that_data.index.astype(long),just_those_vols.index.astype(long),just_those_vols.columns.values,just_those_vols.values) just_that_data['basis'] = basis.asof(just_that_data.index).fillna(method='ffill') just_that_data['fut_bid'] = just_that_fut.bid1.asof(just_that_data.index).fillna(method='ffill') just_that_data['fut_ask'] = just_that_fut.ask1.asof(just_that_data.index).fillna(method='ffill') vol_series_list.append(just_that_data['vols']) basis_series_list.append(just_that_data['basis']) fut_bid_series_list.append(just_that_data['fut_bid']) fut_ask_series_list.append(just_that_data['fut_ask']) pcap_info['vols'] = pd.concat(vol_series_list).reindex_like(pcap_info) pcap_info['basis'] = pd.concat(basis_series_list).reindex_like(pcap_info) pcap_info['fut_bid'] = pd.concat(fut_bid_series_list).reindex_like(pcap_info) pcap_info['fut_ask'] = pd.concat(fut_ask_series_list).reindex_like(pcap_info) h5_pointer.remove('supplementary') print 'Pcap Info Length exiting: ',len(pcap_info) h5_pointer.append('supplementary',pcap_info.ix[:,['vols','basis','fut_bid','fut_ask']])
def save_vol_tables(h5_pointer,expiry_code,vol_results_dict,money_results_dict): is_call = pd.Series(vol_results_dict[expiry_code].columns.values).str[2:4]=='42' call_options = vol_results_dict[expiry_code].ix[:,is_call.values].sort_index(axis=1) call_options.columns = kospi_strikes_from_symbols(call_options.columns) call_moneys = money_results_dict[expiry_code].ix[:,is_call.values].sort_index(axis=1) call_moneys.columns = call_options.columns if not call_options.empty: #don't store empty tables (it's confusing for later analysis, no? h5_pointer.put(expiry_code+'/vols',call_options) h5_pointer.put(expiry_code+'/moneys',call_moneys)
def save_implieds(h5_pointer, new_store,start_time,end_time,MAX_AGE=50,RISK_FREE=.03,GUESS=260): s = pd.Series(h5_pointer['twmids'].columns) front_syms = s[s.str.contains('-')].str[0:2].append(s[s.str.contains('-')].str[3:5]).unique() dat = h5_pointer.select('pcap_data',[pd.Term('index','>=',start_time),pd.Term('index','<=',end_time)]) supp = h5_pointer.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(h5_pointer['dtes'].index).values] recombined_dat['tte'] = recombined_dat.symbol.str[6:8].replace(h5_pointer['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 = recombined_dat[recombined_dat.symbol.str[6:8].isin(front_syms).values] recombined_dat = recombined_dat[kospi_fresh(recombined_dat.symbol,recombined_dat.tte.values,MAX_AGE/260.)] implieds_frame = fast_implieds(recombined_dat.symbol,recombined_dat.bid1.astype(np.float64),recombined_dat.bidsize1,recombined_dat.bid2.astype(np.float64),recombined_dat.bidsize2, recombined_dat.ask1.astype(np.float64),recombined_dat.asksize1,recombined_dat.ask2.astype(np.float64),recombined_dat.asksize2, (recombined_dat.basis+recombined_dat.fut_bid),recombined_dat.basis,recombined_dat.vols,recombined_dat.tte,recombined_dat.strike.astype(np.float64), recombined_dat.type.astype(long),RISK_FREE,GUESS) implieds_frame.index = recombined_dat.index implieds_frame['delta_effect'] = net_effect_cython(recombined_dat.symbol,recombined_dat.bid1.astype(np.float64),recombined_dat.tradeprice.astype(np.float64),recombined_dat.tradesize) implieds_frame['delta'] = deltas(recombined_dat.fut_bid.astype(np.float64),recombined_dat.strike.astype(np.float64),recombined_dat.vols,recombined_dat.tte,recombined_dat.type.astype(long),.03) implieds_frame['implied_trd'] = implied_futs(recombined_dat.tradeprice.astype(np.float64),recombined_dat.strike.astype(np.float64),recombined_dat.vols,recombined_dat.tte,recombined_dat.type.astype(long),.03,26410) - recombined_dat.basis implieds_frame['implied_trd'][(implieds_frame.delta.abs()<.15).values] = np.NaN implieds_frame['delta'][(implieds_frame.delta.abs()<.15).values] = np.NaN implieds_frame['delta_effect'] *= implieds_frame['delta'] #implieds_frame = implieds_frame.drop_duplicates() implieds_frame['symbol'] = recombined_data.symbol implieds_frame['fut_bid'] = recombined_data.fut_bid implieds_frame['fut_ask'] = recombined_data.fut_ask newdat = pd.DataFrame(two_level_fix_a3s(recombined_dat.symbol,recombined_dat.msg_type.str[1:].astype(long),recombined_dat.ix[:,['bid1','bidsize1','ask1','asksize1','bid2','bidsize2','ask2','asksize2','tradeprice','tradesize']].values),columns = ['bid1','bidsize1','ask1','asksize1','bid2','bidsize2','ask2','asksize2','tradeprice','tradesize'], index = recombined_dat.index) combo = implieds_frame.join(newdat,how='inner') print 'Preparing to save implieds info... %d,%d' % (len(implieds_frame),len(combo)) if '/implieds' in new_store.keys(): print 'Deleteing old implieds frame...' new_store.remove('implieds') new_store.append('implieds',combo)
def filter_calls(some_df): is_call = pd.Series(some_df.columns.values).str[2:4]=='42' res = some_df.ix[:,is_call.values].sort_index(axis=1) res.columns = kospi_strikes_from_symbols(res.columns) return res