def sample_935_2(): """ 9.3.5_2 不同权重的评分: 只考虑胜率 :return: """ from abupy import ABuFileUtil score_fn = '../gen/score_tuple_array' if not ABuFileUtil.file_exist(score_fn): print( '../gen/score_tuple_array not exist! please execute sample_933 first!' ) return """ 直接读取本地序列化文件 """ score_tuple_array = ABuFileUtil.load_pickle(score_fn) from abupy import WrsmScorer # 只有第一项为1,其他都是0代表只考虑胜率来评分 scorer = WrsmScorer(score_tuple_array, weights=[1, 0, 0, 0]) # 返回按照评分排序后的队列 scorer_returns_max = scorer.fit_score() # index[-1]为最优参数序号 best_score_tuple_grid = score_tuple_array[scorer_returns_max.index[-1]] AbuMetricsBase.show_general(best_score_tuple_grid.orders_pd, best_score_tuple_grid.action_pd, best_score_tuple_grid.capital, best_score_tuple_grid.benchmark, only_info=False) # 最后打印出只考虑胜率下最优结果使用的买入策略和卖出策略 print( 'best_score_tuple_grid.buy_factors, best_score_tuple_grid.sell_factors:\n', best_score_tuple_grid.buy_factors, best_score_tuple_grid.sell_factors)
def sample_934(): """ 9.3.4 度量结果的评分 :return: """ from abupy import ABuFileUtil score_fn = '../gen/score_tuple_array' if not ABuFileUtil.file_exist(score_fn): print( '../gen/score_tuple_array not exist! please execute sample_933 first!' ) return """ 直接读取本地序列化文件 """ score_tuple_array = ABuFileUtil.load_pickle(score_fn) from abupy import WrsmScorer # 实例化一个评分类WrsmScorer,它的参数为之前GridSearch返回的score_tuple_array对象 scorer = WrsmScorer(score_tuple_array) print('scorer.score_pd.tail():\n', scorer.score_pd.tail()) # score_tuple_array[658]与grid_search.best_score_tuple_grid是一致的 sfs = scorer.fit_score() # 打印前15个高分组合 print('sfs[::-1][:15]:\n', sfs[::-1][:15])
def sample_935_2(): """ 9.3.5_2 不同权重的评分: 只考虑胜率 :return: """ from abupy import ABuFileUtil score_fn = '../gen/score_tuple_array' if not ABuFileUtil.file_exist(score_fn): print('../gen/score_tuple_array not exist! please execute sample_933 first!') return """ 直接读取本地序列化文件 """ score_tuple_array = ABuFileUtil.load_pickle(score_fn) from abupy import WrsmScorer # 只有第一项为1,其他都是0代表只考虑胜率来评分 scorer = WrsmScorer(score_tuple_array, weights=[1, 0, 0, 0]) # 返回按照评分排序后的队列 scorer_returns_max = scorer.fit_score() # index[-1]为最优参数序号 best_score_tuple_grid = score_tuple_array[scorer_returns_max.index[-1]] AbuMetricsBase.show_general(best_score_tuple_grid.orders_pd, best_score_tuple_grid.action_pd, best_score_tuple_grid.capital, best_score_tuple_grid.benchmark, only_info=False) # 最后打印出只考虑胜率下最优结果使用的买入策略和卖出策略 print('best_score_tuple_grid.buy_factors, best_score_tuple_grid.sell_factors:\n', best_score_tuple_grid.buy_factors, best_score_tuple_grid.sell_factors)
def sample_935_1(): """ 9.3.5_1 不同权重的评分: 只考虑投资回报来评分 :return: """ from abupy import ABuFileUtil score_fn = '../gen/score_tuple_array' if not ABuFileUtil.file_exist(score_fn): print('../gen/score_tuple_array not exist! please execute sample_933 first!') return """ 直接读取本地序列化文件 """ score_tuple_array = ABuFileUtil.load_pickle(score_fn) from abupy import WrsmScorer # 实例化WrsmScorer,参数weights,只有第二项为1,其他都是0, # 代表只考虑投资回报来评分 scorer = WrsmScorer(score_tuple_array, weights=[0, 1, 0, 0]) # 返回排序后的队列 scorer_returns_max = scorer.fit_score() # 因为是倒序排序,所以index最后一个为最优参数 best_score_tuple_grid = score_tuple_array[scorer_returns_max.index[-1]] # 由于篇幅,最优结果只打印文字信息 AbuMetricsBase.show_general(best_score_tuple_grid.orders_pd, best_score_tuple_grid.action_pd, best_score_tuple_grid.capital, best_score_tuple_grid.benchmark, only_info=True) # 最后打印出只考虑投资回报下最优结果使用的买入策略和卖出策略 print('best_score_tuple_grid.buy_factors, best_score_tuple_grid.sell_factors:\n', best_score_tuple_grid.buy_factors, best_score_tuple_grid.sell_factors)
def sample_934(): """ 9.3.4 度量结果的评分 :return: """ from abupy import ABuFileUtil score_fn = '../gen/score_tuple_array' if not ABuFileUtil.file_exist(score_fn): print('../gen/score_tuple_array not exist! please execute sample_933 first!') return """ 直接读取本地序列化文件 """ score_tuple_array = ABuFileUtil.load_pickle(score_fn) from abupy import WrsmScorer # 实例化一个评分类WrsmScorer,它的参数为之前GridSearch返回的score_tuple_array对象 scorer = WrsmScorer(score_tuple_array) print('scorer.score_pd.tail():\n', scorer.score_pd.tail()) # score_tuple_array[658]与grid_search.best_score_tuple_grid是一致的 sfs = scorer.fit_score() # 打印前15个高分组合 print('sfs[::-1][:15]:\n', sfs[::-1][:15])
for sell_factors_product_item in sell_factors_product: # sell_factors_product_item[0]['slippage'] = AbuSlippageSellOpen sell_factors_product_item[0]['slippage'] = AbuSlippageSellClose # sell_factors_product_item[0]['slippage'] = AbuSlippageSellMinMax print('卖出因子参数共有{}种组合方式'.format(len(sell_factors_product))) ABuEnv.date_str = datetime.datetime.now().strftime("_%Y_%m_%d") grid_search = GridSearch(read_cash,choice_symbols,buy_factors_product=buy_factors_product, sell_factors_product=sell_factors_product,n_folds=1,start=startDate, end=endDate) if __name__ == '__main__': #多线程必须内容,不可删除。 scores,score_tuple_array = grid_search.fit(n_jobs=-1) store_python_obj(score_tuple_array, 'score_tuple_array'+ABuEnv.date_str, show_log=False) #-------权重方式开始------ if score_tuple_array != None: scorer = WrsmScorer(score_tuple_array, weights=[1, 0, 0, 0]) scorer_returns_max = scorer.fit_score() best_result_tuple = score_tuple_array[scorer_returns_max.index[-1]] print(best_result_tuple.buy_factors) #打印最优因子 print(best_result_tuple.sell_factors) #打印最优因子 store_abu_result_tuple(best_result_tuple, n_folds=None, store_type=EStoreAbu.E_STORE_CUSTOM_NAME, custom_name='best_1_0_0_0') AbuMetricsBase.show_general(best_result_tuple.orders_pd, best_result_tuple.action_pd, best_result_tuple.capital, best_result_tuple.benchmark, only_info=False) scorer = WrsmScorer(score_tuple_array, weights=[0, 1, 0, 0]) scorer_returns_max = scorer.fit_score() best_result_tuple = score_tuple_array[scorer_returns_max.index[-1]] print(best_result_tuple.buy_factors) # 打印最优因子 print(best_result_tuple.sell_factors) # 打印最优因子 store_abu_result_tuple(best_result_tuple, n_folds=None, store_type=EStoreAbu.E_STORE_CUSTOM_NAME,