From 1a79ba37db118b570c94960dce239c19267239fb Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Tue, 9 Apr 2019 18:21:28 +0800 Subject: [PATCH] [Mod]add on_cancel_order_failed callback for OkexGateway --- vnpy/gateway/okex/okex_gateway.py | 21 +++++++++++++++++++++ vnpy/trader/utility.py | 2 -- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/vnpy/gateway/okex/okex_gateway.py b/vnpy/gateway/okex/okex_gateway.py index ded30c13..9ca18217 100644 --- a/vnpy/gateway/okex/okex_gateway.py +++ b/vnpy/gateway/okex/okex_gateway.py @@ -85,6 +85,8 @@ class OkexGateway(BaseGateway): self.rest_api = OkexRestApi(self) self.ws_api = OkexWebsocketApi(self) + self.orders = {} + def connect(self, setting: dict): """""" key = setting["API Key"] @@ -128,6 +130,15 @@ class OkexGateway(BaseGateway): self.rest_api.stop() self.ws_api.stop() + def on_order(self, order: OrderData): + """""" + self.orders[order.vt_orderid] = order + super().on_order(order) + + def get_order(self, vt_orderid: str): + """""" + return self.orders.get(vt_orderid, None) + class OkexRestApi(RestClient): """ @@ -258,6 +269,8 @@ class OkexRestApi(RestClient): callback=self.on_cancel_order, data=data, on_error=self.on_cancel_order_error, + on_failed=self.on_cancel_order_failed, + extra=req ) def query_contract(self): @@ -406,6 +419,14 @@ class OkexRestApi(RestClient): """Websocket will push a new order status""" pass + def on_cancel_order_failed(self, status_code: int, request: Request): + """If cancel failed, mark order status to be rejected.""" + req = request.extra + order = self.gateway.get_order(req.vt_orderid) + if order: + order.status = Status.REJECTED + self.gateway.on_order(order) + def on_failed(self, status_code: int, request: Request): """ Callback to handle request failed. diff --git a/vnpy/trader/utility.py b/vnpy/trader/utility.py index 9951e1f1..89c412a1 100644 --- a/vnpy/trader/utility.py +++ b/vnpy/trader/utility.py @@ -394,5 +394,3 @@ def virtual(func: "callable"): that can be (re)implemented by subclasses. """ return func - -