Beispiel #1
0
    def get_price_between(self, ts_code_arr: list, now_date: str,
                          from_date: str, to_date: str, adj_type: str) -> list:
        """
        获取股票的每日价格信息
        :param ts_code_arr: 股票编码列表,为空时全取
        :param now_date: 查看数据的日期,会显示当时能看到的前复权价格,需为交易日,不能为空
        :param from_date: 获取价格的时间段 开始时间,不能为空 必须小于等于 now_date
        :param to_date: 获取价格的时间段 结束时间,不能为空 必须小于等于 now_date
        :param adj_type: qfq - 前复权, hfq - 后复权, 其他 - 不复权
        :return: list(MqDailyPrice)
        """
        if now_date is None or not date_utils.is_valid_dt(now_date):
            raise Exception('now_date is empty or invalid')
        if from_date is None or not date_utils.is_valid_dt(from_date):
            raise Exception('from_date is empty or invalid')
        if to_date is None or not date_utils.is_valid_dt(to_date):
            raise Exception('to_date is empty or invalid')
        if from_date > now_date or to_date > now_date:
            raise Exception('Cant get future price %s~%s in %s' %
                            (from_date, to_date, now_date))

        all_date_arr = [now_date]
        i = from_date
        while i < to_date:
            all_date_arr.append(i)
            i = date_utils.format_delta(i, 1)

        mq_list: list = self.get_normal_price_in(ts_code_arr, all_date_arr)
        return self.__convert_to_sim_price(mq_list, adj_type, now_date,
                                           now_date > to_date)
Beispiel #2
0
    def init_cache(self, ts_code_set: set, from_date: str, to_date: str):
        """
        预先缓存指定日期的数据
        :param ts_code_set: 编码列表
        :param from_date: 开始日期
        :param to_date: 结束日期
        :return:
        """
        if from_date is None or not date_utils.is_valid_dt(from_date):
            raise Exception('from_date is empty or invalid')
        if to_date is None or not date_utils.is_valid_dt(to_date):
            raise Exception('to_date is empty or invalid')
        if len(ts_code_set) == 0:
            log.warn('No ts code to cache, skip')
            return

        all_date_arr = []
        i = from_date
        while i < to_date:
            all_date_arr.append(i)
            i = date_utils.format_delta(i, 1)
        mq_list: list = self.get_normal_price_in(ts_code_set, all_date_arr)
        for p in mq_list:  # type: MqDailyPrice
            d = p.trade_date
            if d not in self.__cache:
                self.__cache[d] = []
            self.__cache[d].append(p)
        log.info("Cache init: mq_price")
Beispiel #3
0
    def get_daily_metrics_with_period(self,
                                      from_date: str,
                                      to_date: str,
                                      metrics: list,
                                      ts_codes: list = None):
        """
        获取日指标
        :param from_date: 开始日期
        :param to_date: 结束日期
        :param metrics: 指标列表 见 mq_daily_metric_enum
        :param ts_codes: 股票编码列表
        :return:
        """
        if not date_utils.is_valid_dt(from_date):
            raise Exception('Invalid date %s' % from_date)
        if not date_utils.is_valid_dt(to_date):
            raise Exception('Invalid date %s' % to_date)

        session: Session = db_client.get_session()
        ml: list = session.query(MqDailyMetric) \
            .filter(MqDailyMetric.ts_code.in_(ts_codes) if ts_codes is not None else 1 == 1,
                    MqDailyMetric.update_date >= from_date, MqDailyMetric.update_date <= to_date,
                    MqDailyMetric.name.in_(metrics)) \
            .all()
        session.close()
        return ml
Beispiel #4
0
    def get_price_in(self, ts_code_arr: list, now_date: str, date_arr: list,
                     adj_type: str):
        """
        获取股票的每日价格信息
        :param ts_code_arr: 股票编码列表,为空时全取
        :param now_date: 查看数据的日期,会显示当时能看到的前复权价格,需为交易日,不能为空
        :param date_arr: 获取价格的时间,不能为空,必须小于等于 now_date
        :param adj_type: qfq - 前复权, hfq - 后复权, 其他 - 不复权
        :return: list(MqDailyPrice)
        """
        if now_date is None or not date_utils.is_valid_dt(now_date):
            raise Exception('now_date is empty or invalid')
        if date_arr is None or len(date_arr) == 0:
            raise Exception('date_arr cant be empty')
        for d in date_arr:
            if d > now_date:
                raise Exception('Cant get future price %s in %s' %
                                (d, now_date))

        all_date_arr = [now_date]
        all_date_arr = all_date_arr.extend(date_arr)
        mq_list: list = self.get_normal_price_in(ts_code_arr, all_date_arr)

        return self.__convert_to_sim_price(mq_list, adj_type, now_date,
                                           now_date not in date_arr)
Beispiel #5
0
 def init_cache(self, ts_code_set: set, from_date: str, to_date: str):
     """
     预先缓存指定日期的数据
     :param ts_code_set: 编码列表
     :param from_date: 开始日期
     :param to_date: 结束日期
     :return:
     """
     if from_date is None or not date_utils.is_valid_dt(from_date):
         raise Exception('from_date is empty or invalid')
     if to_date is None or not date_utils.is_valid_dt(to_date):
         raise Exception('to_date is empty or invalid')
     session: Session = db_client.get_session()
     d_list: list = session.query(TsDividend)\
         .filter(TsDividend.record_date.between(from_date, to_date), TsDividend.div_proc == '实施').all()
     session.close()
     for d in d_list:  # type: TsDividend
         date = d.record_date
         if date not in self.__cache:
             self.__cache[date] = []
         self.__cache[date].append(d)
     log.info("Cache init: dividend")
Beispiel #6
0
 def get_daily_metrics(self,
                       dates: list,
                       metrics: list,
                       ts_codes: list = None):
     """
     获取日指标
     :param dates: 日期列表 必填
     :param metrics: 指标列表 见 mq_daily_metric_enum,必填
     :param ts_codes: 股票编码列表 不填时获取全部公司
     :return:
     """
     for d in dates:
         if not date_utils.is_valid_dt(d):
             raise Exception('Invalid date %s' % d)
     session: Session = db_client.get_session()
     ml: list = session.query(MqDailyMetric) \
         .filter(MqDailyMetric.ts_code.in_(ts_codes) if ts_codes is not None else 1 == 1,
                 MqDailyMetric.update_date.in_(dates), MqDailyMetric.name.in_(metrics)) \
         .all()
     session.close()
     return ml