def get_aligned_option_indicators(**kwargs): ticker_list = kwargs['ticker_list'] settle_datetime = cu.convert_doubledate_2datetime(kwargs['settle_date']) #print(ticker_list) if 'num_cal_days_back' in kwargs.keys(): num_cal_days_back = kwargs['num_cal_days_back'] else: num_cal_days_back = 20*365 settle_datetime_from = settle_datetime-dt.timedelta(num_cal_days_back) contract_specs_output_list = [cmi.get_contract_specs(x) for x in ticker_list] ticker_head_list = [x['ticker_head'] for x in contract_specs_output_list] contract_multiplier_list = [cmi.contract_multiplier[x['ticker_head']] for x in contract_specs_output_list] cont_indx_list = [x['ticker_year']*100+x['ticker_month_num'] for x in contract_specs_output_list] month_seperation_list = [cmi.get_month_seperation_from_cont_indx(x,cont_indx_list[0]) for x in cont_indx_list] if 'option_ticker_indicator_dictionary' in kwargs.keys(): option_ticker_indicator_dictionary = kwargs['option_ticker_indicator_dictionary'] else: con = msu.get_my_sql_connection(**kwargs) unique_ticker_heads = list(set(ticker_head_list)) option_ticker_indicator_dictionary = {x: get_option_ticker_indicators(ticker_head=x, settle_date_to=kwargs['settle_date'], num_cal_days_back=num_cal_days_back, con=con) for x in unique_ticker_heads} if 'con' not in kwargs.keys(): con.close() option_ticker_indicator_dictionary_final = {ticker_list[x]: option_ticker_indicator_dictionary[ticker_head_list[x]] for x in range(len(ticker_list))} max_available_settle_list = [] tr_dte_list = [] cal_dte_list = [] imp_vol_list = [] theta_list = [] close2close_vol20_list = [] volume_list = [] open_interest_list = [] for x in range(len(ticker_list)): ticker_data = option_ticker_indicator_dictionary_final[ticker_list[x]] ticker_data = ticker_data[ticker_data['settle_date'] <= settle_datetime] option_ticker_indicator_dictionary_final[ticker_list[x]] = ticker_data ticker_data = ticker_data[ticker_data['ticker'] == ticker_list[x]] max_available_settle_list.append(ticker_data['settle_date'].iloc[-1]) last_available_settle = min(max_available_settle_list) for x in range(len(ticker_list)): ticker_data = option_ticker_indicator_dictionary_final[ticker_list[x]] ticker_data = ticker_data[(ticker_data['ticker'] == ticker_list[x]) & (ticker_data['settle_date'] == last_available_settle)] tr_dte_list.append(ticker_data['tr_dte'].iloc[0]) cal_dte_list.append(ticker_data['cal_dte'].iloc[0]) imp_vol_list.append(ticker_data['imp_vol'].iloc[0]) theta_list.append(ticker_data['theta'].iloc[0]*contract_multiplier_list[x]) close2close_vol20_list.append(ticker_data['close2close_vol20'].iloc[0]) volume_list.append(ticker_data['volume'].iloc[0]) open_interest_list.append(ticker_data['open_interest'].iloc[0]) current_data = pd.DataFrame.from_items([('ticker',ticker_list), ('tr_dte', tr_dte_list), ('cal_dte', cal_dte_list), ('imp_vol', imp_vol_list), ('theta', theta_list), ('close2close_vol20', close2close_vol20_list), ('volume', volume_list), ('open_interest', open_interest_list)]) current_data['settle_date'] = last_available_settle current_data.set_index('ticker', drop=True, inplace=True) current_data = current_data[['settle_date', 'tr_dte', 'cal_dte', 'imp_vol', 'close2close_vol20', 'theta', 'volume', 'open_interest']] aggregation_method = max([ocu.get_aggregation_method_contracts_back({'ticker_class': x['ticker_class'], 'ticker_head': x['ticker_head']})['aggregation_method'] for x in contract_specs_output_list]) if (current_data['tr_dte'].min() >= 80) and (aggregation_method == 1): aggregation_method = 3 tr_days_half_band_width_selected = ocu.tr_days_half_band_with[aggregation_method] data_frame_list = [] ref_tr_dte_list_list = [] for x in range(len(ticker_list)): ticker_data = option_ticker_indicator_dictionary_final[ticker_list[x]] if ticker_head_list[x] in ['ED', 'E0', 'E2', 'E3', 'E4', 'E5']: model = 'OU' else: model = 'BS' tr_dte_upper_band = current_data['tr_dte'].loc[ticker_list[x]]+tr_days_half_band_width_selected tr_dte_lower_band = current_data['tr_dte'].loc[ticker_list[x]]-tr_days_half_band_width_selected ref_tr_dte_list = [y for y in cmi.aligned_data_tr_dte_list if y <= tr_dte_upper_band and y>=tr_dte_lower_band] if len(ref_tr_dte_list) == 0: return {'hist': [], 'current': [], 'success': False} if aggregation_method == 12: aligned_data = [gop.load_aligend_options_data_file(ticker_head=cmi.aligned_data_tickerhead[ticker_head_list[x]], tr_dte_center=y, contract_month_letter=contract_specs_output_list[x]['ticker_month_str'], model=model) for y in ref_tr_dte_list] ticker_data = ticker_data[ticker_data['ticker_month'] == contract_specs_output_list[x]['ticker_month_num']] else: aligned_data = [gop.load_aligend_options_data_file(ticker_head=cmi.aligned_data_tickerhead[ticker_head_list[x]], tr_dte_center=y, model=model) for y in ref_tr_dte_list] aligned_data = [y[(y['trDTE'] >= current_data['tr_dte'].loc[ticker_list[x]]-tr_days_half_band_width_selected)& (y['trDTE'] <= current_data['tr_dte'].loc[ticker_list[x]]+tr_days_half_band_width_selected)] for y in aligned_data] aligned_data = pd.concat(aligned_data) aligned_data['settle_date'] = pd.to_datetime(aligned_data['settleDates'].astype('str'), format='%Y%m%d') aligned_data.rename(columns={'TickerYear': 'ticker_year', 'TickerMonth': 'ticker_month', 'trDTE': 'tr_dte', 'calDTE': 'cal_dte', 'impVol': 'imp_vol', 'close2CloseVol20': 'close2close_vol20'}, inplace=True) aligned_data.sort(['settle_date', 'ticker_year', 'ticker_month'], ascending=[True,True,True],inplace=True) aligned_data.drop_duplicates(['settle_date','ticker_year','ticker_month'],inplace=True) aligned_data['old_aligned'] = True aligned_data = aligned_data[['settle_date','ticker_month', 'ticker_year', 'cal_dte', 'tr_dte', 'imp_vol', 'close2close_vol20', 'profit5', 'old_aligned']] tr_dte_selection = (ticker_data['tr_dte'] >= current_data['tr_dte'].loc[ticker_list[x]]-tr_days_half_band_width_selected)&\ (ticker_data['tr_dte'] <= current_data['tr_dte'].loc[ticker_list[x]]+tr_days_half_band_width_selected) ticker_data = ticker_data[tr_dte_selection] ticker_data['old_aligned'] = False ticker_data['profit5'] = np.NaN ticker_data = pd.concat([aligned_data, ticker_data[['settle_date', 'ticker_month', 'ticker_year', 'cal_dte', 'tr_dte', 'imp_vol', 'close2close_vol20', 'profit5', 'old_aligned']]]) ticker_data = ticker_data[(ticker_data['settle_date'] <= settle_datetime)&(ticker_data['settle_date'] >= settle_datetime_from)] ticker_data['cont_indx'] = 100*ticker_data['ticker_year']+ticker_data['ticker_month'] ticker_data['cont_indx_adj'] = [cmi.get_cont_indx_from_month_seperation(y,-month_seperation_list[x]) for y in ticker_data['cont_indx']] data_frame_list.append(ticker_data) ref_tr_dte_list_list.append(ref_tr_dte_list) for x in range(len(ticker_list)): data_frame_list[x].set_index(['settle_date','cont_indx_adj'], inplace=True,drop=False) data_frame_list[x]['imp_vol'] = data_frame_list[x]['imp_vol'].astype('float64') merged_dataframe = pd.concat(data_frame_list, axis=1, join='inner',keys=['c'+ str(x+1) for x in range(len(ticker_list))]) merged_dataframe['abs_tr_dte_diff'] = abs(merged_dataframe['c1']['tr_dte']-tr_dte_list[0]) merged_dataframe['settle_date'] = merged_dataframe['c1']['settle_date'] merged_dataframe.sort(['settle_date', 'abs_tr_dte_diff'], ascending=[True,True], inplace=False) merged_dataframe.drop_duplicates('settle_date', inplace=True, take_last=False) merged_dataframe.index = merged_dataframe.index.droplevel(1) return {'hist': merged_dataframe, 'current': current_data, 'success': True}
def get_aligned_option_indicators(**kwargs): ticker_list = kwargs['ticker_list'] settle_datetime = cu.convert_doubledate_2datetime(kwargs['settle_date']) if 'num_cal_days_back' in kwargs.keys(): num_cal_days_back = kwargs['num_cal_days_back'] else: num_cal_days_back = 20 * 365 settle_datetime_from = settle_datetime - dt.timedelta(num_cal_days_back) contract_specs_output_list = [ cmi.get_contract_specs(x) for x in ticker_list ] ticker_head_list = [x['ticker_head'] for x in contract_specs_output_list] contract_multiplier_list = [ cmi.contract_multiplier[x['ticker_head']] for x in contract_specs_output_list ] cont_indx_list = [ x['ticker_year'] * 100 + x['ticker_month_num'] for x in contract_specs_output_list ] month_seperation_list = [ cmi.get_month_seperation_from_cont_indx(x, cont_indx_list[0]) for x in cont_indx_list ] if 'option_ticker_indicator_dictionary' in kwargs.keys(): option_ticker_indicator_dictionary = kwargs[ 'option_ticker_indicator_dictionary'] else: con = msu.get_my_sql_connection(**kwargs) unique_ticker_heads = list(set(ticker_head_list)) option_ticker_indicator_dictionary = { x: get_option_ticker_indicators(ticker_head=x, settle_date_to=kwargs['settle_date'], num_cal_days_back=num_cal_days_back, con=con) for x in unique_ticker_heads } if 'con' not in kwargs.keys(): con.close() option_ticker_indicator_dictionary_final = { ticker_list[x]: option_ticker_indicator_dictionary[ticker_head_list[x]] for x in range(len(ticker_list)) } max_available_settle_list = [] tr_dte_list = [] cal_dte_list = [] imp_vol_list = [] theta_list = [] close2close_vol20_list = [] volume_list = [] open_interest_list = [] for x in range(len(ticker_list)): ticker_data = option_ticker_indicator_dictionary_final[ticker_list[x]] ticker_data = ticker_data[ ticker_data['settle_date'] <= settle_datetime] option_ticker_indicator_dictionary_final[ticker_list[x]] = ticker_data ticker_data = ticker_data[ticker_data['ticker'] == ticker_list[x]] max_available_settle_list.append(ticker_data['settle_date'].iloc[-1]) last_available_settle = min(max_available_settle_list) for x in range(len(ticker_list)): ticker_data = option_ticker_indicator_dictionary_final[ticker_list[x]] ticker_data = ticker_data[(ticker_data['ticker'] == ticker_list[x]) & ( ticker_data['settle_date'] == last_available_settle)] tr_dte_list.append(ticker_data['tr_dte'].iloc[0]) cal_dte_list.append(ticker_data['cal_dte'].iloc[0]) imp_vol_list.append(ticker_data['imp_vol'].iloc[0]) theta_list.append(ticker_data['theta'].iloc[0] * contract_multiplier_list[x]) close2close_vol20_list.append(ticker_data['close2close_vol20'].iloc[0]) volume_list.append(ticker_data['volume'].iloc[0]) open_interest_list.append(ticker_data['open_interest'].iloc[0]) current_data = pd.DataFrame.from_dict({ 'ticker': ticker_list, 'tr_dte': tr_dte_list, 'cal_dte': cal_dte_list, 'imp_vol': imp_vol_list, 'theta': theta_list, 'close2close_vol20': close2close_vol20_list, 'volume': volume_list, 'open_interest': open_interest_list }) current_data['settle_date'] = last_available_settle current_data.set_index('ticker', drop=True, inplace=True) current_data = current_data[[ 'settle_date', 'tr_dte', 'cal_dte', 'imp_vol', 'close2close_vol20', 'theta', 'volume', 'open_interest' ]] aggregation_method = max([ ocu.get_aggregation_method_contracts_back({ 'ticker_class': x['ticker_class'], 'ticker_head': x['ticker_head'] })['aggregation_method'] for x in contract_specs_output_list ]) if (current_data['tr_dte'].min() >= 80) and (aggregation_method == 1): aggregation_method = 3 tr_days_half_band_width_selected = ocu.tr_days_half_band_with[ aggregation_method] data_frame_list = [] ref_tr_dte_list_list = [] for x in range(len(ticker_list)): ticker_data = option_ticker_indicator_dictionary_final[ticker_list[x]] if ticker_head_list[x] in ['ED', 'E0', 'E2', 'E3', 'E4', 'E5']: model = 'OU' else: model = 'BS' tr_dte_upper_band = current_data['tr_dte'].loc[ ticker_list[x]] + tr_days_half_band_width_selected tr_dte_lower_band = current_data['tr_dte'].loc[ ticker_list[x]] - tr_days_half_band_width_selected ref_tr_dte_list = [ y for y in cmi.aligned_data_tr_dte_list if y <= tr_dte_upper_band and y >= tr_dte_lower_band ] if len(ref_tr_dte_list) == 0: return {'hist': [], 'current': [], 'success': False} if aggregation_method == 12: aligned_data = [ gop.load_aligend_options_data_file( ticker_head=cmi.aligned_data_tickerhead[ ticker_head_list[x]], tr_dte_center=y, contract_month_letter=contract_specs_output_list[x] ['ticker_month_str'], model=model) for y in ref_tr_dte_list ] ticker_data = ticker_data[ticker_data['ticker_month'] == contract_specs_output_list[x] ['ticker_month_num']] else: aligned_data = [ gop.load_aligend_options_data_file( ticker_head=cmi.aligned_data_tickerhead[ ticker_head_list[x]], tr_dte_center=y, model=model) for y in ref_tr_dte_list ] aligned_data = [ y[(y['trDTE'] >= current_data['tr_dte'].loc[ticker_list[x]] - tr_days_half_band_width_selected) & (y['trDTE'] <= current_data['tr_dte'].loc[ticker_list[x]] + tr_days_half_band_width_selected)] for y in aligned_data ] aligned_data = pd.concat(aligned_data) aligned_data['settle_date'] = pd.to_datetime( aligned_data['settleDates'].astype('str'), format='%Y%m%d') aligned_data.rename(columns={ 'TickerYear': 'ticker_year', 'TickerMonth': 'ticker_month', 'trDTE': 'tr_dte', 'calDTE': 'cal_dte', 'impVol': 'imp_vol', 'close2CloseVol20': 'close2close_vol20' }, inplace=True) aligned_data.sort_values( ['settle_date', 'ticker_year', 'ticker_month'], ascending=[True, True, True], inplace=True) aligned_data.drop_duplicates( ['settle_date', 'ticker_year', 'ticker_month'], inplace=True) aligned_data['old_aligned'] = True aligned_data = aligned_data[[ 'settle_date', 'ticker_month', 'ticker_year', 'cal_dte', 'tr_dte', 'imp_vol', 'close2close_vol20', 'profit5', 'old_aligned' ]] tr_dte_selection = (ticker_data['tr_dte'] >= current_data['tr_dte'].loc[ticker_list[x]]-tr_days_half_band_width_selected)&\ (ticker_data['tr_dte'] <= current_data['tr_dte'].loc[ticker_list[x]]+tr_days_half_band_width_selected) ticker_data = ticker_data[tr_dte_selection] ticker_data['old_aligned'] = False ticker_data['profit5'] = ( ticker_data['option_pnl5'] - ticker_data['delta_pnl5'] ).astype(float) * cmi.contract_multiplier[ticker_head_list[x]] #ticker_data['profit5'] = np.nan ticker_data = pd.concat([ aligned_data, ticker_data[[ 'settle_date', 'ticker_month', 'ticker_year', 'cal_dte', 'tr_dte', 'imp_vol', 'close2close_vol20', 'profit5', 'old_aligned' ]] ]) ticker_data = ticker_data[ (ticker_data['settle_date'] <= settle_datetime) & (ticker_data['settle_date'] >= settle_datetime_from)] ticker_data['cont_indx'] = 100 * ticker_data[ 'ticker_year'] + ticker_data['ticker_month'] ticker_data['cont_indx_adj'] = [ cmi.get_cont_indx_from_month_seperation(y, -month_seperation_list[x]) for y in ticker_data['cont_indx'] ] data_frame_list.append(ticker_data) ref_tr_dte_list_list.append(ref_tr_dte_list) for x in range(len(ticker_list)): data_frame_list[x].set_index(['settle_date', 'cont_indx_adj'], inplace=True, drop=False) data_frame_list[x]['imp_vol'] = data_frame_list[x]['imp_vol'].astype( 'float64') merged_dataframe = pd.concat( data_frame_list, axis=1, join='inner', keys=['c' + str(x + 1) for x in range(len(ticker_list))]) merged_dataframe['abs_tr_dte_diff'] = abs( merged_dataframe['c1']['tr_dte'] - tr_dte_list[0]) merged_dataframe['settle_date'] = merged_dataframe['c1']['settle_date'] merged_dataframe.index.names = [ 'settle_date_index' if x is 'settle_date' else x for x in merged_dataframe.index.names ] merged_dataframe.sort_values(['settle_date', 'abs_tr_dte_diff'], ascending=[True, True], inplace=False) unique_index = np.unique(merged_dataframe['settle_date'], return_index=True)[1] merged_dataframe = merged_dataframe.iloc[unique_index] merged_dataframe.index = merged_dataframe.index.droplevel(1) return {'hist': merged_dataframe, 'current': current_data, 'success': True}
def get_aligned_option_indicators_legacy(**kwargs): ticker_list = kwargs['ticker_list'] tr_dte_list = kwargs['tr_dte_list'] settle_datetime = cu.convert_doubledate_2datetime(kwargs['settle_date']) if 'num_cal_days_back' in kwargs.keys(): num_cal_days_back = kwargs['num_cal_days_back'] else: num_cal_days_back = 20*365 settle_datetime_from = settle_datetime-dt.timedelta(num_cal_days_back) contract_specs_output_list = [cmi.get_contract_specs(x) for x in ticker_list] ticker_head_list = [x['ticker_head'] for x in contract_specs_output_list] cont_indx_list = [x['ticker_year']*100+x['ticker_month_num'] for x in contract_specs_output_list] month_seperation_list = [cmi.get_month_seperation_from_cont_indx(x,cont_indx_list[0]) for x in cont_indx_list] aggregation_method = max([ocu.get_aggregation_method_contracts_back({'ticker_class': x['ticker_class'], 'ticker_head': x['ticker_head']})['aggregation_method'] for x in contract_specs_output_list]) if (min(tr_dte_list) >= 80) and (aggregation_method == 1): aggregation_method = 3 tr_days_half_band_width_selected = ocu.tr_days_half_band_with[aggregation_method] data_frame_list = [] for x in range(len(ticker_list)): if ticker_head_list[x] in ['ED', 'E0', 'E2', 'E3', 'E4', 'E5']: model = 'OU' else: model = 'BS' tr_dte_upper_band = tr_dte_list[x]+tr_days_half_band_width_selected tr_dte_lower_band = tr_dte_list[x]-tr_days_half_band_width_selected ref_tr_dte_list = [y for y in cmi.aligned_data_tr_dte_list if y <= tr_dte_upper_band and y>=tr_dte_lower_band] if len(ref_tr_dte_list) == 0: return {'hist': [], 'current': [], 'success': False} if aggregation_method == 12: aligned_data = [gop.load_aligend_options_data_file(ticker_head=cmi.aligned_data_tickerhead[ticker_head_list[x]], tr_dte_center=y, contract_month_letter=contract_specs_output_list[x]['ticker_month_str'], model=model) for y in ref_tr_dte_list] else: aligned_data = [gop.load_aligend_options_data_file(ticker_head=cmi.aligned_data_tickerhead[ticker_head_list[x]], tr_dte_center=y, model=model) for y in ref_tr_dte_list] aligned_data = [y[(y['trDTE'] >= tr_dte_lower_band)&(y['trDTE'] <= tr_dte_upper_band)] for y in aligned_data] aligned_data = pd.concat(aligned_data) aligned_data.drop('theta', axis=1, inplace=True) aligned_data['settle_date'] = pd.to_datetime(aligned_data['settleDates'].astype('str'), format='%Y%m%d') aligned_data = aligned_data[(aligned_data['settle_date'] <= settle_datetime)&(aligned_data['settle_date'] >= settle_datetime_from)] aligned_data.rename(columns={'TickerYear': 'ticker_year', 'TickerMonth': 'ticker_month', 'trDTE': 'tr_dte', 'calDTE': 'cal_dte', 'impVol': 'imp_vol', 'close2CloseVol20': 'close2close_vol20', 'dollarTheta': 'theta'}, inplace=True) aligned_data.sort(['settle_date', 'ticker_year', 'ticker_month'], ascending=[True,True,True],inplace=True) aligned_data.drop_duplicates(['settle_date','ticker_year','ticker_month'],inplace=True) aligned_data = aligned_data[['settle_date','ticker_month', 'ticker_year', 'cal_dte', 'tr_dte', 'imp_vol', 'theta', 'close2close_vol20', 'profit5']] aligned_data['cont_indx'] = 100*aligned_data['ticker_year']+aligned_data['ticker_month'] aligned_data['cont_indx_adj'] = [cmi.get_cont_indx_from_month_seperation(y,-month_seperation_list[x]) for y in aligned_data['cont_indx']] data_frame_list.append(aligned_data) for x in range(len(ticker_list)): data_frame_list[x].set_index(['settle_date','cont_indx_adj'], inplace=True,drop=False) merged_dataframe = pd.concat(data_frame_list, axis=1, join='inner',keys=['c'+ str(x+1) for x in range(len(ticker_list))]) merged_dataframe['abs_tr_dte_diff'] = abs(merged_dataframe['c1']['tr_dte']-tr_dte_list[0]) merged_dataframe['settle_date'] = merged_dataframe['c1']['settle_date'] merged_dataframe.sort(['settle_date', 'abs_tr_dte_diff'], ascending=[True,True], inplace=False) merged_dataframe.drop_duplicates('settle_date', inplace=True, take_last=False) merged_dataframe.index = merged_dataframe.index.droplevel(1) tr_dte_list = [] cal_dte_list = [] imp_vol_list = [] theta_list = [] close2close_vol20_list = [] for x in range(len(ticker_list)): selected_data = merged_dataframe['c' + str(x+1)] if settle_datetime in selected_data.index: selected_data = selected_data.loc[settle_datetime] else: return {'hist': [], 'current': [], 'success': False} if selected_data['cont_indx'] != cont_indx_list[x]: return {'hist': [], 'current': [], 'success': False} tr_dte_list.append(selected_data['tr_dte']) cal_dte_list.append(selected_data['cal_dte']) imp_vol_list.append(selected_data['imp_vol']) theta_list.append(selected_data['theta']) close2close_vol20_list.append(selected_data['close2close_vol20']) current_data = pd.DataFrame.from_items([('ticker',ticker_list), ('tr_dte', tr_dte_list), ('cal_dte', cal_dte_list), ('imp_vol', imp_vol_list), ('theta', theta_list), ('close2close_vol20', close2close_vol20_list)]) current_data['settle_date'] = settle_datetime current_data.set_index('ticker', drop=True, inplace=True) return {'hist': merged_dataframe, 'current': current_data, 'success': True}
def get_aligned_option_indicators_legacy(**kwargs): ticker_list = kwargs['ticker_list'] tr_dte_list = kwargs['tr_dte_list'] settle_datetime = cu.convert_doubledate_2datetime(kwargs['settle_date']) if 'num_cal_days_back' in kwargs.keys(): num_cal_days_back = kwargs['num_cal_days_back'] else: num_cal_days_back = 20 * 365 settle_datetime_from = settle_datetime - dt.timedelta(num_cal_days_back) contract_specs_output_list = [ cmi.get_contract_specs(x) for x in ticker_list ] ticker_head_list = [x['ticker_head'] for x in contract_specs_output_list] cont_indx_list = [ x['ticker_year'] * 100 + x['ticker_month_num'] for x in contract_specs_output_list ] month_seperation_list = [ cmi.get_month_seperation_from_cont_indx(x, cont_indx_list[0]) for x in cont_indx_list ] aggregation_method = max([ ocu.get_aggregation_method_contracts_back({ 'ticker_class': x['ticker_class'], 'ticker_head': x['ticker_head'] })['aggregation_method'] for x in contract_specs_output_list ]) if (min(tr_dte_list) >= 80) and (aggregation_method == 1): aggregation_method = 3 tr_days_half_band_width_selected = ocu.tr_days_half_band_with[ aggregation_method] data_frame_list = [] for x in range(len(ticker_list)): if ticker_head_list[x] in ['ED', 'E0', 'E2', 'E3', 'E4', 'E5']: model = 'OU' else: model = 'BS' tr_dte_upper_band = tr_dte_list[x] + tr_days_half_band_width_selected tr_dte_lower_band = tr_dte_list[x] - tr_days_half_band_width_selected ref_tr_dte_list = [ y for y in cmi.aligned_data_tr_dte_list if y <= tr_dte_upper_band and y >= tr_dte_lower_band ] if len(ref_tr_dte_list) == 0: return {'hist': [], 'current': [], 'success': False} if aggregation_method == 12: aligned_data = [ gop.load_aligend_options_data_file( ticker_head=cmi.aligned_data_tickerhead[ ticker_head_list[x]], tr_dte_center=y, contract_month_letter=contract_specs_output_list[x] ['ticker_month_str'], model=model) for y in ref_tr_dte_list ] else: aligned_data = [ gop.load_aligend_options_data_file( ticker_head=cmi.aligned_data_tickerhead[ ticker_head_list[x]], tr_dte_center=y, model=model) for y in ref_tr_dte_list ] aligned_data = [ y[(y['trDTE'] >= tr_dte_lower_band) & (y['trDTE'] <= tr_dte_upper_band)] for y in aligned_data ] aligned_data = pd.concat(aligned_data) aligned_data.drop('theta', axis=1, inplace=True) aligned_data['settle_date'] = pd.to_datetime( aligned_data['settleDates'].astype('str'), format='%Y%m%d') aligned_data = aligned_data[ (aligned_data['settle_date'] <= settle_datetime) & (aligned_data['settle_date'] >= settle_datetime_from)] aligned_data.rename(columns={ 'TickerYear': 'ticker_year', 'TickerMonth': 'ticker_month', 'trDTE': 'tr_dte', 'calDTE': 'cal_dte', 'impVol': 'imp_vol', 'close2CloseVol20': 'close2close_vol20', 'dollarTheta': 'theta' }, inplace=True) aligned_data.sort(['settle_date', 'ticker_year', 'ticker_month'], ascending=[True, True, True], inplace=True) aligned_data.drop_duplicates( ['settle_date', 'ticker_year', 'ticker_month'], inplace=True) aligned_data = aligned_data[[ 'settle_date', 'ticker_month', 'ticker_year', 'cal_dte', 'tr_dte', 'imp_vol', 'theta', 'close2close_vol20', 'profit5' ]] aligned_data['cont_indx'] = 100 * aligned_data[ 'ticker_year'] + aligned_data['ticker_month'] aligned_data['cont_indx_adj'] = [ cmi.get_cont_indx_from_month_seperation(y, -month_seperation_list[x]) for y in aligned_data['cont_indx'] ] data_frame_list.append(aligned_data) for x in range(len(ticker_list)): data_frame_list[x].set_index(['settle_date', 'cont_indx_adj'], inplace=True, drop=False) merged_dataframe = pd.concat( data_frame_list, axis=1, join='inner', keys=['c' + str(x + 1) for x in range(len(ticker_list))]) merged_dataframe['abs_tr_dte_diff'] = abs( merged_dataframe['c1']['tr_dte'] - tr_dte_list[0]) merged_dataframe['settle_date'] = merged_dataframe['c1']['settle_date'] merged_dataframe.sort(['settle_date', 'abs_tr_dte_diff'], ascending=[True, True], inplace=False) merged_dataframe.drop_duplicates('settle_date', inplace=True, take_last=False) merged_dataframe.index = merged_dataframe.index.droplevel(1) tr_dte_list = [] cal_dte_list = [] imp_vol_list = [] theta_list = [] close2close_vol20_list = [] for x in range(len(ticker_list)): selected_data = merged_dataframe['c' + str(x + 1)] if settle_datetime in selected_data.index: selected_data = selected_data.loc[settle_datetime] else: return {'hist': [], 'current': [], 'success': False} if selected_data['cont_indx'] != cont_indx_list[x]: return {'hist': [], 'current': [], 'success': False} tr_dte_list.append(selected_data['tr_dte']) cal_dte_list.append(selected_data['cal_dte']) imp_vol_list.append(selected_data['imp_vol']) theta_list.append(selected_data['theta']) close2close_vol20_list.append(selected_data['close2close_vol20']) current_data = pd.DataFrame.from_items([('ticker', ticker_list), ('tr_dte', tr_dte_list), ('cal_dte', cal_dte_list), ('imp_vol', imp_vol_list), ('theta', theta_list), ('close2close_vol20', close2close_vol20_list)]) current_data['settle_date'] = settle_datetime current_data.set_index('ticker', drop=True, inplace=True) return {'hist': merged_dataframe, 'current': current_data, 'success': True}