Ejemplo n.º 1
0
 def get_k_data_in_range(self, start_date, end_date):
     ndays = delta_days(start_date, end_date)
     date_dmy_format = time.strftime("%m/%d/%Y",
                                     time.strptime(start_date, "%Y-%m-%d"))
     data_times = pd.date_range(date_dmy_format, periods=ndays, freq='D')
     date_only_array = np.vectorize(lambda s: s.strftime('%Y-%m-%d'))(
         data_times.to_pydatetime())
     data_dict = OrderedDict()
     for _date in date_only_array:
         if CCalendar.is_trading_day(_date, redis=self.redis):
             table_name = self.get_table_name(_date)
             if table_name not in data_dict: data_dict[table_name] = list()
             data_dict[table_name].append(str(_date))
     all_df = pd.DataFrame()
     for key in data_dict:
         table_list = sorted(data_dict[key], reverse=False)
         if len(table_list) == 1:
             df = self.get_data(table_list[0])
             if df is not None: all_df = all_df.append(df)
         else:
             start_date = table_list[0]
             end_date = table_list[len(table_list) - 1]
             df = self.get_data_between(start_date, end_date)
             if df is not None: all_df = all_df.append(df)
     return all_df
Ejemplo n.º 2
0
 def init_all_stock_tick(self):
     start_date = '2015-01-01'
     _today = datetime.now().strftime('%Y-%m-%d')
     num_days = delta_days(start_date, _today)
     start_date_dmy_format = time.strftime(
         "%m/%d/%Y", time.strptime(start_date, "%Y-%m-%d"))
     data_times = pd.date_range(start_date_dmy_format,
                                periods=num_days,
                                freq='D')
     date_only_array = np.vectorize(lambda s: s.strftime('%Y-%m-%d'))(
         data_times.to_pydatetime())
     date_only_array = date_only_array[::-1]
     obj_pool = Pool(4)
     df = self.stock_info_client.get()
     for _, code_id in df.code.iteritems():
         _obj = self.stock_objs[
             code_id] if code_id in self.stock_objs else CStock(
                 self.dbinfo, code_id)
         for _date in date_only_array:
             if self.cal_client.is_trading_day(_date):
                 try:
                     if obj_pool.full(): obj_pool.join()
                     obj_pool.spawn(_obj.set_ticket, _date)
                 except Exception as e:
                     logger.info(e)
     obj_pool.join()
     obj_pool.kill()
Ejemplo n.º 3
0
def get_stock_pool(start_date, end_date):
    '''
    更新股票池。该方法在收盘后调用。
    1. 全市场(不包含ST)股票作为基础股票池
    2. 剔除自制股票黑名单中的股票
    3. 剔除成交额小于1个亿的股票
    4. 剔除总市值中位数在100亿以下的股票
    5. 取25日跌幅前10%的股票作为最终的股票池
    '''
    #获取所有股票数据
    num = delta_days(start_date, end_date)
    all_df = get_stock_data(start_date, end_date, num)
    #黑名单股票
    black_list = get_blacklist()
    #所有单股票
    all_code_list = get_all_codelist()
    all_code_list = list(set(all_code_list).difference(set(black_list)))
    all_df = all_df.loc[all_df.code.isin(all_code_list)]
    #获取开盘天数大于30%的股票的数量
    codes = list()
    amounts = list()
    outstandings = list()
    pchanges = list()
    for code, df in all_df.groupby('code'):
        df = df.sort_values(by=['date'], ascending=True)
        df = df.reset_index(drop=True)
        if len(df) > int(num * 0.5):
            codes.append(code)
            amounts.append(np.median(df.amount))
            close = np.median(df.close)
            open_ = df['open'][0]
            close_ = df['close'][len(df) - 1]
            pchange = (close_ - open_) / open_
            pchanges.append(pchange)
            totals = np.median(df.totals)
            outstandings.append(close * totals)
    all_df = all_df.loc[all_df.code.isin(codes)]
    #info = {'code': codes, 'amount': amounts, 'outstanding': outstandings, 'pchange': pchanges}
    #stock_df = pd.DataFrame(info)
    #stock_df = stock_df.reset_index(drop = True)
    ##总市值大于100亿的股票的列表
    #stock_df = stock_df.sort_values(by=['outstanding'], ascending = False)
    #biglist = stock_df.loc[stock_df.outstanding > 1e10].code.tolist()
    #all_df = all_df.loc[all_df.code.isin(biglist)]
    ##获取成交额大于1个亿的股票
    #stock_df = stock_df.sort_values(by=['amount'], ascending = False)
    #code_list = stock_df.loc[stock_df.amount > 1e8].code.tolist()
    #all_df = all_df.loc[all_df.code.isin(code_list)]
    #取25日跌幅前10%的股票
    #stock_df = stock_df.sort_values(by=['pchange'], ascending = True)
    #code_list = stock_df.head(int(len(stock_df) * 0.5)).code.tolist()
    #all_df = all_df.loc[all_df.code.isin(code_list)]
    return all_df
Ejemplo n.º 4
0
 def test_delta_days(self):
     _from = "2019-06-07"
     _to = "2019-06-07"
     self.assertEqual(common.delta_days(_from, _to), 0)