def peg(self): # PEG = PE / 过去12个月的EPS增长率 pe_daily = self.pe_daily basicepsyoy = self.basicepsyoy basicepsyoy = adjust_months(basicepsyoy) epsyoy = append_df(basicepsyoy, target_feq='D', fill_type='preceding') pe_daily = CALFUNC.del_dat_early_than(pe_daily, START_YEAR) epsyoy = CALFUNC.del_dat_early_than(epsyoy, START_YEAR) [pe_daily, epsyoy] = align(pe_daily, epsyoy) [h, l] = pe_daily.shape pe_ar = pe_daily.values eps_ar = epsyoy.values res = np.zeros([h, l]) for i in range(0, h): for j in range(0, l): if pd.isna(eps_ar[i, j]) or eps_ar[i, j] == 0: res[i, j] = np.nan else: res[i, j] = pe_ar[i, j] / eps_ar[i, j] res_df = pd.DataFrame(data=res, index=pe_daily.index, columns=pe_daily.columns) return res_df
def mgmt_ben_top3m(self): mgmt_ben_top3m = self.stmnote_mgmt_ben_top3m mgmt_ben_top3m = np.log(mgmt_ben_top3m) mgmt_ben_top3m = adjust_months(mgmt_ben_top3m, orig='Y') mgmt_ben_top3m = append_df(mgmt_ben_top3m) return mgmt_ben_top3m
def Profit_G_q(self): # qfa_yoyprofit:单季度.净利润同比增长率 netprofit = self.netprofitcut # 扣除非经常损益后的净利润 sig_season_netprofit = get_signal_season_value(netprofit) p_g = CALFUNC.generate_yoygr(sig_season_netprofit) p_g = adjust_months(p_g) p_g = append_df(p_g) profit_g_q = CALFUNC.del_dat_early_than(p_g, START_YEAR) return profit_g_q
def ROE_G_q(self): # 单季度.ROE同比增长率 roe = self.roe sig_season_roe = get_signal_season_value(roe) roe_g = CALFUNC.generate_yoygr(sig_season_roe) roe_g = adjust_months(roe_g) roe_g = append_df(roe_g) roe_g_q = CALFUNC.del_dat_early_than(roe_g, START_YEAR) return roe_g_q
def grossprofitmargin_ttm(self): gir = self.grossincomeratiottm gir = adjust_months(gir) # 用来扩展月度数据 gir = append_df(gir) res = CALFUNC.del_dat_early_than(gir, START_YEAR) return res
def Sales_G_q(self): # qfa_yoysales:单季度.营业收入同比增长率 operatingrevenue = self.operatingrevenue sig_season_operatingrevenue = get_signal_season_value(operatingrevenue) sales_g = CALFUNC.generate_yoygr(sig_season_operatingrevenue) sales_g = adjust_months(sales_g) sales_g = append_df(sales_g) sales_g = CALFUNC.del_dat_early_than(sales_g, START_YEAR) return sales_g
def totalassetturnover(self): totalassettrate = self.totalassettrate tmp0 = adjust_months(totalassettrate) tmp1 = append_df(tmp0) res = CALFUNC.del_dat_early_than(tmp1, START_YEAR) return res
def ROA_q(self): totalassets = self.totalassets netprofit = self.netprofit # 得到单季度 净利润 sig_season_netprofit = get_signal_season_value(netprofit) # 得到季度平均总资产 s_mean_totalassets = get_season_mean_value(totalassets) roa_q = (sig_season_netprofit / s_mean_totalassets) * 100 roa_q = adjust_months(roa_q) roa_q = append_df(roa_q) roa_q = CALFUNC.del_dat_early_than(roa_q, START_YEAR) return roa_q
def ROE_q(self): totalshareholderequity = self.totalshareholderequity netprofit = self.netprofit # 得到单季度 净利润 sig_season_netprofit = get_signal_season_value(netprofit) # 得到季度平均总资产 s_mean_equity = get_season_mean_value(totalshareholderequity) roe_q = (sig_season_netprofit / s_mean_equity) * 100 roe_q = adjust_months(roe_q) roe_q = append_df(roe_q) roe_q = CALFUNC.del_dat_early_than(roe_q, START_YEAR) return roe_q
def assetturnover_q(self): totalassets = self.totalassets revenue = self.operatingrevenue # 得到单季度 净利润 sig_season_revenue = get_signal_season_value(revenue) # 得到季度平均总资产 s_mean_totalassets = get_season_mean_value(totalassets) turnover_q = (sig_season_revenue / s_mean_totalassets) * 100 turnover_q = adjust_months(turnover_q) turnover_q = append_df(turnover_q) turnover_q = CALFUNC.del_dat_early_than(turnover_q, START_YEAR) return turnover_q
def grossprofitmargin_diff(self): revenue = self.operatingrevenue # 营业收入 cost = self.operatingcost # 营业成本 # 财务指标常规处理,移动月份,改月末日期 revenue_q = get_signal_season_value(revenue) cost_q = get_signal_season_value(cost) gross_q = (revenue_q - cost_q) / revenue_q gir_d = CALFUNC.generate_diff(gross_q) gir_d = adjust_months(gir_d) # 用来扩展月度数据 gir_d = append_df(gir_d) res = CALFUNC.del_dat_early_than(gir_d, START_YEAR) return res
def RDtosales(self): data = Data() rd_exp = data.rd_exp revenue = data.operatingrevenue rd_exp = CALFUNC.del_dat_early_than(rd_exp, 2018) revenue = CALFUNC.del_dat_early_than(revenue, 2018) res = rd_exp / revenue res = adjust_months(res) res = append_df(res) to_del = res.columns[res.isna().sum() / len(res) > 0.9] res.drop(to_del, axis=1, inplace=True) return res
def grossprofitmargin_q(self): ''' 计算公示:(营业收入 - 营业成本) / 营业收入 * 100 % 计算单季度指标,应该先对 营业收入 和 营业成本 分别计算单季度指标,再计算 ''' revenue = self.operatingrevenue # 营业收入 cost = self.operatingcost # 营业成本 # 财务指标常规处理,移动月份,改月末日期 revenue_q = get_signal_season_value(revenue) cost_q = get_signal_season_value(cost) gross_q = (revenue_q - cost_q) / revenue_q # 调整为公告日期 tmp = adjust_months(gross_q) # 用来扩展月度数据 tmp = append_df(tmp) res = CALFUNC.del_dat_early_than(tmp, START_YEAR) return res
def Profitmargin_q(self): # 单季度净利润率 ''' 1.qfa_deductedprofit:单季度.扣除非经常损益后的净利润 2.qfa_oper_rev: 单季度.营业收入 :return: ''' netprofit = self.netprofitcut # 扣除非经常损益后的净利润 operatingrevenue = self.operatingrevenue sig_season_netprofit = get_signal_season_value(netprofit) sig_season_operatingrevenue = get_signal_season_value(operatingrevenue) profitmargin_q = sig_season_netprofit / sig_season_operatingrevenue profitmargin_q = adjust_months(profitmargin_q) profitmargin_q = append_df(profitmargin_q) pq = CALFUNC.del_dat_early_than(profitmargin_q, START_YEAR) return pq
def REVSU(self): netprofit = self.totaloperatingrevenueps # 得到单季度的数据。 sig_season_va = get_signal_season_value(netprofit) cols = pd.DataFrame([i for i in sig_season_va.columns]) revsu = pd.DataFrame() rolling_cols = rolling_windows(cols, 6) for roll in rolling_cols: res = _calculate_su_simple(sig_season_va[roll]) res = pd.DataFrame(res.values, index=res.index, columns=[roll[-1]]) revsu = pd.concat([revsu, res], axis=1) revsu.dropna(how='all', axis=0, inplace=True) revsu = adjust_months(revsu) revsu = append_df(revsu) return revsu
def SUE(self): # 使用原始的财务数据 eps = self.basiceps # 得到单季度的数据。 sig_season_va = get_signal_season_value(eps) cols = pd.DataFrame([i for i in sig_season_va.columns]) sue = pd.DataFrame() rolling_cols = rolling_windows(cols, 6) for roll in rolling_cols: res = _calculate_su_simple(sig_season_va[roll]) res = pd.DataFrame(res.values, index=res.index, columns=[roll[-1]]) sue = pd.concat([sue, res], axis=1) sue.dropna(how='all', axis=0, inplace=True) sue = adjust_months(sue) sue = append_df(sue) return sue
def layer_test(factor_name, pool): para_dict = {'因子名称': factor_name, 'max_para': 30, 'min_para': 1, } pool_tmp = test_mode(pool, para_dict) pool_tmp = append_df(pool_tmp, target_feq='D') pool_wei_tmp = get_wei(pool_tmp) pool_wei_tmp = append_df(pool_wei_tmp, target_feq='D') # 简单回测 daily_return_bottom, net_cpd_1_30, cum_excess_df = easy_bt(pool_wei_tmp, basic_return_path=None) para_dict = {'因子名称': factor_name, 'max_para': 69, 'min_para': 31, } pool_tmp = test_mode(pool, para_dict) pool_tmp = append_df(pool_tmp, target_feq='D') pool_wei_tmp = get_wei(pool_tmp) pool_wei_tmp = append_df(pool_wei_tmp, target_feq='D') # 简单回测 daily_return, net_cpd_31_69, cum_excess_df = easy_bt(pool_wei_tmp, basic_return_path=None) para_dict = {'因子名称': factor_name, 'max_para': 100, 'min_para': 70, } pool_tmp = test_mode(pool, para_dict) pool_tmp = append_df(pool_tmp, target_feq='D') pool_wei_tmp = get_wei(pool_tmp) pool_wei_tmp = append_df(pool_wei_tmp, target_feq='D') # 简单回测 daily_return_top, net_cpd_70_100, cum_excess_df = easy_bt(pool_wei_tmp, basic_return_path=None) res_df = pd.DataFrame({'top': net_cpd_70_100['net_value'], 'middle': net_cpd_31_69['net_value'], 'bottom': net_cpd_1_30['net_value']}) fig = plt.figure() plt.plot(res_df) plt.legend(list(res_df.columns)) plt.show() top_sub_bottom = daily_return_top['daily_return'] - daily_return_bottom['daily_return'] top_sub_bottom_cum = (top_sub_bottom + 1).cumprod() top_sub_bottom_df = pd.DataFrame({"top减bottom累计收益": top_sub_bottom_cum}) top_sub_bottom_df.index.name = '日期' fig = plt.figure() plt.plot(top_sub_bottom_df) plt.show() save_path = r'D:\pythoncode\IndexEnhancement\股票池\计算机' res_df.to_csv(os.path.join(save_path, factor_name+'_3.csv')) top_sub_bottom_df.to_csv(os.path.join(save_path, factor_name+'_top减bottom_3.csv'))
def financial_condition_pool(selection_dict, start_date, end_date): data = Data() stock_basic = data.stock_basic_inform firstindustry = stock_basic[['中信一级行业']] all_stocks_code = stock_basic[['sec_name'.upper(), 'ipo_date'.upper()]] # roe roettm = data.roettm # 净利润同比增速 netprofitgrowrate = data.netprofitgrowrate # 基本每股收益同比增长率 basicepsyoy = data.basicepsyoy # 销售毛利率 grossincome = data.grossincomeratiottm # 资产负债率 debtassetsratio = data.debtassetsratio # 估值 pe = data.pe cond_total = pd.DataFrame() for plate_name, conditions_dict in selection_dict.items(): pe_con = None con_plate = None if plate_name == 'all': codes_in_industry = list(firstindustry.index) else: codes_in_industry = [ind for ind in firstindustry.index if firstindustry[ind] == plate_name] for conditions_type, tuples in conditions_dict.items(): if conditions_type.split('_')[0] == 'scope': myfun = 'scopy_condition' for t in tuples: if t == 'pe': has_pe = 1 pe_con = copy.deepcopy(t) continue res = eval('select_stocks(' + tuples[0] + ', codes_in_industry, start_date, end_date)') res_con = eval(myfun + '(res, minV=' + str(tuples[1]) + ', maxV= ' + str(tuples[2]) + ')') if isinstance(con_plate, pd.DataFrame): con_plate = con_plate & res_con else: con_plate = res_con elif conditions_type.split('_')[0] == 'rise': myfun = 'rise_condition' # for t in tuples: res = eval('select_stocks(' + tuples[0] + ', codes_in_industry, start_date, end_date)') res_con = eval(myfun + '(res,' + str(tuples[1]) + ')') if isinstance(con_plate, pd.DataFrame): con_plate = con_plate & res_con else: con_plate = res_con # 不同行业之间合并 cond_total = pd.concat([cond_total, con_plate], axis=1) # 剔除上市未满N年得股票,N = 1 # N = 1 # for col, items in cond_total.iteritems(): # for i in items.index: # if items[i]: # if i in all_stocks_code.index: # de = col - all_stocks_code.loc[i, 'ipo_date'] # if de.days < N * 365: # items[i] = False # else: # # i 不在all_stocks_code里面,是因为all_stocks_code没有更新,说明该股票是最近上市的股票, # # 直接全部复制为False # items[i] = False # 剔除st for col, items in cond_total.iteritems(): for i in items.index: if i in all_stocks_code.index and 'ST' in all_stocks_code.loc[i, 'sec_name'.upper()]: items[i] = False # 调整为公告日期 cond_total = adjust_months(cond_total) # 用来扩展月度数据 cond_total = append_df(cond_total) return cond_total
def ROA_ttm(self): roa_ttm = self.roattm roa_ttm = adjust_months(roa_ttm) roa_ttm = append_df(roa_ttm) roa_ttm = CALFUNC.del_dat_early_than(roa_ttm, START_YEAR) return roa_ttm