2017-06-02 13:44:28 +00:00
|
|
|
|
{
|
|
|
|
|
"cells": [
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"execution_count": 1,
|
|
|
|
|
"metadata": {
|
|
|
|
|
"collapsed": false
|
|
|
|
|
},
|
|
|
|
|
"outputs": [],
|
|
|
|
|
"source": [
|
|
|
|
|
"%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"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"execution_count": 2,
|
|
|
|
|
"metadata": {
|
|
|
|
|
"collapsed": true
|
|
|
|
|
},
|
|
|
|
|
"outputs": [],
|
|
|
|
|
"source": [
|
|
|
|
|
"# 创建回测引擎对象\n",
|
|
|
|
|
"engine = BacktestingEngine()"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
2017-07-14 12:27:34 +00:00
|
|
|
|
"execution_count": 4,
|
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-07-14 12:27:34 +00:00
|
|
|
|
"engine.setStartDate('20120101') # 设置回测用的数据起始日期"
|
2017-06-02 13:44:28 +00:00
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
2017-07-14 12:27:34 +00:00
|
|
|
|
"execution_count": 5,
|
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-07-14 12:27:34 +00:00
|
|
|
|
"execution_count": 6,
|
2017-06-02 13:44:28 +00:00
|
|
|
|
"metadata": {
|
|
|
|
|
"collapsed": true
|
|
|
|
|
},
|
|
|
|
|
"outputs": [],
|
|
|
|
|
"source": [
|
|
|
|
|
"# 在引擎中创建策略对象\n",
|
|
|
|
|
"d = {'atrLength': 11} # 策略参数配置\n",
|
|
|
|
|
"engine.initStrategy(AtrRsiStrategy, d) # 创建策略对象"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
2017-07-14 12:27:34 +00:00
|
|
|
|
"execution_count": 7,
|
2017-06-02 13:44:28 +00:00
|
|
|
|
"metadata": {
|
|
|
|
|
"collapsed": false
|
|
|
|
|
},
|
|
|
|
|
"outputs": [
|
|
|
|
|
{
|
|
|
|
|
"name": "stdout",
|
|
|
|
|
"output_type": "stream",
|
|
|
|
|
"text": [
|
2017-07-14 12:27:34 +00:00
|
|
|
|
"2017-07-14 20:13:38.855000\t开始载入数据\n",
|
|
|
|
|
"2017-07-14 20:13:39.021000\t载入完成,数据量:331890\n",
|
|
|
|
|
"2017-07-14 20:13:39.021000\t开始回测\n",
|
|
|
|
|
"2017-07-14 20:13:39.041000\t策略初始化完成\n",
|
|
|
|
|
"2017-07-14 20:13:39.041000\t策略启动完成\n",
|
|
|
|
|
"2017-07-14 20:13:39.041000\t开始回放数据\n",
|
|
|
|
|
"2017-07-14 20:14:03.242000\t数据回放结束\n"
|
2017-06-02 13:44:28 +00:00
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
"source": [
|
|
|
|
|
"# 运行回测\n",
|
|
|
|
|
"engine.runBacktesting() # 运行回测"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
2017-07-14 12:27:34 +00:00
|
|
|
|
"execution_count": 8,
|
2017-06-02 13:44:28 +00:00
|
|
|
|
"metadata": {
|
|
|
|
|
"collapsed": false
|
|
|
|
|
},
|
|
|
|
|
"outputs": [
|
|
|
|
|
{
|
|
|
|
|
"name": "stdout",
|
|
|
|
|
"output_type": "stream",
|
|
|
|
|
"text": [
|
2017-07-14 12:27:34 +00:00
|
|
|
|
"2017-07-14 20:16:24.541000\t计算按日统计结果\n",
|
|
|
|
|
"2017-07-14 20:16:24.610000\t------------------------------\n",
|
|
|
|
|
"2017-07-14 20:16:24.610000\t首个交易日:\t2012-01-11\n",
|
|
|
|
|
"2017-07-14 20:16:24.610000\t最后交易日:\t2017-03-20\n",
|
|
|
|
|
"2017-07-14 20:16:24.610000\t总交易日:\t1259\n",
|
|
|
|
|
"2017-07-14 20:16:24.610000\t盈利交易日\t617\n",
|
|
|
|
|
"2017-07-14 20:16:24.610000\t亏损交易日:\t641\n",
|
|
|
|
|
"2017-07-14 20:16:24.610000\t起始资金:\t1000000\n",
|
|
|
|
|
"2017-07-14 20:16:24.610000\t结束资金:\t1,681,826.5\n",
|
|
|
|
|
"2017-07-14 20:16:24.610000\t总收益率:\t68.18\n",
|
|
|
|
|
"2017-07-14 20:16:24.610000\t总盈亏:\t681,826.5\n",
|
|
|
|
|
"2017-07-14 20:16:24.610000\t最大回撤: \t-185,949.45\n",
|
|
|
|
|
"2017-07-14 20:16:24.610000\t总手续费:\t216,553.5\n",
|
|
|
|
|
"2017-07-14 20:16:24.610000\t总滑点:\t449,940.0\n",
|
|
|
|
|
"2017-07-14 20:16:24.610000\t总成交金额:\t7,218,450,000.0\n",
|
|
|
|
|
"2017-07-14 20:16:24.610000\t总成交笔数:\t7,498.0\n",
|
|
|
|
|
"2017-07-14 20:16:24.610000\t日均盈亏:\t541.56\n",
|
|
|
|
|
"2017-07-14 20:16:24.611000\t日均手续费:\t172.0\n",
|
|
|
|
|
"2017-07-14 20:16:24.611000\t日均滑点:\t357.38\n",
|
|
|
|
|
"2017-07-14 20:16:24.611000\t日均成交金额:\t5,733,478.95\n",
|
|
|
|
|
"2017-07-14 20:16:24.611000\t日均成交笔数:\t5.96\n",
|
|
|
|
|
"2017-07-14 20:16:24.611000\t日均收益率:\t0.04%\n",
|
|
|
|
|
"2017-07-14 20:16:24.611000\t收益标准差:\t1.09%\n",
|
|
|
|
|
"2017-07-14 20:16:24.611000\tSharpe Ratio:\t0.6\n"
|
2017-06-02 13:44:28 +00:00
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"data": {
|
2017-07-14 12:27:34 +00:00
|
|
|
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAoEAAAOlCAYAAAASGT0sAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3Xt8jvX/wPHXjtgZa2zIKYdoxKbTEEVUhOQ0Vg5J4tcB\nWUVCByWnvqGvKJVTppSUnIotSthiwvCNHDan2cbujW3s+v3xdu/ekW223Tu8n4/HHtd9Xfd1X/fn\nvq+m996fz+f9sTEMw0AppZRSSlUottZugFJKKaWUKnkaBCqllFJKVUAaBCqllFJKVUAaBCqllFJK\nVUAaBCqllFJKVUAaBCqllFJKVUD21m6AUkoVpejoaDp37kyT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MTN327dt58skn6d27N/Xq1WPLli2kp6ff8L1atmzJ0aNH+eeffwDYvHkz\n48eP5+677+bff/9l9+7dABw8eJAuXbpw7ty54vrYSilVYc2ZA//+C1eu5P78yJHw5psFu2Z8PHz9\n9S03TVUgFTYTWJo0bNiQefPmceLECTw9Pfnggw/YunUro0aNwsPDgypVqnDPPfdw4sQJwJIJHDp0\nKJMmTWL16tXY2trSvHlzDh8+nOUcM/N+9erVmTFjBuPHjyc9PR0XFxdmz55N1apV+fjjj5k+fTop\nKSkYhsGHH36It7d3CX4TSilVMbzyimwzjwk0DOjZExo0APPon1OnoHbtnK/ftk3Kyzz+uOwnJ8t2\n7165jhaRUPlhY5jTSuV
|
2017-06-02 13:44:28 +00:00
|
|
|
|
"text/plain": [
|
2017-07-14 12:27:34 +00:00
|
|
|
|
"<matplotlib.figure.Figure at 0x1d7477b0>"
|
2017-06-02 13:44:28 +00:00
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
"metadata": {},
|
|
|
|
|
"output_type": "display_data"
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
"source": [
|
2017-07-14 12:12:42 +00:00
|
|
|
|
"# 显示逐日回测结果\n",
|
|
|
|
|
"df = engine.showDailyResult()"
|
2017-06-02 13:44:28 +00:00
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
2017-07-14 12:27:34 +00:00
|
|
|
|
"execution_count": null,
|
2017-06-02 13:44:28 +00:00
|
|
|
|
"metadata": {
|
|
|
|
|
"collapsed": false
|
|
|
|
|
},
|
2017-07-14 12:27:34 +00:00
|
|
|
|
"outputs": [],
|
2017-07-14 12:12:42 +00:00
|
|
|
|
"source": [
|
|
|
|
|
"# 显示逐笔回测结果\n",
|
|
|
|
|
"engine.showBacktestingResult()"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"execution_count": null,
|
|
|
|
|
"metadata": {
|
|
|
|
|
"collapsed": false
|
|
|
|
|
},
|
|
|
|
|
"outputs": [],
|
2017-06-02 13:44:28 +00:00
|
|
|
|
"source": [
|
|
|
|
|
"# 显示前10条成交记录\n",
|
|
|
|
|
"for i in range(10):\n",
|
|
|
|
|
" print engine.tradeDict[str(i+1)].__dict__"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
2017-07-14 01:05:11 +00:00
|
|
|
|
"execution_count": null,
|
2017-06-02 13:44:28 +00:00
|
|
|
|
"metadata": {
|
|
|
|
|
"collapsed": false
|
|
|
|
|
},
|
2017-07-14 01:05:11 +00:00
|
|
|
|
"outputs": [],
|
2017-06-02 13:44:28 +00:00
|
|
|
|
"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",
|
|
|
|
|
"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
|
|
|
|
|
}
|