From 0dc30ce4e2240aff59de4b9b06e2350fc7feda47 Mon Sep 17 00:00:00 2001 From: chenxy123 Date: Tue, 22 Nov 2016 21:42:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0CTA=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E7=9A=84=E7=AD=96=E7=95=A5=E9=9A=94=E5=A4=9C=E6=8C=81=E4=BB=93?= =?UTF-8?q?=E4=BF=9D=E5=AD=98=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vn.trader/ctaAlgo/ctaBase.py | 3 +++ vn.trader/ctaAlgo/ctaEngine.py | 31 +++++++++++++++++++++++++++++ vn.trader/ctaAlgo/strategyAtrRsi.py | 7 ++++--- vn.trader/ctaAlgo/uiCtaWidget.py | 3 +++ 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/vn.trader/ctaAlgo/ctaBase.py b/vn.trader/ctaAlgo/ctaBase.py index 8060b6a3..6b16a85e 100644 --- a/vn.trader/ctaAlgo/ctaBase.py +++ b/vn.trader/ctaAlgo/ctaBase.py @@ -29,6 +29,8 @@ STOPORDERPREFIX = 'CtaStopOrder.' # 数据库名称 SETTING_DB_NAME = 'VnTrader_Setting_Db' +POSITION_DB_NAME = 'VnTrader_Position_Db' + TICK_DB_NAME = 'VnTrader_Tick_Db' DAILY_DB_NAME = 'VnTrader_Daily_Db' MINUTE_DB_NAME = 'VnTrader_1Min_Db' @@ -40,6 +42,7 @@ ENGINETYPE_TRADING = 'trading' # 实盘 # CTA引擎中涉及的数据类定义 from vtConstant import EMPTY_UNICODE, EMPTY_STRING, EMPTY_FLOAT, EMPTY_INT + ######################################################################## class StopOrder(object): """本地停止单""" diff --git a/vn.trader/ctaAlgo/ctaEngine.py b/vn.trader/ctaAlgo/ctaEngine.py index 0b095b20..27ff0bbd 100644 --- a/vn.trader/ctaAlgo/ctaEngine.py +++ b/vn.trader/ctaAlgo/ctaEngine.py @@ -481,6 +481,8 @@ class CtaEngine(object): for setting in l: self.loadStrategy(setting) + + self.loadPosition() #---------------------------------------------------------------------- def getStrategyVar(self, name): @@ -535,6 +537,35 @@ class CtaEngine(object): content = '\n'.join([u'策略%s触发异常已停止' %strategy.name, traceback.format_exc()]) 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 loadPosition(self): + """从数据库载入策略的持仓情况""" + for strategy in self.strategyDict.values(): + flt = {'name': strategy.name, + 'vtSymbol': strategy.vtSymbol} + + cx = self.mainEngine.dbQuery(POSITION_DB_NAME, strategy.className, flt) + if cx: + for d in cx: + strategy.pos = d['pos'] ######################################################################## diff --git a/vn.trader/ctaAlgo/strategyAtrRsi.py b/vn.trader/ctaAlgo/strategyAtrRsi.py index d19660c7..bc37d35c 100644 --- a/vn.trader/ctaAlgo/strategyAtrRsi.py +++ b/vn.trader/ctaAlgo/strategyAtrRsi.py @@ -272,8 +272,9 @@ if __name__ == '__main__': # 跑优化 setting = OptimizationSetting() # 新建一个优化任务设置对象 setting.setOptimizeTarget('capital') # 设置优化排序的目标是策略净盈利 - setting.addParameter('atrLength', 11, 20, 1) # 增加第一个优化参数atrLength,起始11,结束12,步进1 + setting.addParameter('atrLength', 12, 20, 2) # 增加第一个优化参数atrLength,起始11,结束12,步进1 setting.addParameter('atrMa', 20, 30, 5) # 增加第二个优化参数atrMa,起始20,结束30,步进1 + setting.addParameter('rsiLength', 5) # 增加一个固定数值的参数 # 性能测试环境:I7-3770,主频3.4G, 8核心,内存16G,Windows 7 专业版 # 测试时还跑着一堆其他的程序,性能仅供参考 @@ -281,9 +282,9 @@ if __name__ == '__main__': start = time.time() # 运行单进程优化函数,自动输出结果,耗时:359秒 - #engine.runOptimization(AtrRsiStrategy, setting) + engine.runOptimization(AtrRsiStrategy, setting) # 多进程优化,耗时:89秒 - engine.runParallelOptimization(AtrRsiStrategy, setting) + #engine.runParallelOptimization(AtrRsiStrategy, setting) print u'耗时:%s' %(time.time()-start) \ No newline at end of file diff --git a/vn.trader/ctaAlgo/uiCtaWidget.py b/vn.trader/ctaAlgo/uiCtaWidget.py index 730783cc..e095480f 100644 --- a/vn.trader/ctaAlgo/uiCtaWidget.py +++ b/vn.trader/ctaAlgo/uiCtaWidget.py @@ -174,11 +174,13 @@ class CtaEngineManager(QtGui.QWidget): initAllButton = QtGui.QPushButton(u'全部初始化') startAllButton = QtGui.QPushButton(u'全部启动') stopAllButton = QtGui.QPushButton(u'全部停止') + savePositionButton = QtGui.QPushButton(u'保存持仓') 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 = QtGui.QScrollArea() @@ -195,6 +197,7 @@ class CtaEngineManager(QtGui.QWidget): hbox2.addWidget(initAllButton) hbox2.addWidget(startAllButton) hbox2.addWidget(stopAllButton) + hbox2.addWidget(savePositionButton) hbox2.addStretch() vbox = QtGui.QVBoxLayout()