2015-10-19 08:42:17 +00:00
|
|
|
|
# encoding: UTF-8
|
|
|
|
|
|
|
|
|
|
'''CTA模块相关的GUI控制组件'''
|
|
|
|
|
|
2015-11-13 03:39:26 +00:00
|
|
|
|
|
2015-10-19 09:05:00 +00:00
|
|
|
|
from uiBasicWidget import QtGui, QtCore, BasicCell
|
|
|
|
|
from eventEngine import *
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
########################################################################
|
|
|
|
|
class ValueMonitor(QtGui.QTableWidget):
|
|
|
|
|
"""数值监控"""
|
|
|
|
|
signal = QtCore.pyqtSignal()
|
|
|
|
|
|
2015-11-15 07:53:54 +00:00
|
|
|
|
# ----------------------------------------------------------------------
|
2015-10-19 09:05:00 +00:00
|
|
|
|
def __init__(self, parent=None):
|
|
|
|
|
"""Constructor"""
|
|
|
|
|
super(ValueMonitor , self).__init__(parent)
|
|
|
|
|
|
|
|
|
|
self.keyCellDict = {}
|
|
|
|
|
self.row = 0
|
|
|
|
|
self.data = None
|
|
|
|
|
|
|
|
|
|
self.initUi()
|
|
|
|
|
self.signal.connect(self.updateTable)
|
|
|
|
|
|
2015-11-15 07:53:54 +00:00
|
|
|
|
# ----------------------------------------------------------------------
|
2015-10-19 09:05:00 +00:00
|
|
|
|
def initUi(self):
|
|
|
|
|
"""初始化界面"""
|
|
|
|
|
self.setColumnCount(2)
|
|
|
|
|
|
|
|
|
|
self.verticalHeader().setVisible(False)
|
|
|
|
|
self.horizontalHeader().setVisible(False)
|
|
|
|
|
|
|
|
|
|
self.setEditTriggers(self.NoEditTriggers)
|
|
|
|
|
self.setAlternatingRowColors(True)
|
|
|
|
|
|
2015-11-15 07:53:54 +00:00
|
|
|
|
# ----------------------------------------------------------------------
|
2015-10-19 09:05:00 +00:00
|
|
|
|
def updateData(self, data):
|
|
|
|
|
"""更新数据"""
|
|
|
|
|
self.data = data
|
|
|
|
|
self.signal.emit()
|
|
|
|
|
|
2015-11-15 07:53:54 +00:00
|
|
|
|
# ----------------------------------------------------------------------
|
2015-10-19 09:05:00 +00:00
|
|
|
|
def updateTable(self):
|
|
|
|
|
"""更新表格"""
|
|
|
|
|
for key, value in self.data.items():
|
|
|
|
|
if key in self.keyCellDict:
|
|
|
|
|
cell = self.keyCellDict[key]
|
|
|
|
|
cell.setText(unicode(value))
|
|
|
|
|
else:
|
|
|
|
|
# 创建并保存单元格
|
|
|
|
|
keyCell = BasicCell(unicode(key))
|
|
|
|
|
cell = BasicCell(unicode(value))
|
|
|
|
|
self.keyCellDict[key] = cell
|
|
|
|
|
|
|
|
|
|
# 移动到下一行
|
|
|
|
|
self.insertRow(self.row)
|
|
|
|
|
self.setItem(self.row, 0, keyCell)
|
|
|
|
|
self.setItem(self.row, 1, cell)
|
|
|
|
|
self.row += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
########################################################################
|
|
|
|
|
class CtaStrategyManager(QtGui.QGroupBox):
|
2015-11-15 07:53:54 +00:00
|
|
|
|
"""策略管理组件
|
|
|
|
|
即策略的UI,提供了启动、停止,查看参数,参看变量等显示信息
|
|
|
|
|
"""
|
2015-10-19 09:05:00 +00:00
|
|
|
|
|
2015-11-15 07:53:54 +00:00
|
|
|
|
# ----------------------------------------------------------------------
|
2015-10-19 09:05:00 +00:00
|
|
|
|
def __init__(self, ctaEngine, eventEngine, name, parent=None):
|
|
|
|
|
"""Constructor"""
|
|
|
|
|
super(CtaStrategyManager, self).__init__(parent)
|
|
|
|
|
|
|
|
|
|
self.ctaEngine = ctaEngine
|
|
|
|
|
self.eventEngine = eventEngine
|
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
|
|
self.initUi()
|
|
|
|
|
self.updateMonitor()
|
|
|
|
|
self.registerEvent()
|
|
|
|
|
|
2015-11-15 07:53:54 +00:00
|
|
|
|
# ----------------------------------------------------------------------
|
2015-10-19 09:05:00 +00:00
|
|
|
|
def initUi(self):
|
|
|
|
|
"""初始化界面"""
|
|
|
|
|
self.setTitle(self.name)
|
|
|
|
|
|
|
|
|
|
paramLabel = QtGui.QLabel(u'参数')
|
|
|
|
|
varLabel = QtGui.QLabel(u'变量')
|
|
|
|
|
|
|
|
|
|
self.paramMonitor = ValueMonitor(self)
|
|
|
|
|
self.varMonitor = ValueMonitor(self)
|
|
|
|
|
|
|
|
|
|
buttonStart = QtGui.QPushButton(u'启动')
|
|
|
|
|
buttonStop = QtGui.QPushButton(u'停止')
|
|
|
|
|
buttonStart.clicked.connect(self.start)
|
|
|
|
|
buttonStop.clicked.connect(self.stop)
|
|
|
|
|
|
|
|
|
|
hbox = QtGui.QHBoxLayout()
|
|
|
|
|
hbox.addWidget(buttonStart)
|
|
|
|
|
hbox.addWidget(buttonStop)
|
|
|
|
|
hbox.addStretch()
|
|
|
|
|
|
|
|
|
|
vbox = QtGui.QVBoxLayout()
|
|
|
|
|
vbox.addLayout(hbox)
|
|
|
|
|
vbox.addWidget(paramLabel)
|
|
|
|
|
vbox.addWidget(self.paramMonitor)
|
|
|
|
|
vbox.addWidget(varLabel)
|
|
|
|
|
vbox.addWidget(self.varMonitor)
|
|
|
|
|
self.setLayout(vbox)
|
|
|
|
|
|
2015-11-15 07:53:54 +00:00
|
|
|
|
# ----------------------------------------------------------------------
|
2015-10-19 09:05:00 +00:00
|
|
|
|
def updateMonitor(self, event=None):
|
|
|
|
|
"""显示策略最新状态"""
|
2015-11-15 07:53:54 +00:00
|
|
|
|
|
|
|
|
|
# 调用CTA策略引擎,获取策略的所有参数的值(依赖策略的ParamList配置)
|
2015-10-19 09:05:00 +00:00
|
|
|
|
paramDict = self.ctaEngine.getStrategyParam(self.name)
|
|
|
|
|
if paramDict:
|
|
|
|
|
self.paramMonitor.updateData(paramDict)
|
2015-11-15 07:53:54 +00:00
|
|
|
|
|
|
|
|
|
# 调用CTA策略引擎,获取策略的所有变量的值(依赖策略的varList配置)
|
2015-10-19 09:05:00 +00:00
|
|
|
|
varDict = self.ctaEngine.getStrategyVar(self.name)
|
|
|
|
|
if varDict:
|
|
|
|
|
self.varMonitor.updateData(varDict)
|
|
|
|
|
|
2015-11-15 07:53:54 +00:00
|
|
|
|
# ----------------------------------------------------------------------
|
2015-10-19 09:05:00 +00:00
|
|
|
|
def registerEvent(self):
|
|
|
|
|
"""注册事件监听"""
|
2015-11-15 07:53:54 +00:00
|
|
|
|
# 注册定时器,定时更新策略最新状态
|
2015-10-19 09:05:00 +00:00
|
|
|
|
self.eventEngine.register(EVENT_TIMER, self.updateMonitor)
|
|
|
|
|
|
2015-11-15 07:53:54 +00:00
|
|
|
|
# ----------------------------------------------------------------------
|
2015-10-19 09:05:00 +00:00
|
|
|
|
def start(self):
|
|
|
|
|
"""启动策略"""
|
|
|
|
|
self.ctaEngine.startStrategy(self.name)
|
|
|
|
|
|
2015-11-15 07:53:54 +00:00
|
|
|
|
# ----------------------------------------------------------------------
|
2015-10-19 09:05:00 +00:00
|
|
|
|
def stop(self):
|
|
|
|
|
"""停止策略"""
|
|
|
|
|
self.ctaEngine.stopStrategy(self.name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
########################################################################
|
|
|
|
|
class CtaEngineManager(QtGui.QWidget):
|
|
|
|
|
"""CTA引擎管理组件"""
|
2015-11-15 07:53:54 +00:00
|
|
|
|
|
|
|
|
|
# 定义信号对象,为Event的类型
|
2015-10-19 09:05:00 +00:00
|
|
|
|
signal = QtCore.pyqtSignal(type(Event()))
|
|
|
|
|
|
2015-11-15 07:53:54 +00:00
|
|
|
|
# ----------------------------------------------------------------------
|
2015-10-19 09:05:00 +00:00
|
|
|
|
def __init__(self, ctaEngine, eventEngine, parent=None):
|
|
|
|
|
"""Constructor"""
|
|
|
|
|
super(CtaEngineManager, self).__init__(parent)
|
|
|
|
|
|
|
|
|
|
self.ctaEngine = ctaEngine
|
|
|
|
|
self.eventEngine = eventEngine
|
|
|
|
|
|
|
|
|
|
self.strategyLoaded = False
|
|
|
|
|
|
|
|
|
|
self.initUi()
|
|
|
|
|
self.registerEvent()
|
|
|
|
|
|
|
|
|
|
# 记录日志
|
|
|
|
|
self.ctaEngine.writeCtaLog(u'CTA引擎启动成功')
|
|
|
|
|
|
2015-11-15 07:53:54 +00:00
|
|
|
|
# ----------------------------------------------------------------------
|
2015-10-19 09:05:00 +00:00
|
|
|
|
def initUi(self):
|
|
|
|
|
"""初始化界面"""
|
|
|
|
|
self.setWindowTitle(u'CTA策略')
|
|
|
|
|
|
|
|
|
|
# 按钮
|
|
|
|
|
loadButton = QtGui.QPushButton(u'加载策略')
|
|
|
|
|
startAllButton = QtGui.QPushButton(u'全部启动')
|
|
|
|
|
stopAllButton = QtGui.QPushButton(u'全部停止')
|
|
|
|
|
|
|
|
|
|
loadButton.clicked.connect(self.load)
|
|
|
|
|
startAllButton.clicked.connect(self.startAll)
|
|
|
|
|
stopAllButton.clicked.connect(self.stopAll)
|
|
|
|
|
|
|
|
|
|
# 滚动区域,放置所有的CtaStrategyManager
|
|
|
|
|
self.scrollArea = QtGui.QScrollArea()
|
|
|
|
|
|
|
|
|
|
# CTA组件的日志监控
|
|
|
|
|
self.ctaLogMonitor = QtGui.QTextEdit()
|
|
|
|
|
self.ctaLogMonitor.setReadOnly(True)
|
|
|
|
|
|
|
|
|
|
# 设置布局
|
|
|
|
|
hbox2 = QtGui.QHBoxLayout()
|
|
|
|
|
hbox2.addWidget(loadButton)
|
|
|
|
|
hbox2.addWidget(startAllButton)
|
|
|
|
|
hbox2.addWidget(stopAllButton)
|
|
|
|
|
hbox2.addStretch()
|
|
|
|
|
|
|
|
|
|
vbox = QtGui.QVBoxLayout()
|
|
|
|
|
vbox.addLayout(hbox2)
|
|
|
|
|
vbox.addWidget(self.scrollArea)
|
|
|
|
|
vbox.addWidget(self.ctaLogMonitor)
|
|
|
|
|
self.setLayout(vbox)
|
|
|
|
|
|
2015-11-15 07:53:54 +00:00
|
|
|
|
# ----------------------------------------------------------------------
|
2015-10-19 09:05:00 +00:00
|
|
|
|
def initStrategyManager(self):
|
2015-11-15 07:53:54 +00:00
|
|
|
|
"""初始化策略管理组件界面"""
|
|
|
|
|
# 构建组件w
|
2015-10-19 09:05:00 +00:00
|
|
|
|
w = QtGui.QWidget()
|
2015-11-15 07:53:54 +00:00
|
|
|
|
|
|
|
|
|
# 构建Layout层
|
2015-10-19 09:05:00 +00:00
|
|
|
|
hbox = QtGui.QHBoxLayout()
|
2015-11-15 07:53:54 +00:00
|
|
|
|
|
|
|
|
|
# 将策略UI组件逐一添加Layout层
|
2015-10-19 09:05:00 +00:00
|
|
|
|
for name in self.ctaEngine.strategyDict.keys():
|
|
|
|
|
strategyManager = CtaStrategyManager(self.ctaEngine, self.eventEngine, name)
|
|
|
|
|
hbox.addWidget(strategyManager)
|
2015-11-15 07:53:54 +00:00
|
|
|
|
|
|
|
|
|
# w绑定Layout层
|
2015-10-19 09:05:00 +00:00
|
|
|
|
w.setLayout(hbox)
|
2015-11-15 07:53:54 +00:00
|
|
|
|
|
|
|
|
|
# 滚动区域,设置为组件w
|
2015-10-19 09:05:00 +00:00
|
|
|
|
self.scrollArea.setWidget(w)
|
|
|
|
|
|
2015-11-15 07:53:54 +00:00
|
|
|
|
# ----------------------------------------------------------------------
|
2015-10-19 09:05:00 +00:00
|
|
|
|
def startAll(self):
|
|
|
|
|
"""全部启动"""
|
|
|
|
|
for name in self.ctaEngine.strategyDict.keys():
|
|
|
|
|
self.ctaEngine.startStrategy(name)
|
|
|
|
|
|
2015-11-15 07:53:54 +00:00
|
|
|
|
# ----------------------------------------------------------------------
|
2015-10-19 09:05:00 +00:00
|
|
|
|
def stopAll(self):
|
|
|
|
|
"""全部停止"""
|
|
|
|
|
for name in self.ctaEngine.strategyDict.keys():
|
|
|
|
|
self.ctaEngine.stopStrategy(name)
|
|
|
|
|
|
2015-11-15 07:53:54 +00:00
|
|
|
|
# ----------------------------------------------------------------------
|
2015-10-19 09:05:00 +00:00
|
|
|
|
def load(self):
|
|
|
|
|
"""加载策略"""
|
|
|
|
|
if not self.strategyLoaded:
|
2015-11-15 07:53:54 +00:00
|
|
|
|
|
|
|
|
|
# 调用策略引擎,加载所有策略设置
|
2015-10-19 09:05:00 +00:00
|
|
|
|
self.ctaEngine.loadStrategySetting()
|
2015-11-15 07:53:54 +00:00
|
|
|
|
|
|
|
|
|
# 初始化所有策略的UI组件
|
2015-10-19 09:05:00 +00:00
|
|
|
|
self.initStrategyManager()
|
2015-11-15 07:53:54 +00:00
|
|
|
|
|
2015-10-19 09:05:00 +00:00
|
|
|
|
self.strategyLoaded = True
|
2015-11-15 07:53:54 +00:00
|
|
|
|
|
2015-10-19 09:05:00 +00:00
|
|
|
|
self.ctaEngine.writeCtaLog(u'策略加载成功')
|
|
|
|
|
|
2015-11-15 07:53:54 +00:00
|
|
|
|
# ----------------------------------------------------------------------
|
2015-10-19 09:05:00 +00:00
|
|
|
|
def updateCtaLog(self, event):
|
|
|
|
|
"""更新CTA相关日志"""
|
|
|
|
|
log = event.dict_['data']
|
|
|
|
|
content = '\t'.join([log.logTime, log.logContent])
|
|
|
|
|
self.ctaLogMonitor.append(content)
|
|
|
|
|
|
2015-11-15 07:53:54 +00:00
|
|
|
|
# ----------------------------------------------------------------------
|
2015-10-19 09:05:00 +00:00
|
|
|
|
def registerEvent(self):
|
|
|
|
|
"""注册事件监听"""
|
2015-11-15 07:53:54 +00:00
|
|
|
|
# 定义信号绑定方法
|
2015-10-19 09:05:00 +00:00
|
|
|
|
self.signal.connect(self.updateCtaLog)
|
2015-11-15 07:53:54 +00:00
|
|
|
|
|
|
|
|
|
# 注册EVENT_CAT_LOG类型的事件,触发方法为信号的焕发(emit)
|
2015-10-19 09:05:00 +00:00
|
|
|
|
self.eventEngine.register(EVENT_CTA_LOG, self.signal.emit)
|
|
|
|
|
|