将CTA策略模块的持仓改为实时保存到数据库

This commit is contained in:
vn.py 2017-08-30 21:16:18 +08:00
parent 61dfdeea38
commit be70d535ef
2 changed files with 19 additions and 34 deletions

View File

@ -313,6 +313,9 @@ class CtaEngine(object):
self.posBufferDict[trade.vtSymbol] = posBuffer
posBuffer.updateTradeData(trade)
# 保存策略持仓到数据库
self.savePosition(strategy)
#----------------------------------------------------------------------
def processPositionEvent(self, event):
"""处理持仓推送"""
@ -573,21 +576,20 @@ class CtaEngine(object):
self.writeCtaLog(content)
#----------------------------------------------------------------------
def savePosition(self):
"""保存所有策略的持仓情况到数据库"""
for strategy in self.strategyDict.values():
flt = {'name': strategy.name,
'vtSymbol': strategy.vtSymbol}
def savePosition(self, strategy):
"""保存策略的持仓情况到数据库"""
flt = {'name': strategy.name,
'vtSymbol': strategy.vtSymbol}
d = {'name': strategy.name,
'vtSymbol': strategy.vtSymbol,
'pos': strategy.pos}
d = {'name': strategy.name,
'vtSymbol': strategy.vtSymbol,
'pos': strategy.pos}
self.mainEngine.dbUpdate(POSITION_DB_NAME, strategy.className,
d, flt, True)
self.mainEngine.dbUpdate(POSITION_DB_NAME, strategy.className,
d, flt, True)
content = '策略%s持仓保存成功' %strategy.name
self.writeCtaLog(content)
content = '策略%s持仓保存成功,当前持仓%s' %(strategy.name, strategy.pos)
self.writeCtaLog(content)
#----------------------------------------------------------------------
def loadPosition(self):

View File

@ -178,13 +178,11 @@ class CtaEngineManager(QtWidgets.QWidget):
initAllButton = QtWidgets.QPushButton(text.INIT_ALL)
startAllButton = QtWidgets.QPushButton(text.START_ALL)
stopAllButton = QtWidgets.QPushButton(text.STOP_ALL)
savePositionButton = QtWidgets.QPushButton(text.SAVE_POSITION_DATA)
loadButton.clicked.connect(self.load)
initAllButton.clicked.connect(self.initAll)
startAllButton.clicked.connect(self.startAll)
stopAllButton.clicked.connect(self.stopAll)
savePositionButton.clicked.connect(self.ctaEngine.savePosition)
# 滚动区域放置所有的CtaStrategyManager
self.scrollArea = QtWidgets.QScrollArea()
@ -201,7 +199,6 @@ class CtaEngineManager(QtWidgets.QWidget):
hbox2.addWidget(initAllButton)
hbox2.addWidget(startAllButton)
hbox2.addWidget(stopAllButton)
hbox2.addWidget(savePositionButton)
hbox2.addStretch()
vbox = QtWidgets.QVBoxLayout()
@ -262,20 +259,6 @@ class CtaEngineManager(QtWidgets.QWidget):
self.signal.connect(self.updateCtaLog)
self.eventEngine.register(EVENT_CTA_LOG, self.signal.emit)
#----------------------------------------------------------------------
def closeEvent(self, event):
"""关闭窗口时的事件"""
if self.isVisible():
reply = QtWidgets.QMessageBox.question(self, text.SAVE_POSITION_DATA,
text.SAVE_POSITION_QUESTION, QtWidgets.QMessageBox.Yes |
QtWidgets.QMessageBox.No, QtWidgets.QMessageBox.No)
if reply == QtWidgets.QMessageBox.Yes:
self.ctaEngine.savePosition()
event.accept()