From c5f71910d546b659ec8e9fbf8b62f4d881cb488e Mon Sep 17 00:00:00 2001 From: msincenselee Date: Thu, 20 Feb 2020 11:40:56 +0800 Subject: [PATCH] =?UTF-8?q?[=E5=A2=9E=E5=BC=BA]=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E7=95=8C=E9=9D=A2=E8=81=94=E5=8A=A8=20=E8=A1=8C=E6=83=85->?= =?UTF-8?q?=E4=BA=A4=E6=98=93=E7=95=8C=E9=9D=A2=E6=9B=B4=E6=96=B0=20?= =?UTF-8?q?=E6=8C=81=E4=BB=93->=E4=BA=A4=E6=98=93=E7=95=8C=E9=9D=A2?= =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnpy/trader/ui/mainwindow.py | 14 ++++++-- vnpy/trader/ui/widget.py | 65 +++++++++++++++++++++++++++++++++++- 2 files changed, 76 insertions(+), 3 deletions(-) diff --git a/vnpy/trader/ui/mainwindow.py b/vnpy/trader/ui/mainwindow.py index 92c311e0..5c55b26e 100644 --- a/vnpy/trader/ui/mainwindow.py +++ b/vnpy/trader/ui/mainwindow.py @@ -45,6 +45,10 @@ class MainWindow(QtWidgets.QMainWindow): self.connect_dialogs = {} self.widgets = {} + self.trading_widget = None + self.position_widget = None + self.tick_widget = None + self.init_ui() def init_ui(self): @@ -55,12 +59,18 @@ class MainWindow(QtWidgets.QMainWindow): self.init_menu() self.load_window_setting("custom") + # 绑定行情双击 ~ 交易界面自动填写 + if self.tick_widget and self.trading_widget: + self.tick_widget.itemDoubleClicked.connect(self.trading_widget.auto_fill_symbol) + if self.position_widget and self.trading_widget: + self.position_widget.itemDoubleClicked.connect(self.trading_widget.close_position) + def init_dock(self): """""" self.trading_widget, trading_dock = self.create_dock( TradingWidget, "交易", QtCore.Qt.LeftDockWidgetArea ) - tick_widget, tick_dock = self.create_dock( + self.tick_widget, tick_dock = self.create_dock( TickMonitor, "行情", QtCore.Qt.RightDockWidgetArea ) order_widget, order_dock = self.create_dock( @@ -78,7 +88,7 @@ class MainWindow(QtWidgets.QMainWindow): account_widget, account_dock = self.create_dock( AccountMonitor, "资金", QtCore.Qt.BottomDockWidgetArea ) - position_widget, position_dock = self.create_dock( + self.position_widget, position_dock = self.create_dock( PositionMonitor, "持仓", QtCore.Qt.BottomDockWidgetArea ) diff --git a/vnpy/trader/ui/widget.py b/vnpy/trader/ui/widget.py index 5a7a7a15..deae03bb 100644 --- a/vnpy/trader/ui/widget.py +++ b/vnpy/trader/ui/widget.py @@ -631,6 +631,8 @@ class TradingWidget(QtWidgets.QWidget): cancel_button = QtWidgets.QPushButton("全撤") cancel_button.clicked.connect(self.cancel_all) + self.checkFixed = QtWidgets.QCheckBox("价格") # 价格固定选择框 + form1 = QtWidgets.QFormLayout() form1.addRow("交易所", self.exchange_combo) form1.addRow("代码", self.symbol_line) @@ -638,7 +640,7 @@ class TradingWidget(QtWidgets.QWidget): form1.addRow("方向", self.direction_combo) form1.addRow("开平", self.offset_combo) form1.addRow("类型", self.order_type_combo) - form1.addRow("价格", self.price_line) + form1.addRow( self.checkFixed, self.price_line) form1.addRow("数量", self.volume_line) form1.addRow("接口", self.gateway_combo) form1.addRow(send_button) @@ -725,6 +727,9 @@ class TradingWidget(QtWidgets.QWidget): if tick.vt_symbol != self.vt_symbol: return + if not self.checkFixed.isChecked(): + self.price_line.setText(str(tick.last_price)) + self.lp_label.setText(str(tick.last_price)) self.bp1_label.setText(str(tick.bid_price_1)) self.bv1_label.setText(str(tick.bid_volume_1)) @@ -869,6 +874,64 @@ class TradingWidget(QtWidgets.QWidget): req = order.create_cancel_request() self.main_engine.cancel_order(req, order.gateway_name) + def auto_fill_symbol(self, cell): + """根据行情信息自动填写交易组件""" + try: + # 读取行情数据,cell是一个表格中的单元格对象 + tick = cell.get_data() + if tick is None: + return + + if tick.symbol: + self.symbol_line.setText(tick.symbol) + + if tick.exchange: + self.exchange_combo.setCurrentText(tick.exchange.value) + + # 自动填写gateway信息 + if tick.gateway_name: + self.gateway_combo.setCurrentText(tick.gateway_name) + + self.volume_line.setText(str(1)) + + self.set_vt_symbol() + + except Exception as ex: + self.main_engine.write_log(u'tradingWg.autoFillSymbol exception:{}'.format(str(ex))) + + + #---------------------------------------------------------------------- + def close_position(self, cell): + """根据持仓信息自动填写交易组件""" + try: + # 读取持仓数据,cell是一个表格中的单元格对象 + pos = cell.get_data() + if pos is None: + return + if pos.symbol: + self.symbol_line.setText(pos.symbol) + + if pos.exchange: + self.exchange_combo.setCurrentText(pos.exchange.value) + + if pos.gateway_name: + self.gateway_combo.setCurrentText(pos.gateway_name) + + self.set_vt_symbol() + + self.order_type_combo.setCurrentText(OrderType.LIMIT.value) + + self.offset_combo.setCurrentText(Offset.CLOSE.value) + + self.volume_line.setText(str(pos.volume)) + + if pos.direction in [Direction.LONG, Direction.NET]: + self.direction_combo.setCurrentText(Direction.SHORT) + else: + self.direction_combo.setCurrentText(Direction.LONG) + + except Exception as ex: + self.main_engine.write_log(u'tradingWg.closePosition exception:{}'.format(str(ex))) class ActiveOrderMonitor(OrderMonitor): """