2017-06-02 13:44:28 +00:00
|
|
|
|
{
|
|
|
|
|
"cells": [
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
2017-10-07 15:25:59 +00:00
|
|
|
|
"execution_count": null,
|
2017-06-02 13:44:28 +00:00
|
|
|
|
"metadata": {
|
|
|
|
|
"collapsed": false
|
|
|
|
|
},
|
|
|
|
|
"outputs": [],
|
|
|
|
|
"source": [
|
|
|
|
|
"%matplotlib inline\n",
|
|
|
|
|
"\n",
|
|
|
|
|
"from vnpy.trader.app.ctaStrategy.ctaBacktesting import BacktestingEngine, OptimizationSetting, MINUTE_DB_NAME\n",
|
2017-12-04 07:37:15 +00:00
|
|
|
|
"from vnpy.trader.app.ctaStrategy.strategy.strategyAtrRsi import AtrRsiStrategy\n",
|
|
|
|
|
"from vnpy.trader.app.ctaStrategy.strategy.strategyMultiTimeframe import MultiTimeframeStrategy"
|
2017-06-02 13:44:28 +00:00
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
2017-10-07 15:25:59 +00:00
|
|
|
|
"execution_count": null,
|
2017-06-02 13:44:28 +00:00
|
|
|
|
"metadata": {
|
|
|
|
|
"collapsed": true
|
|
|
|
|
},
|
|
|
|
|
"outputs": [],
|
|
|
|
|
"source": [
|
|
|
|
|
"# 创建回测引擎对象\n",
|
|
|
|
|
"engine = BacktestingEngine()"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
2017-10-07 15:25:59 +00:00
|
|
|
|
"execution_count": null,
|
2017-06-02 13:44:28 +00:00
|
|
|
|
"metadata": {
|
|
|
|
|
"collapsed": false
|
|
|
|
|
},
|
|
|
|
|
"outputs": [],
|
|
|
|
|
"source": [
|
|
|
|
|
"# 设置回测使用的数据\n",
|
|
|
|
|
"engine.setBacktestingMode(engine.BAR_MODE) # 设置引擎的回测模式为K线\n",
|
|
|
|
|
"engine.setDatabase(MINUTE_DB_NAME, 'IF0000') # 设置使用的历史数据库\n",
|
2017-09-25 05:51:24 +00:00
|
|
|
|
"engine.setStartDate('20120101') # 设置回测用的数据起始日期"
|
2017-06-02 13:44:28 +00:00
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
2017-10-07 15:25:59 +00:00
|
|
|
|
"execution_count": null,
|
2017-06-02 13:44:28 +00:00
|
|
|
|
"metadata": {
|
|
|
|
|
"collapsed": true
|
|
|
|
|
},
|
|
|
|
|
"outputs": [],
|
|
|
|
|
"source": [
|
|
|
|
|
"# 配置回测引擎参数\n",
|
|
|
|
|
"engine.setSlippage(0.2) # 设置滑点为股指1跳\n",
|
|
|
|
|
"engine.setRate(0.3/10000) # 设置手续费万0.3\n",
|
|
|
|
|
"engine.setSize(300) # 设置股指合约大小 \n",
|
2017-07-14 12:12:42 +00:00
|
|
|
|
"engine.setPriceTick(0.2) # 设置股指最小价格变动 \n",
|
|
|
|
|
"engine.setCapital(1000000) # 设置回测本金"
|
2017-06-02 13:44:28 +00:00
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
2017-10-07 15:25:59 +00:00
|
|
|
|
"execution_count": null,
|
2017-06-02 13:44:28 +00:00
|
|
|
|
"metadata": {
|
2017-10-07 14:36:23 +00:00
|
|
|
|
"collapsed": false
|
2017-06-02 13:44:28 +00:00
|
|
|
|
},
|
|
|
|
|
"outputs": [],
|
|
|
|
|
"source": [
|
|
|
|
|
"# 在引擎中创建策略对象\n",
|
|
|
|
|
"d = {'atrLength': 11} # 策略参数配置\n",
|
2017-12-04 07:37:15 +00:00
|
|
|
|
"#engine.initStrategy(AtrRsiStrategy, d) # 创建策略对象\n",
|
|
|
|
|
"engine.initStrategy(MultiTimeframeStrategy, d) # 创建策略对象"
|
2017-06-02 13:44:28 +00:00
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
2017-10-07 15:25:59 +00:00
|
|
|
|
"execution_count": null,
|
2017-06-02 13:44:28 +00:00
|
|
|
|
"metadata": {
|
|
|
|
|
"collapsed": false
|
|
|
|
|
},
|
2017-10-07 15:25:59 +00:00
|
|
|
|
"outputs": [],
|
2017-06-02 13:44:28 +00:00
|
|
|
|
"source": [
|
|
|
|
|
"# 运行回测\n",
|
|
|
|
|
"engine.runBacktesting() # 运行回测"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
2017-10-07 15:25:59 +00:00
|
|
|
|
"execution_count": null,
|
2017-06-02 13:44:28 +00:00
|
|
|
|
"metadata": {
|
|
|
|
|
"collapsed": false
|
|
|
|
|
},
|
2017-10-07 15:25:59 +00:00
|
|
|
|
"outputs": [],
|
2017-06-02 13:44:28 +00:00
|
|
|
|
"source": [
|
2017-07-14 12:12:42 +00:00
|
|
|
|
"# 显示逐日回测结果\n",
|
2017-10-07 15:25:59 +00:00
|
|
|
|
"engine.showDailyResult()"
|
2017-06-02 13:44:28 +00:00
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
2017-10-07 14:36:23 +00:00
|
|
|
|
"execution_count": null,
|
2017-06-02 13:44:28 +00:00
|
|
|
|
"metadata": {
|
|
|
|
|
"collapsed": false
|
|
|
|
|
},
|
2017-10-07 14:36:23 +00:00
|
|
|
|
"outputs": [],
|
2017-07-14 12:12:42 +00:00
|
|
|
|
"source": [
|
|
|
|
|
"# 显示逐笔回测结果\n",
|
|
|
|
|
"engine.showBacktestingResult()"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
2017-10-07 14:36:23 +00:00
|
|
|
|
"execution_count": null,
|
2017-07-14 12:12:42 +00:00
|
|
|
|
"metadata": {
|
|
|
|
|
"collapsed": false
|
|
|
|
|
},
|
2017-10-07 14:36:23 +00:00
|
|
|
|
"outputs": [],
|
2017-06-02 13:44:28 +00:00
|
|
|
|
"source": [
|
|
|
|
|
"# 显示前10条成交记录\n",
|
|
|
|
|
"for i in range(10):\n",
|
2017-09-25 05:51:24 +00:00
|
|
|
|
" d = engine.tradeDict[str(i+1)].__dict__\n",
|
|
|
|
|
" print 'TradeID: %s, Time: %s, Direction: %s, Price: %s, Volume: %s' %(d['tradeID'], d['dt'], d['direction'], d['price'], d['volume'])"
|
2017-06-02 13:44:28 +00:00
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"execution_count": null,
|
|
|
|
|
"metadata": {
|
2017-07-14 12:12:42 +00:00
|
|
|
|
"collapsed": false
|
2017-06-02 13:44:28 +00:00
|
|
|
|
},
|
|
|
|
|
"outputs": [],
|
|
|
|
|
"source": [
|
|
|
|
|
"# 优化配置\n",
|
|
|
|
|
"setting = OptimizationSetting() # 新建一个优化任务设置对象\n",
|
|
|
|
|
"setting.setOptimizeTarget('capital') # 设置优化排序的目标是策略净盈利\n",
|
|
|
|
|
"setting.addParameter('atrLength', 12, 20, 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",
|
2017-09-25 05:51:24 +00:00
|
|
|
|
"start = time.time()\n",
|
2017-06-02 13:44:28 +00:00
|
|
|
|
"engine.runParallelOptimization(AtrRsiStrategy, setting)\n",
|
|
|
|
|
"print u'耗时:%s' %(time.time()-start)"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"execution_count": null,
|
|
|
|
|
"metadata": {
|
|
|
|
|
"collapsed": true
|
|
|
|
|
},
|
|
|
|
|
"outputs": [],
|
|
|
|
|
"source": []
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
"metadata": {
|
|
|
|
|
"kernelspec": {
|
|
|
|
|
"display_name": "Python 2",
|
|
|
|
|
"language": "python",
|
|
|
|
|
"name": "python2"
|
|
|
|
|
},
|
|
|
|
|
"language_info": {
|
|
|
|
|
"codemirror_mode": {
|
|
|
|
|
"name": "ipython",
|
|
|
|
|
"version": 2
|
|
|
|
|
},
|
|
|
|
|
"file_extension": ".py",
|
|
|
|
|
"mimetype": "text/x-python",
|
|
|
|
|
"name": "python",
|
|
|
|
|
"nbconvert_exporter": "python",
|
|
|
|
|
"pygments_lexer": "ipython2",
|
|
|
|
|
"version": "2.7.13"
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
"nbformat": 4,
|
|
|
|
|
"nbformat_minor": 0
|
|
|
|
|
}
|