From 88f661615c398f56f1c6e4f3b9a07a224e297745 Mon Sep 17 00:00:00 2001 From: nanoric Date: Fri, 15 Mar 2019 02:43:05 -0400 Subject: [PATCH] [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,