def get_portfoio_with_best_param(self, param_str=''): if not param_str: param_str = "组合34号" param_df = pd.read_excel("%s最优参数.xlsx" % param_str, index_col=0) # param_df = pd.read_excel("组合31号最优参长调仓日期.xlsx", index_col=0) total_df = pd.read_excel("组合明细.xlsx", index_col=0) asset_index = eval(total_df.loc[param_str]['模型参数']) method = total_df.loc[param_str]['配置模型'] # method = 'industry_recyle' param_info_dic = eval(param_df.loc['misc'][method]) best_param_dic = {} for param_key, param_values in param_info_dic['vals'].items(): best_param_dic[param_key] = param_values[0] # best_param_dic = {'adjust_day_limit': 26, 'back_day_limit': 16, 'max_index_loss_limit': 1.0163546802363896, 'poc_value_limit': 0.8998090608862988}#组合33阶段参数 # best_param_dic={'adjust_day_limit': 73, 'back_day_limit': 100, 'bond_limit': 0.07325363231739307, 'com_limit': 0.7055617284378413, 'xfc_back_day': 30, 'xfc_chage_rate': 1.1837053863199578, 'control_method': 'xfc'} # best_param_dic={'adjust_day_limit': 9, 'back_day_limit': 7, 'max_index_loss_limit': 1.0336336758278335, 'poc_value_limit': 0.4002284797771605}#组合32号短周期场内 self.logger.info("----------------------best_param----------------->") fundPortfolioDemo = fundPortfolio(startDate='2015-09-01', file_path=param_str) # fund_type = 'ETF' fund_type = 'OTC' fundPortfolioDemo.setMain(method=method, productFlag=True, asset_index=asset_index, best_param_dic=best_param_dic, fund_type=fund_type) reslut_file_loc = fundPortfolioDemo.PathFolder + method + '\\' self.get_PMS_format(file_path=reslut_file_loc, param_str=param_str)
def calcResearch(self, method): #创建储存结果文件夹 fundPortfolioDemo = fundPortfolio() newFolder = fundPortfolioDemo.fileMake(u'用户分层投资组合' + method) #获取底层产品历史净值数据 SetPortfolioDemo = SetPortfolio() dicResult, resultDf = SetPortfolioDemo.goMain() nameDic = { keyName: SetPortfolioDemo.dicProduct[keyName[:-3]] for keyName in resultDf } resultDf.rename(columns=nameDic, inplace=True) usefulNetDf = resultDf.dropna(axis=0) usefulReturnDf = (usefulNetDf - usefulNetDf.shift(1)) / usefulNetDf.shift(1) usefulReturnDf.fillna(0, inplace=True) #获取所有投资组合历史收益及风险指标 returnAndRiskTotal, portFolioDf = self.getData(method=method) #获取五类风险等级走势,存储,并返回 targetDf = self.researchTotalPortfolio(portFolioDf, method, newFolder) #五类风险等级与底层产品走势图,存储 self.researchPortfolioFund(targetDf, usefulReturnDf, newFolder, method) #风险收益指标与rate走势图,存储 self.researchRiskReturn(method=method, returnAndRiskTotal=returnAndRiskTotal, newFolder=newFolder)
def ResearchModel(self, calcNum=50, method='target_risk'): modelResult = {} fundPortfolioDemo = fundPortfolio() timeBack = 0 for rate in np.linspace(start=0, stop=1, num=calcNum): timeBack += 1 self.PrintInfoDemo.PrintLog('回测第%s次' % str(timeBack)) modelResult['rate=' + str(rate)] = fundPortfolioDemo.setMain( method=method, rate=rate) pickleFile = open(method + 'modelResult.pkl', 'wb') pickle.dump(modelResult, pickleFile) pickleFile.close() return modelResult
def get_portfolio_industry_recyle_param(self): param_str = "行业轮动" add_str = '' param_str = param_str + add_str method = 'industry_recyle' IndustryRecyleDemo = IndustryRecyle() fund_type = 'ETF' # fund_type='OTC' industry_index_name_dic, product_name_dic = IndustryRecyleDemo.get_main( fund_type=fund_type) asset_index = { 'stock': industry_index_name_dic, 'bond': { 'H00140.SH': u'上证五年期国债指数' } } if 'H00140.SH' not in product_name_dic: if fund_type == 'ETF': product_name_dic['H00140.SH'] = {'511010.SH': "国债ETF "} else: product_name_dic['H00140.SH'] = {'000088.OF': "嘉实中期国债ETF联接C "} # best_param_dic ={'adjust_day_limit': 59, 'back_day_limit': 22, 'max_index_loss_limit': 1.0658809306041392, 'poc_value_limit': 0.795065864572404}#回撤阈值0.15,长周期参数1 # best_param_dic={'adjust_day_limit': 43, 'back_day_limit': 7, 'max_index_loss_limit': 1.0588023004774523, 'poc_value_limit': 0.6781040531662471} #场内长周期adjust!=back,长周期参数2 best_param_dic = { 'adjust_day_limit': 9, 'back_day_limit': 6, 'max_index_loss_limit': 1.003732683071061, 'poc_value_limit': 0.8918295289003106 } # 场内短周期最优参数 # best_param_dic={'adjust_day_limit': 33, 'back_day_limit': 17, 'max_index_loss_limit': 1.0087987474146602, 'poc_value_limit': 0.402127727489241}#场外最优参数 fundPortfolioDemo = fundPortfolio(startDate='2015-09-01', file_path=param_str) fundPortfolioDemo.setMain(method=method, productFlag=True, asset_index=asset_index, best_param_dic=best_param_dic, product_name_dic=product_name_dic, fund_type=fund_type) reslut_file_loc = fundPortfolioDemo.PathFolder + method + '\\' self.get_PMS_format(file_path=reslut_file_loc, param_str=param_str)
def fix_portfolio_best_param(self): # param_str = "组合31号" # 大类资产轮动 # param_str='组合32号' #风格轮动 # param_str = '行业' # param_str='主题' param_str = '行业主题' fund_type = 'ETF' # fund_type = 'OTC' file_str = param_str + fund_type total_df = pd.read_excel("组合明细.xlsx", index_col=0) if fund_type == 'ETF': if param_str == "组合32号": best_param_dic = { 'adjust_day_limit': 9, 'back_day_limit': 7, 'max_index_loss_limit': 1.0336336758278335, 'poc_value_limit': 0.4002284797771605 } # 组合32号短周期场内 elif param_str == "组合31号": best_param_dic = { 'adjust_day_limit': 6, 'max_index_loss_limit': 1.0427257786765352, 'poc_value_limit': 0.5181759212161646 } elif param_str in ['行业', '主题', '行业主题']: if param_str == '行业': best_param_dic = { 'adjust_day_limit': 9, 'back_day_limit': 6, 'max_index_loss_limit': 1.003732683071061, 'poc_value_limit': 0.8918295289003106 } # 场内短周期最优参数 # best_param_dic = {'adjust_day_limit': 5, 'back_day_limit': 7, 'max_index_loss_limit': 1.0438246646746496, # 'poc_value_limit': 0.8998182356292519} elif param_str == '主题': best_param_dic = { 'adjust_day_limit': 11, 'back_day_limit': 10, 'max_index_loss_limit': 1.0981107733412536, 'poc_value_limit': 0.8809468667158603 } else: best_param_dic = { 'adjust_day_limit': 11, 'adjust_maxdown': 0.06745476221138974, 'back_day_limit': 7, 'poc_value_limit': 0.7115231344778375, } # 投顾产品 best_param_dic = { 'back_day_limit': 6, 'corr_limit': 0.9871519191133329, 'max_loss_limit': 0.07901894294893107 } # 新止损优化 # best_param_dic = {'back_day_limit': 9, 'corr_limit': 0.9860977873659047, 'max_loss_limit': 0.06353494612453964} # best_param_dic={'adjust_day_limit': 6, 'back_day_limit': 7, 'poc_num': 3} best_param_dic = { 'back_day_limit': 14, 'corr_limit': 0.8327902772212527, 'down_max_num': 0.447583290105109, 'judge_market': 52, 'max_loss_limit': 0.057219432873010984, 'up_day_num': 0.8399154687488591, 'up_max': 0.2705637796307785, 'vol_rate': 0.7675222487190907 } best_param_dic = { 'back_day_limit': 14, 'conti_up_day': 0.4490201635500951, 'corr_limit': 0.9177608537272707, 'down_max_num': 0.7910721185996274, 'judge_market': 42, 'max_loss_limit': 0.1305479472011245, 'up_day_num': 0.568715917676664, 'up_max': 0.4146799716429238, 'vol_rate': 0.45970281879809183 } method = 'industry_recyle' # method='industry_recyle_equ' IndustryRecyleDemo = IndustryRecyle() # industry_index_name_dic, product_name_dic = IndustryRecyleDemo.get_ZZmain(fund_type=fund_type) industry_index_name_dic, product_name_dic = IndustryRecyleDemo.get_fund_index( fund_type=fund_type, style_flag=param_str) asset_index = { 'stock': industry_index_name_dic, 'bond': { 'H00140.SH': u'上证五年期国债指数' } } if 'H00140.SH' not in product_name_dic: product_name_dic['H00140.SH'] = {'511880.SH': "银华日利ETF "} fundPortfolioDemo = fundPortfolio(startDate='2015-09-01', file_path=param_str) # fundPortfolioDemo = fundPortfolio(startDate='2019-01-01', file_path=param_str) fundPortfolioDemo.setMain(method=method, productFlag=True, asset_index=asset_index, best_param_dic=best_param_dic, product_name_dic=product_name_dic, fund_type=fund_type) reslut_file_loc = fundPortfolioDemo.PathFolder + method + '\\' self.get_PMS_format(file_path=reslut_file_loc, param_str=file_str) return else: if param_str == '组合31号': best_param_dic = { 'adjust_day_limit': 54, 'back_day_limit': 29, 'max_index_loss_limit': 1.1330075022050305, 'poc_value_limit': 0.8914724483780754 } elif param_str == '组合32号': best_param_dic = { 'adjust_day_limit': 9, 'back_day_limit': 7, 'max_index_loss_limit': 1.0336336758278335, 'poc_value_limit': 0.4002284797771605 } # 组合32号短周期场内 elif param_str in ['行业', '主题', '行业主题']: # method = 'industry_recyle' # method = 'industry_recyle_stock' method = 'industry_recyle_mean_var_stock' if method == 'industry_recyle_stock': best_param_dic = { 'adjust_day_limit': 42, 'back_day_limit': 29 } elif method == 'industry_recyle': best_param_dic = { 'adjust_day_limit': 33, 'back_day_limit': 17, 'max_index_loss_limit': 1.0087987474146602, 'poc_value_limit': 0.402127727489241 } # 场外最优参数 elif method == 'industry_recyle_mean_var_stock': # best_param_dic = {'adjust_day_limit': 49, 'back_day_limit': 114} # best_param_dic = {'adjust_day_limit': 18, 'back_day_limit': 40} # best_param_dic = {'adjust_day_limit': 39, 'back_day_limit': 34} # 行业 best_param_dic = { 'adjust_day_limit': 24, 'back_day_limit': 30 } # 主题 IndustryRecyleDemo = IndustryRecyle() industry_index_name_dic, product_name_dic = IndustryRecyleDemo.get_fund_index( fund_type=fund_type, style_flag=param_str) if method not in [ 'industry_recyle_stock', 'industry_recyle_mean_var_stock' ]: asset_index = { 'stock': industry_index_name_dic, 'bond': { 'H00140.SH': u'上证五年期国债指数' } } if 'H00140.SH' not in product_name_dic: product_name_dic['H00140.SH'] = { '000088.OF': "嘉实中期国债ETF联接C " } else: asset_index = { 'stock': industry_index_name_dic, } fundPortfolioDemo = fundPortfolio(startDate='2013-09-01', file_path=param_str) fundPortfolioDemo.setMain(method=method, productFlag=True, asset_index=asset_index, best_param_dic=best_param_dic, product_name_dic=product_name_dic, fund_type=fund_type) reslut_file_loc = fundPortfolioDemo.PathFolder + method + '\\' self.get_PMS_format(file_path=reslut_file_loc, param_str=file_str) return asset_index = eval(total_df.loc[param_str]['模型参数']) method = total_df.loc[param_str]['配置模型'] self.logger.info("----------------------best_param----------------->") fundPortfolioDemo = fundPortfolio(startDate='2015-09-01', file_path=param_str) fundPortfolioDemo.setMain(method=method, productFlag=True, asset_index=asset_index, best_param_dic=best_param_dic, fund_type=fund_type) reslut_file_loc = fundPortfolioDemo.PathFolder + method + '\\' self.get_PMS_format(file_path=reslut_file_loc, param_str=file_str) return
def fix_portfolio_best_param(self): # param_str = "组合31号" # 大类资产轮动 # param_str='组合32号' #风格轮动 # param_str = '行业' # param_str='主题' param_str = '行业主题' fund_type = 'ETF' # fund_type = 'OTC' file_str = param_str + fund_type total_df = pd.read_excel("组合明细.xlsx", index_col=0) if fund_type == 'ETF': if param_str == "组合32号": best_param_dic = { 'adjust_day_limit': 9, 'back_day_limit': 7, 'max_index_loss_limit': 1.0336336758278335, 'poc_value_limit': 0.4002284797771605 } # 组合32号短周期场内 elif param_str == "组合31号": best_param_dic = { 'adjust_day_limit': 6, 'max_index_loss_limit': 1.0427257786765352, 'poc_value_limit': 0.5181759212161646 } elif param_str in ['行业', '主题', '行业主题']: if param_str == '行业': best_param_dic = { 'adjust_day_limit': 9, 'back_day_limit': 6, 'max_index_loss_limit': 1.003732683071061, 'poc_value_limit': 0.8918295289003106 } # 场内短周期最优参数 elif param_str == '主题': best_param_dic = { 'adjust_day_limit': 11, 'back_day_limit': 10, 'max_index_loss_limit': 1.0981107733412536, 'poc_value_limit': 0.8809468667158603 } else: # best_param_dic = {'adjust_day_limit': 11, 'adjust_maxdown': 0.06745476221138974, # 'back_day_limit': 7, 'poc_value_limit': 0.7115231344778375, # } # 投顾产品 # best_param_dic = {'back_day_limit': 6, 'corr_limit': 0.9871519191133329, # 'max_loss_limit': 0.07901894294893107} # 新止损优化 # best_param_dic = {'back_day_limit': 9, 'corr_limit': 0.9860977873659047, 'max_loss_limit': 0.06353494612453964} # best_param_dic={'adjust_day_limit': 6, 'back_day_limit': 7, 'poc_num': 3} best_param_dic = { 'back_day_limit': 15, 'conti_up_day': 0.226022464737267, 'corr_limit': 0.845795176222653, 'down_max_num': 0.241858070432695, 'judge_market': 43, 'max_loss_limit': 0.062941738243683, 'up_day_num': 0.138838063975115, 'up_max': 0.268757678425416, 'vol_rate': 0.121065600027675 } # best_param_dic = {'back_day_limit': 12, 'conti_up_day': 0.8034284902193588, # 'corr_limit': 0.9334124090808452, 'down_max_num': 0.5627057548392841, # 'judge_market': 43, 'max_loss_limit': 0.0963097290973298, # 'up_day_num': 0.4768294352965751, 'up_max': 0.5870445113408896, # 'vol_rate': 0.8126182559511609} # 最优 method = 'industry_recyle' # method='industry_recyle_equ' IndustryRecyleDemo = IndustryRecyle() # industry_index_name_dic, product_name_dic = IndustryRecyleDemo.get_ZZmain(fund_type=fund_type) industry_index_name_dic, product_name_dic = IndustryRecyleDemo.get_fund_index( fund_type=fund_type, style_flag=param_str) asset_index = { 'stock': industry_index_name_dic, 'bond': { 'H00140.SH': u'上证五年期国债指数' } } if 'H00140.SH' not in product_name_dic: product_name_dic['H00140.SH'] = { '511880.SH': "银华日利ETF " } # fundPortfolioDemo = fundPortfolio(startDate='2015-09-01', file_path=param_str) fundPortfolioDemo = fundPortfolio(startDate='2018-01-01', file_path=param_str) fundPortfolioDemo.setMain( method=method, productFlag=True, asset_index=asset_index, best_param_dic=best_param_dic, product_name_dic=product_name_dic, fund_type=fund_type) reslut_file_loc = fundPortfolioDemo.PathFolder + method + '\\' self.get_PMS_format(file_path=reslut_file_loc, param_str=file_str) return else: if param_str == '组合31号': best_param_dic = { 'adjust_day_limit': 54, 'back_day_limit': 29, 'max_index_loss_limit': 1.1330075022050305, 'poc_value_limit': 0.8914724483780754 } elif param_str == '组合32号': best_param_dic = { 'adjust_day_limit': 9, 'back_day_limit': 7, 'max_index_loss_limit': 1.0336336758278335, 'poc_value_limit': 0.4002284797771605 } # 组合32号短周期场内 elif param_str in ['行业', '主题', '行业主题']: # method = 'industry_recyle' # method = 'industry_recyle_stock' method = 'industry_recyle_mean_var_stock' if method == 'industry_recyle_stock': best_param_dic = { 'adjust_day_limit': 42, 'back_day_limit': 29 } elif method == 'industry_recyle': best_param_dic = { 'adjust_day_limit': 33, 'back_day_limit': 17, 'max_index_loss_limit': 1.0087987474146602, 'poc_value_limit': 0.402127727489241 } # 场外最优参数 elif method == 'industry_recyle_mean_var_stock': # best_param_dic = {'adjust_day_limit': 49, 'back_day_limit': 114} # best_param_dic = {'adjust_day_limit': 18, 'back_day_limit': 40} # best_param_dic = {'adjust_day_limit': 39, 'back_day_limit': 34} # 行业 best_param_dic = { 'adjust_day_limit': 24, 'back_day_limit': 30 } # 主题 IndustryRecyleDemo = IndustryRecyle() industry_index_name_dic, product_name_dic = IndustryRecyleDemo.get_fund_index( fund_type=fund_type, style_flag=param_str) if method not in [ 'industry_recyle_stock', 'industry_recyle_mean_var_stock' ]: asset_index = { 'stock': industry_index_name_dic, 'bond': { 'H00140.SH': u'上证五年期国债指数' } } if 'H00140.SH' not in product_name_dic: product_name_dic['H00140.SH'] = { '000088.OF': "嘉实中期国债ETF联接C " } else: asset_index = { 'stock': industry_index_name_dic, } fundPortfolioDemo = fundPortfolio(startDate='2015-09-01', file_path=param_str) fundPortfolioDemo.setMain(method=method, productFlag=True, asset_index=asset_index, best_param_dic=best_param_dic, product_name_dic=product_name_dic, fund_type=fund_type) reslut_file_loc = fundPortfolioDemo.PathFolder + method + '\\' self.get_PMS_format(file_path=reslut_file_loc, param_str=file_str) return asset_index = eval(total_df.loc[param_str]['模型参数']) method = total_df.loc[param_str]['配置模型'] self.logger.info("----------------------best_param----------------->") fundPortfolioDemo = fundPortfolio(startDate='2015-09-01', file_path=param_str) fundPortfolioDemo.setMain(method=method, productFlag=True, asset_index=asset_index, best_param_dic=best_param_dic, fund_type=fund_type) reslut_file_loc = fundPortfolioDemo.PathFolder + method + '\\' self.get_PMS_format(file_path=reslut_file_loc, param_str=file_str) return