def factor_leverage(cur_date): # 现金比率,-1列为rank,-2列为所需要的 fa_cashtoliqdebt = om.get_fa_cashtoliqdebt(cur_date) # 流动比率,-1列为rank,-2列为所需要的 fa_current = om.get_fa_current(cur_date) # 权益乘数,-1列为rank,-2列为所需要的 fa_assetstoequity = om.get_fa_assetstoequity(cur_date) # 获取个股市值(带index) index_mkvalue = om.get_index_mkvalue(cur_date) cashration = fa_cashtoliqdebt.iloc[:, -2] # 现金比率 currentratio = fa_current.iloc[:, -2] # 流动比率 equitymultiplier = fa_assetstoequity.iloc[:, -2] ln_capital = np.log(index_mkvalue.iloc[:, -1]) # 市值对数 # 入库 stock_list = om.get_all_stock(cur_date) cashration_flag = fh.df_to_list(cur_date, cashration, 'cashration', stock_list) currentratio_flag = fh.df_to_list(cur_date, currentratio, 'currentratio', stock_list) equitymultiplier_flag = fh.df_to_list(cur_date, equitymultiplier, 'equitymultiplier', stock_list) ln_capital_flag = fh.df_to_list(cur_date, ln_capital, 'ln_capital', stock_list) if cashration_flag & currentratio_flag & equitymultiplier_flag & ln_capital_flag != 1: gl.logger.error('factor_leverage insert fail') return 0 else: gl.logger.info('factor_leverage insert successful') return 1
def factor_growth(cur_date): # 单季度 营业收入环比增长率,-1列为rank,-2列为所需要的 qfa_cgrsales = om.get_qfa_cgrsales(cur_date) # 营业收入同比增长率,-1列为rank,-2列为所需要的 fa_yoy_or = om.get_fa_yoy_or(cur_date) # 单季度.净利润同比增长率(%),-1列为rank,-2列为所需要的 qfa_yoyprofit = om.get_qfa_yoyprofit(cur_date) # 同比增长率-归属母公司股东的净利润-扣除非经常损益(%),-1列为rank,-2列为所需要的 profit_deducted = om.get_profit_deducted(cur_date) # 同比增长率-经营活动产生的现金流量净额(%),-1列为rank,-2列为所需要的 fa_yoyocf = om.get_fa_yoyocf(cur_date) # 同比增长率-归属母公司股东的净利润-扣除非经常损益(%),-1列为rank,-2列为所需要的 profit_deducted_second = om.get_profit_deducted(cur_date - 10000) # 同比增长率-归属母公司股东的净利润-扣除非经常损益(%),-1列为rank,-2列为所需要的 profit_deducted_third = om.get_profit_deducted(cur_date - 20000) # 同比增长率-经营活动产生的现金流量净额(%),-1列为rank,-2列为所需要的 fa_yoyocf_second = om.get_fa_yoyocf(cur_date - 10000) # 同比增长率-经营活动产生的现金流量净额(%),-1列为rank,-2列为所需要的 fa_yoyocf_third = om.get_fa_yoyocf(cur_date - 20000) # 营业收入同比增长率,-1列为rank,-2列为所需要的(去年的) fa_yoy_or_second = om.get_fa_yoy_or(cur_date - 10000) # 营业收入同比增长率,-1列为rank,-2列为所需要的(前年的) fa_yoy_or_third = om.get_fa_yoy_or(cur_date - 20000) sales_growth_q = qfa_cgrsales.iloc[:, -2] sales_growth_ttm = fa_yoy_or.iloc[:, -2] sales_growth_3y = (((fa_yoy_or.iloc[:, -2] / 100 + 1) * (fa_yoy_or_second.iloc[:, -2] / 100 + 1) * (fa_yoy_or_third.iloc[:, -2] / 100 + 1)) ** (1 / 3) - 1) * 100 profit_growth_q = qfa_yoyprofit.iloc[:, -2] profit_growth_ttm = profit_deducted.iloc[:, -2] profit_growth_3y = (((profit_deducted.iloc[:, -2] / 100 + 1) * (profit_deducted_second.iloc[:, -2] / 100 + 1) * (profit_deducted_third.iloc[:, -2] / 100 + 1)) ** (1 / 3) - 1) * 100 operationcashflow_growth_ttm = fa_yoyocf.iloc[:, -2] operationcashflow_growth_3y = (((fa_yoyocf.iloc[:, -2] / 100 + 1) * (fa_yoyocf_second.iloc[:, -2] / 100 + 1) * (fa_yoyocf_third.iloc[:, -2] / 100 + 1)) ** (1 / 3) - 1) * 100 # 入库 stock_list = om.get_all_stock(cur_date) sales_growth_q_flag = fh.df_to_list(cur_date, sales_growth_q, 'sales_growth_q', stock_list) sales_growth_ttm_flag = fh.df_to_list(cur_date, sales_growth_ttm, 'sales_growth_ttm', stock_list) sales_growth_3y_flag = fh.df_to_list(cur_date, sales_growth_3y, 'sales_growth_3y', stock_list) profit_growth_q_flag = fh.df_to_list(cur_date, profit_growth_q, 'profit_growth_q', stock_list) profit_growth_ttm_flag = fh.df_to_list(cur_date, profit_growth_ttm, 'profit_growth_ttm', stock_list) profit_growth_3y_flag = fh.df_to_list(cur_date, profit_growth_3y, 'profit_growth_3y', stock_list) operationcashflow_growth_ttm_flag = fh.df_to_list(cur_date, operationcashflow_growth_ttm, 'operationcashflow_growth_ttm', stock_list) operationcashflow_growth_3y_flag = fh.df_to_list(cur_date, operationcashflow_growth_3y, 'operationcashflow_growth_3y', stock_list) if sales_growth_q_flag & sales_growth_ttm_flag & sales_growth_3y_flag & profit_growth_q_flag & profit_growth_ttm_flag & \ profit_growth_3y_flag & operationcashflow_growth_ttm_flag & operationcashflow_growth_3y_flag != 1: gl.logger.error('factor_growth insert fail') return 0 else: gl.logger.info('factor_growth insert successful') return 1
def factor_close_bak(cur_date): close_bak = om.get_close_bak(cur_date) close_bak = close_bak.iloc[:, -1] stock_list = om.get_all_stock(cur_date) close_bak_flag = fh.df_to_list(cur_date, close_bak, 'close_bak', stock_list) if close_bak_flag != 1: gl.logger.error('factor_close_bak insert fail') return 0 else: gl.logger.info('factor_close_bak insert successful') return 1
def factor_turnover(cur_date, date_list): m1, m2, m3, m6, m12 = 20, 40, 60, 120, 240 # 此处为了获得换手率,取得成交量和流通股本 # 获取成交量,和当日流通股本,不带index,为了格式化df成为index为日期,columns为股票代码 dq_volume = om.get_dq_volume(cur_date, date_list) shr_today = om.get_shr_today(cur_date, date_list) # 得到近一年的最高价,最低价,并转换为index为日期,columns为股票代码 date_dq_volume = fh.transform_large_df(dq_volume) date_shr_today = fh.transform_large_df(shr_today) turnover = date_dq_volume / date_shr_today turnover = turnover.loc[list(date_dq_volume.index)] # 在中国:换手率= 成交量/流通股本×100%,炒股软件的成交量单位是万,流通股本单位是亿 # 流通股本基本不变(大概1年变一次),我都取得各自的总量,然后相除 format = lambda x: '%.4f' % x turnover_1m = turnover[:m1].sum() turnover_2m = turnover[:m2].sum() turnover_3m = turnover[:m3].sum() turnover_6m = turnover[:m6].sum() turnover_12m = turnover[:m12].sum() turnover_1m = turnover_1m[~turnover_1m.isin([np.nan, np.inf, -np.inf])] turnover_2m = turnover_2m[~turnover_2m.isin([np.nan, np.inf, -np.inf])] turnover_3m = turnover_3m[~turnover_3m.isin([np.nan, np.inf, -np.inf])] turnover_6m = turnover_6m[~turnover_6m.isin([np.nan, np.inf, -np.inf])] turnover_12m = turnover_12m[~turnover_12m.isin([np.nan, np.inf, -np.inf])] turnover_1m = turnover_1m.dropna() turnover_2m = turnover_2m.dropna() turnover_3m = turnover_3m.dropna() turnover_6m = turnover_6m.dropna() turnover_12m = turnover_12m.dropna() turnover_1m = turnover_1m.map(format) turnover_2m = turnover_2m.map(format) turnover_3m = turnover_3m.map(format) turnover_6m = turnover_6m.map(format) turnover_12m = turnover_12m.map(format) # 入库 stock_list = om.get_all_stock(cur_date) turnover_1m_flag = fh.df_to_list(cur_date, turnover_1m, 'turnover_1m', stock_list) turnover_2m_flag = fh.df_to_list(cur_date, turnover_2m, 'turnover_2m', stock_list) turnover_3m_flag = fh.df_to_list(cur_date, turnover_3m, 'turnover_3m', stock_list) turnover_6m_flag = fh.df_to_list(cur_date, turnover_6m, 'turnover_6m', stock_list) turnover_12m_flag = fh.df_to_list(cur_date, turnover_12m, 'turnover_12m', stock_list) if turnover_1m_flag & turnover_2m_flag & turnover_3m_flag & turnover_6m_flag & turnover_12m_flag != 1: gl.logger.error('factor_turnover insert fail') return 0 else: gl.logger.info('factor_turnover insert successful') return 1
def factor_financial_quality(cur_date): # 获取经营性现金流(ttm) val_pcf_ocfttm = om.get_index_val_pcf_ocfttm(cur_date) # 单季度.净资产收益率,-1列为rank,-2列为所需要的 qfa_roe = om.get_qfa_roe(cur_date) # 净资产收益率(ttm),-1列为rank,-2列为所需要的 fa_roe = om.get_fa_roe(cur_date) # 单季度.总资产净利润,-1列为rank,-2列为所需要的 qfa_roa = om.get_qfa_roa(cur_date) # 总资产净利润(ttm),-1列为rank,-2列为所需要的 fa_roa = om.get_fa_roa(cur_date) # 单季度.销售毛利率,-1列为rank,-2列为所需要的 qfa_grossprofitmargin = om.get_qfa_grossprofitmargin(cur_date) # 销售毛利率(ttm),-1列为rank,-2列为所需要的 fa_grossprofitmargin = om.get_fa_grossprofitmargin(cur_date) # 扣除非经常损益后的净利润/净利润,-1列为rank,-2列为所需要的 deductedprofittoprofit = om.get_deductedprofittoprofit(cur_date) # 总资产周转率(ttm),-1列为rank,-2列为所需要的 fa_assetsturn = om.get_fa_assetsturn(cur_date) # 获取经营性现金流(lyr) val_pcf_ocf = om.get_index_val_pcf_ocf(cur_date) roe_q = qfa_roe.iloc[:, -2] roe_ttm = fa_roe.iloc[:, -2] roa_q = qfa_roa.iloc[:, -2] roa_ttm = fa_roa.iloc[:, -2] grossprofitmargin_q = qfa_grossprofitmargin.iloc[:, -2] grossprofitmargin_ttm = fa_grossprofitmargin.iloc[:, -2] profitmargin_ttm = deductedprofittoprofit.iloc[:, -2] assetturnover_ttm = fa_assetsturn.iloc[:, -2] operationcashflowratio_q = val_pcf_ocf.iloc[:, -1] / qfa_roa.iloc[:, -2] operationcashflowratio_ttm = val_pcf_ocfttm.iloc[:, -1] / fa_roa.iloc[:, -2] # 入库 stock_list = om.get_all_stock(cur_date) roe_q_flag = fh.df_to_list(cur_date, roe_q, 'roe_q', stock_list) roe_ttm_flag = fh.df_to_list(cur_date, roe_ttm, 'roe_ttm', stock_list) roa_q_flag = fh.df_to_list(cur_date, roa_q, 'roa_q', stock_list) roa_ttm_flag = fh.df_to_list(cur_date, roa_ttm, 'roa_ttm', stock_list) grossprofitmargin_q_flag = fh.df_to_list(cur_date, grossprofitmargin_q, 'grossprofitmargin_q', stock_list) grossprofitmargin_ttm_flag = fh.df_to_list(cur_date, grossprofitmargin_ttm, 'grossprofitmargin_ttm', stock_list) profitmargin_ttm_flag = fh.df_to_list(cur_date, profitmargin_ttm, 'profitmargin_ttm', stock_list) assetturnover_ttm_flag = fh.df_to_list(cur_date, assetturnover_ttm, 'assetturnover_ttm', stock_list) operationcashflowratio_q_flag = fh.df_to_list(cur_date, operationcashflowratio_q, 'operationcashflowratio_q', stock_list) operationcashflowratio_ttm_flag = fh.df_to_list(cur_date, operationcashflowratio_ttm, 'operationcashflowratio_ttm', stock_list) if roe_q_flag & roe_ttm_flag & roa_q_flag & roa_ttm_flag & grossprofitmargin_q_flag & grossprofitmargin_ttm_flag \ & profitmargin_ttm_flag & assetturnover_ttm_flag & operationcashflowratio_q_flag & operationcashflowratio_ttm_flag != 1: gl.logger.error('factor_financial_quality insert fail') return 0 else: gl.logger.info('factor_financial_quality insert successful') return 1
def factor_volatility(cur_date, date_list, regress_result, date_adjclose): m1, m2, m3, m6, m12 = 20, 40, 60, 120, 240 # 获取N天交易日中每天的最高价和最低价,不带index,为了格式化df成为index为日期,columns为股票代码 price_high = om.get_ndays_dq_high(cur_date, date_list) price_low = om.get_ndays_dq_low(cur_date, date_list) # 得到近一年的最高价,最低价,并转换为index为日期,columns为股票代码 date_price_high = fh.transform_large_df(price_high) date_price_low = fh.transform_large_df(price_low) # 降序排列index(最上面的是最新的日期), 结果为20天内最高价和最低价 # 获取N天交易日中每天的最高价,不带index,为了格式化df成为index为日期,columns为股票代码 price_max_1m, price_min_1m = price(date_price_high, date_price_low, m1) price_max_2m, price_min_2m = price(date_price_high, date_price_low, m2) price_max_3m, price_min_3m = price(date_price_high, date_price_low, m3) price_max_6m, price_min_6m = price(date_price_high, date_price_low, m6) price_max_12m, price_min_12m = price(date_price_high, date_price_low, m12) high_low_1m = price_max_1m / price_min_1m high_low_2m = price_max_2m / price_min_2m high_low_3m = price_max_3m / price_min_3m high_low_6m = price_max_6m / price_min_6m high_low_12m = price_max_12m / price_min_12m std_1m = std_adjclose_rate(date_adjclose, m1) std_2m = std_adjclose_rate(date_adjclose, m2) std_3m = std_adjclose_rate(date_adjclose, m3) std_6m = std_adjclose_rate(date_adjclose, m6) std_12m = std_adjclose_rate(date_adjclose, m12) ln_price = np.log(date_adjclose.iloc[0]) # 用的每日收盘价 beta_consistence = regress_result.loc['beta_consis'] # 入库 stock_list = om.get_all_stock(cur_date) high_low_1m_flag = fh.df_to_list(cur_date, high_low_1m, 'high_low_1m', stock_list) high_low_2m_flag = fh.df_to_list(cur_date, high_low_2m, 'high_low_2m', stock_list) high_low_3m_flag = fh.df_to_list(cur_date, high_low_3m, 'high_low_3m', stock_list) high_low_6m_flag = fh.df_to_list(cur_date, high_low_6m, 'high_low_6m', stock_list) high_low_12m_flag = fh.df_to_list(cur_date, high_low_12m, 'high_low_12m', stock_list) std_1m_flag = fh.df_to_list(cur_date, std_1m, 'std_1m', stock_list) std_2m_flag = fh.df_to_list(cur_date, std_2m, 'std_2m', stock_list) std_3m_flag = fh.df_to_list(cur_date, std_3m, 'std_3m', stock_list) std_6m_flag = fh.df_to_list(cur_date, std_6m, 'std_6m', stock_list) std_12m_flag = fh.df_to_list(cur_date, std_12m, 'std_12m', stock_list) ln_price_flag = fh.df_to_list(cur_date, ln_price, 'ln_price', stock_list) beta_consistence_flag = fh.df_to_list(cur_date, beta_consistence, 'beta_consistence', stock_list) if high_low_1m_flag & high_low_2m_flag & high_low_3m_flag & high_low_6m_flag & high_low_12m_flag & \ std_1m_flag & std_2m_flag & std_3m_flag & std_6m_flag & std_12m_flag & ln_price_flag & beta_consistence_flag != 1: gl.logger.error('factor_volatility insert fail') return 0 else: gl.logger.info('factor_volatility insert successful') return 1
def factor_value(cur_date): # 获取EP(TTM),净利润/总市值(带index),(若净利润<=0,则返回空) inverse_ep = om.get_index_ep(cur_date) # 获取ps,市销率(PS,TTM) ps = om.get_ps(cur_date) # 获取pb值 也就是需要的bp,的倒数 inverse_bp = om.get_index_val_pb_new(cur_date) # 获取净现金流 val_pcf_ncfttm = om.get_index_val_pcf_ncfttm(cur_date) # 获取经营性现金流(ttm) val_pcf_ocfttm = om.get_index_val_pcf_ocfttm(cur_date) # 获取个股市值(带index) index_mkvalue = om.get_index_mkvalue(cur_date) ep = 1 / inverse_ep.iloc[:, -1] bp = 1 / inverse_bp.iloc[:, -1] sp = 1 / ps.iloc[:, -1] ncfp = val_pcf_ncfttm.iloc[:, -1] / index_mkvalue.iloc[:, -1] ocfp = val_pcf_ocfttm.iloc[:, -1] / index_mkvalue.iloc[:, -1] # 分红放在最后面了,主要是不是我写的 # 红利(近12个月股息率),首先拼接一年前的日期 struct_time = time.strptime(str(cur_date), '%Y%m%d') y = struct_time.tm_year - 1 m = struct_time.tm_mon d = struct_time.tm_mday year_before = str(date(y, m, d).replace(year=y)) year_before = year_before.replace('-', '') divdend = om.get_dps(year_before, str(cur_date)) divdend.index = divdend.iloc[:, 0] dp = divdend.iloc[:, -1] # 入库 stock_list = om.get_all_stock(cur_date) ep_flag = fh.df_to_list(cur_date, ep, 'ep', stock_list) bp_flag = fh.df_to_list(cur_date, bp, 'bp', stock_list) sp_flag = fh.df_to_list(cur_date, sp, 'sp', stock_list) ncfp_flag = fh.df_to_list(cur_date, ncfp, 'ncfp', stock_list) ocfp_flag = fh.df_to_list(cur_date, ocfp, 'ocfp', stock_list) dp_flag = fh.df_to_list(cur_date, dp, 'dp', stock_list) if ep_flag & bp_flag & sp_flag & ncfp_flag & ocfp_flag & dp_flag != 1: gl.logger.error('factor_value insert fail') return 0 else: gl.logger.info('factor_value insert successful') return 1
def factor_modified_momentum(cur_date, date_list, date_adjclose, date_dq_turn): m1, m2, m3, m6, m12 = 20, 40, 60, 120, 240 weighted_strength_1m = calculate_rate(date_adjclose, date_dq_turn, m1) weighted_strength_2m = calculate_rate(date_adjclose, date_dq_turn, m2) weighted_strength_3m = calculate_rate(date_adjclose, date_dq_turn, m3) weighted_strength_6m = calculate_rate(date_adjclose, date_dq_turn, m6) weighted_strength_12m = calculate_rate(date_adjclose, date_dq_turn, m12) # 入库 stock_list = om.get_all_stock(cur_date) weighted_strength_1m_flag = fh.df_to_list(cur_date, weighted_strength_1m, 'weighted_strength_1m', stock_list) weighted_strength_2m_flag = fh.df_to_list(cur_date, weighted_strength_2m, 'weighted_strength_2m', stock_list) weighted_strength_3m_flag = fh.df_to_list(cur_date, weighted_strength_3m, 'weighted_strength_3m', stock_list) weighted_strength_6m_flag = fh.df_to_list(cur_date, weighted_strength_6m, 'weighted_strength_6m', stock_list) weighted_strength_12m_flag = fh.df_to_list(cur_date, weighted_strength_12m, 'weighted_strength_12m', stock_list) if weighted_strength_1m_flag & weighted_strength_2m_flag & weighted_strength_3m_flag & weighted_strength_6m_flag & \ weighted_strength_12m_flag != 1: gl.logger.error('factor_modified_momentum insert fail') return 0 else: gl.logger.info('factor_modified_momentum insert successful') return 1
def factor_momentum(cur_date, regress_result, date_adjclose): HAlpha = regress_result.loc['const'] relative_strength_1m = date_adjclose.iloc[0] / date_adjclose.shift(-20).iloc[0] - 1 relative_strength_2m = date_adjclose.iloc[0] / date_adjclose.shift(-20 * 2).iloc[0] - 1 relative_strength_3m = date_adjclose.iloc[0] / date_adjclose.shift(-20 * 3).iloc[0] - 1 relative_strength_6m = date_adjclose.iloc[0] / date_adjclose.shift(-20 * 6).iloc[0] - 1 relative_strength_12m = date_adjclose.iloc[0] / date_adjclose.shift(-20 * 12).iloc[0] - 1 # 入库 stock_list = om.get_all_stock(cur_date) HAlpha_flag = fh.df_to_list(cur_date, HAlpha, 'HAlpha', stock_list) relative_strength_1m_flag = fh.df_to_list(cur_date, relative_strength_1m, 'relative_strength_1m', stock_list) relative_strength_2m_flag = fh.df_to_list(cur_date, relative_strength_2m, 'relative_strength_2m', stock_list) relative_strength_3m_flag = fh.df_to_list(cur_date, relative_strength_3m, 'relative_strength_3m', stock_list) relative_strength_6m_flag = fh.df_to_list(cur_date, relative_strength_6m, 'relative_strength_6m', stock_list) relative_strength_12m_flag = fh.df_to_list(cur_date, relative_strength_12m, 'relative_strength_12m', stock_list) if HAlpha_flag & relative_strength_1m_flag & relative_strength_2m_flag & relative_strength_3m_flag & \ relative_strength_6m_flag & relative_strength_12m_flag != 1: gl.logger.error('factor_momentum insert fail') return 0 else: gl.logger.info('factor_momentum insert successful') return 1