From 3b279c68eb0b061e1e27d19ad0408fa212d3fefb Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Fri, 10 May 2019 14:06:27 +0800 Subject: [PATCH] [Add] query history order and trade data --- vnpy/gateway/hbdm/hbdm_gateway.py | 115 +++++++++++++++++++++++++++--- 1 file changed, 104 insertions(+), 11 deletions(-) diff --git a/vnpy/gateway/hbdm/hbdm_gateway.py b/vnpy/gateway/hbdm/hbdm_gateway.py index 022f6842..e6df8b20 100644 --- a/vnpy/gateway/hbdm/hbdm_gateway.py +++ b/vnpy/gateway/hbdm/hbdm_gateway.py @@ -57,7 +57,8 @@ ORDERTYPE_VT2HBDM = { OrderType.LIMIT: "limit", } ORDERTYPE_HBDM2VT = {v: k for k, v in ORDERTYPE_VT2HBDM.items()} - +ORDERTYPE_HBDM2VT[1] = OrderType.LIMIT +ORDERTYPE_HBDM2VT[3] = OrderType.MARKET DIRECTION_VT2HBDM = { Direction.LONG: "buy", @@ -174,7 +175,7 @@ class HbdmRestApi(RestClient): self.gateway = gateway self.gateway_name = gateway.gateway_name - + self.host = "" self.key = "" self.secret = "" @@ -254,12 +255,47 @@ class HbdmRestApi(RestClient): def query_order(self): """""" for currency in self.currencies: + # Open Orders data = {"symbol": currency} self.add_request( method="POST", path="/api/v1/contract_openorders", - callback=self.on_query_order, + callback=self.on_query_active_order, + data=data, + extra=currency + ) + + # History Orders + data = { + "symbol": currency, + "trade_type": 0, + "type": 2, + "status": 0, + "create_date": 7 + } + + self.add_request( + method="POST", + path="/api/v1/contract_hisorders", + callback=self.on_query_history_order, + data=data, + extra=currency + ) + + def query_trade(self): + """""" + for currency in self.currencies: + data = { + "symbol": currency, + "trade_type": 0, + "create_date": 7 + } + + self.add_request( + method="POST", + path="/api/v1/contract_matchresults", + callback=self.on_query_trade, data=data, extra=currency ) @@ -271,7 +307,7 @@ class HbdmRestApi(RestClient): path="/api/v1/contract_contract_info", callback=self.on_query_contract ) - + def new_local_orderid(self): """""" with self.order_count_lock: @@ -320,7 +356,7 @@ class HbdmRestApi(RestClient): for req in reqs: local_orderid = self.new_local_orderid() - + order = req.create_order_data( local_orderid, self.gateway_name @@ -412,7 +448,7 @@ class HbdmRestApi(RestClient): for d in data["data"]: key = f"{d['contract_code']}_{d['direction']}" position = self.positions.get(key, None) - + if not position: position = PositionData( symbol=d["contract_code"], @@ -426,17 +462,18 @@ class HbdmRestApi(RestClient): position.frozen = d["frozen"] position.price = d["cost_hold"] position.pnl = d["profit"] - + for position in self.positions.values(): self.gateway.on_position(position) - def on_query_order(self, data, request): + def on_query_active_order(self, data, request): """""" - if self.check_error(data, "查询委托"): + if self.check_error(data, "查询活动委托"): return for d in data["data"]["orders"]: - dt = datetime.fromtimestamp(d["created_at"] / 1000) + timestamp = d["created_at"] + dt = datetime.fromtimestamp(timestamp / 1000) time = dt.strftime("%H:%M:%S") if d["client_order_id"]: @@ -460,7 +497,62 @@ class HbdmRestApi(RestClient): ) self.gateway.on_order(order) - self.gateway.write_log(f"{request.extra}委托信息查询成功") + self.gateway.write_log(f"{request.extra}活动委托信息查询成功") + + def on_query_history_order(self, data, request): + """""" + if self.check_error(data, "查询历史委托"): + return + + for d in data["data"]["orders"]: + timestamp = d["create_date"] + dt = datetime.fromtimestamp(timestamp / 1000) + time = dt.strftime("%H:%M:%S") + + orderid = d["order_id"] + + order = OrderData( + orderid=orderid, + symbol=d["contract_code"], + exchange=Exchange.HUOBI, + price=d["price"], + volume=d["volume"], + type=ORDERTYPE_HBDM2VT[d["order_price_type"]], + direction=DIRECTION_HBDM2VT[d["direction"]], + offset=OFFSET_HBDM2VT[d["offset"]], + traded=d["trade_volume"], + status=STATUS_HBDM2VT[d["status"]], + time=time, + gateway_name=self.gateway_name, + ) + self.gateway.on_order(order) + + self.gateway.write_log(f"{request.extra}历史委托信息查询成功") + + def on_query_trade(self, data, request): + """""" + if self.check_error(data, "查询成交"): + return + + for d in data["data"]["trades"]: + dt = datetime.fromtimestamp(d["create_date"] / 1000) + time = dt.strftime("%H:%M:%S") + + trade = TradeData( + tradeid=d["match_id"], + orderid=d["order_id"], + symbol=d["contract_code"], + exchange=Exchange.HUOBI, + price=d["trade_price"], + volume=d["trade_volume"], + direction=DIRECTION_HBDM2VT[d["direction"]], + offset=OFFSET_HBDM2VT[d["offset"]], + time=time, + gateway_name=self.gateway_name, + ) + self.gateway.on_trade(trade) + + self.gateway.write_log(f"{request.extra}成交信息查询成功") def on_query_contract(self, data, request): # type: (dict, Request)->None """""" @@ -487,6 +579,7 @@ class HbdmRestApi(RestClient): self.gateway.write_log("合约信息查询成功") self.query_order() + self.query_trade() def on_send_order(self, data, request): """"""