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_711(): """ 7.1.1 趋势跟踪和均值回复的周期重叠性 :return: """ sns.set_context(rc={'figure.figsize': (14, 7)}) sns.regplot(x=np.arange(0, kl_pd.shape[0]), y=kl_pd.CLOSE.values, marker='+') plt.show() from abupy import ABuRegUtil deg = ABuRegUtil.calc_regress_deg(kl_pd.CLOSE.values) plt.show() print('趋势角度:' + str(deg)) start = 0 # 前1/4的数据 end = int(kl_pd.shape[0] / 4) # 将x也使用arange切割 x = np.arange(start, end) # y根据start,end进行切片 y = kl_pd.CLOSE.values[start:end] sns.regplot(x=x, y=y, marker='+') plt.show() start = int(kl_pd.shape[0] / 4) # 向前推1/4单位个时间 end = start + int(kl_pd.shape[0] / 4) sns.regplot(x=np.arange(start, end), y=kl_pd.CLOSE.values[start:end], marker='+') 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_711(): """ 7.1.1 趋势跟踪和均值回复的周期重叠性 :return: """ sns.set_context(rc={'figure.figsize': (14, 7)}) sns.regplot(x=np.arange(0, kl_pd.shape[0]), y=kl_pd.close.values, marker='+') plt.show() from abupy import ABuRegUtil deg = ABuRegUtil.calc_regress_deg(kl_pd.close.values) plt.show() print('趋势角度:' + str(deg)) start = 0 # 前1/4的数据 end = int(kl_pd.shape[0] / 4) # 将x也使用arange切割 x = np.arange(start, end) # y根据start,end进行切片 y = kl_pd.close.values[start:end] sns.regplot(x=x, y=y, marker='+') plt.show() start = int(kl_pd.shape[0] / 4) # 向前推1/4单位个时间 end = start + int(kl_pd.shape[0] / 4) sns.regplot(x=np.arange(start, end), y=kl_pd.close.values[start:end], marker='+') plt.show()
def fit_pick(self, kl_pd, target_symbol): """开始根据自定义拟合角度边际参数进行选股""" # 计算走势角度 ang = ABuRegUtil.calc_regress_deg(kl_pd.close, show=False) # 根据参数进行角度条件判断 if self.threshold_ang_min < ang < self.threshold_ang_max: return True return False
def trendSingle(stock): """ calculate the trend of given data length, if you need latest month then you can pass like pdDataFrame[-20:-1] """ from abupy import ABuRegUtil return stock, ABuRegUtil.calc_regress_deg( alldata[alldata.code == stock].reset_index(drop=True).close.values, show=False)
def sample_12(): from abupy import ABuRegUtil # kl_pd = ABuSymbolPd.make_kl_df('601398', start='2015-03-01', end='2015-04-01', parallel=False, n_folds=1) kl_pd = ABuSymbolPd.make_kl_df('601398', start='2011-03-01', parallel=False, n_folds=1) deg = ABuRegUtil.calc_regress_deg(kl_pd.close.values, show=False) print('趋势角度:' + str(deg)) pd = calc_wave_std(kl_pd, show=False) print(pd) print(pd.high) print(pd.low) print(pd.close)
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 calAngle(df): """ trend angle based on provided dataframe """ return ABuRegUtil.calc_regress_deg(df.close.values,show=False)
# 开始执行 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) 同样,上面是采用了一种选股因子,那么,当我采用多种选股因子时会怎样呢?
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)))
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) print(picked_stocks)
industry_data = industry_data[~industry_data.name.str.contains('ST')] industry_data2 = industry_data['code'][industry_data.c_name == '公路桥梁'] #过滤 # industry_data2 = industry_data['code'] choice_symbols2 = industry_data2.values choice_symbols = choice_symbols2 # choice_symbols = ['000895'] benchmark = AbuBenchmark(n_folds=1, start=None, end=None) kl_pd = benchmark.kl_pd pd_len = kl_pd.shape[0] kl_pd = kl_pd[kl_pd.key > pd_len - 16][kl_pd.key < pd_len - 11] plt.plot(kl_pd.index, kl_pd.close) plt.show() ang = ABuRegUtil.calc_regress_deg(kl_pd.close, show=True) print('test OK') # factor_dict = {'class': AbuPickRegressAngMinMax, # 'xd': self.xd.value, # 'reversed': self.reversed.value, # 'threshold_ang_min': ang_min, # 'threshold_ang_max': ang_max} # # factor_desc_key = u'角度选股最大:{}最小:{},周期:{},反转:{}'.format( # ang_max, ang_min, self.xd.value, self.reversed.value) # abu_result_tuple,kl_pd_manager = abu.run_loop_back(read_cash,buy_factors,sell_factors,stock_pickers,choice_symbols=choice_symbols,n_folds = 1) # store_abu_result_out_put(abu_result_tuple)