def _prepare_config(self, **kwargs): config_dict = parse_config('config.yml') for k, v in kwargs: if k in config_dict: config_dict[k] = v if isinstance(config_dict['factors'][0], str): factors = [] for f in config_dict['factors']: factors.append(globals()[f]) config_dict['factors'] = factors self.config = AttrDict(config_dict)
def create_from_directory(self, src, if_exists='error'): cwd = os.getcwd() os.chdir(src) strategy_config = AttrDict( parse_config(os.path.join(src, 'config.yml'))) if self.if_exists(strategy_config.name): if if_exists == 'error': raise KeyError("strategy %s already exists" % strategy_config.name) elif if_exists == 'replace': self.delete(name=strategy_config.name) strategy_path = os.path.join(self._strategy_path, strategy_config.name) # 创建策略文件夹 os.mkdir(strategy_path) # 复制初始股票列表 stocklist_filename = strategy_config.stocklist.output stocklist_name = stocklist_filename.replace('.csv', '') # 策略的调仓日期 if os.path.isfile(stocklist_filename): shutil.copy(stocklist_filename, strategy_path) self._stocklist_manager.add_new_one( os.path.abspath(stocklist_filename)) first_rebalance_date = self._stocklist_manager.min_rebalance_date( stocklist_name) latest_rebalance_date = self._stocklist_manager.max_rebalance_date( stocklist_name) else: first_rebalance_date = np.nan latest_rebalance_date = np.nan # 复制中间数据 if os.path.isdir('temp'): shutil.copytree('temp', os.path.join(strategy_path, 'temp')) # 复制股票列表更新程序 if os.path.isdir('update'): shutil.copytree('update', os.path.join(strategy_path, 'update')) # 复制设置文件 shutil.copy("config.yml", strategy_path) # 行业中性 industry_neutral = '是' if strategy_config.stocklist.industry_neutral else '否' industry_class = strategy_config.stocklist.industry # 添加新的记录 self._add_record(stocklist_name=stocklist_name, first_rebalance_date=first_rebalance_date, latest_rebalance_date=latest_rebalance_date, benchmark=strategy_config.stocklist.benchmark, industry_neutral=industry_neutral, industry_class=industry_class, **strategy_config.__dict__) os.chdir(cwd)
def run(config_path): config = parse_config(config_path) config = AttrDict(config) # 加载因子 print("加载因子...") factors = load_factors(config) factor_list = [] for factor in factors: factor_list.append(panelFactor(*factor)) load_factor_data(factor_list, config) print("计算...") corr, corr_mean = cal_corr(factor_list) auto_corr, auto_corr_mean = cal_auto_corr(factor_list) print("存储...") write_to_excel(corr, corr_mean, auto_corr, auto_corr_mean, config)
def run(config_path, user_config=None): # 加载设置文件 config = parse_config(config_path) if user_config is None: user_config = {} deep_update_dict(user_config, config) config = AttrDict(config) # 初始化运行环境 print("初始化运行环境...") _env = Environment(config) _env._initialize() # 加载因子 print("加载因子...") factors = load_factors(config) factor_list = [] for factor in factors: factor_list.append(panelFactor(*factor)) _env.set_factors(factor_list) # 数据提取加工 print("正在计算...") data_handler = FactorDataProcessModHandler() data_handler.set_env(_env) data_handler.mod_start() # 计算因子收益 print("计算收益...") performance_handler = FactorPerformanceModHandler() performance_handler.set_env(_env) performance_handler.mod_start() # 因子存储 print("存储因子...") store_handler = FactorStoreModHandler() store_handler.set_env(_env) store_handler.mod_start()