Esempio n. 1
0
def short_line_ma(days=5):
    # 连续n天股价 ma5*1.08>收盘价>=ma5, 最低价>=ma10,ma10*1.05>ma5
    # ma5>ma10>ma20>ma60>ma120
    ds_tushare = DataServiceTushare()
    lst_code_picked = list()
    code_lst = ds_tushare.get_stock_list()
    for item_code in code_lst:
        date_begin = ds_tushare.get_pre_trade_date('202200804', days)
        k_data_lst = ds_tushare.get_stock_price_lst(item_code, date_begin,
                                                    '202200804')
        if len(k_data_lst) == 0:
            continue
        cnt = 0
        for item_price in k_data_lst:
            if item_price['close'] < item_price['ma_5'] or item_price[
                    'close'] > item_price['ma_5'] * 1.08 or item_price[
                        'low'] < item_price['ma_10']:
                break
            if item_price['ma_5'] > item_price['ma_10'] * 1.05:
                break
            if item_price['ma_120'] > item_price['ma_60'] or item_price['ma_60'] > item_price['ma_30'] or item_price['ma_30'] > item_price['ma_20'] \
                or item_price['ma_20'] > item_price['ma_10'] or item_price['ma_10'] > item_price['ma_5']:
                break
            cnt += 1
            if cnt == days:
                print(item_price['ts_code'])
                lst_code_picked.append(item_price['ts_code'])
    print(lst_code_picked)
Esempio n. 2
0
 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
Esempio n. 3
0
def rise_ma(percent=8):
    close_cur = np.array(list())
    percent_cur = np.array(list())
    close_pre = np.array(list())
    ma5_pre = np.array(list())
    ma10_pre = np.array(list())
    ma20_pre = np.array(list())
    ma30_pre = np.array(list())
    ma60_pre = np.array(list())
    ma120_pre = np.array(list())
    ma250_pre = np.array(list())
    ma500_pre = np.array(list())
    ds_tushare = DataServiceTushare()
    code_lst = ds_tushare.get_stock_list()
    for item_code in code_lst:
        k_data_lst = ds_tushare.get_stock_price_lst(item_code, '20100101',
                                                    '20180101')
        if len(k_data_lst) == 0:
            continue
        k_data_pre = k_data_lst.pop(0)
        for item_price in k_data_lst:
            if item_price['pct_chg'] > percent:
                close_cur = np.append(close_cur, item_price['close'])
                percent_cur = np.append(percent_cur, item_price['pct_chg'])
                close_pre = np.append(close_pre, k_data_pre['close'])
                ma5_pre = np.append(ma5_pre, k_data_pre['ma_5'])
                ma10_pre = np.append(ma10_pre, k_data_pre['ma_10'])
                ma20_pre = np.append(ma20_pre, k_data_pre['ma_20'])
                ma30_pre = np.append(ma30_pre, k_data_pre['ma_30'])
                ma60_pre = np.append(ma60_pre, k_data_pre['ma_60'])
                ma120_pre = np.append(ma120_pre, k_data_pre['ma_120'])
                ma250_pre = np.append(ma250_pre, k_data_pre['ma_250'])
                ma500_pre = np.append(ma500_pre, k_data_pre['ma_500'])
            k_data_pre = item_price
    index = [0, 1, 2, 3, 4, 5, 6, 7]
    cnt_rise = float(len(percent_cur))
    values = [np.sum(close_pre > ma5_pre)/cnt_rise, np.sum(close_pre > ma10_pre)/cnt_rise, np.sum(close_pre > ma20_pre)/cnt_rise, np.sum(close_pre > ma30_pre)/cnt_rise, \
            np.sum(close_pre > ma60_pre)/cnt_rise, np.sum(close_pre > ma120_pre)/cnt_rise, np.sum(close_pre > ma250_pre)/cnt_rise, np.sum(close_pre > ma500_pre)/cnt_rise]
    print(values)
    plt.bar(index, values)
    plt.xticks(index, ['5', '10', '20', '30', '60', '120', '250', '500'])
    plt.show()
Esempio n. 4
0
 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
Esempio n. 5
0
 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