예제 #1
0
파일: backtest.py 프로젝트: ghotiv/Bigfish
def _get_bar_from_dataframe(symbol, time_frame, data):
    bar = Bar(symbol)
    bar.time_frame = time_frame
    for field in ['open', 'high', 'low', 'close', 'volume']:
        setattr(bar, field, data[field])
    bar.timestamp = get_datetime(data.name).timestamp()
    return bar
예제 #2
0
파일: backtest.py 프로젝트: ghotiv/Bigfish
def _get_bar_from_dict(symbol, time_frame, data):
    bar = Bar(symbol)
    bar.time_frame = time_frame
    for field in ['open', 'high', 'low', 'close', 'volume']:
        setattr(bar, field, data[field])
    bar.timestamp = data['ctime']
    return bar
예제 #3
0
def _get_bar_from_dict(symbol, time_frame, data):
    bar = Bar(symbol)
    bar.time_frame = time_frame
    for field in ['open', 'high', 'low', 'close', 'volume']:
        setattr(bar, field, data[field])
    bar.timestamp = data['ctime']
    return bar
예제 #4
0
def _get_bar_from_dataframe(symbol, time_frame, data):
    bar = Bar(symbol)
    bar.time_frame = time_frame
    for field in ['open', 'high', 'low', 'close', 'volume']:
        setattr(bar, field, data[field])
    bar.timestamp = get_datetime(data.name).timestamp()
    return bar
예제 #5
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
예제 #6
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()
예제 #7
0
 def on_tick(self, event: Event):
     if self._running:
         tick = event.content['data']
         symbol = tick.symbol
         for time_frame in {item[1] for item in self._data_view.get_keys() if item[0] == symbol}:
             bar_interval = tf2s(time_frame)
             if symbol not in self._tick_cache:
                 self._tick_cache[symbol] = {}
             if time_frame not in self._tick_cache[symbol]:
                 self._tick_cache[symbol][time_frame] = {
                     'open': tick.openPrice, 'high': tick.highPrice,
                     'low': tick.lastPrice, 'close': tick.lastPrice,
                     'volume': tick.volume, 'timestamp': tick.time // self.TICK_INTERVAL * self.TICK_INTERVAL,
                 }
             else:
                 dict_ = self._tick_cache[symbol][time_frame]
                 if tick.time - dict_['timestamp'] >= self.TICK_INTERVAL:  # bar_interval 能被TICK_INTERVAL整除
                     bar = Bar(symbol)
                     bar.time_frame = time_frame
                     bar.timestamp = dict_['timestamp'] // bar_interval * bar_interval
                     bar.open = dict_['open']
                     bar.high = dict_['high']
                     bar.low = dict_['low']
                     bar.close = dict_['close']
                     self.update_bar(bar)
                     dict_["open"] = tick.openPrice
                     dict_["high"] = tick.highPrice
                     dict_["low"] = tick.lowPrice
                     dict_["close"] = tick.lastPrice
                     dict_["volume"] = tick.volume
                     dict_["timestamp"] = tick.time // self.TICK_INTERVAL * self.TICK_INTERVAL
                 else:
                     dict_['low'] = min(dict_['low'], tick.lowPrice)
                     dict_['high'] = max(dict_['high'], tick.highPrice)
                     dict_['close'] = tick.lastPrice
                     dict_["volume"] += tick.volume
                     dict_['timestamp'] = tick.time // self.TICK_INTERVAL * self.TICK_INTERVAL