Пример #1
0
    def product(self):
        """
        从数据库中取出数据
        """
        conn = MySql().get_connection()  # 得到连接
        # 构造查询条件
        query_params = " where ctime >= '%s'" % (self.__start_time,)
        query_params += " and ctime < '%s'" % (self.__end_time,)

        coll = "%s_%s" % (self.__symbol, self.__config.time_frame)

        cur = conn.cursor(pymysql.cursors.DictCursor)

        cur.execute("select * from %s " % coll + query_params + " limit %d " % self.__maxsize)
        # print(cur.fetchall())
        for row in cur.fetchall():
            bar = Bar(self.__symbol)
            bar.close = row["close"]
            bar.timestamp = int(row["ctime"])
            bar.high = row["high"]
            bar.low = row["low"]
            bar.open = row["open"]
            bar.volume = row["volume"]
            bar.time_frame = self.__config.time_frame
            self.__dq.put(bar)
            self.__start_time = bar.timestamp + tf2s(self.__config.time_frame)
        cur.close()
        # 如果开始时间距结束时间的距离不超过当前时间尺度,证明数据查询完成
        if (cur.rownumber == 0) or self.__end_time - self.__start_time <= tf2s(self.__config.time_frame):
            self._finished = True
Пример #2
0
    def product(self):
        """
        从数据库中取出数据
        """
        try:
            conn = MySql().get_connection()  # 得到连接
            # 构造查询条件
            query_params = " where ctime >= '%s'" % (self.__start_time, )
            query_params += " and ctime < '%s'" % (self.__end_time, )

            coll = "%s_%s" % (self.__symbol, self.config.time_frame)

            cur = conn.cursor(pymysql.cursors.DictCursor)

            cur.execute("select * from %s " % coll + query_params +
                        " limit %d " % self.__maxsize)
            # print(cur.fetchall())
            for row in cur.fetchall():
                bar = Bar(self.__symbol)
                bar.close = row["close"]
                bar.timestamp = int(row["ctime"])
                bar.high = row["high"]
                bar.low = row["low"]
                bar.open = row["open"]
                bar.volume = row["volume"]
                # volume maybe None in mysql
                if bar.volume is None:
                    bar.volume = 0
                bar.time_frame = self.config.time_frame
                self.__dq.put(bar)
                self.__start_time = bar.timestamp + tf2s(
                    self.config.time_frame)
            cur.close()
            # 如果开始时间距结束时间的距离不超过当前时间尺度,证明数据查询完成
            if (cur.rownumber
                    == 0) or self.__end_time - self.__start_time <= tf2s(
                        self.config.time_frame):
                self.stop()
        except:
            self.logger.error('\n' + traceback.format_exc())
            self.stop()