[Add]增加参数优化的其他统计数据输出

This commit is contained in:
vn.py 2018-01-08 17:28:30 +08:00
parent 132e08e281
commit 5a954875d0
4 changed files with 316 additions and 101 deletions

View File

@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 7,
"metadata": {
"collapsed": false
},
@ -11,7 +11,7 @@
"%matplotlib inline\n",
"\n",
"from vnpy.trader.app.ctaStrategy.ctaBacktesting import BacktestingEngine, OptimizationSetting, MINUTE_DB_NAME\n",
"#from vnpy.trader.app.ctaStrategy.strategy.strategyAtrRsi import AtrRsiStrategy\n",
"from vnpy.trader.app.ctaStrategy.strategy.strategyAtrRsi import AtrRsiStrategy\n",
"#from vnpy.trader.app.ctaStrategy.strategy.strategyMultiTimeframe import MultiTimeframeStrategy\n",
"from vnpy.trader.app.ctaStrategy.strategy.strategyMultiSignal import MultiSignalStrategy"
]
@ -39,14 +39,14 @@
"# 设置回测使用的数据\n",
"engine.setBacktestingMode(engine.BAR_MODE) # 设置引擎的回测模式为K线\n",
"engine.setDatabase(MINUTE_DB_NAME, 'IF0000') # 设置使用的历史数据库\n",
"engine.setStartDate('20100101') # 设置回测用的数据起始日期"
"engine.setStartDate('20130101') # 设置回测用的数据起始日期"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
"collapsed": false
},
"outputs": [],
"source": [
@ -60,7 +60,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 5,
"metadata": {
"collapsed": false
},
@ -75,25 +75,11 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2017-12-15 23:01:54.728000\t开始载入数据\n",
"2017-12-15 23:01:54.765000\t载入完成数据量0\n",
"2017-12-15 23:01:54.765000\t开始回测\n",
"2017-12-15 23:01:54.765000\t策略初始化完成\n",
"2017-12-15 23:01:54.765000\t策略启动完成\n",
"2017-12-15 23:01:54.765000\t开始回放数据\n",
"2017-12-15 23:01:54.766000\t数据回放结束\n"
]
}
],
"outputs": [],
"source": [
"# 运行回测\n",
"engine.runBacktesting() # 运行回测"
@ -139,23 +125,42 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2018-01-08 17:21:35.783000\t------------------------------\n",
"2018-01-08 17:21:35.784000\t优化结果\n",
"2018-01-08 17:21:35.784000\t参数{'rsiLength': 5, 'atrMa': 20, 'atrLength': 12}目标0\n",
"2018-01-08 17:21:35.784000\t参数{'rsiLength': 5, 'atrMa': 25, 'atrLength': 12}目标0\n",
"2018-01-08 17:21:35.784000\t参数{'rsiLength': 5, 'atrMa': 30, 'atrLength': 12}目标0\n",
"2018-01-08 17:21:35.784000\t参数{'rsiLength': 5, 'atrMa': 20, 'atrLength': 14}目标0\n",
"2018-01-08 17:21:35.784000\t参数{'rsiLength': 5, 'atrMa': 25, 'atrLength': 14}目标0\n",
"2018-01-08 17:21:35.784000\t参数{'rsiLength': 5, 'atrMa': 30, 'atrLength': 14}目标0\n",
"2018-01-08 17:21:35.784000\t参数{'rsiLength': 5, 'atrMa': 20, 'atrLength': 16}目标0\n",
"2018-01-08 17:21:35.784000\t参数{'rsiLength': 5, 'atrMa': 25, 'atrLength': 16}目标0\n",
"2018-01-08 17:21:35.784000\t参数{'rsiLength': 5, 'atrMa': 30, 'atrLength': 16}目标0\n",
"耗时214.858999968\n"
]
}
],
"source": [
"# 优化配置\n",
"setting = OptimizationSetting() # 新建一个优化任务设置对象\n",
"setting.setOptimizeTarget('capital') # 设置优化排序的目标是策略净盈利\n",
"setting.addParameter('atrLength', 12, 20, 2) # 增加第一个优化参数atrLength起始12结束20步进2\n",
"setting.setOptimizeTarget('totalNetPnl') # 设置优化排序的目标是策略净盈利\n",
"setting.addParameter('atrLength', 12, 16, 2) # 增加第一个优化参数atrLength起始12结束20步进2\n",
"setting.addParameter('atrMa', 20, 30, 5) # 增加第二个优化参数atrMa起始20结束30步进5\n",
"setting.addParameter('rsiLength', 5) # 增加一个固定数值的参数\n",
"\n",
"# 执行多进程优化\n",
"import time\n",
"start = time.time()\n",
"engine.runParallelOptimization(AtrRsiStrategy, setting)\n",
"resultList = engine.runParallelOptimization(AtrRsiStrategy, setting)\n",
"print u'耗时:%s' %(time.time()-start)"
]
},
@ -166,7 +171,11 @@
"collapsed": true
},
"outputs": [],
"source": []
"source": [
"# 显示优化的所有统计数据\n",
"for result in resultList:\n",
" print u'参数:%s目标%s统计数据%s' %(result[0], result[1], result[2])"
]
}
],
"metadata": {

File diff suppressed because one or more lines are too long

View File

@ -876,15 +876,15 @@ class BacktestingEngine(object):
targetValue = d[targetName]
except KeyError:
targetValue = 0
resultList.append(([str(setting)], targetValue))
resultList.append(([str(setting)], targetValue, d))
# 显示结果
resultList.sort(reverse=True, key=lambda result:result[1])
self.output('-' * 30)
self.output(u'优化结果:')
for result in resultList:
self.output(u'%s: %s' %(result[0], result[1]))
return result
self.output(u'参数:%s,目标:%s' %(result[0], result[1]))
return resultList
#----------------------------------------------------------------------
def runParallelOptimization(self, strategyClass, optimizationSetting):
@ -916,7 +916,9 @@ class BacktestingEngine(object):
self.output('-' * 30)
self.output(u'优化结果:')
for result in resultList:
self.output(u'%s: %s' %(result[0], result[1]))
self.output(u'参数:%s,目标:%s' %(result[0], result[1]))
return resultList
#----------------------------------------------------------------------
def updateDailyClose(self, dt, price):
@ -1270,5 +1272,5 @@ def optimize(strategyClass, setting, targetName,
targetValue = d[targetName]
except KeyError:
targetValue = 0
return (str(setting), targetValue)
return (str(setting), targetValue, d)

View File

@ -600,7 +600,7 @@ class CtaEngine(object):
syncData = self.mainEngine.dbQuery(POSITION_DB_NAME, strategy.className, flt)
if not syncData:
continue
return
d = syncData[0]