From 39effc29b4407f000d68d07c6b745b423c7a9208 Mon Sep 17 00:00:00 2001 From: nanoric Date: Fri, 15 Mar 2019 02:28:54 -0400 Subject: [PATCH 1/3] [Mod] BtimexGateway: renaeme setting.session into session_number --- vnpy/gateway/bitmex/bitmex_gateway.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/vnpy/gateway/bitmex/bitmex_gateway.py b/vnpy/gateway/bitmex/bitmex_gateway.py index 638ee2a6..437564fe 100644 --- a/vnpy/gateway/bitmex/bitmex_gateway.py +++ b/vnpy/gateway/bitmex/bitmex_gateway.py @@ -62,7 +62,7 @@ class BitmexGateway(BaseGateway): default_setting = { "key": "", "secret": "", - "session": 3, + "session_number": 3, "server": ["REAL", "TESTNET"], "proxy_host": "127.0.0.1", "proxy_port": 1080, @@ -79,12 +79,12 @@ class BitmexGateway(BaseGateway): """""" key = setting["key"] secret = setting["secret"] - session = setting["session"] + session_number = setting["session_number"] server = setting["server"] proxy_host = setting["proxy_host"] proxy_port = setting["proxy_port"] - self.rest_api.connect(key, secret, session, + self.rest_api.connect(key, secret, session_number, server, proxy_host, proxy_port) self.ws_api.connect(key, secret, server, proxy_host, proxy_port) @@ -172,7 +172,7 @@ class BitmexRestApi(RestClient): self, key: str, secret: str, - session: int, + session_number: int, server: str, proxy_host: str, proxy_port: int, @@ -192,7 +192,7 @@ class BitmexRestApi(RestClient): else: self.init(TESTNET_REST_HOST, proxy_host, proxy_port) - self.start(session) + self.start(session_number) self.gateway.write_log("REST API启动成功") From 88f661615c398f56f1c6e4f3b9a07a224e297745 Mon Sep 17 00:00:00 2001 From: nanoric Date: Fri, 15 Mar 2019 02:43:05 -0400 Subject: [PATCH 2/3] [Add] BitmexGateway: make send_order thread-safe --- vnpy/gateway/bitmex/bitmex_gateway.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/vnpy/gateway/bitmex/bitmex_gateway.py b/vnpy/gateway/bitmex/bitmex_gateway.py index 437564fe..e7fe1fc8 100644 --- a/vnpy/gateway/bitmex/bitmex_gateway.py +++ b/vnpy/gateway/bitmex/bitmex_gateway.py @@ -8,6 +8,7 @@ import sys import time from copy import copy from datetime import datetime +from threading import Lock from urllib.parse import urlencode from requests import ConnectionError @@ -131,6 +132,8 @@ class BitmexRestApi(RestClient): self.secret = "" self.order_count = 1_000_000 + self.order_count_lock = Lock() + self.connect_time = 0 def sign(self, request): @@ -196,10 +199,14 @@ class BitmexRestApi(RestClient): self.gateway.write_log("REST API启动成功") + def _new_order_id(self): + with self.order_count_lock: + self.order_count += 1 + return self.order_count + def send_order(self, req: OrderRequest): """""" - self.order_count += 1 - orderid = str(self.connect_time + self.order_count) + orderid = str(self.connect_time + self._new_order_id()) data = { "symbol": req.symbol, From fed48e1a061e8df3023d083ffc26df46d78ddb75 Mon Sep 17 00:00:00 2001 From: nanoric Date: Fri, 15 Mar 2019 02:43:21 -0400 Subject: [PATCH 3/3] [Add] BitmexGateway: Added some comment to make logic more clear. --- vnpy/gateway/bitmex/bitmex_gateway.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/vnpy/gateway/bitmex/bitmex_gateway.py b/vnpy/gateway/bitmex/bitmex_gateway.py index e7fe1fc8..e610e32e 100644 --- a/vnpy/gateway/bitmex/bitmex_gateway.py +++ b/vnpy/gateway/bitmex/bitmex_gateway.py @@ -89,6 +89,7 @@ class BitmexGateway(BaseGateway): server, proxy_host, proxy_port) self.ws_api.connect(key, secret, server, proxy_host, proxy_port) + # websocket will push all account status on connected, including asset, position and orders. def subscribe(self, req: SubscribeRequest): """""" @@ -279,7 +280,7 @@ class BitmexRestApi(RestClient): self.on_error(exception_type, exception_value, tb, request) def on_send_order(self, data, request): - """""" + """Websocket will push a new order status""" pass def on_cancel_order_error( @@ -293,7 +294,7 @@ class BitmexRestApi(RestClient): self.on_error(exception_type, exception_value, tb, request) def on_cancel_order(self, data, request): - """""" + """Websocket will push a new order status""" pass def on_failed(self, status_code: int, request: Request):