def dailyCalculation(cur_date,T,N): gl.logger.info('Start to do daily caculation!') #日期获取 date_list=om.getNtradeDate(cur_date,-(T+N+1)) gl.logger.info('Date list get successfully,date interval is [%d,%d]'%(date_list[0],date_list[-1])) #收盘价数据、因子数据准备 gl.logger.info('Start to do data prepare!') date_time = datetime.datetime.now() [return_df,train_factor_df,test_factor_df]=dataPrepare(date_list,T,N) cost_seconds=(datetime.datetime.now()-date_time).seconds gl.logger.info('Data preparation is done,consumes %d seconds.'%cost_seconds) #模型预测 model_task_flag=modelPredict(train_factor_df,return_df,test_factor_df) #标准化的因子数据 normalized_flag=factorDataNormalized(test_factor_df) if(model_task_flag==1 and normalized_flag==1): gl.logger.info('Daily stock rating task successfully! ') else: gl.logger.error('The model falg is %d, the normalized data flag is %d,daily task is failed, please check!'%(model_task_flag,normalized_flag))
def factor_prepare(cur_date): gl.logger.info("start to prepare factor data----------") n = -365 # 主要因为dq_turn会返回周六周日的日期,数据为na,但是占了行数 # 取某个日期前(负,-7)or后(正,+7)N天的交易日日期,date_list[0]为最近的一天 date_list = om.getNtradeDate(cur_date, n) # 12个月的个股收益对沪深300 线性回归 gl.logger.info('start to prepare regress data') regress_result = stock_regress(cur_date, date_list) regress_result = pd.DataFrame.from_dict(regress_result, orient='columns') # index分别为权重,常数项,经过处理的残差 regress_result.index = ['coef', 'const', 'beta_consis'] # 获取500天的复权收益列表,为降序排列,第0个位最新的 gl.logger.info('start to prepare adjclose data') adjclose = om.get_ndays_adjclose(cur_date, date_list) # 格式化index为日期,columns为股票代码,降序,第0个位最新的 date_adjclose = fh.transform_large_df(adjclose) # 换手率加权*日均收益率 # 获取换手率,不带index,为了格式化df成为index为日期,columns为股票代码 gl.logger.info('start to prepare dq_turn data') dq_turn = om.get_dq_turn(cur_date, date_list) date_dq_turn = fh.transform_large_df(dq_turn) # 这个会返回周六周日,数据为na,所以导致n取365 date_dq_turn.dropna(how='all', inplace=True) # ----------------------------------------------------------------------------- # 估值因子,value factor gl.logger.info('start to run factor_value function') factor_value_flag = factor_value(cur_date) # ----------------------------------------------------------------------------- # 成长因子,growth factor gl.logger.info('start to run factor_growth function') factor_growth_flag = factor_growth(cur_date) # ----------------------------------------------------------------------------- # 财务质量因子,financial quality factor gl.logger.info('start to run factor_financial_quality function') factor_financial_flag = factor_financial_quality(cur_date) # ----------------------------------------------------------------------------- # 杠杠因子,leverage factor # 规模因子,size factor gl.logger.info('start to run factor_leverage function') factor_leverage_flag = factor_leverage(cur_date) # ----------------------------------------------------------------------------- # 动量因子,momentum factor gl.logger.info('start to run factor_momentum function') factor_momentum_flag = factor_momentum(cur_date, regress_result, date_adjclose) # ----------------------------------------------------------------------------- # 波动率因子,volatility factor gl.logger.info('start to run factor_volatility function') factor_volatility_flag = factor_volatility(cur_date, date_list, regress_result, date_adjclose) # ----------------------------------------------------------------------------- # 换手率因子,turnover factor gl.logger.info('start to run factor_turnover function') factor_turnover_flag = factor_turnover(cur_date, date_list) # factor_turnover_flag = 1 # ----------------------------------------------------------------------------- # 改进的动量因子,modified momentum factor gl.logger.info('start to run factor_modified_momentum function') factor_modified_flag = factor_modified_momentum(cur_date, date_list, date_adjclose, date_dq_turn) # ----------------------------------------------------------------------------- gl.logger.info('start to run factor_close_bak function') close_bak_flag = factor_close_bak(cur_date) flag = factor_value_flag & factor_growth_flag & factor_financial_flag & factor_leverage_flag & factor_momentum_flag \ & factor_volatility_flag & factor_turnover_flag & factor_modified_flag & close_bak_flag # 所有函数的返回值是不是都成功了 work_time = time.strftime('%Y%m%d%H%M%S') work_id = 'FactorPrepare_{}'.format(work_time) entry_time = time.strftime('%Y-%m-%d %H:%M:%S') if flag != 1: status_flag = om.status_insert([[work_id, 0, entry_time]]) gl.logger.error('some function are failed----------') return 0 else: status_flag = om.status_insert([[work_id, 1, entry_time]]) gl.logger.info('all function are successful----------') return 1