def pick_stock(self, date_picked): ds_tushare = DataServiceTushare() lst_code_picked = list() for ts_code in ds_tushare.get_stock_list(): stock_basic = ds_tushare.get_stock_basic_info(ts_code) if stock_basic is None: self.logger.info('None stock basic info %s' % ts_code) continue dt_date = string_to_datetime(date_picked) d = timedelta(days=-365 * self.n_years) if stock_basic['list_date'] > time_to_str( dt_date + d, '%Y%m%d') or 'ST' in stock_basic['name']: # 排除上市时间小于2年和st股票 continue dic_stock_price = ds_tushare.get_stock_price_info( ts_code, date_picked) if dic_stock_price is None: # 排除选股日停牌的股票 continue str_ma_long = 'ma_' + str(self.ma_long) flag_in_pool = True try: if dic_stock_price['high_250'] / dic_stock_price['low_250'] < self.max_times_in_year \ and dic_stock_price['ma_120'] > dic_stock_price['ma_250'] \ and dic_stock_price['ma_250'] > dic_stock_price['ma_500'] and dic_stock_price['close'] < dic_stock_price['high_250'] * (1.0 - self.pct_back)\ and dic_stock_price['close'] > dic_stock_price[str_ma_long] * (1.0-self.pct_near) and dic_stock_price['close'] < dic_stock_price[str_ma_long] * (1.0+self.pct_near): date_pre = ds_tushare.get_pre_trade_date(date_picked) price_pre = ds_tushare.get_stock_price_info( ts_code, date_pre) if dic_stock_price['ma_500'] > price_pre[ 'ma_500'] and dic_stock_price[ 'ma_250'] > price_pre['ma_250']: cal = ds_tushare.get_pre_n_trade_date( dic_stock_price['trade_date'], self.day_ma_long_effective) k_data_lst = ds_tushare.get_stock_price_lst( dic_stock_price['ts_code'], cal[-1], cal[0]) for k_data in k_data_lst: if (k_data['close'] < k_data[str_ma_long]): flag_in_pool = False break if flag_in_pool is True: lst_code_picked.append(dic_stock_price['ts_code']) except: self.logger.info( 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') self.logger.info(dic_stock_price) return lst_code_picked
def pick_stock(self, date_picked): ds_tushare = DataServiceTushare() lst_code_picked = list() for ts_code in ds_tushare.get_stock_list(): stock_basic = ds_tushare.get_stock_basic_info(ts_code) if stock_basic is None: self.logger.info('None stock basic info %s' % ts_code) continue dt_date = string_to_datetime(date_picked) d = timedelta(days=-365 * self.n_years) if stock_basic['list_date'] > time_to_str( dt_date + d, '%Y%m%d') or 'ST' in stock_basic['name']: # 排除上市时间小于2年和st股票 continue dic_stock_price = ds_tushare.get_stock_price_info( ts_code, date_picked) if dic_stock_price is None: # 排除选股日停牌的股票 continue high_gap = 'high_' + str(self.days_gap) low_gap = 'low_' + str(self.days_gap) days_break_gap = 'high_' + str(self.days_break) try: if dic_stock_price['high_250'] / dic_stock_price['low_250'] < self.max_times_in_year and \ dic_stock_price['ma_60'] > dic_stock_price['ma_120'] and \ dic_stock_price['ma_120'] > dic_stock_price['ma_250']: date_pre = ds_tushare.get_pre_trade_date( date_picked, self.days_gap) price_pre = ds_tushare.get_stock_price_info( ts_code, date_pre) if dic_stock_price['ts_code'] == '000998_SZ': a = 1 if price_pre['high'] < dic_stock_price[low_gap] and \ price_pre['high'] > price_pre[days_break_gap]*(1.0-self.pct_high_break) and \ dic_stock_price['low'] < price_pre['high']*(1.0+self.pct_near_gap) and \ dic_stock_price[high_gap]/price_pre['high'] < (1.0+self.pct_max): lst_code_picked.append(dic_stock_price['ts_code']) except: self.logger.info( 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') self.logger.info(dic_stock_price) return lst_code_picked
cnt_loss += 1 lst_sh.append(round(info_k_data['pct_chg'], 1)) lst_sh_date.append(info_k_data['trade_date']) info_k_data = ds_tushare.getStockPriceInfo('399001.SZ', day_next) lst_sz.append(round(info_k_data['pct_chg'], 1)) # info_k_data = ds_tushare.getStockPriceInfo('399005.SH', day_next) # lst_zxb.append(round(info_k_data['pct_chg'], 1)) # info_k_data = ds_tushare.getStockPriceInfo('399006.SH', day_next) # lst_cyb.append(round(info_k_data['pct_chg'], 1)) idx_pre = 0 idx_loop = 0 month_cur = 1 lst_sh_part = list() for item_hold_date in lst_hold_date: date = string_to_datetime(item_hold_date) if month_cur != date.month or idx_loop == len(lst_hold_date)-1: if idx_loop != len(lst_hold_date)-1: lst_sh_part = lst_sh[idx_pre:idx_loop] else: lst_sh_part = lst_sh[idx_pre:] lst_sh_part.sort() lst_pct_chg = list() cnt_pct_chg = list() for k, g in groupby(lst_sh_part, key=lambda x: int(round((x*10)/5, 0)) * 0.5): lst_pct_chg.append(k) cnt_pct_chg.append(len(list(g))) slices = cnt_pct_chg activities = lst_pct_chg # if month_cur in [1]: print(month_cur)
def pick_stock(self, date): self.set_date(date) ds_tushare = DataServiceTushare() lst_code_pool = list() lst_code_picked = list() for ts_code in ds_tushare.get_stock_list(): stock_basic = ds_tushare.get_stock_basic_info(ts_code) if stock_basic is None: self.logger.info('None stock basic info %s' % ts_code) continue dt_date = string_to_datetime(self.stock_picked_date) d = timedelta(days=-365 * self.n_years) if stock_basic['list_date'] > time_to_str( dt_date + d, '%Y%m%d') or 'ST' in stock_basic['name']: # 排除上市时间小于2年和st股票 continue dic_stock_price = ds_tushare.get_stock_price_info( ts_code, self.stock_picked_date) if dic_stock_price is None: # 排除选股日停牌的股票 continue try: if dic_stock_price['circ_mv'] > self.circ_mv_max or dic_stock_price['turnover_rate_f'] < self.turnover_rate_f_min \ or dic_stock_price['high_250'] / dic_stock_price['low_250'] > self.pct_chg_max_year \ or dic_stock_price['ma_120'] > dic_stock_price['ma_60'] or dic_stock_price['ma_60'] > dic_stock_price['ma_30'] \ or dic_stock_price['ma_30'] > dic_stock_price['ma_10'] or dic_stock_price['ma_10'] > dic_stock_price['ma_5'] \ or dic_stock_price['close'] > dic_stock_price['ma_5'] * (1 + self.pct_close_to_ma5) \ or dic_stock_price['close'] < dic_stock_price['ma_5'] * (1 - self.pct_close_to_ma5): continue except: self.logger.info( 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') self.logger.info(dic_stock_price) lst_code_pool.append(dic_stock_price['ts_code']) # self.logger.info(lst_code_pool) lst_n_days = ds_tushare.get_pre_n_trade_date(self.stock_picked_date, self.n_days) # 日期从大到小排列 arr_code = list() arr_a1 = list() # 最近一天的数据 arr_a2 = list() arr_a3 = list() arr_a4 = list() arr_a5 = list() arr_b1 = list() arr_b2 = list() arr_b3 = list() arr_b4 = list() arr_b5 = list() for item_code in lst_code_pool: lst_stock_price = ds_tushare.get_stock_price_lst( item_code, lst_n_days[-1], lst_n_days[0]) if len(lst_stock_price) < self.n_days: # 排除最近5个交易日有停牌情况的股票 continue arr_code.append(item_code) idx = 0 for item_price in lst_stock_price: idx += 1 if idx == 1: # 第一天数据 n-1 arr_a1.append(item_price['turnover_rate_f']) arr_b1.append(item_price['volume_ratio']) elif idx == 2: # 第二天数据 n-2 arr_a2.append(item_price['turnover_rate_f']) arr_b2.append(item_price['volume_ratio']) elif idx == 3: arr_a3.append(item_price['turnover_rate_f']) arr_b3.append(item_price['volume_ratio']) elif idx == 4: arr_a4.append(item_price['turnover_rate_f']) arr_b4.append(item_price['volume_ratio']) elif idx == 5: arr_a5.append(item_price['turnover_rate_f']) arr_b5.append(item_price['volume_ratio']) else: self.logger.info('lst_stock_price data error!!!') self.logger.info(lst_stock_price) break arr_a1_idx = np.array(arr_a1).argsort()[-(self.n_rank_turnover + 1):-1] arr_a2_idx = np.array(arr_a2).argsort()[-(self.n_rank_turnover + 1):-1] arr_a3_idx = np.array(arr_a3).argsort()[-(self.n_rank_turnover + 1):-1] arr_a4_idx = np.array(arr_a4).argsort()[-(self.n_rank_turnover + 1):-1] arr_a5_idx = np.array(arr_a5).argsort()[-(self.n_rank_turnover + 1):-1] arr_b1_idx = np.array(arr_b1).argsort()[-(self.n_rank_vol + 1):-1] arr_b2_idx = np.array(arr_b2).argsort()[-(self.n_rank_vol + 1):-1] arr_b3_idx = np.array(arr_b3).argsort()[-(self.n_rank_vol + 1):-1] arr_b4_idx = np.array(arr_b4).argsort()[-(self.n_rank_vol + 1):-1] arr_b5_idx = np.array(arr_b5).argsort()[-(self.n_rank_vol + 1):-1] arr_combine = np.hstack((arr_a1_idx, arr_a2_idx, arr_a3_idx, arr_a4_idx, arr_a5_idx, \ arr_b1_idx, arr_b2_idx, arr_b3_idx, arr_b4_idx ,arr_b5_idx)) res_count = Counter(arr_combine) res_stock_idx = res_count.most_common(self.n_rank_times) if len(res_stock_idx) != 0: for item in res_stock_idx: lst_code_picked.append(arr_code[item[0]]) return lst_code_picked