From 91cb3d103d6f2782e5baac8d54414c065076e384 Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Sat, 12 Jan 2019 09:38:22 +0800 Subject: [PATCH] [Add] ConnectDialog supports field value save and load --- vnpy/gateway/futu/futu_gateway.py | 22 +++++++------- vnpy/trader/ui/widget.py | 48 ++++++++++++++++++++++++------- vnpy/trader/utility.py | 23 +++++++++++++++ 3 files changed, 73 insertions(+), 20 deletions(-) diff --git a/vnpy/gateway/futu/futu_gateway.py b/vnpy/gateway/futu/futu_gateway.py index 6eec536d..29d7a887 100644 --- a/vnpy/gateway/futu/futu_gateway.py +++ b/vnpy/gateway/futu/futu_gateway.py @@ -92,11 +92,13 @@ class FutuGateway(BaseGateway): """""" default_setting = { - "password": "123123", - "market": "HK", - "env": "REAL", + "password": "", "host": "127.0.0.1", - "port": 11111 + "port": 11111, + "market": ["HK", + "US"], + "env": [TrdEnv.REAL, + TrdEnv.SIMULATE] } def __init__(self, event_engine): @@ -257,7 +259,7 @@ class FutuGateway(BaseGateway): futu_symbol = convert_symbol_vt2futu(req.symbol, req.exchange) code, data = self.trade_ctx.place_order( - eq.price, + req.price, req.volume, futu_symbol, side, @@ -395,7 +397,7 @@ class FutuGateway(BaseGateway): tick = TickData( symbol=symbol, exchange=exchange, - datatime=datetime.now(), + datetime=datetime.now(), gateway_name=self.gateway_name ) self.ticks[code] = tick @@ -439,10 +441,10 @@ class FutuGateway(BaseGateway): ask_data = data["Ask"][i] n = i + 1 - d["bid_price%s" % n] = bidData[0] - d["bid_volume%s" % n] = bidData[1] - d["ask_price%s" % n] = askData[0] - d["ask_volume%s" % n] = askData[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)) diff --git a/vnpy/trader/ui/widget.py b/vnpy/trader/ui/widget.py index cea5ad2b..6c32b24d 100644 --- a/vnpy/trader/ui/widget.py +++ b/vnpy/trader/ui/widget.py @@ -18,6 +18,7 @@ from ..engine import MainEngine from ..event import (EVENT_TICK, EVENT_ORDER, EVENT_TRADE, EVENT_ACCOUNT, EVENT_POSITION, EVENT_CONTRACT, EVENT_LOG) from ..object import SubscribeRequest, OrderRequest, CancelRequest +from ..utility import load_setting, save_setting COLOR_LONG = QtGui.QColor("red") COLOR_SHORT = QtGui.QColor("green") @@ -613,15 +614,16 @@ class ConnectDialog(QtWidgets.QDialog): """ Start connection of a certain gateway. """ - + def __init__(self, main_engine: MainEngine, gateway_name: str): """""" super(ConnectDialog, self).__init__() self.main_engine = main_engine self.gateway_name = gateway_name + self.file_name = f"Connect{gateway_name}.vt" - self.line_edits = {} + self.widgets = {} self.init_ui() @@ -629,16 +631,35 @@ class ConnectDialog(QtWidgets.QDialog): """""" self.setWindowTitle(f"连接{self.gateway_name}") - form = QtWidgets.QFormLayout() + # Default setting provides field name, field data type and field default value. default_setting = self.main_engine.get_default_setting(self.gateway_name) + + # Saved setting provides field data used last time. + loaded_setting = load_setting(self.file_name) - # Initialize line edits and form layout based on default setting. + # Initialize line edits and form layout based on setting. + form = QtWidgets.QFormLayout() + for field_name, field_value in default_setting.items(): field_type = type(field_value) - line_edit = QtWidgets.QLineEdit(str(field_value)) - form.addRow(f"{field_name} <{field_type.__name__}>", line_edit) - self.line_edits[field_name] = (line_edit, field_type) + if field_type == list: + widget = QtWidgets.QComboBox() + widget.addItems(field_value) + + if field_name in loaded_setting: + saved_value = loaded_setting[field_name] + ix = widget.findText(saved_value) + widget.setCurrentIndex(ix) + else: + widget = QtWidgets.QLineEdit(str(field_value)) + + if field_name in loaded_setting: + saved_value = loaded_setting[field_name] + widget.setText(str(saved_value)) + + form.addRow(f"{field_name} <{field_type.__name__}>", widget) + self.widgets[field_name] = (widget, field_type) button = QtWidgets.QPushButton(u"连接") button.clicked.connect(self.connect) @@ -651,13 +672,20 @@ class ConnectDialog(QtWidgets.QDialog): Get setting value from line edits and connect the gateway. """ setting = {} - for field_name, tp in self.line_edits.items(): - line_edit, field_type = tp - field_value = field_type(line_edit.text()) + for field_name, tp in self.widgets.items(): + widget, field_type = tp + if field_type == list: + field_value = str(widget.currentText()) + else: + field_value = field_type(widget.text()) setting[field_name] = field_value self.main_engine.connect(setting, self.gateway_name) + + save_setting(self.file_name, setting) + self.accept() + class TradingWidget(QtWidgets.QWidget): diff --git a/vnpy/trader/utility.py b/vnpy/trader/utility.py index 6a186561..8c6a7789 100644 --- a/vnpy/trader/utility.py +++ b/vnpy/trader/utility.py @@ -2,6 +2,7 @@ General utility functions. """ +import shelve from pathlib import Path from .constant import (STATUS_NOTTRADED, STATUS_PARTTRADED, STATUS_SUBMITTING) @@ -47,3 +48,25 @@ def get_icon_path(file_path: str, ico_name: str): ui_path = Path(file_path).parent icon_path = ui_path.joinpath("ico", ico_name) return str(icon_path) + + +def load_setting(file_name: str): + """ + Load setting from shelve file in temp path. + """ + file_path = get_temp_path(file_name) + f = shelve.open(str(file_path)) + setting = dict(f) + f.close() + return setting + + +def save_setting(file_name: str, setting: dict): + """ + Save setting into shelve file in temp path. + """ + file_path = get_temp_path(file_name) + f = shelve.open(str(file_path)) + for k, v in setting.items(): + f[k] = v + f.close()