增加CTA模块的策略隔夜持仓保存功能

This commit is contained in:
chenxy123 2016-11-22 21:42:57 +08:00
parent 9e0d28c12a
commit 0dc30ce4e2
4 changed files with 41 additions and 3 deletions

View File

@ -29,6 +29,8 @@ STOPORDERPREFIX = 'CtaStopOrder.'
# 数据库名称 # 数据库名称
SETTING_DB_NAME = 'VnTrader_Setting_Db' SETTING_DB_NAME = 'VnTrader_Setting_Db'
POSITION_DB_NAME = 'VnTrader_Position_Db'
TICK_DB_NAME = 'VnTrader_Tick_Db' TICK_DB_NAME = 'VnTrader_Tick_Db'
DAILY_DB_NAME = 'VnTrader_Daily_Db' DAILY_DB_NAME = 'VnTrader_Daily_Db'
MINUTE_DB_NAME = 'VnTrader_1Min_Db' MINUTE_DB_NAME = 'VnTrader_1Min_Db'
@ -40,6 +42,7 @@ ENGINETYPE_TRADING = 'trading' # 实盘
# CTA引擎中涉及的数据类定义 # CTA引擎中涉及的数据类定义
from vtConstant import EMPTY_UNICODE, EMPTY_STRING, EMPTY_FLOAT, EMPTY_INT from vtConstant import EMPTY_UNICODE, EMPTY_STRING, EMPTY_FLOAT, EMPTY_INT
######################################################################## ########################################################################
class StopOrder(object): class StopOrder(object):
"""本地停止单""" """本地停止单"""

View File

@ -482,6 +482,8 @@ class CtaEngine(object):
for setting in l: for setting in l:
self.loadStrategy(setting) self.loadStrategy(setting)
self.loadPosition()
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def getStrategyVar(self, name): def getStrategyVar(self, name):
"""获取策略当前的变量字典""" """获取策略当前的变量字典"""
@ -536,6 +538,35 @@ class CtaEngine(object):
traceback.format_exc()]) traceback.format_exc()])
self.writeCtaLog(content) 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']
######################################################################## ########################################################################
class PositionBuffer(object): class PositionBuffer(object):

View File

@ -272,8 +272,9 @@ if __name__ == '__main__':
# 跑优化 # 跑优化
setting = OptimizationSetting() # 新建一个优化任务设置对象 setting = OptimizationSetting() # 新建一个优化任务设置对象
setting.setOptimizeTarget('capital') # 设置优化排序的目标是策略净盈利 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('atrMa', 20, 30, 5) # 增加第二个优化参数atrMa起始20结束30步进1
setting.addParameter('rsiLength', 5) # 增加一个固定数值的参数
# 性能测试环境I7-3770主频3.4G, 8核心内存16GWindows 7 专业版 # 性能测试环境I7-3770主频3.4G, 8核心内存16GWindows 7 专业版
# 测试时还跑着一堆其他的程序,性能仅供参考 # 测试时还跑着一堆其他的程序,性能仅供参考
@ -281,9 +282,9 @@ if __name__ == '__main__':
start = time.time() start = time.time()
# 运行单进程优化函数自动输出结果耗时359秒 # 运行单进程优化函数自动输出结果耗时359秒
#engine.runOptimization(AtrRsiStrategy, setting) engine.runOptimization(AtrRsiStrategy, setting)
# 多进程优化耗时89秒 # 多进程优化耗时89秒
engine.runParallelOptimization(AtrRsiStrategy, setting) #engine.runParallelOptimization(AtrRsiStrategy, setting)
print u'耗时:%s' %(time.time()-start) print u'耗时:%s' %(time.time()-start)

View File

@ -174,11 +174,13 @@ class CtaEngineManager(QtGui.QWidget):
initAllButton = QtGui.QPushButton(u'全部初始化') initAllButton = QtGui.QPushButton(u'全部初始化')
startAllButton = QtGui.QPushButton(u'全部启动') startAllButton = QtGui.QPushButton(u'全部启动')
stopAllButton = QtGui.QPushButton(u'全部停止') stopAllButton = QtGui.QPushButton(u'全部停止')
savePositionButton = QtGui.QPushButton(u'保存持仓')
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 = QtGui.QScrollArea() self.scrollArea = QtGui.QScrollArea()
@ -195,6 +197,7 @@ class CtaEngineManager(QtGui.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 = QtGui.QVBoxLayout() vbox = QtGui.QVBoxLayout()