From be70d535efa4792fbbd52809ace36712ef5339e2 Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Wed, 30 Aug 2017 21:16:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86CTA=E7=AD=96=E7=95=A5=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E7=9A=84=E6=8C=81=E4=BB=93=E6=94=B9=E4=B8=BA=E5=AE=9E?= =?UTF-8?q?=E6=97=B6=E4=BF=9D=E5=AD=98=E5=88=B0=E6=95=B0=E6=8D=AE=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnpy/trader/app/ctaStrategy/ctaEngine.py | 34 ++++++++++++---------- vnpy/trader/app/ctaStrategy/uiCtaWidget.py | 19 +----------- 2 files changed, 19 insertions(+), 34 deletions(-) diff --git a/vnpy/trader/app/ctaStrategy/ctaEngine.py b/vnpy/trader/app/ctaStrategy/ctaEngine.py index fc6f69d9..0da9a1e6 100644 --- a/vnpy/trader/app/ctaStrategy/ctaEngine.py +++ b/vnpy/trader/app/ctaStrategy/ctaEngine.py @@ -311,7 +311,10 @@ class CtaEngine(object): posBuffer = PositionBuffer() posBuffer.vtSymbol = trade.vtSymbol self.posBufferDict[trade.vtSymbol] = posBuffer - posBuffer.updateTradeData(trade) + 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} - - d = {'name': strategy.name, - 'vtSymbol': strategy.vtSymbol, - 'pos': strategy.pos} - - self.mainEngine.dbUpdate(POSITION_DB_NAME, strategy.className, - d, flt, True) - - content = '策略%s持仓保存成功' %strategy.name - self.writeCtaLog(content) + def savePosition(self, strategy): + """保存策略的持仓情况到数据库""" + flt = {'name': strategy.name, + 'vtSymbol': strategy.vtSymbol} + + d = {'name': strategy.name, + 'vtSymbol': strategy.vtSymbol, + 'pos': strategy.pos} + + self.mainEngine.dbUpdate(POSITION_DB_NAME, strategy.className, + d, flt, True) + + content = '策略%s持仓保存成功,当前持仓%s' %(strategy.name, strategy.pos) + self.writeCtaLog(content) #---------------------------------------------------------------------- def loadPosition(self): diff --git a/vnpy/trader/app/ctaStrategy/uiCtaWidget.py b/vnpy/trader/app/ctaStrategy/uiCtaWidget.py index 7223a673..6c8421b3 100644 --- a/vnpy/trader/app/ctaStrategy/uiCtaWidget.py +++ b/vnpy/trader/app/ctaStrategy/uiCtaWidget.py @@ -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() @@ -261,21 +258,7 @@ 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() - - +