def sample_821_2(): """ 8.2.1_2 ABuPickStockExecute :return: """ stock_pickers = [{ 'class': AbuPickRegressAngMinMax, 'threshold_ang_min': 0.0, 'threshold_ang_max': 10.0, 'reversed': False }] choice_symbols = [ 'usNOAH', 'usSFUN', 'usBIDU', 'usAAPL', 'usGOOG', 'usTSLA', 'usWUBA', 'usVIPS' ] benchmark = AbuBenchmark() capital = AbuCapital(1000000, benchmark) kl_pd_manager = AbuKLManager(benchmark, capital) print( 'ABuPickStockExecute.do_pick_stock_work:\n', ABuPickStockExecute.do_pick_stock_work(choice_symbols, benchmark, capital, stock_pickers)) kl_pd_sfun = kl_pd_manager.get_pick_stock_kl_pd('usSFUN') print('sfun 选股周期内角度={}'.format( round(ABuRegUtil.calc_regress_deg(kl_pd_sfun.close), 3)))
def sample_821_1(): """ 8.2.1_1 选股使用示例 :return: """ # 选股条件threshold_ang_min=0.0, 即要求股票走势为向上上升趋势 stock_pickers = [{'class': AbuPickRegressAngMinMax, 'threshold_ang_min': 0.0, 'reversed': False}] # 从这几个股票里进行选股,只是为了演示方便 # 一般的选股都会是数量比较多的情况比如全市场股票 choice_symbols = ['usNOAH', 'usSFUN', 'usBIDU', 'usAAPL', 'usGOOG', 'usTSLA', 'usWUBA', 'usVIPS'] benchmark = AbuBenchmark() capital = AbuCapital(1000000, benchmark) kl_pd_manager = AbuKLManager(benchmark, capital) stock_pick = AbuPickStockWorker(capital, benchmark, kl_pd_manager, choice_symbols=choice_symbols, stock_pickers=stock_pickers) stock_pick.fit() # 打印最后的选股结果 print('stock_pick.choice_symbols:', stock_pick.choice_symbols) # 从kl_pd_manager缓存中获取选股走势数据,注意get_pick_stock_kl_pd为选股数据,get_pick_time_kl_pd为择时 kl_pd_noah = kl_pd_manager.get_pick_stock_kl_pd('usNOAH') # 绘制并计算角度 deg = ABuRegUtil.calc_regress_deg(kl_pd_noah.close) print('noah 选股周期内角度={}'.format(round(deg, 3)))
def sample_821_2(): """ 8.2.1_2 ABuPickStockExecute :return: """ stock_pickers = [{ 'class': AbuPickRegressAngMinMax, 'threshold_ang_min': 0.0, 'threshold_ang_max': 10.0, 'reversed': False }] choice_symbols = [ '601398', '601988', '601939', '603993', '600999', '300059', '600900', '601328', '601288', '600887', '600029', '000002' ] benchmark = AbuBenchmark() capital = AbuCapital(1000000, benchmark) kl_pd_manager = AbuKLManager(benchmark, capital) print( 'ABuPickStockExecute.do_pick_stock_work:\n', ABuPickStockExecute.do_pick_stock_work(choice_symbols, benchmark, capital, stock_pickers)) kl_pd_sfun = kl_pd_manager.get_pick_stock_kl_pd('601398') print('sfun 选股周期内角度={}'.format( round(ABuRegUtil.calc_regress_deg(kl_pd_sfun.close), 3)))
def sample_821_2(): """ 8.2.1_2 ABuPickStockExecute :return: """ stock_pickers = [{'class': AbuPickRegressAngMinMax, 'threshold_ang_min': 0.0, 'threshold_ang_max': 10.0, 'reversed': False}] choice_symbols = ['usNOAH', 'usSFUN', 'usBIDU', 'usAAPL', 'usGOOG', 'usTSLA', 'usWUBA', 'usVIPS'] benchmark = AbuBenchmark() capital = AbuCapital(1000000, benchmark) kl_pd_manager = AbuKLManager(benchmark, capital) print('ABuPickStockExecute.do_pick_stock_work:\n', ABuPickStockExecute.do_pick_stock_work(choice_symbols, benchmark, capital, stock_pickers)) kl_pd_sfun = kl_pd_manager.get_pick_stock_kl_pd('usSFUN') print('sfun 选股周期内角度={}'.format(round(ABuRegUtil.calc_regress_deg(kl_pd_sfun.close), 3)))
# 开始执行 benchmark = AbuBenchmark() capital = AbuCapital(1000000, benchmark) kl_pd_manager = AbuKLManager(benchmark, capital) stock_pick = AbuPickStockWorker(capital, benchmark, kl_pd_manager, choice_symbols=choice_symbols, stock_pickers=stock_pickers) stock_pick.fit() print(stock_pick.choice_symbols) # 绘图 kl_pd_SFUN = kl_pd_manager.get_pick_stock_kl_pd('usNOAH') deg = ABuRegUtil.calc_regress_deg(kl_pd_SFUN.close) print(deg) # 上面使用worker的操作太麻烦,下面可以直接使用executer stock_pickers = [{ 'class': AbuPickRegressAngMinMax, 'threshold_ang_min': 0.0, 'threshold_ang_max': 10.0, 'reversed': False }] result = ABuPickStockExecute.do_pick_stock_work(choice_symbols, benchmark, capital, stock_pickers) print(result) ''' 2)
'class': AbuFactorBuyBreak }] benchmark = AbuBenchmark() #benchmark的意义为参考基准,基准默认使用回测股票对应市场的大盘指数 """ 下面构建择时操作主类AbuPickTimeWorker,通过fit()函数执行回测,%time可以看到整个回测的用时情况: """ from abupy import AbuPickTimeWorker from abupy import AbuCapital from abupy import AbuKLManager capital = AbuCapital(1000000, benchmark) kl_pd_manager = AbuKLManager(benchmark, capital) #获取特斯拉择时阶段的走势数据 kl_pd = kl_pd_manager.get_pick_stock_kl_pd('usTSLA') abu_worker = AbuPickTimeWorker(capital, kl_pd, buy_factors, buy_factors, None) abu_worker.fit() """ 下面使用ABuTradeProxy.trade_summary()函数将abu_worker中生成的所有orders对象 进行转换及可视化,由图开发--1所示,由于AbuPickTimeWorker没有设置sell_factors, 所以所有的交易单子都一只保留没有卖出 orders_pd:所有交易的相关数据 action_pd:所有交易的行为数据 """ from abupy import ABuTradeProxy order_pd, action_pd, _ = ABuTradeProxy.trade_summary(abu_worker.orders, kl_pd, draw=True, show_info=True) """
from abupy import ABuRegUtil,EMarketTargetType abupy.env.enable_example_env_ipython() abupy.env.g_market_target = EMarketTargetType.E_MARKET_TARGET_CN # 选股条件threshold_ang_min=0.0, 即要求股票走势为向上上升趋势 stock_pickers = [{'class': AbuPickRegressAngMinMax, 'threshold_ang_min': 0.0, 'reversed': False}] # 从这几个股票里进行选股,只是为了演示方便 # 一般的选股都会是数量比较多的情况比如全市场股票 choice_symbols = ['usNOAH', 'usSFUN', 'usBIDU', 'usAAPL', 'usGOOG', 'usTSLA', 'usWUBA', 'usVIPS'] benchmark = AbuBenchmark() capital = AbuCapital(1000000, benchmark) kl_pd_manger = AbuKLManager(benchmark, capital) stock_pick = AbuPickStockWorker(capital, benchmark, kl_pd_manger, choice_symbols=choice_symbols, stock_pickers=stock_pickers) stock_pick.fit() # 打印最后的选股结果 print(stock_pick.choice_symbols) # 从kl_pd_manger缓存中获取选股走势数据, # 注意get_pick_stock_kl_pd()为选股数据,get_pick_time_kl_pd()为择时 kl_pd_noah = kl_pd_manger.get_pick_stock_kl_pd('usNOAH') # 绘制并计算角度 deg = ABuRegUtil.calc_regress_deg(kl_pd_noah.close) print('noah 选股周期内角度={}'.format(round(deg, 3)))
''' # tedious way from abupy import AbuPickStockWorker; stock_pick = AbuPickStockWorker(capital, benchmark, kl_pd_manager, choice_symbols = list(symbols.keys()), stock_pickers = stock_pickers); stock_pick.fit(); print('candidates:', list(symbols.keys())); print('picked:', stock_pick.choice_symbols); ''' # simple way from abupy import ABuPickStockExecute from abupy import ABuRegUtil picked_stocks = ABuPickStockExecute.do_pick_stock_work( list(symbols.keys()), benchmark, capital, stock_pickers) print('candidates:', list(symbols.keys())) for stock in picked_stocks: kl = kl_pd_manager.get_pick_stock_kl_pd(stock) print('stock name: ' + stock + ' degree: {}'.format( round(ABuRegUtil.calc_regress_deg(kl.close), 3))) # 2) pick stocks with multiple processes from abupy import AbuPickStockMaster picked_stocks = AbuPickStockMaster.do_pick_stock_with_process( capital, benchmark, stock_pickers, list(symbols.keys())) print(picked_stocks) # 3) pick stocks with multiple processes and threads picked_stocks = AbuPickStockMaster.do_pick_stock_with_process_mix_thread( capital, benchmark, stock_pickers, list(symbols.keys()), n_process=8, n_thread=3)