将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

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

View File

@ -178,13 +178,11 @@ class CtaEngineManager(QtWidgets.QWidget):
initAllButton = QtWidgets.QPushButton(text.INIT_ALL) initAllButton = QtWidgets.QPushButton(text.INIT_ALL)
startAllButton = QtWidgets.QPushButton(text.START_ALL) startAllButton = QtWidgets.QPushButton(text.START_ALL)
stopAllButton = QtWidgets.QPushButton(text.STOP_ALL) stopAllButton = QtWidgets.QPushButton(text.STOP_ALL)
savePositionButton = QtWidgets.QPushButton(text.SAVE_POSITION_DATA)
loadButton.clicked.connect(self.load) loadButton.clicked.connect(self.load)
initAllButton.clicked.connect(self.initAll) initAllButton.clicked.connect(self.initAll)
startAllButton.clicked.connect(self.startAll) startAllButton.clicked.connect(self.startAll)
stopAllButton.clicked.connect(self.stopAll) stopAllButton.clicked.connect(self.stopAll)
savePositionButton.clicked.connect(self.ctaEngine.savePosition)
# 滚动区域放置所有的CtaStrategyManager # 滚动区域放置所有的CtaStrategyManager
self.scrollArea = QtWidgets.QScrollArea() self.scrollArea = QtWidgets.QScrollArea()
@ -201,7 +199,6 @@ class CtaEngineManager(QtWidgets.QWidget):
hbox2.addWidget(initAllButton) hbox2.addWidget(initAllButton)
hbox2.addWidget(startAllButton) hbox2.addWidget(startAllButton)
hbox2.addWidget(stopAllButton) hbox2.addWidget(stopAllButton)
hbox2.addWidget(savePositionButton)
hbox2.addStretch() hbox2.addStretch()
vbox = QtWidgets.QVBoxLayout() vbox = QtWidgets.QVBoxLayout()
@ -261,21 +258,7 @@ class CtaEngineManager(QtWidgets.QWidget):
"""注册事件监听""" """注册事件监听"""
self.signal.connect(self.updateCtaLog) self.signal.connect(self.updateCtaLog)
self.eventEngine.register(EVENT_CTA_LOG, self.signal.emit) 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()