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_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_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_a23(): # 设置初始资金数 read_cash = 50000000 # abupy.env.g_enable_ump_main_deg_block = True # abupy.env.g_enable_ump_main_jump_block = True # abupy.env.g_enable_ump_main_price_block = True # abupy.env.g_enable_ump_main_wave_block = True ########################################################################################### # 回测生成买入时刻特征 abupy.env.g_enable_ml_feature = True # 回测将symbols切割分为训练集数据和测试集数据 abupy.env.g_enable_train_test_split = True # 下面设置回测时切割训练集,测试集使用的切割比例参数,默认为10,即切割为10份,9份做为训练,1份做为测试, # 由于美股股票数量多,所以切割分为4份,3份做为训练集,1份做为测试集 abupy.env.g_split_tt_n_folds = 10 ########################################################################################### # 择时股票池 # choice_symbols = ['603993', '601998', '601992', '601991', '601989', '601988', '601985', '601939', '601933', '601919', '601901', '601899', '601898', '601881', '601877', '601857', '601828', '601818', '601808', '601800', '601788', '601766', '601727', '601688', '601669', '601668', '601633', '601628', '601618', '601607', '601601', '601600', '601398', '601390', '601360', '601328', '601288', '601238', '601229', '601225', '601211', '601186', '601169', '601166', '601155', '601111', '601108', '601088', '601018', '601012', '601009', '601006', '600999', '600958', '600919', '600900', '600893', '600887', '600837', '600816', '600795', '600703', '600690', '600688', '600663', '600660', '600606', '600600', '600588', '600585', '600518', '600487', '600406', '600398', '600383', '600362', '600346', '600340', '600309', '600297', '600221', '600196', '600188', '600176', '600115', '600111', '600104', '600061', '600050', '600048', '600036', '600031', '600030', '600029', '600028', '600025', '600023', '600019', '600018', '600016', '600015', '600011', '600010', '600000', '300498', '300433', '300124', '300072', '300070', '300059', '300015', '300003', '002739', '002736', '002714', '002600', '002558', '002493', '002475', '002456', '002450', '002415', '002310', '002252', '002241', '002236', '002202', '002142', '002120', '002044', '002027', '002024', '002010', '001979', '001965', '000895', '000776', '000725', '000617', '000166', '000069', '000063', '000039', '000002', '000001'] # choice_symbols = ['601398'] # choice_symbols = ['600036'] # choice_symbols = ['601398', '601988', '601939', '601328', '601288', '600887', '600029', '000002'] choice_symbols = ['601398', '601988', '601939', '603993', '600999', '300059', '600900', '601328', '601288', '600887', '600029', '000002', '002024', '002241', '600050', '601989', '601992', '601901'] abu_result_tuple, _ = abu.run_loop_back(read_cash, buy_factors, sell_factors, stock_pickers, choice_symbols=choice_symbols, n_folds=6, start='2012-04-20', end='2018-04-20', commission_dict=commission_dict) # 把运行的结果保存在本地,以便之后分析回测使用,保存回测结果数据代码如下所示 abu.store_abu_result_tuple(abu_result_tuple, n_folds=6, store_type=abupy.EStoreAbu.E_STORE_CUSTOM_NAME, custom_name='18_top_train_cn') abu.store_abu_result_tuple(abu_result_tuple, n_folds=6, store_type=abupy.EStoreAbu.E_STORE_CUSTOM_NAME, custom_name='18_top_test_cn') AbuMetricsBase.show_general(*abu_result_tuple, only_show_returns=True) metrics = AbuMetricsBase(*abu_result_tuple) metrics.fit_metrics() # 筛出有交易结果的 orders_pd_atr = abu_result_tuple.orders_pd[abu_result_tuple.orders_pd.result != 0] orders_pd_atr.filter(['buy_cnt', 'buy_pos', 'buy_price', 'profit', 'result']) metrics.plot_returns_cmp(only_info=True) # metrics.plot_buy_factors() # metrics.plot_sell_factors() metrics.plot_effect_mean_day() # plt.show() metrics.plot_keep_days() # plt.show() metrics.plot_max_draw_down()
def run_loo_back(choice_symbols, start, end): abu_result_tuple, _ = abu.run_loop_back(read_cash, buy_factors, sell_factors, start=start, end=end, choice_symbols=choice_symbols, n_process_pick=1) ABuProgress.clear_output() AbuMetricsBase.show_general(*abu_result_tuple, returns_cmp=True, only_info=True) return abu_result_tuple
def sample_94_4(from_cache=False): """ 满仓乘数 9.4_4 《量化交易之路》中通过把初始资金扩大到非常大,但是每笔交易的买入基数却不增高,来使交易全部都成交, 再使用满仓乘数的示例,由于需要再次进行全市场回测,比较耗时。 下面直接示例通过AbuMetricsBase中的transform_to_full_rate_factor接口将之前的回测结果转换为使用大初始资金回测的结果 :return: """ metrics_test = sample_94_3(from_cache=True, show=False) from abupy import EStoreAbu if from_cache: test_us_fr = abu.load_abu_result_tuple( n_folds=5, store_type=EStoreAbu.E_STORE_CUSTOM_NAME, custom_name='test_us_full_rate') # 本地读取后使用AbuMetricsBase构造度量对象,参数enable_stocks_full_rate_factor=True, 即使用满仓乘数 test_frm = AbuMetricsBase(test_us_fr.orders_pd, test_us_fr.action_pd, test_us_fr.capital, test_us_fr.benchmark, enable_stocks_full_rate_factor=True) test_frm.fit_metrics() else: test_frm = metrics_test.transform_to_full_rate_factor(n_process_kl=4, show=False) # 转换后保存起来,下次直接读取,不用再转换了 from abupy import AbuResultTuple test_us_fr = AbuResultTuple(test_frm.orders_pd, test_frm.action_pd, test_frm.capital, test_frm.benchmark) abu.store_abu_result_tuple(test_us_fr, n_folds=5, store_type=EStoreAbu.E_STORE_CUSTOM_NAME, custom_name='test_us_full_rate') """ 使用test_frm进行度量结果可以看到所有交易都顺利成交了,策略买入成交比例:100.0000%,但资金利用率显然过低, 它导致基准收益曲线和策略收益曲线不在一个量级上,无法有效的进行对比 """ AbuMetricsBase.show_general(test_frm.orders_pd, test_frm.action_pd, test_frm.capital, test_frm.benchmark, only_show_returns=True) """转换出来的test_frm即是一个使用满仓乘数的度量对象,下面使用test_frm直接进行满仓度量即可""" print(type(test_frm)) test_frm.plot_returns_cmp(only_show_returns=True) # 如果不需要与基准进行对比,最简单的方式是使用plot_order_returns_cmp metrics_test.plot_order_returns_cmp()
def run_loo_back(choice_symbols, ps=None, n_folds=3, start=None, end=None, only_info=False): """封装一个回测函数,返回回测结果,以及回测度量对象""" if choice_symbols[0].startswith('us'): abupy.env.g_market_target = EMarketTargetType.E_MARKET_TARGET_US else: abupy.env.g_market_target = EMarketTargetType.E_MARKET_TARGET_CN abu_result_tuple, _ = abu.run_loop_back(cash, buy_factors, sell_factors, ps, start=start, end=end, n_folds=n_folds, choice_symbols=choice_symbols) ABuProgress.clear_output() metrics = AbuMetricsBase.show_general(*abu_result_tuple, returns_cmp=only_info, only_info=only_info, only_show_returns=True) return abu_result_tuple, metrics
def sample_94_4(from_cache=False): """ 满仓乘数 9.4_4 《量化交易之路》中通过把初始资金扩大到非常大,但是每笔交易的买入基数却不增高,来使交易全部都成交, 再使用满仓乘数的示例,由于需要再次进行全市场回测,比较耗时。 下面直接示例通过AbuMetricsBase中的transform_to_full_rate_factor接口将之前的回测结果转换为使用大初始资金回测的结果 :return: """ metrics_test = sample_94_3(from_cache=True, show=False) from abupy import EStoreAbu if from_cache: test_us_fr = abu.load_abu_result_tuple(n_folds=5, store_type=EStoreAbu.E_STORE_CUSTOM_NAME, custom_name='test_us_full_rate') # 本地读取后使用AbuMetricsBase构造度量对象,参数enable_stocks_full_rate_factor=True, 即使用满仓乘数 test_frm = AbuMetricsBase(test_us_fr.orders_pd, test_us_fr.action_pd, test_us_fr.capital, test_us_fr.benchmark, enable_stocks_full_rate_factor=True) test_frm.fit_metrics() else: test_frm = metrics_test.transform_to_full_rate_factor(n_process_kl=4, show=False) # 转换后保存起来,下次直接读取,不用再转换了 from abupy import AbuResultTuple test_us_fr = AbuResultTuple(test_frm.orders_pd, test_frm.action_pd, test_frm.capital, test_frm.benchmark) abu.store_abu_result_tuple(test_us_fr, n_folds=5, store_type=EStoreAbu.E_STORE_CUSTOM_NAME, custom_name='test_us_full_rate') """ 使用test_frm进行度量结果可以看到所有交易都顺利成交了,策略买入成交比例:100.0000%,但资金利用率显然过低, 它导致基准收益曲线和策略收益曲线不在一个量级上,无法有效的进行对比 """ AbuMetricsBase.show_general(test_frm.orders_pd, test_frm.action_pd, test_frm.capital, test_frm.benchmark, only_show_returns=True) """转换出来的test_frm即是一个使用满仓乘数的度量对象,下面使用test_frm直接进行满仓度量即可""" print(type(test_frm)) test_frm.plot_returns_cmp(only_show_returns=True) # 如果不需要与基准进行对比,最简单的方式是使用plot_order_returns_cmp metrics_test.plot_order_returns_cmp()
def run_loo_back(choice_symbols, ps=None, n_folds=3, start=None, end=None, only_info=False): abu_result_tuple, _ = abu.run_loop_back(cash, buy_factors, sell_factors, ps, start=start, end=end, n_folds=n_folds, choice_symbols=choice_symbols) abu_result_tuple.orders_pd['buy_factor'] = abu_result_tuple.orders_pd[ 'buy_factor'].apply(lambda bf: bf.split(':')[0]) ABuProgress.clear_output() metrics = AbuMetricsBase.show_general(*abu_result_tuple, returns_cmp=only_info, only_info=only_info, only_show_returns=True) return abu_result_tuple, metrics
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, custom_name='best_0_1_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=True) else: print("没有订单生成!") end = datetime.datetime.now() print("程序运行时间:" +str(end - start)) #-------权重方式结束------
# 卖出因子 sell_factors = [{ 'stop_loss_n': 1.0, 'stop_win_n': 3.0, 'class': AbuFactorAtrNStop }, { 'class': AbuFactorPreAtrNStop, 'pre_atr_n': 1.5 }, { 'class': AbuFactorCloseAtrNStop, 'close_atr_n': 1.5 }] # 择时股票池 choice_symbols = [ 'hk03333', 'hk00700', 'hk02333', 'hk01359', 'hk00656', 'hk03888', 'hk02318' ] # 设置市场类型为港股 abupy.env.g_market_target = EMarketTargetType.E_MARKET_TARGET_HK abu_result_tuple, kl_pd_manger = \ abu.run_loop_back(read_cash, buy_factors, sell_factors, n_folds=6,choice_symbols=choice_symbols) ABuProgress.clear_output() AbuMetricsBase.show_general(*abu_result_tuple, only_show_returns=True)
from abupy import AbuFactorAtrNStop, AbuFactorPreAtrNStop, AbuFactorCloseAtrNStop, AbuFactorBuyBreak from abupy import abu, EMarketTargetType, AbuMetricsBase, ABuMarketDrawing, ABuProgress, ABuSymbolPd from abupy import EMarketTargetType, EDataCacheType, EMarketSourceType, EMarketDataFetchMode, EStoreAbu, AbuUmpMainMul from abupy import AbuUmpMainDeg, AbuUmpMainJump, AbuUmpMainPrice, AbuUmpMainWave, feature, AbuFeatureDegExtend from abupy import AbuUmpEdgeDeg, AbuUmpEdgePrice, AbuUmpEdgeWave, AbuUmpEdgeFull, AbuUmpEdgeMul, AbuUmpEegeDegExtend from abupy import AbuUmpMainDegExtend, ump, Parallel, delayed, AbuMulPidProgress, AbuProgress if __name__ == '__main__': abu_result_tuple_train = abu.load_abu_result_tuple( n_folds=5, store_type=EStoreAbu.E_STORE_CUSTOM_NAME, custom_name='train_cn') abu_result_tuple_test = abu.load_abu_result_tuple( n_folds=5, store_type=EStoreAbu.E_STORE_CUSTOM_NAME, custom_name='test_cn') ABuProgress.clear_output() print('训练集结果:') metrics_train = AbuMetricsBase.show_general(*abu_result_tuple_train, only_show_returns=True) print('测试集结果:') metrics_test = AbuMetricsBase.show_general(*abu_result_tuple_test, only_show_returns=True)
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 select(select): if select == 'run gird search': run_grid_search() else: # load score cache load_score_cache() _ = ipywidgets.interact_manual(select, select=['run gird search', 'load score cache']) 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)