def query_history(self, req: HistoryRequest):
        """"""
        # Convert symbol
        contract_type = symbol_type_map.get(req.symbol, "")
        buf = [i for i in req.symbol if not i.isdigit()]
        symbol = "".join(buf)

        ws_contract_type = CONTRACT_TYPE_MAP[contract_type]
        ws_symbol = f"{symbol}_{ws_contract_type}"

        # Create query params
        params = {
            "symbol": ws_symbol,
            "period": INTERVAL_VT2HBDM[req.interval],
            "size": 2000
        }

        # Get response from server
        resp = self.request("GET", "/market/history/kline", params=params)

        # Break if request failed with other status code
        history = []

        if resp.status_code // 100 != 2:
            msg = f"获取历史数据失败,状态码:{resp.status_code},信息:{resp.text}"
            self.gateway.write_log(msg)
        else:
            data = resp.json()
            if not data:
                msg = f"获取历史数据为空"
                self.gateway.write_log(msg)
            else:
                for d in data["data"]:
                    dt = datetime.fromtimestamp(d["id"])

                    bar = BarData(symbol=req.symbol,
                                  exchange=req.exchange,
                                  datetime=dt,
                                  interval=req.interval,
                                  volume=d["vol"],
                                  open_price=d["open"],
                                  high_price=d["high"],
                                  low_price=d["low"],
                                  close_price=d["close"],
                                  gateway_name=self.gateway_name)
                    history.append(bar)

                begin = history[0].datetime
                end = history[-1].datetime
                msg = f"获取历史数据成功,{req.symbol} - {req.interval.value},{begin} - {end}"
                self.gateway.write_log(msg)

        return history
Exemple #2
0
    def load_by_handle(
        self,
        f: TextIO,
        symbol: str,
        exchange: Exchange,
        interval: Interval,
        datetime_head: str,
        open_head: str,
        high_head: str,
        low_head: str,
        close_head: str,
        volume_head: str,
        datetime_format: str,
    ):
        """
        load by text mode file handle
        """
        reader = csv.DictReader(f)

        bars = []
        start = None
        count = 0
        for item in reader:
            if datetime_format:
                dt = datetime.strptime(item[datetime_head], datetime_format)
            else:
                dt = datetime.fromisoformat(item[datetime_head])

            bar = BarData(
                symbol=symbol,
                exchange=exchange,
                datetime=dt,
                interval=interval,
                volume=item[volume_head],
                open_price=item[open_head],
                high_price=item[high_head],
                low_price=item[low_head],
                close_price=item[close_head],
                gateway_name="DB",
            )

            bars.append(bar)

            # do some statistics
            count += 1
            if not start:
                start = bar.datetime
        end = bar.datetime

        # insert into database
        database_manager.save_bar_data(bars)
        return start, end, count
Exemple #3
0
def generate_bar_from_row(row, symbol, exchange):
    """"""
    bar = BarData(symbol=symbol,
                  exchange=Exchange(exchange),
                  interval=Interval.MINUTE,
                  open_price=row["open"],
                  high_price=row["high"],
                  low_price=row["low"],
                  close_price=row["close"],
                  volume=row["volume"],
                  datetime=row.name.to_pydatetime(),
                  gateway_name="DB")
    return bar
 def to_bar(self):
     """
     Generate BarData object from DbBarData.
     """
     bar = BarData(
         symbol=self.symbol,
         exchange=Exchange(self.exchange),
         datetime=self.datetime,
         interval=Interval(self.interval),
         volume=self.volume,
         open_price=self.open_price,
         high_price=self.high_price,
         low_price=self.low_price,
         close_price=self.close_price,
         gateway_name="DB",
     )
     return bar
            "port": int(os.environ["fcs_trade_TEST_MONGODB_PORT"]),
            "user": "",
            "password": "",
            "authentication_source": "",
        },
    })


def now():
    return datetime.utcnow()


bar = BarData(
    gateway_name="DB",
    symbol="test_symbol",
    exchange=Exchange.BITMEX,
    datetime=now(),
    interval=Interval.MINUTE,
)

tick = TickData(
    gateway_name="DB",
    symbol="test_symbol",
    exchange=Exchange.BITMEX,
    datetime=now(),
    name="DB_test_symbol",
)


class TestDatabase(unittest.TestCase):
    def connect(self, settings: dict):
    def query_history(self, req: HistoryRequest):
        """"""
        history = []
        count = 750
        start_time = req.start.isoformat()

        while True:
            # Create query params
            params = {
                "binSize": INTERVAL_VT2BITMEX[req.interval],
                "symbol": req.symbol,
                "count": count,
                "startTime": start_time
            }

            # Add end time if specified
            if req.end:
                params["endTime"] = req.end.isoformat()

            # Get response from server
            resp = self.request("GET", "/trade/bucketed", params=params)

            # Break if request failed with other status code
            if resp.status_code // 100 != 2:
                msg = f"获取历史数据失败,状态码:{resp.status_code},信息:{resp.text}"
                self.gateway.write_log(msg)
                break
            else:
                data = resp.json()
                if not data:
                    msg = f"获取历史数据为空,开始时间:{start_time},数量:{count}"
                    break

                for d in data:
                    dt = datetime.strptime(d["timestamp"],
                                           "%Y-%m-%dT%H:%M:%S.%fZ")
                    bar = BarData(symbol=req.symbol,
                                  exchange=req.exchange,
                                  datetime=dt,
                                  interval=req.interval,
                                  volume=d["volume"],
                                  open_price=d["open"],
                                  high_price=d["high"],
                                  low_price=d["low"],
                                  close_price=d["close"],
                                  gateway_name=self.gateway_name)
                    history.append(bar)

                begin = data[0]["timestamp"]
                end = data[-1]["timestamp"]
                msg = f"获取历史数据成功,{req.symbol} - {req.interval.value},{begin} - {end}"
                self.gateway.write_log(msg)

                # Break if total data count less than 750 (latest date collected)
                if len(data) < 750:
                    break

                # Update start time
                start_time = bar.datetime + TIMEDELTA_MAP[req.interval]

        return history