From fde4c74ca5b36932c1765ec4090fb90ed2cbcb7d Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Thu, 7 Nov 2019 09:50:10 +0800 Subject: [PATCH] [Mod] add average price for BitMEX position data --- vnpy/gateway/bitmex/bitmex_gateway.py | 32 +++++++++++++++++---------- vnpy/trader/ui/widget.py | 2 +- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/vnpy/gateway/bitmex/bitmex_gateway.py b/vnpy/gateway/bitmex/bitmex_gateway.py index ac143694..d57b6ac6 100644 --- a/vnpy/gateway/bitmex/bitmex_gateway.py +++ b/vnpy/gateway/bitmex/bitmex_gateway.py @@ -533,6 +533,7 @@ class BitmexWebsocketApi(WebsocketClient): self.ticks = {} self.accounts = {} self.orders = {} + self.positions = {} self.trades = set() def connect( @@ -735,20 +736,27 @@ class BitmexWebsocketApi(WebsocketClient): def on_position(self, d): """""" - position = PositionData( - symbol=d["symbol"], - exchange=Exchange.BITMEX, - direction=Direction.NET, - volume=d.get("currentQty", 0), - price=d.get("avgEntryPrice", 0), - gateway_name=self.gateway_name, - ) + symbol = d["symbol"] - # avgEntryPrice may be None instead of 0 sometimes - if position.price is None: - position.price = 0 + position = self.positions.get(symbol, None) + if not position: + position = PositionData( + symbol=d["symbol"], + exchange=Exchange.BITMEX, + direction=Direction.NET, + gateway_name=self.gateway_name, + ) + self.positions[symbol] = position - self.gateway.on_position(position) + volume = d.get("currentQty", None) + if volume is not None: + position.volume = volume + + price = d.get("avgEntryPrice", None) + if price is not None: + position.price = price + + self.gateway.on_position(copy(position)) def on_account(self, d): """""" diff --git a/vnpy/trader/ui/widget.py b/vnpy/trader/ui/widget.py index 591b0b00..c5322c4e 100644 --- a/vnpy/trader/ui/widget.py +++ b/vnpy/trader/ui/widget.py @@ -463,7 +463,7 @@ class PositionMonitor(BaseMonitor): "volume": {"display": "数量", "cell": BaseCell, "update": True}, "yd_volume": {"display": "昨仓", "cell": BaseCell, "update": True}, "frozen": {"display": "冻结", "cell": BaseCell, "update": True}, - "price": {"display": "均价", "cell": BaseCell, "update": False}, + "price": {"display": "均价", "cell": BaseCell, "update": True}, "pnl": {"display": "盈亏", "cell": PnlCell, "update": True}, "gateway_name": {"display": "接口", "cell": BaseCell, "update": False}, }