def get_single_contracts_4date(**kwargs): option_frame = ops.get_option_ticker_indicators(**kwargs) if 'open_interest_filter' in kwargs.keys(): open_interest_filter = kwargs['open_interest_filter'] else: open_interest_filter = 100 option_frame = option_frame[ option_frame['open_interest'] >= open_interest_filter] option_frame['ticker_class'] = [ cmi.ticker_class[x] for x in option_frame['ticker_head'] ] selection_indx = (option_frame['ticker_class'] == 'Livestock') | (option_frame['ticker_class'] == 'Ag') | \ (option_frame['ticker_class'] == 'Treasury') | (option_frame['ticker_head'] == 'CL') | \ (option_frame['ticker_class'] == 'FX') | (option_frame['ticker_class'] == 'Index') | \ (option_frame['ticker_class'] == 'Metal') #selection_indx = (option_frame['ticker_head'] == 'S') option_frame = option_frame[selection_indx] option_frame = option_frame[option_frame['tr_dte'] >= 20] option_frame.reset_index(drop=True, inplace=True) return option_frame[['ticker', 'ticker_head', 'ticker_class', 'tr_dte']]
def get_single_contracts_4date(**kwargs): option_frame = ops.get_option_ticker_indicators(**kwargs) if 'open_interest_filter' in kwargs.keys(): open_interest_filter = kwargs['open_interest_filter'] else: open_interest_filter = 100 option_frame = option_frame[option_frame['open_interest']>=open_interest_filter] option_frame['ticker_class'] = [cmi.ticker_class[x] for x in option_frame['ticker_head']] selection_indx = (option_frame['ticker_class'] == 'Livestock') | (option_frame['ticker_class'] == 'Ag') | \ (option_frame['ticker_class'] == 'Treasury') | (option_frame['ticker_head'] == 'CL') | \ (option_frame['ticker_class'] == 'FX') | (option_frame['ticker_class'] == 'Index') | \ (option_frame['ticker_class'] == 'Metal') #selection_indx = (option_frame['ticker_head'] == 'S') option_frame = option_frame[selection_indx] option_frame = option_frame[option_frame['tr_dte'] >= 20] option_frame.reset_index(drop=True,inplace=True) return option_frame[['ticker','ticker_head','ticker_class','tr_dte']]
def get_vcs_pairs_4date(**kwargs): option_frame = ops.get_option_ticker_indicators(**kwargs) if 'open_interest_filter' in kwargs.keys(): open_interest_filter = kwargs['open_interest_filter'] else: open_interest_filter = 100 option_frame = option_frame[ option_frame['open_interest'] >= open_interest_filter] option_frame['ticker_class'] = [ cmi.ticker_class[x] for x in option_frame['ticker_head'] ] selection_indx = (option_frame['ticker_class'] == 'Livestock') | (option_frame['ticker_class'] == 'Ag') | \ (option_frame['ticker_class'] == 'Treasury') | (option_frame['ticker_class'] == 'Energy') | \ (option_frame['ticker_class'] == 'FX') | (option_frame['ticker_class'] == 'Index') | \ (option_frame['ticker_class'] == 'Metal') option_frame = option_frame[selection_indx] option_frame = option_frame[option_frame['tr_dte'] >= 35] option_frame.reset_index(drop=True, inplace=True) unique_ticker_heads = option_frame['ticker_head'].unique() tuples = [] for ticker_head_i in unique_ticker_heads: ticker_head_data = option_frame[option_frame['ticker_head'] == ticker_head_i] ticker_head_data.sort_values(['ticker_year', 'ticker_month'], ascending=[True, True], inplace=True) if len(ticker_head_data.index) >= 2: for i in range(len(ticker_head_data.index) - 1): for j in range(i + 1, len(ticker_head_data.index)): tuples = tuples + [ (ticker_head_data.index[i], ticker_head_data.index[j]) ] return pd.DataFrame( [(option_frame['ticker'][indx[0]], option_frame['ticker'][indx[1]], option_frame['ticker_head'][indx[0]], option_frame['ticker_class'][indx[0]], option_frame['tr_dte'][indx[0]], option_frame['tr_dte'][indx[1]]) for indx in tuples], columns=[ 'ticker1', 'ticker2', 'tickerHead', 'tickerClass', 'trDte1', 'trDte2' ])
def get_vcs_pairs_4date(**kwargs): option_frame = ops.get_option_ticker_indicators(**kwargs) if 'open_interest_filter' in kwargs.keys(): open_interest_filter = kwargs['open_interest_filter'] else: open_interest_filter = 100 option_frame = option_frame[option_frame['open_interest']>=open_interest_filter] option_frame['ticker_class'] = [cmi.ticker_class[x] for x in option_frame['ticker_head']] selection_indx = (option_frame['ticker_class'] == 'Livestock') | (option_frame['ticker_class'] == 'Ag') | \ (option_frame['ticker_class'] == 'Treasury') | (option_frame['ticker_class'] == 'Energy') | \ (option_frame['ticker_class'] == 'FX') | (option_frame['ticker_class'] == 'Index') | \ (option_frame['ticker_class'] == 'Metal') option_frame = option_frame[selection_indx] option_frame = option_frame[option_frame['tr_dte'] >= 35] option_frame.reset_index(drop=True,inplace=True) unique_ticker_heads = option_frame['ticker_head'].unique() tuples = [] for ticker_head_i in unique_ticker_heads: ticker_head_data = option_frame[option_frame['ticker_head'] == ticker_head_i] ticker_head_data.sort(['ticker_year', 'ticker_month'], ascending=[True, True], inplace=True) if len(ticker_head_data.index) >= 2: for i in range(len(ticker_head_data.index)-1): for j in range(i+1,len(ticker_head_data.index)): tuples = tuples + [(ticker_head_data.index[i], ticker_head_data.index[j])] return pd.DataFrame([(option_frame['ticker'][indx[0]], option_frame['ticker'][indx[1]], option_frame['ticker_head'][indx[0]], option_frame['ticker_class'][indx[0]], option_frame['tr_dte'][indx[0]], option_frame['tr_dte'][indx[1]]) for indx in tuples],columns=['ticker1','ticker2','tickerHead','tickerClass','trDte1','trDte2'])
def load_pnls_4settle_date(**kwargs): if 'con' not in kwargs.keys(): close_connection_before_exit = True con = msu.get_my_sql_connection(**kwargs) kwargs['con'] = con else: close_connection_before_exit = False con = kwargs['con'] if not exp.is_business_day(double_date=kwargs['settle_date']): if close_connection_before_exit: con.close() return settle_date_ex = exp.doubledate_shift_bus_days( double_date=kwargs['settle_date'], shift_in_days=7) indicator_frame = ops.get_option_ticker_indicators( settle_date=settle_date_ex, delta=0.5, column_names=['id', 'ticker', 'price_date', 'tr_dte', 'ticker_head']) if indicator_frame.empty: if close_connection_before_exit: con.close() return indicator_frame.reset_index(drop=True, inplace=True) delta_vol_output = [ ops.calc_delta_vol_4ticker(ticker=x, settle_date=settle_date_ex, delta_target=0.5) for x in indicator_frame['ticker'] ] delta_vol_output = pd.DataFrame(delta_vol_output) indicator_frame['option_pnl5'] = delta_vol_output['option_pnl5'] indicator_frame['delta_pnl5'] = delta_vol_output['delta_pnl5'] indicator_frame.dropna(inplace=True) column_names = indicator_frame.columns.tolist() #indicator_frame['option_pnl5'] = indicator_frame['option_pnl5'].astype(object) #indicator_frame['delta_pnl5'] = indicator_frame['delta_pnl5'].astype(object) id_indx = column_names.index('id') option_pnl_indx = column_names.index('option_pnl5') delta_pnl_indx = column_names.index('delta_pnl5') tuples = [ tuple([x[option_pnl_indx], x[delta_pnl_indx], x[id_indx]]) for x in indicator_frame.values ] final_str = "UPDATE option_ticker_indicators SET option_pnl5 = %s, delta_pnl5 = %s WHERE id=%s" msu.sql_execute_many_wrapper(final_str=final_str, tuples=tuples, con=con) if close_connection_before_exit: con.close()
def generate_vcs_sheet_4date(**kwargs): kwargs['settle_date'] = kwargs['date_to'] num_cal_days_back = 20*365 output_dir = ts.create_strategy_output_dir(strategy_class='vcs', report_date=kwargs['date_to']) if os.path.isfile(output_dir + '/summary.pkl'): vcs_pairs = pd.read_pickle(output_dir + '/summary.pkl') return {'vcs_pairs': vcs_pairs,'success': True} vcs_pairs = get_vcs_pairs_4date(**kwargs) num_pairs = len(vcs_pairs.index) unique_ticker_heads = list(set(vcs_pairs['tickerHead'])) con = msu.get_my_sql_connection(**kwargs) option_ticker_indicator_dictionary = {x: ops.get_option_ticker_indicators(ticker_head=x, settle_date_to=kwargs['date_to'], num_cal_days_back=num_cal_days_back, con=con) for x in unique_ticker_heads} if 'con' not in kwargs.keys(): con.close() q_list = [None]*num_pairs q1_list = [None]*num_pairs fwd_vol_q_list = [None]*num_pairs downside_list = [None]*num_pairs upside_list = [None]*num_pairs atm_vol_ratio_list = [None]*num_pairs real_vol_ratio_list = [None]*num_pairs atm_real_vol_ratio_list = [None]*num_pairs theta_list = [None]*num_pairs fwd_vol = [None]*num_pairs for i in range(num_pairs): vcs_output = ops.get_vcs_signals(ticker_list=[vcs_pairs['ticker1'].iloc[i], vcs_pairs['ticker2'].iloc[i]], option_ticker_indicator_dictionary=option_ticker_indicator_dictionary, settle_date=kwargs['date_to']) q_list[i] = vcs_output['q'] q1_list[i] = vcs_output['q1'] fwd_vol_q_list[i] = vcs_output['fwd_vol_q'] downside_list[i] = vcs_output['downside'] upside_list[i] = vcs_output['upside'] atm_vol_ratio_list[i] = vcs_output['atm_vol_ratio'] fwd_vol[i] = vcs_output['fwd_vol'] real_vol_ratio_list[i] = vcs_output['real_vol_ratio'] atm_real_vol_ratio_list[i] = vcs_output['atm_real_vol_ratio'] theta_list[i] = vcs_output['theta'] vcs_pairs['Q'] = q_list vcs_pairs['Q1'] = q1_list vcs_pairs['fwdVolQ'] = fwd_vol_q_list vcs_pairs['downside'] = downside_list vcs_pairs['upside'] = upside_list vcs_pairs['atmVolRatio'] = atm_vol_ratio_list vcs_pairs['fwdVol'] = fwd_vol vcs_pairs['realVolRatio'] = real_vol_ratio_list vcs_pairs['atmRealVolRatio'] = atm_real_vol_ratio_list vcs_pairs['theta'] = theta_list vcs_pairs['downside'] = vcs_pairs['downside'].round(3) vcs_pairs['upside'] = vcs_pairs['upside'].round(3) vcs_pairs['atmVolRatio'] = vcs_pairs['atmVolRatio'].round(3) vcs_pairs['fwdVol'] = vcs_pairs['fwdVol'].round(3) vcs_pairs['realVolRatio'] = vcs_pairs['realVolRatio'].round(3) vcs_pairs['atmRealVolRatio'] = vcs_pairs['atmRealVolRatio'].round(3) vcs_pairs['theta'] = vcs_pairs['theta'].round(3) vcs_pairs.to_pickle(output_dir + '/summary.pkl') return {'vcs_pairs': vcs_pairs,'success': True}
def generate_vcs_sheet_4date(**kwargs): kwargs['settle_date'] = kwargs['date_to'] num_cal_days_back = 20 * 365 output_dir = ts.create_strategy_output_dir(strategy_class='vcs', report_date=kwargs['date_to']) if os.path.isfile(output_dir + '/summary.pkl'): vcs_pairs = pd.read_pickle(output_dir + '/summary.pkl') return {'vcs_pairs': vcs_pairs, 'success': True} vcs_pairs = get_vcs_pairs_4date(**kwargs) num_pairs = len(vcs_pairs.index) unique_ticker_heads = list(set(vcs_pairs['tickerHead'])) con = msu.get_my_sql_connection(**kwargs) option_ticker_indicator_dictionary = { x: ops.get_option_ticker_indicators(ticker_head=x, settle_date_to=kwargs['date_to'], num_cal_days_back=num_cal_days_back, con=con) for x in unique_ticker_heads } if 'con' not in kwargs.keys(): con.close() q_list = [None] * num_pairs q1_list = [None] * num_pairs fwd_vol_q_list = [None] * num_pairs downside_list = [None] * num_pairs upside_list = [None] * num_pairs atm_vol_ratio_list = [None] * num_pairs real_vol_ratio_list = [None] * num_pairs atm_real_vol_ratio_list = [None] * num_pairs theta_list = [None] * num_pairs fwd_vol = [None] * num_pairs for i in range(num_pairs): vcs_output = ops.get_vcs_signals(ticker_list=[ vcs_pairs['ticker1'].iloc[i], vcs_pairs['ticker2'].iloc[i] ], option_ticker_indicator_dictionary= option_ticker_indicator_dictionary, settle_date=kwargs['date_to']) q_list[i] = vcs_output['q'] q1_list[i] = vcs_output['q1'] fwd_vol_q_list[i] = vcs_output['fwd_vol_q'] downside_list[i] = vcs_output['downside'] upside_list[i] = vcs_output['upside'] atm_vol_ratio_list[i] = vcs_output['atm_vol_ratio'] fwd_vol[i] = vcs_output['fwd_vol'] real_vol_ratio_list[i] = vcs_output['real_vol_ratio'] atm_real_vol_ratio_list[i] = vcs_output['atm_real_vol_ratio'] theta_list[i] = vcs_output['theta'] vcs_pairs['Q'] = q_list vcs_pairs['Q1'] = q1_list vcs_pairs['fwdVolQ'] = fwd_vol_q_list vcs_pairs['downside'] = downside_list vcs_pairs['upside'] = upside_list vcs_pairs['atmVolRatio'] = atm_vol_ratio_list vcs_pairs['fwdVol'] = fwd_vol vcs_pairs['realVolRatio'] = real_vol_ratio_list vcs_pairs['atmRealVolRatio'] = atm_real_vol_ratio_list vcs_pairs['theta'] = theta_list vcs_pairs['downside'] = vcs_pairs['downside'].round(3) vcs_pairs['upside'] = vcs_pairs['upside'].round(3) vcs_pairs['atmVolRatio'] = vcs_pairs['atmVolRatio'].round(3) vcs_pairs['fwdVol'] = vcs_pairs['fwdVol'].round(3) vcs_pairs['realVolRatio'] = vcs_pairs['realVolRatio'].round(3) vcs_pairs['atmRealVolRatio'] = vcs_pairs['atmRealVolRatio'].round(3) vcs_pairs['theta'] = vcs_pairs['theta'].round(3) vcs_pairs.to_pickle(output_dir + '/summary.pkl') return {'vcs_pairs': vcs_pairs, 'success': True}