[Add] Add active order monitor and contract manager

This commit is contained in:
vn.py 2019-01-11 12:21:55 +08:00
parent 40b5a4127c
commit 9b2b0561b1
4 changed files with 133 additions and 4 deletions

View File

@ -263,6 +263,7 @@ class OmsEngine(BaseEngine):
self.main_engine.get_all_trades = self.get_all_trades
self.main_engine.get_all_positions = self.get_all_positions
self.main_engine.get_all_accounts = self.get_all_accounts
self.main_engine.get_all_contracts = self.get_all_contracts
self.main_engine.get_all_active_orders = self.get_all_active_orders
def register_event(self):
@ -285,7 +286,7 @@ class OmsEngine(BaseEngine):
self.orders[order.vt_orderid] = order
# If order is active, then update data in dict.
if order.check_active():
if order.is_active():
self.active_orders[order.vt_orderid] = order
# Otherwise, pop inactive order from in dict
elif order.vt_orderid in self.active_orders:

View File

@ -114,7 +114,7 @@ class OrderData(BaseData):
self.vt_symbol = f"{self.symbol}.{self.exchange}"
self.vt_orderid = f"{self.gateway_name}.{self.orderid}"
def check_active(self):
def is_active(self):
"""
Check if the order is active.
"""

View File

@ -17,7 +17,9 @@ from .widget import (
AccountMonitor,
LogMonitor,
ConnectDialog,
TradingWidget
TradingWidget,
ActiveOrderMonitor,
ContractManager
)
@ -48,11 +50,14 @@ class MainWindow(QtWidgets.QMainWindow):
trading_widget, trading_dock = self.create_dock(TradingWidget, "交易", QtCore.Qt.LeftDockWidgetArea)
tick_widget, tick_dock = self.create_dock(TickMonitor, "行情", QtCore.Qt.RightDockWidgetArea)
order_widget, order_dock = self.create_dock(OrderMonitor, "委托", QtCore.Qt.RightDockWidgetArea)
active_widget, active_dock = self.create_dock(ActiveOrderMonitor, "活动", QtCore.Qt.RightDockWidgetArea)
trade_widget, trade_dock = self.create_dock(TradeMonitor, "成交", QtCore.Qt.RightDockWidgetArea)
log_widget, log_dock = self.create_dock(LogMonitor, "日志", QtCore.Qt.BottomDockWidgetArea)
account_widget, account_dock = self.create_dock(AccountMonitor, "资金", QtCore.Qt.BottomDockWidgetArea)
position_widget, position_dock = self.create_dock(PositionMonitor, "持仓", QtCore.Qt.BottomDockWidgetArea)
self.tabifyDockWidget(active_dock, order_dock)
def init_menu(self):
""""""
bar = self.menuBar()
@ -61,6 +66,7 @@ class MainWindow(QtWidgets.QMainWindow):
app_menu = bar.addMenu("功能")
help_menu = bar.addMenu("帮助")
# System menu
gateway_names = self.main_engine.get_all_gateway_names()
for name in gateway_names:
func = partial(self.connect, name)
@ -72,6 +78,16 @@ class MainWindow(QtWidgets.QMainWindow):
sys_menu.addAction(action)
# App menu
# Help menu
contract_action = QtWidgets.QAction("查询合约", self)
contract_action.triggered.connect(self.open_contract)
contract_icon = QtGui.QIcon(get_icon_path(__file__, "contract.ico"))
contract_action.setIcon(contract_icon)
help_menu.addAction(contract_action)
def create_dock(
self,
widget_class: QtWidgets.QWidget,
@ -121,3 +137,13 @@ class MainWindow(QtWidgets.QMainWindow):
event.accept()
else:
event.ignore()
def open_contract(self):
"""
Open contract manager.
"""
widget = self.widgets.get("contract", None)
if not widget:
widget = ContractManager(self.main_engine, self.event_engine)
self.widgets["contract"] = widget
widget.show()

View File

@ -959,3 +959,105 @@ class TradingWidget(QtWidgets.QWidget):
for order in order_list:
req = order.create_cancel_request()
self.main_engine.cancel_order(req, order.gateway_name)
class ActiveOrderMonitor(OrderMonitor):
"""
Monitor which shows active order only.
"""
def process_event(self, event):
"""
Hides the row if order is not active.
"""
super(ActiveOrderMonitor, self).process_event(event)
order = event.data
row_cells = self.cells[order.vt_orderid]
row = self.row(row_cells["volume"])
if order.is_active():
self.showRow(row)
else:
self.hideRow(row)
class ContractManager(QtWidgets.QWidget):
"""
Query contract data available to trade in system.
"""
headers = {
"vt_symbol": "本地代码",
"symbol": "代码",
"exchange": "交易所",
"name": "名称",
"product": "合约分类",
"size": "合约乘数",
"pricetick": "价格跳动",
"gateway_name": "交易接口"
}
def __init__(self, main_engine, event_engine):
super(ContractManager, self).__init__()
self.main_engine = main_engine
self.event_engine = event_engine
self.init_ui()
def init_ui(self):
""""""
self.setWindowTitle("合约查询")
self.resize(1000, 600)
self.filter_line = QtWidgets.QLineEdit()
self.filter_line.setPlaceholderText("输入合约代码或者交易所,留空则查询所有合约")
self.button_show = QtWidgets.QPushButton("查询")
self.button_show.clicked.connect(self.show_contracts)
labels = []
for name, display in self.headers.items():
label = f"{display}\n{name}"
labels.append(label)
self.contract_table = QtWidgets.QTableWidget()
self.contract_table.setColumnCount(len(self.headers))
self.contract_table.setHorizontalHeaderLabels(labels)
self.contract_table.verticalHeader().setVisible(False)
self.contract_table.setEditTriggers(self.contract_table.NoEditTriggers)
self.contract_table.setAlternatingRowColors(True)
hbox = QtWidgets.QHBoxLayout()
hbox.addWidget(self.filter_line)
hbox.addWidget(self.button_show)
vbox = QtWidgets.QVBoxLayout()
vbox.addLayout(hbox)
vbox.addWidget(self.contract_table)
self.setLayout(vbox)
def show_contracts(self):
"""
Show contracts by symbol
"""
flt = str(self.filter_line.text())
all_contracts = self.main_engine.get_all_contracts()
if flt:
contracts = [contract for contract in all_contracts if flt in contract.vt_symbol]
else:
contracts = all_contracts
self.contract_table.clearContents()
self.contract_table.setRowCount(len(contracts))
for row, contract in enumerate(contracts):
for column, name in enumerate(self.headers.keys()):
cell = BaseCell(getattr(contract, name), contract)
self.contract_table.setItem(row, column, cell)
self.contract_table.resizeColumnsToContents()