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_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_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)))
def sample_b3_4(): """ 【示例4】abu量化系统选股结合相关性,编写相关性选股策略 AbuPickSimilarNTop源代码请自行阅读,只简单示例使用。 :return: """ from abupy import AbuPickSimilarNTop from abupy import AbuPickStockWorker from abupy import AbuBenchmark, AbuCapital, AbuKLManager benchmark = AbuBenchmark() # 选股因子AbuPickSimilarNTop, 寻找与usTSLA相关性不低于0.95的股票 # 这里内部使用以整个市场作为观察者方式计算,即取值范围0-1 stock_pickers = [{ 'class': AbuPickSimilarNTop, 'similar_stock': 'usTSLA', 'threshold_similar_min': 0.95 }] # 从这几个股票里进行选股,只是为了演示方便,一般的选股都会是数量比较多的情况比如全市场股票 choice_symbols = [ 'usNOAH', 'usSFUN', 'usBIDU', 'usAAPL', 'usGOOG', 'usTSLA', 'usWUBA', 'usVIPS' ] 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:\n', stock_pick.choice_symbols) """ 通过选股因子first_choice属性执行批量优先选股操作,具体阅读源代码 """ # 选股因子AbuPickSimilarNTop, 寻找与usTSLA相关性不低于0.95的股票 # 通过设置'first_choice':True,进行优先批量操作,默认从对应市场选股 stock_pickers = [{ 'class': AbuPickSimilarNTop, 'first_choice': True, 'similar_stock': 'usTSLA', 'threshold_similar_min': 0.95 }] benchmark = AbuBenchmark() capital = AbuCapital(1000000, benchmark) kl_pd_manager = AbuKLManager(benchmark, capital) stock_pick = AbuPickStockWorker(capital, benchmark, kl_pd_manager, choice_symbols=None, stock_pickers=stock_pickers) stock_pick.fit() print('stock_pick.choice_symbols:\n', stock_pick.choice_symbols)
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_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()
# 1、这里是买入突破 # 创建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) 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())
def sample_821_3(): """ 8.2.1_2 ABuPickStockExecute :return: """ stock_pickers = [{ 'class': abupy.FuWeekVolumeBoll, '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' ] choice_symbols = [ 'sz000983', 'sh600338', 'sh600511', 'sh600196', 'sh600423', 'sz399136', 'sz002044', 'sh601800', 'sz300132', 'sz300133', 'sh000821', 'sz300003', 'sz300009', 'sz200045', 'sh600998', 'sz300313', 'sh601607', 'sz002644', 'sh600697', 'sz000627', 'sh000003', 'sz399302', 'sh600984', 'sz399301', 'sz000916', 'sz000911', 'sz000912', 'sz000688', 'sh600079', 'sh601101', 'sz000861', 'sz000736', 'sz002053', 'sz000048', 'sh600703', 'sh000814', 'sz300015', 'sh000818', 'sz399352', 'sz399356', 'sh900911', 'sh600395', 'sh000075', 'sz002323', 'sh000101', 'sh600285', 'sh600882', 'sz000789', 'sh601398', 'sz000898', 'sh601390', 'sh601009', 'sh601001', 'sz000525', 'sh600713', 'sh601628', 'sz399299', 'sz399298', 'sh600800', 'sh000808', 'sh900909', 'sh900908', 'sh000061', 'sh000068', 'sh000116', 'sz000617', 'sh600535', 'sz000792', 'sz000889', 'sz000065', 'sh601015', 'sz000089', 'sh600871', 'sz002412', 'sz399400', 'sz399402', 'sz399404', 'sh000057', 'sh900930', 'sh900936', 'sh900934', 'sh900935', 'sh600267', 'sz000650', 'sz399978', 'sh600485', 'sh601021', 'sh601601', 'sh600208', 'sh601288', 'sh600062', 'sh600015', 'sh600016', 'sz300197', 'sz300199', 'sz399413', 'sz399411', 'sz399416', 'sh000134', 'sh000136', 'sh000139', 'sz002007', 'sh600258', 'sh600123', 'sz000511', 'sh601618', 'sh600745', 'sz399170', 'sh000923', 'sz399319', 'sz399554', 'sz399555', 'sz002530', 'sh000145', 'sz002070', 'sh000149', 'sz399220', 'sh601998', 'sh600111', 'sh600023', 'sz000560', 'sh601699', 'sz399305', 'sz399431', 'sz000766', 'sz399436', 'sz399230', 'sz399237', 'sz002661', 'sz002599', 'sh000155', 'sh000152', 'sh000151', 'sh600806', 'sh601988', 'sh600693', 'sh600699', 'sh600582', 'sz000995', 'sh600566', 'sh601318', 'sz399150', 'sz399441', 'sz399200', 'sh000841', 'sh600917', 'sz002128', 'sh600176', 'sz000968', 'sh600771', 'sh600579', 'sh600578', 'sh600572', 'sh600681', 'sh600680', 'sz399140', 'sz000540', 'sz000545', 'sz200022', 'sz200026', 'sz200025', 'sz399210', 'sz200029', 'sz002601', 'sz002656', 'sz002204', 'sz002737', 'sz000748', 'sh600965', 'sz002135', 'sh000934', 'sh601169', 'sh601899', 'sh601898', 'sh600549', 'sh600546', 'sh600545', 'sz000778', 'sh600141', 'sh600145', 'sh601231', 'sz399139', 'sz000630', 'sz000613', 'sz399137', 'sz399130', 'sz399131', 'sz399132', 'sz399133', 'sz200019', 'sz300146', 'sz300144', 'sz399661', 'sz002701', 'sh600971', 'sz002382', 'sz002385', 'sh600085', 'sh603158', 'sz002602', 'sh601939', 'sh600007', 'sz399645', 'sh600000', 'sh601339', 'sh601336', 'sh000125', 'sz399674', 'sh000974', 'sz399160', 'sz002653', 'sz002717', 'sz200726', 'sz399647', 'sz300294', 'sh000100', 'sz300347', 'sh600348', 'sh000933', 'sh600401', 'sh000109', 'sz000034', 'sh600623', 'sz000581', 'sz000672', 'sz300028', 'sh603368', 'sh000023', 'sh000021', 'sz002198', 'sh600432', 'sh603989', 'sz000937', 'sh600508', 'sh600500', 'sh000159', 'sz000732', 'sh600188', 'sz000598', 'sz000029', 'sz000028', 'sz399394', 'sh000832', 'sz300036', 'sz200053', 'sz300326', 'sz002742', 'sz300253', 'sh000013', 'sh000011' ] # choice_symbols = ['002656', '000903'] benchmark = AbuBenchmark(n_folds=15) capital = AbuCapital(1000000, benchmark) kl_pd_manager = AbuKLManager(benchmark, capital) stock_pickers = ABuPickStockExecute.do_pick_stock_work( None, benchmark, # stock_pickers = ABuPickStockExecute.do_pick_stock_work(choice_symbols, benchmark, capital, stock_pickers) print('ABuPickvStockExecute.do_pick_stock_work:\n', stock_pickers) for stock_symbol in stock_pickers: if ~fetch_stock_base_info(stock_symbol): continue draw_candle(stock_symbol, 15)
# stock_kd = pd.getByName(stock) ABuEnv.g_cpu_cnt = 4#并发运行线程数 ABuEnv.draw_order_num = 0 #要绘制的订单数 ABuEnv.g_data_fetch_mode = EMarketDataFetchMode.E_DATA_FETCH_FORCE_NET #强制从网络获取 #ABuEnv.g_data_fetch_mode = EMarketDataFetchMode.E_DATA_FETCH_FORCE_LOCAL #强制本地,可多线程 ABuEnv.g_market_source = EMarketSourceType.E_MARKET_SOURCE_tx #作用同上点击效果。腾讯数据源(美股,A股,港股) ABuEnv.g_market_target = EMarketTargetType.E_MARKET_TARGET_CN benchmark = AbuBenchmark(n_folds=1, start=None, end=None) # 资金类初始化 capital = AbuCapital(1000000, benchmark, user_commission_dict=None) # kl数据管理类初始化 kl_pd_manager = AbuKLManager(benchmark, capital) # 批量获取择时kl数据 industryPchangeAllDaetDic = dict() if __name__ == '__main__': kl_pd_manager.batch_get_pick_time_kl_pd(choice_symbols, n_process=4) kl_pd_dick = kl_pd_manager.pick_kl_pd_dict['pick_time'] for stockCode, stockData in kl_pd_dick.items(): if stockData is None: continue pd_data = stockData.p_change stockIndustryNames = industry_data['c_name'][industry_data.code == stockCode] for _, stockIndustryName in stockIndustryNames.items(): if stockIndustryName in industryPchangeAllDaetDic.keys(): left = industryPchangeAllDaetDic[stockIndustryName] right = pd.DataFrame({'date': stockData.date, stockCode: stockData.p_change}) industryPchangeAllDaetDic[stockIndustryName] = pd.merge(left, right, on='date', how='outer')
stock_pickers = [{ 'class': AbuPickRegressAngMinMax, 'threshold_ang_min': 0.0, 'received': 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) # 绘图 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
#设置暴跌止损因子 sell_factor3 = {'pre_atr_n':1.0, 'class':AbuFactorPreAtrNStop} #设置保护止盈因子组成dict sell_factor4 = {'close_atr_n':1.5,'class': AbuFactorCloseAtrNStop} sell_factors = [sell_factor1, sell_factor2, sell_factor3,sell_factor4] benchmark = AbuBenchmark() choice_symbols = ['usTSLA', 'usNOAH', 'usSFUN', 'usBIDU', 'usAAPL', 'usGOOG', 'usWUBA', 'usVIPS'] capital = AbuCapital(1000000, benchmark) kl_pd_manager = AbuKLManager(benchmark,capital) #orders_pd, action_pd, _ = ABuTradeProxy.trade_summary(abu_worker.orders, kl_pd, draw=True) #orders_pd, action_pd, all_fit_symbols_cnt = ABuPickTimeExecute.do_symbols_with_same_factors(choice_symbols, benchmark, buy_factors, sell_factors, capital, show=False) orders_pd, action_pd, _ = ABuPickTimeExecute.do_symbols_with_same_factors(['usAAPL'], benchmark, buy_factors, sell_factors, capital, show=True) orders_pd[:10].filter(['symbol', 'buy_price', 'buy_cnt', 'buy_factor', 'buy_pos', 'sell_date', 'sell_type_extra', 'sell_type', 'profit']) print(action_pd[:10])
stock_pickers = [{ 'threshold_ang_min': 0.0, 'threshold_ang_max': 10.0, 'reversed': False, 'class': AbuPickRegressAngMinMax }, { 'threshold_price_min': 50.0, 'threshold_price_max': 100.0, 'reserved': False, 'class': AbuPickStockPriceMinMax }] symbols = ABuSymbol.search_to_symbol_dict('黄金') benchmark = AbuBenchmark() capital = AbuCapital(1000000, benchmark) # assign money kl_pd_manager = AbuKLManager(benchmark, capital) ''' # 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: