From 9de35bfd432ba56f6b473b705ec8042990b6aaa4 Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Wed, 17 Apr 2019 09:20:29 +0800 Subject: [PATCH] [Fix]use abs for trade volume --- tests/trader/run.py | 2 ++ vnpy/gateway/bitfinex/bitfinex_gateway.py | 25 +++++++++++++++-------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/tests/trader/run.py b/tests/trader/run.py index 2cda6ae2..e9d5d892 100644 --- a/tests/trader/run.py +++ b/tests/trader/run.py @@ -12,6 +12,7 @@ from vnpy.gateway.tiger import TigerGateway from vnpy.gateway.oes import OesGateway from vnpy.gateway.okex import OkexGateway from vnpy.gateway.huobi import HuobiGateway +from vnpy.gateway.bitfinex import BitfinexGateway from vnpy.app.cta_strategy import CtaStrategyApp from vnpy.app.csv_loader import CsvLoaderApp @@ -34,6 +35,7 @@ def main(): main_engine.add_gateway(OesGateway) main_engine.add_gateway(OkexGateway) main_engine.add_gateway(HuobiGateway) + main_engine.add_gateway(BitfinexGateway) main_engine.add_app(CtaStrategyApp) main_engine.add_app(CtaBacktesterApp) diff --git a/vnpy/gateway/bitfinex/bitfinex_gateway.py b/vnpy/gateway/bitfinex/bitfinex_gateway.py index 2b85988a..c418813b 100644 --- a/vnpy/gateway/bitfinex/bitfinex_gateway.py +++ b/vnpy/gateway/bitfinex/bitfinex_gateway.py @@ -50,11 +50,11 @@ ORDERTYPE_VT2BITFINEX = { OrderType.MARKET: "EXCHANGE MARKET", } DIRECTION_VT2BITFINEX = { - Direction.LONG: "Buy", + Direction.LONG: "Buy", Direction.SHORT: "Sell", } DIRECTION_BITFINEX2VT = { - "Buy": Direction.LONG, + "Buy": Direction.LONG, "Sell": Direction.SHORT, } @@ -204,7 +204,7 @@ class BitfinexRestApi(RestClient): def on_query_contract(self, data, request): """""" - for d in data: + for d in data: contract = ContractData( symbol=d["pair"].upper(), exchange=Exchange.BITFINEX, @@ -215,7 +215,7 @@ class BitfinexRestApi(RestClient): gateway_name=self.gateway_name, ) self.gateway.on_contract(contract) - + self.gateway.write_log("账户资金查询成功") def on_failed(self, status_code: int, request: Request): @@ -357,7 +357,7 @@ class BitfinexWebsocketApi(WebsocketClient): def on_packet(self, packet: dict): """""" - if isinstance(packet, dict): + if isinstance(packet, dict): self.on_response(packet) else: self.on_update(packet) @@ -412,7 +412,7 @@ class BitfinexWebsocketApi(WebsocketClient): tick.low_price = float(l_data1[-1]) tick.last_price = float(l_data1[-4]) tick.open_price = float(tick.last_price - l_data1[4]) - + # Update deep quote elif channel == "book": bid = self.bidDict.setdefault(symbol, {}) @@ -490,6 +490,7 @@ class BitfinexWebsocketApi(WebsocketClient): def on_wallet(self, data): """""" if str(data[0]) == "exchange": + print("wallet", data) accountid = str(data[1]) account = self.accounts.get(accountid, None) if not account: @@ -602,7 +603,8 @@ class BitfinexWebsocketApi(WebsocketClient): tick.__setattr__("ask_price_%s" % (n + 1), price) tick.__setattr__("ask_volume_%s" % (n + 1), volume) - tick.datetime = datetime.strptime(d["timestamp"], "%Y-%m-%dT%H:%M:%S.%fZ") + tick.datetime = datetime.strptime( + d["timestamp"], "%Y-%m-%dT%H:%M:%S.%fZ") self.gateway.on_tick(copy(tick)) def on_trade(self, data): @@ -619,7 +621,7 @@ class BitfinexWebsocketApi(WebsocketClient): exchange=Exchange.BITFINEX, orderid=data[-1], direction=direction, - volume=data[4], + volume=abs(data[4]), price=data[5], tradeid=str(self.trade_id), time=self.generate_date_time(data[2]), @@ -641,7 +643,12 @@ class BitfinexWebsocketApi(WebsocketClient): data = d[4] error_info = d[-1] + # Filter cancel of non-existing order orderid = str(data[2]) + if orderid == "None": + self.gateway.write_log("撤单失败,委托不存在") + return + if data[6] > 0: direction = Direction.LONG else: @@ -666,7 +673,7 @@ class BitfinexWebsocketApi(WebsocketClient): def on_order(self, data): """""" orderid = str(data[2]) - + if data[7] > 0: direction = Direction.LONG else: