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_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_933(): """ 9.3.3 GridSearch寻找最优参数 :return: """ from abupy import GridSearch read_cash = 1000000 choice_symbols = ['usNOAH', 'usSFUN', 'usBIDU', 'usAAPL', 'usGOOG', 'usTSLA', 'usWUBA', 'usVIPS'] sell_factors_product, buy_factors_product = sample_932(show=False) grid_search = GridSearch(read_cash, choice_symbols, buy_factors_product=buy_factors_product, sell_factors_product=sell_factors_product) from abupy import ABuFileUtil """ 注意下面的运行耗时大约1小时多,如果所有cpu都用上的话,也可以设置n_jobs为 < cpu进程数,一边做其它的一边跑 """ # 运行GridSearch n_jobs=-1启动cpu个数的进程数 scores, score_tuple_array = grid_search.fit(n_jobs=-1) """ 针对运行完成输出的score_tuple_array可以使用dump_pickle保存在本地,以方便修改其它验证效果。 """ ABuFileUtil.dump_pickle(score_tuple_array, '../gen/score_tuple_array') print('组合因子参数数量{}'.format(len(buy_factors_product) * len(sell_factors_product))) print('最终评分结果数量{}'.format(len(scores))) best_score_tuple_grid = grid_search.best_score_tuple_grid 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)
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 load_score_cache(): """有本地数据score_tuple_array后,即可以从本地缓存读取score_tuple_array""" global scores, score_tuple_array with AbuBlockProgress('load score cache'): score_tuple_array = ABuFileUtil.load_pickle('../gen/score_tuple_array') if not hasattr(grid_search, 'best_score_tuple_grid'): # load_pickle的grid_search没有赋予best_score_tuple_grid,这里补上 from abupy import make_scorer, WrsmScorer scores = make_scorer(score_tuple_array, WrsmScorer) grid_search.best_score_tuple_grid = score_tuple_array[ scores.index[-1]] print('load complete!')
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 run_grid_search(): global scores, score_tuple_array # 运行GridSearch n_jobs=-1启动cpu个数的进程数 scores, score_tuple_array = grid_search.fit(n_jobs=-1) # 运行完成输出的score_tuple_array可以使用dump_pickle保存在本地,以方便之后使用 ABuFileUtil.dump_pickle(score_tuple_array, '../gen/score_tuple_array')