def load_weight_china_index_date(self, date): """ 利用自由流通市值作为指数权重 """ date = Date().change_to_str(date) date_1y = Date().get_normal_date_offset(date, -180) data = Stock().get_free_market_value_date(date) ipo_data = Stock().get_ipo_date() data = pd.concat([data, ipo_data], axis=1) data = data[data['IPO_DATE'] <= date_1y] data = data[data['DELIST_DATE'] >= date] code = "China_Index_Benchmark" data = data.dropna() free_mv_sum = data['Free_Market_Value'].sum() weight = pd.DataFrame(data['Free_Market_Value'].values / free_mv_sum, index=data.index, columns=['WEIGHT']) weight.index.name = "CODE" out_sub_path = os.path.join(self.load_out_path_weight, code) if not os.path.exists(out_sub_path): os.makedirs(out_sub_path) out_file = os.path.join(out_sub_path, date + '.csv') weight.to_csv(out_file)
def cal_factor_exposure(self, beg_date, end_date): """ 计算因子暴露 """ # params long_term = 40 short_term = int(long_term * 0.5) min_term = int(long_term * 0.8) # read data trade_amount = Stock().read_factor_h5("TradeAmount").T trade_amount = trade_amount.dropna(how='all') # calculate data daily date_series = Date().get_trade_date_series(beg_date, end_date) date_series = list(set(trade_amount.index) & set(date_series)) date_series.sort() res = pd.DataFrame([]) for i in range(0, len(date_series)): current_date = date_series[i] data_beg_date = Date().get_trade_date_offset( current_date, -(long_term - 1)) amount_before = trade_amount.loc[data_beg_date:current_date, :] amount_before = amount_before.fillna(0.0) if len(amount_before) >= min_term: print('Calculating factor %s at date %s' % (self.raw_factor_name, current_date)) zero_number = amount_before.applymap( lambda x: 1.0 if x == 0.0 else 0.0).sum() code_filter_list = ( zero_number[zero_number < short_term]).index amount_pre = trade_amount.loc[data_beg_date:current_date, code_filter_list] amount_pre_cv = -amount_pre.std() / amount_pre.mean() amount_pre_cv = pd.DataFrame(amount_pre_cv) amount_pre_cv.columns = [current_date] else: print('Calculating factor %s at date %s is null' % (self.raw_factor_name, current_date)) amount_pre_cv = pd.DataFrame([], columns=[current_date], index=trade_amount.columns) res = pd.concat([res, amount_pre_cv], axis=1) res = res.T.dropna(how='all').T self.save_alpha_factor_exposure(res, self.raw_factor_name)
def cal_factor_exposure(self, beg_date, end_date): """ 计算因子暴露 """ # params long_term = 120 short_term = int(long_term * 0.5) # read data trade_amount = Stock().read_factor_h5("TradeAmount").T / 100000000 trade_amount = trade_amount.dropna(how='all') # calculate data daily date_series = Date().get_trade_date_series(beg_date, end_date) date_series = list(set(trade_amount.index) & set(date_series)) date_series.sort() res = pd.DataFrame([]) for i in range(0, len(date_series)): current_date = date_series[i] data_beg_date = Date().get_trade_date_offset(current_date, -(long_term - 1)) amount_before = trade_amount.loc[data_beg_date:current_date, :] amount_before = amount_before.fillna(0.0) if len(amount_before) == long_term: print('Calculating factor %s at date %s' % (self.raw_factor_name, current_date)) zero_number = amount_before.applymap(lambda x: 1.0 if x == 0.0 else 0.0).sum() code_filter_list = (zero_number[zero_number < short_term]).index amount_before = trade_amount.loc[data_beg_date:current_date, code_filter_list] amount_log = amount_before.applymap(lambda x: np.nan if x == 0 else -np.log(x)) weight = np.array(list(range(1, long_term + 1))) weight_amount = np.dot(amount_log.T.values, weight) weight_amount = pd.DataFrame(weight_amount, index=amount_log.columns, columns=[current_date]) else: print('Calculating factor %s at date %s is null' % (self.raw_factor_name, current_date)) weight_amount = pd.DataFrame([], columns=[current_date], index=trade_amount.columns) res = pd.concat([res, weight_amount], axis=1) res = res.T.dropna(how='all').T self.save_alpha_factor_exposure(res, self.raw_factor_name)