def mix_factor_mcap_(self, data_name, grow_1_name, grow_2_name, sector_df, percent): df = bt.AZ_Load_csv(f'{self.root_path}/EM_Funda/daily/R_{data_name}_TTM_First.csv') \ .reindex(index=self.xinx, columns=self.xnms) mcap = bt.AZ_Load_csv(f'{self.root_path}/EM_Funda/LICO_YS_STOCKVALUE/AmarketCap.csv') \ .reindex(index=self.xinx, columns=self.xnms) grow_1 = bt.AZ_Load_csv(f'{self.root_path}/EM_Funda/daily/R_{data_name}_TTM_{grow_1_name}.csv') \ .reindex(index=self.xinx, columns=self.xnms) grow_2 = bt.AZ_Load_csv(f'{self.root_path}/EM_Funda/daily/R_{data_name}_TTM_{grow_2_name}.csv') \ .reindex(index=self.xinx, columns=self.xnms) std = bt.AZ_Load_csv(f'{self.root_path}/EM_Funda/daily/R_{data_name}_TTM_QSD4Y.csv') \ .reindex(index=self.xinx, columns=self.xnms) target_df = self.mix_factor_mcap(df, mcap, grow_1, grow_2, std, sector_df, percent) file_name = '{}_and_mcap_{}_{}_{}'.format(data_name, grow_1_name, grow_2_name, percent) fun = 'EM_Funda.EM_Funda_Deal.mix_factor_mcap' raw_data_path = (f'EM_Funda/daily/R_{data_name}_TTM_First.csv', 'EM_Funda/LICO_YS_STOCKVALUE/AmarketCap.csv', f'EM_Funda/daily/R_{data_name}_TTM_{grow_1_name}.csv', f'EM_Funda/daily/R_{data_name}_TTM_{grow_2_name}.csv', f'EM_Funda/daily/R_{data_name}_TTM_QSD4Y.csv') args = (percent, ) self.judge_save_fun(target_df, file_name, self.save_root_path, fun, raw_data_path, args)
def mix_factor_asset_(self, data_name, grow_1_name, grow_2_name, sector_df, percent): df = bt.AZ_Load_csv(f'{self.root_path}/EM_Funda/daily/R_{data_name}_TTM_First.csv') \ .reindex(index=self.xinx, columns=self.xnms) asset = bt.AZ_Load_csv(f'{self.root_path}/EM_Funda/daily/R_SUMASSET_First.csv') \ .reindex(index=self.xinx, columns=self.xnms) grow_1 = bt.AZ_Load_csv(f'{self.root_path}/EM_Funda/daily/R_{data_name}_TTM_{grow_1_name}.csv') \ .reindex(index=self.xinx, columns=self.xnms) grow_2 = bt.AZ_Load_csv(f'{self.root_path}/EM_Funda/daily/R_{data_name}_TTM_{grow_2_name}.csv') \ .reindex(index=self.xinx, columns=self.xnms) std = bt.AZ_Load_csv(f'{self.root_path}/EM_Funda/daily/R_{data_name}_TTM_QSD4Y.csv') \ .reindex(index=self.xinx, columns=self.xnms) target_df = self.mix_factor_asset(df, asset, grow_1, grow_2, std, sector_df, percent) file_name = '{}_and_asset_{}_{}_{}'.format(data_name, grow_1_name, grow_2_name, percent) fun = 'EM_Funda.EM_Funda_Deal.mix_factor_asset' raw_data_path = (f'EM_Funda/daily/R_{data_name}_TTM_First.csv', 'EM_Funda/daily/R_SUMASSET_First.csv', f'EM_Funda/daily/R_{data_name}_TTM_{grow_1_name}.csv', f'EM_Funda/daily/R_{data_name}_TTM_{grow_2_name}.csv', f'EM_Funda/daily/R_{data_name}_TTM_QSD4Y.csv') args = (percent, ) self.judge_save_fun(target_df, file_name, self.save_root_path, fun, raw_data_path, args)
def get_corr_matrix(cut_date=None): pos_file_list = [ x for x in os.listdir('/mnt/mfs/AAPOS') if x.startswith('WHS') ] return_df = bt.AZ_Load_csv( '/mnt/mfs/DAT_EQT/EM_Funda/DERIVED_14/aadj_r.csv').astype(float) index_df_1 = load_index_data('000300', return_df.index).fillna(0) index_df_2 = load_index_data('000905', return_df.index).fillna(0) sum_pnl_df = pd.DataFrame() for pos_file_name in pos_file_list: pos_df = bt.AZ_Load_csv('/mnt/mfs/AAPOS/{}'.format(pos_file_name)) cond_1 = 'IF01' in pos_df.columns cond_2 = 'IC01' in pos_df.columns if cond_1 and cond_2: hedge_df = 0.5 * index_df_1 + 0.5 * index_df_2 return_df_c = return_df.sub(hedge_df, axis=0) elif cond_1: hedge_df = index_df_1 return_df_c = return_df.sub(hedge_df, axis=0) elif cond_2: hedge_df = index_df_2 return_df_c = return_df.sub(hedge_df, axis=0) else: print('alpha hedge error') continue pnl_df = (pos_df.shift(2) * return_df_c).sum(axis=1) pnl_df.name = pos_file_name sum_pnl_df = pd.concat([sum_pnl_df, pnl_df], axis=1) # plot_send_result(pnl_df, bt.AZ_Sharpe_y(pnl_df), 'mix_factor') if cut_date is not None: sum_pnl_df = sum_pnl_df[sum_pnl_df.index > cut_date] return sum_pnl_df
def get_new_stock_info(self, xnms, xinx): new_stock_data = bt.AZ_Load_csv(self.root_path.EM_Tab01.CDSY_SECUCODE / 'LISTSTATE.csv') new_stock_data.fillna(method='ffill', inplace=True) # 获取交易日信息 return_df = bt.AZ_Load_csv(self.root_path.EM_Funda.DERIVED_14 / 'aadj_r.csv').astype(float) trade_time = return_df.index new_stock_data = new_stock_data.reindex(index=trade_time).fillna( method='ffill') target_df = new_stock_data.shift(40).notnull().astype(int) target_df = target_df.reindex(columns=xnms, index=xinx) return target_df
def dev_row_extre2_(self, percent): change_list = [ 'R_ACCOUNTPAY_QYOY', 'R_ACCOUNTREC_QYOY', 'R_ASSETDEVALUELOSS_s_QYOY', 'R_Cashflow_s_YOY_First', 'R_CFO_s_YOY_First', 'R_CostSales_QYOY', 'R_EBITDA2_QYOY', 'R_EPSDiluted_YOY_First', 'R_ESTATEINVEST_QYOY', 'R_FairVal_TotProfit_QYOY', 'R_FINANCEEXP_s_QYOY', 'R_GrossProfit_TTM_QYOY', 'R_GSCF_sales_QYOY', 'R_IntDebt_Mcap_QYOY', 'R_INVESTINCOME_s_QYOY', 'R_LTDebt_WorkCap_QYOY', 'R_LOANREC_s_QYOY', 'R_NetAssets_s_YOY_First', 'R_NetInc_s_QYOY', 'R_NETPROFIT_s_QYOY', 'R_OPCF_TTM_QYOY', 'R_OperCost_sales_QYOY', 'R_OperProfit_YOY_First', 'R_OPEX_sales_QYOY', 'R_ROE1_QYOY', 'R_SUMLIAB_QYOY', 'R_TangAssets_IntDebt_QYOY', 'R_WorkCapital_QYOY', 'R_OTHERLASSET_QYOY', ] for file_name in change_list: data1_path = self.load_path / f'{file_name}.csv' data2_path = self.load_path / ( '_'.join(file_name.split('_')[:-1]) + '_QTTM.csv') data1 = bt.AZ_Load_csv(data1_path) data2 = bt.AZ_Load_csv(data2_path) target_df = self.dev_row_extre(data1, data2, self.sector_df, percent) file_name = '{}_and_{}_{}'.format(file_name, 'QTTM', percent) fun = 'EM_Funda.EM_Funda_Deal.dev_row_extre' raw_data_path = (data1_path, data2_path) args = (percent, ) self.judge_save_fun(target_df, file_name, self.save_root_path, fun, raw_data_path, args)
def __init__(self, sector_df, root_path, table_num, table_name, data_name, save_root_path, if_replace=False): xnms = sector_df.columns xinx = sector_df.index data = bt.AZ_Load_csv( getattr(getattr(root_path, table_num), table_name) / (data_name + '.csv')) if if_replace: self.raw_df = data.replace(0, np.nan).reindex(columns=xnms, index=xinx) else: self.raw_df = data.reindex(columns=xnms, index=xinx) self.sector_df = sector_df self.table_num = table_num self.table_name = table_name self.data_name = data_name self.save_root_path = save_root_path self.factor_to_fun = '/mnt/mfs/dat_whs/data/factor_to_fun' self.if_replace = if_replace
def get_st_stock_info(self, xnms, xinx): data = bt.AZ_Load_csv(self.root_path.EM_Tab01.CDSY_CHANGEINFO / 'CHANGEA.csv') data = data.reindex(columns=xnms, index=xinx) data.fillna(method='ffill', inplace=True) data = data.astype(str) target_df = data.applymap(lambda x: 0 if 'ST' in x or 'PT' in x else 1) return target_df
def __init__(self, sector_df, root_path, save_root_path): xnms = sector_df.columns xinx = sector_df.index self.sector_df = sector_df self.load_path = root_path.EM_Funda.daily self.part_load_path = 'EM_Funda/daily' self.R_IntDebt_Y3YGR_path = self.load_path / 'R_IntDebt_Y3YGR.csv' self.R_IntDebt_Y3YGR = bt.AZ_Load_csv( self.R_IntDebt_Y3YGR_path).reindex(columns=xnms, index=xinx) self.R_SUMASSET_First_path = self.load_path / 'R_SUMASSET_First.csv' self.R_SUMASSET_First = bt.AZ_Load_csv( self.R_SUMASSET_First_path).reindex(columns=xnms, index=xinx) # self.R_EBITDA_QTTM_path = self.load_path / 'R_EBITDA_QTTM.csv' # self.R_EBITDA_QTTM = bt.AZ_Load_csv(self.R_EBITDA_QTTM_path).reindex(columns=xnms, index=xinx) self.MCAP_path = root_path.EM_Funda.LICO_YS_STOCKVALUE / 'AmarketCapExStri.csv' self.MCAP = bt.AZ_Load_csv(self.MCAP_path).reindex(columns=xnms, index=xinx) self.R_EBITDA_QYOY_path = self.load_path / 'R_EBITDA_QYOY.csv' self.R_EBITDA_QYOY = bt.AZ_Load_csv(self.R_EBITDA_QYOY_path).reindex( columns=xnms, index=xinx) self.R_EBIT2_Y3YGR_path = self.load_path / 'R_EBIT2_Y3YGR.csv' self.R_EBIT2_Y3YGR = bt.AZ_Load_csv(self.R_EBIT2_Y3YGR_path).reindex( columns=xnms, index=xinx) self.R_SalesGrossMGN_QYOY_path = self.load_path / 'R_SalesGrossMGN_QYOY.csv' self.R_SalesGrossMGN_QYOY = bt.AZ_Load_csv( self.R_SalesGrossMGN_QYOY_path).reindex(columns=xnms, index=xinx) self.R_SalesGrossMGN_QTTM_path = self.load_path / 'R_SalesGrossMGN_QTTM.csv' self.R_SalesGrossMGN_QTTM = bt.AZ_Load_csv( self.R_SalesGrossMGN_QTTM_path).reindex(columns=xnms, index=xinx) self.R_OPCF_CurrentLiab_First_path = self.load_path / 'R_OPCF_CurrentLiab_First.csv' self.R_OPCF_CurrentLiab_First = bt.AZ_Load_csv(self.R_OPCF_CurrentLiab_First_path) \ .reindex(columns=xnms, index=xinx) self.R_CurrentRatio_QTTM_path = self.load_path / 'R_CurrentRatio_QTTM.csv' self.R_CurrentRatio_QTTM = bt.AZ_Load_csv( self.R_CurrentRatio_QTTM_path).reindex(columns=xnms, index=xinx) self.save_root_path = save_root_path
def load_sector_data(self, begin_date, end_date, sector_name): if sector_name.startswith('index'): index_name = sector_name.split('_')[-1] market_top_n = bt.AZ_Load_csv( self.root_path.EM_Funda.IDEX_YS_WEIGHT_A / f'SECURITYNAME_{index_name}.csv') market_top_n[market_top_n == market_top_n] = 1 else: market_top_n = bt.AZ_Load_csv(self.root_path.EM_Funda.DERIVED_10 / f'{sector_name}.csv') market_top_n = market_top_n[(market_top_n.index >= begin_date) & (market_top_n.index < end_date)] market_top_n.dropna(how='all', axis='columns', inplace=True) xnms = market_top_n.columns xinx = market_top_n.index new_stock_df = self.get_new_stock_info(xnms, xinx) st_stock_df = self.get_st_stock_info(xnms, xinx) sector_df = market_top_n * new_stock_df * st_stock_df sector_df.replace(0, np.nan, inplace=True) return sector_df
def __init__(self, sector_df, root_path, save_root_path): xnms = sector_df.columns xinx = sector_df.index self.load_path = root_path.EM_Funda.DERIVED_14 self.part_load_path = 'EM_Funda/DERIVED_14' self.open_path = self.load_path / 'aadj_p_OPEN.csv' self.open = bt.AZ_Load_csv(self.open_path).reindex(columns=xnms, index=xinx) self.high_path = self.load_path / 'aadj_p_HIGH.csv' self.high = bt.AZ_Load_csv(self.high_path).reindex(columns=xnms, index=xinx) self.low_path = self.load_path / 'aadj_p_LOW.csv' self.low = bt.AZ_Load_csv(self.low_path).reindex(columns=xnms, index=xinx) self.close_path = self.load_path / 'aadj_p.csv' self.close = bt.AZ_Load_csv(self.close_path).reindex(columns=xnms, index=xinx) self.volume_path = root_path.EM_Funda.TRAD_SK_DAILY_JC / 'TVOL.csv' self.volume = bt.AZ_Load_csv(self.volume_path).reindex(columns=xnms, index=xinx) self.amount_path = root_path.EM_Funda.TRAD_SK_DAILY_JC / 'TVALCNY.csv' self.amount = bt.AZ_Load_csv(self.amount_path).reindex(columns=xnms, index=xinx) self.turnrate_path = root_path.EM_Funda.TRAD_SK_DAILY_JC / 'TURNRATE.csv' self.turnrate = bt.AZ_Load_csv(self.turnrate_path).reindex( columns=xnms, index=xinx) self.aadj_r_path = self.load_path / 'aadj_r.csv' self.aadj_r = bt.AZ_Load_csv(self.aadj_r_path).reindex(columns=xnms, index=xinx) self.sector_df = sector_df self.save_root_path = save_root_path self.factor_to_fun = '/mnt/mfs/dat_whs/data/factor_to_fun'
def __init__(self, sector_df, root_path, save_root_path): xnms = sector_df.columns xinx = sector_df.index self.load_path = root_path.EM_Funda.DERIVED_14 self.part_load_path1 = 'EM_Funda/TRAD_SK_DAILY_JC' self.part_load_path = 'EM_Funda/DERIVED_14' self.sector_open = bt.AZ_Load_csv(root_path.EM_Funda.DERIVED_14 / 'aadj_p_OPEN.csv')\ .reindex(columns=xnms, index=xinx) self.sector_high = bt.AZ_Load_csv(root_path.EM_Funda.DERIVED_14 / 'aadj_p_HIGH.csv')\ .reindex(columns=xnms, index=xinx) self.sector_low = bt.AZ_Load_csv(root_path.EM_Funda.DERIVED_14 / 'aadj_p_LOW.csv')\ .reindex(columns=xnms, index=xinx) self.sector_close = bt.AZ_Load_csv(root_path.EM_Funda.DERIVED_14 / 'aadj_p.csv')\ .reindex(columns=xnms, index=xinx) self.sector_volume = bt.AZ_Load_csv(root_path.EM_Funda.TRAD_SK_DAILY_JC / 'TVOL.csv')\ .reindex(columns=xnms, index=xinx) self.sector_amount = bt.AZ_Load_csv(root_path.EM_Funda.TRAD_SK_DAILY_JC / 'TVALCNY.csv')\ .reindex(columns=xnms, index=xinx) self.sector_adj_r = bt.AZ_Load_csv(root_path.EM_Funda.DERIVED_14 / 'aadj_r.csv')\ .reindex(columns=xnms, index=xinx) self.sector_df = sector_df self.save_root_path = save_root_path self.factor_to_fun = '/mnt/mfs/dat_whs/data/factor_to_fun'
def load_daily_data(self, file_name): return bt.AZ_Load_csv(f'{self.root_path}/EM_Funda/daily/{file_name}.csv')\ .reindex(index=self.xinx, columns=self.xnms)
def load_index_data(index_name, xinx): data = bt.AZ_Load_csv( os.path.join('/mnt/mfs/DAT_EQT', 'EM_Tab09/INDEX_TD_DAILYSYS/CHG.csv')) target_df = data[index_name].reindex(index=xinx) return target_df * 0.01