2015-10-16 08:50:44 +00:00
|
|
|
|
# encoding: UTF-8
|
|
|
|
|
|
2016-02-04 12:41:37 +00:00
|
|
|
|
'''
|
|
|
|
|
CTA模块相关的GUI控制组件
|
|
|
|
|
'''
|
|
|
|
|
|
2015-10-16 08:50:44 +00:00
|
|
|
|
|
|
|
|
|
from uiBasicWidget import QtGui, QtCore, BasicCell
|
|
|
|
|
from eventEngine import *
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
########################################################################
|
2016-02-04 12:41:37 +00:00
|
|
|
|
class CtaValueMonitor(QtGui.QTableWidget):
|
|
|
|
|
"""参数监控"""
|
2015-10-16 08:50:44 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def __init__(self, parent=None):
|
|
|
|
|
"""Constructor"""
|
2016-02-04 12:41:37 +00:00
|
|
|
|
super(CtaValueMonitor, self).__init__(parent)
|
2015-10-16 08:50:44 +00:00
|
|
|
|
|
|
|
|
|
self.keyCellDict = {}
|
|
|
|
|
self.data = None
|
2016-02-04 12:41:37 +00:00
|
|
|
|
self.inited = False
|
2015-10-16 08:50:44 +00:00
|
|
|
|
|
|
|
|
|
self.initUi()
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def initUi(self):
|
|
|
|
|
"""初始化界面"""
|
2016-02-04 12:41:37 +00:00
|
|
|
|
self.setRowCount(1)
|
|
|
|
|
self.verticalHeader().setVisible(False)
|
2015-10-16 08:50:44 +00:00
|
|
|
|
self.setEditTriggers(self.NoEditTriggers)
|
2016-02-04 12:41:37 +00:00
|
|
|
|
|
|
|
|
|
self.setMaximumHeight(self.sizeHint().height())
|
2015-10-16 08:50:44 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def updateData(self, data):
|
|
|
|
|
"""更新数据"""
|
2016-02-04 12:41:37 +00:00
|
|
|
|
if not self.inited:
|
|
|
|
|
self.setColumnCount(len(data))
|
|
|
|
|
self.setHorizontalHeaderLabels(data.keys())
|
|
|
|
|
|
|
|
|
|
col = 0
|
|
|
|
|
for k, v in data.items():
|
|
|
|
|
cell = QtGui.QTableWidgetItem(unicode(v))
|
|
|
|
|
self.keyCellDict[k] = cell
|
|
|
|
|
self.setItem(0, col, cell)
|
|
|
|
|
col += 1
|
|
|
|
|
|
|
|
|
|
self.inited = True
|
|
|
|
|
else:
|
|
|
|
|
for k, v in data.items():
|
|
|
|
|
cell = self.keyCellDict[k]
|
|
|
|
|
cell.setText(unicode(v))
|
2015-10-16 08:50:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
########################################################################
|
|
|
|
|
class CtaStrategyManager(QtGui.QGroupBox):
|
|
|
|
|
"""策略管理组件"""
|
2016-02-04 12:41:37 +00:00
|
|
|
|
signal = QtCore.pyqtSignal(type(Event()))
|
2015-10-16 08:50:44 +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()
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def initUi(self):
|
|
|
|
|
"""初始化界面"""
|
|
|
|
|
self.setTitle(self.name)
|
|
|
|
|
|
2016-02-04 12:41:37 +00:00
|
|
|
|
self.paramMonitor = CtaValueMonitor(self)
|
|
|
|
|
self.varMonitor = CtaValueMonitor(self)
|
2015-10-16 08:50:44 +00:00
|
|
|
|
|
2016-11-29 13:34:35 +00:00
|
|
|
|
height = 60
|
|
|
|
|
self.paramMonitor.setFixedHeight(height)
|
|
|
|
|
self.varMonitor.setFixedHeight(height)
|
2015-10-16 08:50:44 +00:00
|
|
|
|
|
2016-02-04 12:41:37 +00:00
|
|
|
|
buttonInit = QtGui.QPushButton(u'初始化')
|
2015-10-16 08:50:44 +00:00
|
|
|
|
buttonStart = QtGui.QPushButton(u'启动')
|
|
|
|
|
buttonStop = QtGui.QPushButton(u'停止')
|
2016-02-04 12:41:37 +00:00
|
|
|
|
buttonInit.clicked.connect(self.init)
|
2015-10-16 08:50:44 +00:00
|
|
|
|
buttonStart.clicked.connect(self.start)
|
|
|
|
|
buttonStop.clicked.connect(self.stop)
|
|
|
|
|
|
2016-02-04 12:41:37 +00:00
|
|
|
|
hbox1 = QtGui.QHBoxLayout()
|
|
|
|
|
hbox1.addWidget(buttonInit)
|
|
|
|
|
hbox1.addWidget(buttonStart)
|
|
|
|
|
hbox1.addWidget(buttonStop)
|
|
|
|
|
hbox1.addStretch()
|
|
|
|
|
|
|
|
|
|
hbox2 = QtGui.QHBoxLayout()
|
|
|
|
|
hbox2.addWidget(self.paramMonitor)
|
|
|
|
|
|
|
|
|
|
hbox3 = QtGui.QHBoxLayout()
|
|
|
|
|
hbox3.addWidget(self.varMonitor)
|
2015-10-16 08:50:44 +00:00
|
|
|
|
|
|
|
|
|
vbox = QtGui.QVBoxLayout()
|
2016-02-04 12:41:37 +00:00
|
|
|
|
vbox.addLayout(hbox1)
|
|
|
|
|
vbox.addLayout(hbox2)
|
|
|
|
|
vbox.addLayout(hbox3)
|
|
|
|
|
|
2015-10-16 08:50:44 +00:00
|
|
|
|
self.setLayout(vbox)
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def updateMonitor(self, event=None):
|
|
|
|
|
"""显示策略最新状态"""
|
|
|
|
|
paramDict = self.ctaEngine.getStrategyParam(self.name)
|
|
|
|
|
if paramDict:
|
|
|
|
|
self.paramMonitor.updateData(paramDict)
|
|
|
|
|
|
|
|
|
|
varDict = self.ctaEngine.getStrategyVar(self.name)
|
|
|
|
|
if varDict:
|
|
|
|
|
self.varMonitor.updateData(varDict)
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def registerEvent(self):
|
|
|
|
|
"""注册事件监听"""
|
2016-02-04 12:41:37 +00:00
|
|
|
|
self.signal.connect(self.updateMonitor)
|
|
|
|
|
self.eventEngine.register(EVENT_CTA_STRATEGY+self.name, self.signal.emit)
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def init(self):
|
|
|
|
|
"""初始化策略"""
|
|
|
|
|
self.ctaEngine.initStrategy(self.name)
|
2015-10-16 08:50:44 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def start(self):
|
|
|
|
|
"""启动策略"""
|
|
|
|
|
self.ctaEngine.startStrategy(self.name)
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def stop(self):
|
|
|
|
|
"""停止策略"""
|
|
|
|
|
self.ctaEngine.stopStrategy(self.name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
########################################################################
|
|
|
|
|
class CtaEngineManager(QtGui.QWidget):
|
|
|
|
|
"""CTA引擎管理组件"""
|
|
|
|
|
signal = QtCore.pyqtSignal(type(Event()))
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
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引擎启动成功')
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def initUi(self):
|
|
|
|
|
"""初始化界面"""
|
|
|
|
|
self.setWindowTitle(u'CTA策略')
|
|
|
|
|
|
|
|
|
|
# 按钮
|
|
|
|
|
loadButton = QtGui.QPushButton(u'加载策略')
|
2016-02-04 12:41:37 +00:00
|
|
|
|
initAllButton = QtGui.QPushButton(u'全部初始化')
|
2015-10-16 08:50:44 +00:00
|
|
|
|
startAllButton = QtGui.QPushButton(u'全部启动')
|
|
|
|
|
stopAllButton = QtGui.QPushButton(u'全部停止')
|
2016-11-22 13:42:57 +00:00
|
|
|
|
savePositionButton = QtGui.QPushButton(u'保存持仓')
|
2015-10-16 08:50:44 +00:00
|
|
|
|
|
|
|
|
|
loadButton.clicked.connect(self.load)
|
2016-02-04 12:41:37 +00:00
|
|
|
|
initAllButton.clicked.connect(self.initAll)
|
2015-10-16 08:50:44 +00:00
|
|
|
|
startAllButton.clicked.connect(self.startAll)
|
|
|
|
|
stopAllButton.clicked.connect(self.stopAll)
|
2016-11-22 13:42:57 +00:00
|
|
|
|
savePositionButton.clicked.connect(self.ctaEngine.savePosition)
|
2015-10-16 08:50:44 +00:00
|
|
|
|
|
|
|
|
|
# 滚动区域,放置所有的CtaStrategyManager
|
|
|
|
|
self.scrollArea = QtGui.QScrollArea()
|
2016-02-04 12:41:37 +00:00
|
|
|
|
self.scrollArea.setWidgetResizable(True)
|
2015-10-16 08:50:44 +00:00
|
|
|
|
|
|
|
|
|
# CTA组件的日志监控
|
|
|
|
|
self.ctaLogMonitor = QtGui.QTextEdit()
|
|
|
|
|
self.ctaLogMonitor.setReadOnly(True)
|
2016-02-04 12:41:37 +00:00
|
|
|
|
self.ctaLogMonitor.setMaximumHeight(200)
|
2015-10-16 08:50:44 +00:00
|
|
|
|
|
|
|
|
|
# 设置布局
|
|
|
|
|
hbox2 = QtGui.QHBoxLayout()
|
|
|
|
|
hbox2.addWidget(loadButton)
|
2016-02-04 12:41:37 +00:00
|
|
|
|
hbox2.addWidget(initAllButton)
|
2015-10-16 08:50:44 +00:00
|
|
|
|
hbox2.addWidget(startAllButton)
|
|
|
|
|
hbox2.addWidget(stopAllButton)
|
2016-11-22 13:42:57 +00:00
|
|
|
|
hbox2.addWidget(savePositionButton)
|
2015-10-16 08:50:44 +00:00
|
|
|
|
hbox2.addStretch()
|
|
|
|
|
|
|
|
|
|
vbox = QtGui.QVBoxLayout()
|
|
|
|
|
vbox.addLayout(hbox2)
|
|
|
|
|
vbox.addWidget(self.scrollArea)
|
|
|
|
|
vbox.addWidget(self.ctaLogMonitor)
|
|
|
|
|
self.setLayout(vbox)
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def initStrategyManager(self):
|
|
|
|
|
"""初始化策略管理组件界面"""
|
|
|
|
|
w = QtGui.QWidget()
|
2016-02-04 12:41:37 +00:00
|
|
|
|
vbox = QtGui.QVBoxLayout()
|
2015-10-16 08:50:44 +00:00
|
|
|
|
|
|
|
|
|
for name in self.ctaEngine.strategyDict.keys():
|
|
|
|
|
strategyManager = CtaStrategyManager(self.ctaEngine, self.eventEngine, name)
|
2016-02-04 12:41:37 +00:00
|
|
|
|
vbox.addWidget(strategyManager)
|
|
|
|
|
|
|
|
|
|
vbox.addStretch()
|
|
|
|
|
|
|
|
|
|
w.setLayout(vbox)
|
|
|
|
|
self.scrollArea.setWidget(w)
|
2015-10-16 08:50:44 +00:00
|
|
|
|
|
2016-02-04 12:41:37 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def initAll(self):
|
|
|
|
|
"""全部初始化"""
|
|
|
|
|
for name in self.ctaEngine.strategyDict.keys():
|
|
|
|
|
self.ctaEngine.initStrategy(name)
|
2015-10-16 08:50:44 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def startAll(self):
|
|
|
|
|
"""全部启动"""
|
|
|
|
|
for name in self.ctaEngine.strategyDict.keys():
|
|
|
|
|
self.ctaEngine.startStrategy(name)
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def stopAll(self):
|
|
|
|
|
"""全部停止"""
|
|
|
|
|
for name in self.ctaEngine.strategyDict.keys():
|
|
|
|
|
self.ctaEngine.stopStrategy(name)
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def load(self):
|
|
|
|
|
"""加载策略"""
|
|
|
|
|
if not self.strategyLoaded:
|
2016-02-04 12:41:37 +00:00
|
|
|
|
self.ctaEngine.loadSetting()
|
2015-10-16 08:50:44 +00:00
|
|
|
|
self.initStrategyManager()
|
|
|
|
|
self.strategyLoaded = True
|
|
|
|
|
self.ctaEngine.writeCtaLog(u'策略加载成功')
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def updateCtaLog(self, event):
|
|
|
|
|
"""更新CTA相关日志"""
|
|
|
|
|
log = event.dict_['data']
|
|
|
|
|
content = '\t'.join([log.logTime, log.logContent])
|
|
|
|
|
self.ctaLogMonitor.append(content)
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def registerEvent(self):
|
|
|
|
|
"""注册事件监听"""
|
|
|
|
|
self.signal.connect(self.updateCtaLog)
|
|
|
|
|
self.eventEngine.register(EVENT_CTA_LOG, self.signal.emit)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|