From 33aff65e60047465c1bd9d11296d19ca0a286e04 Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Mon, 14 Jan 2019 22:52:03 +0800 Subject: [PATCH] [Add] Complete futu gateway test --- vnpy/gateway/futu/futu_gateway.py | 11 ++++++----- vnpy/trader/engine.py | 5 +++-- vnpy/trader/ui/widget.py | 22 +++++++++++++++++----- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/vnpy/gateway/futu/futu_gateway.py b/vnpy/gateway/futu/futu_gateway.py index 458a96f2..a6170802 100644 --- a/vnpy/gateway/futu/futu_gateway.py +++ b/vnpy/gateway/futu/futu_gateway.py @@ -77,6 +77,7 @@ DIRECTION_FUTU2VT = {v: k for k, v in DIRECTION_VT2FUTU.items()} STATUS_FUTU2VT = { OrderStatus.NONE: STATUS_SUBMITTING, + OrderStatus.SUBMITTING: STATUS_SUBMITTING, OrderStatus.SUBMITTED: STATUS_NOTTRADED, OrderStatus.FILLED_PART: STATUS_PARTTRADED, OrderStatus.FILLED_ALL: STATUS_ALLTRADED, @@ -449,10 +450,10 @@ class FutuGateway(BaseGateway): ask_data = data["Ask"][i] n = i + 1 - d["bid_price%s" % n] = bid_data[0] - d["bid_volume%s" % n] = bid_data[1] - d["ask_price%s" % n] = ask_data[0] - d["ask_volume%s" % n] = ask_data[1] + d["bid_price_%s" % n] = bid_data[0] + d["bid_volume_%s" % n] = bid_data[1] + d["ask_price_%s" % n] = ask_data[0] + d["ask_volume_%s" % n] = ask_data[1] if tick.datetime: self.on_tick(copy(tick)) @@ -490,7 +491,7 @@ class FutuGateway(BaseGateway): tradeid = str(row["deal_id"]) if tradeid in self.trades: continue - self.trades.add(tradeID) + self.trades.add(tradeid) symbol, exchange = convert_symbol_futu2vt(row["code"]) trade = TradeData( diff --git a/vnpy/trader/engine.py b/vnpy/trader/engine.py index 82b75484..2eff1943 100644 --- a/vnpy/trader/engine.py +++ b/vnpy/trader/engine.py @@ -140,14 +140,15 @@ class MainEngine: Make sure every gateway and app is closed properly before programme exit. """ + # Stop event engine first to prevent new timer event. + self.event_engine.stop() + for engine in self.engines.values(): engine.close() for gateway in self.gateways.values(): gateway.close() - self.event_engine.stop() - class BaseEngine(ABC): """ diff --git a/vnpy/trader/ui/widget.py b/vnpy/trader/ui/widget.py index 2fb3a94a..396853c1 100644 --- a/vnpy/trader/ui/widget.py +++ b/vnpy/trader/ui/widget.py @@ -281,8 +281,9 @@ class BaseMonitor(QtWidgets.QTableWidget): if not path: return - with open(path, "wb") as f: - writer = csv.writer(f) + with open(path, "w") as f: + writer = csv.writer(f, lineterminator='\n') + writer.writerow(self.headers.keys()) for row in range(self.rowCount()): @@ -290,10 +291,16 @@ class BaseMonitor(QtWidgets.QTableWidget): for column in range(self.columnCount()): item = self.item(row, column) if item: - row_data.append(text_type(item.text())) + row_data.append(str(item.text())) else: row_data.append("") writer.writerow(row_data) + + def contextMenuEvent(self, event): + """ + Show menu with right click. + """ + self.menu.popup(QtGui.QCursor.pos()) class TickMonitor(BaseMonitor): @@ -936,8 +943,14 @@ class TradingWidget(QtWidgets.QWidget): contract = self.main_engine.get_contract(vt_symbol) if not contract: self.name_line.setText("") + gateway_name = (self.gateway_combo.currentText()) else: self.name_line.setText(contract.name) + gateway_name = contract.gateway_name + + # Update gateway combo box. + ix = self.gateway_combo.findText(gateway_name) + self.gateway_combo.setCurrentIndex(ix) self.clear_label_text() @@ -946,8 +959,7 @@ class TradingWidget(QtWidgets.QWidget): symbol=symbol, exchange=exchange ) - gateway_name = (self.gateway_combo.currentText()) - + self.main_engine.subscribe(req, gateway_name) def clear_label_text(self):