def sample_811(): """ 8.1.1 买入因子的实现 :return: """ # buy_factors 60日向上突破,42日向上突破两个因子 buy_factors = [{ 'xd': 60, 'class': AbuFactorBuyBreak }, { 'xd': 42, 'class': AbuFactorBuyBreak }] benchmark = AbuBenchmark() capital = AbuCapital(1000000, benchmark) kl_pd_manager = AbuKLManager(benchmark, capital) # 获取TSLA的交易数据 kl_pd = kl_pd_manager.get_pick_time_kl_pd('usTSLA') abu_worker = AbuPickTimeWorker(capital, kl_pd, benchmark, buy_factors, None) abu_worker.fit() orders_pd, action_pd, _ = ABuTradeProxy.trade_summary(abu_worker.orders, kl_pd, draw=True) ABuTradeExecute.apply_action_to_capital(capital, action_pd, kl_pd_manager) capital.capital_pd.capital_blance.plot() plt.show()
def sample_811(): """ 8.1.1 买入因子的实现 :return: """ # buy_factors 60日向上突破,42日向上突破两个因子 buy_factors = [{'xd': 60, 'class': AbuFactorBuyBreak}, {'xd': 42, 'class': AbuFactorBuyBreak}] benchmark = AbuBenchmark() capital = AbuCapital(1000000, benchmark) kl_pd_manager = AbuKLManager(benchmark, capital) # 获取TSLA的交易数据 kl_pd = kl_pd_manager.get_pick_time_kl_pd('usTSLA') abu_worker = AbuPickTimeWorker(capital, kl_pd, benchmark, buy_factors, None) abu_worker.fit() orders_pd, action_pd, _ = ABuTradeProxy.trade_summary(abu_worker.orders, kl_pd, draw=True) ABuTradeExecute.apply_action_to_capital(capital, action_pd, kl_pd_manager) capital.capital_pd.capital_blance.plot() plt.show()
benchmark = AbuBenchmark() # 现金和基准利润 capital = AbuCapital(1000000, benchmark) # 多线程管理类 kl_pd_manager = AbuKLManager(benchmark, capital) # 获取TSLA的股票信息 kl_pd = kl_pd_manager.get_pick_time_kl_pd('usTSLA') # 准备开始工作 abu_worker = AbuPickTimeWorker(capital, kl_pd, benchmark, buy_factors, None) abu_worker.fit() # 画出哪几个点可以买入,以及最终的收益情况 # orders_pd,action_pd,_=ABuTradeProxy.trade_summary(abu_worker.orders,kl_pd,draw=True) orders_pd, action_pd, _ = ABuTradeProxy.trade_summary(abu_worker.orders, kl_pd, draw=False) # 上面是从股价角度,下面从我们的资金来看 # ABuTradeExecute.apply_action_to_capital(capital, action_pd, kl_pd_manager) # print(capital.capital_pd.head()) # capital.capital_pd.capital_blance.plot() # plt.show() # 上面只是实现了买入突破,下面我们用卖出突破来试试 # 2、这里是卖出突破 sell_factor1 = {'xd': 120, 'class': AbuFactorSellBreak} sell_factors = [sell_factor1] capital = AbuCapital(1000000, benchmark) orders_pd, action_pd, _ = ABuPickTimeExecute.do_symbols_with_same_factors( ['usTSLA'], benchmark, buy_factors, sell_factors, capital, show=False)
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 ABuTradeExecute #将action_pd作用在capital上,即开始涉及资金 ABuTradeExecute.apply_action_to_capital(capital, action_pd, kl_pd_manager) #绘制资产曲线 capital.capital_pd.capital_blance.plot() """ 卖出因子的实现 上面所有单子都没有成交的原因是没有卖出因子,下面首先实现类似买入策略的N日趋势突破策略AbuFactorSellBreak 当股价向下突破N日最低价格时卖出股票 """ #需要继承自AbuFactorSellBase