2017-10-07 15:25:59 +00:00
|
|
|
|
{
|
|
|
|
|
"cells": [
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"execution_count": 1,
|
|
|
|
|
"metadata": {
|
|
|
|
|
"collapsed": true
|
|
|
|
|
},
|
|
|
|
|
"outputs": [],
|
|
|
|
|
"source": [
|
|
|
|
|
"%matplotlib inline\n",
|
|
|
|
|
"\n",
|
|
|
|
|
"from vnpy.trader.app.ctaStrategy.ctaBacktesting import BacktestingEngine, MINUTE_DB_NAME\n",
|
|
|
|
|
"\n",
|
|
|
|
|
"def runBacktesting(strategyClass, settingDict, symbol, \n",
|
|
|
|
|
" startDate, endDate, slippage, \n",
|
|
|
|
|
" rate, size, priceTick):\n",
|
|
|
|
|
" \"\"\"运行单标的回测\"\"\"\n",
|
|
|
|
|
" engine = BacktestingEngine()\n",
|
|
|
|
|
" engine.setBacktestingMode(engine.BAR_MODE)\n",
|
|
|
|
|
" engine.setDatabase(MINUTE_DB_NAME, symbol)\n",
|
|
|
|
|
" engine.setStartDate(startDate)\n",
|
|
|
|
|
" engine.setEndDate(endDate)\n",
|
|
|
|
|
" engine.setSlippage(slippage)\n",
|
|
|
|
|
" engine.setRate(rate) \n",
|
|
|
|
|
" engine.setSize(size) \n",
|
|
|
|
|
" engine.setPriceTick(priceTick)\n",
|
|
|
|
|
" \n",
|
|
|
|
|
" engine.initStrategy(strategyClass, settingDict)\n",
|
|
|
|
|
" engine.runBacktesting()\n",
|
|
|
|
|
" df = engine.calculateDailyResult()\n",
|
|
|
|
|
" return df"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"execution_count": 2,
|
|
|
|
|
"metadata": {
|
|
|
|
|
"collapsed": false
|
|
|
|
|
},
|
|
|
|
|
"outputs": [
|
|
|
|
|
{
|
|
|
|
|
"name": "stdout",
|
|
|
|
|
"output_type": "stream",
|
|
|
|
|
"text": [
|
2017-11-30 01:44:09 +00:00
|
|
|
|
"2017-11-30 08:58:50.792000\t开始载入数据\n",
|
|
|
|
|
"2017-11-30 08:58:50.948000\t载入完成,数据量:348690\n",
|
|
|
|
|
"2017-11-30 08:58:50.948000\t开始回测\n",
|
|
|
|
|
"2017-11-30 08:58:50.989000\t策略初始化完成\n",
|
|
|
|
|
"2017-11-30 08:58:50.989000\t策略启动完成\n",
|
|
|
|
|
"2017-11-30 08:58:50.989000\t开始回放数据\n",
|
|
|
|
|
"2017-11-30 08:59:21.510000\t数据回放结束\n",
|
|
|
|
|
"2017-11-30 08:59:21.510000\t计算按日统计结果\n"
|
2017-10-07 15:25:59 +00:00
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
"source": [
|
|
|
|
|
"# 运行IF回测,交易1手\n",
|
|
|
|
|
"from vnpy.trader.app.ctaStrategy.strategy.strategyAtrRsi import AtrRsiStrategy\n",
|
|
|
|
|
"df1 = runBacktesting(AtrRsiStrategy, {}, 'IF0000', \n",
|
|
|
|
|
" '20120101', '20170630', 0.2, \n",
|
|
|
|
|
" 0.3/10000, 300, 0.2)"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"execution_count": 3,
|
|
|
|
|
"metadata": {
|
|
|
|
|
"collapsed": false
|
|
|
|
|
},
|
|
|
|
|
"outputs": [
|
|
|
|
|
{
|
|
|
|
|
"name": "stdout",
|
|
|
|
|
"output_type": "stream",
|
|
|
|
|
"text": [
|
2017-11-30 01:44:09 +00:00
|
|
|
|
"2017-11-30 08:59:21.585000\t开始载入数据\n",
|
|
|
|
|
"2017-11-30 08:59:21.745000\t载入完成,数据量:370838\n",
|
|
|
|
|
"2017-11-30 08:59:21.745000\t开始回测\n",
|
|
|
|
|
"2017-11-30 08:59:21.754000\t策略初始化完成\n",
|
|
|
|
|
"2017-11-30 08:59:21.754000\t策略启动完成\n",
|
|
|
|
|
"2017-11-30 08:59:21.754000\t开始回放数据\n",
|
|
|
|
|
"2017-11-30 08:59:38.014000\t数据回放结束\n",
|
|
|
|
|
"2017-11-30 08:59:38.014000\t计算按日统计结果\n"
|
2017-10-07 15:25:59 +00:00
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
"source": [
|
|
|
|
|
"# 运行rb回测,交易16手\n",
|
|
|
|
|
"from vnpy.trader.app.ctaStrategy.strategy.strategyBollChannel import BollChannelStrategy\n",
|
|
|
|
|
"settingDict = {'fixedSize': 16}\n",
|
|
|
|
|
"df2 = runBacktesting(BollChannelStrategy, settingDict, 'rb0000', \n",
|
|
|
|
|
" '20120101', '20170630', 1, \n",
|
|
|
|
|
" 1/10000, 10, 1)"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
2017-11-30 01:44:09 +00:00
|
|
|
|
"execution_count": 6,
|
2017-10-07 15:25:59 +00:00
|
|
|
|
"metadata": {
|
|
|
|
|
"collapsed": false
|
|
|
|
|
},
|
|
|
|
|
"outputs": [
|
|
|
|
|
{
|
|
|
|
|
"name": "stdout",
|
|
|
|
|
"output_type": "stream",
|
|
|
|
|
"text": [
|
2017-11-30 01:44:09 +00:00
|
|
|
|
"2017-11-30 09:42:39.063000\t------------------------------\n",
|
|
|
|
|
"2017-11-30 09:42:39.064000\t首个交易日:\t2012-01-11\n",
|
|
|
|
|
"2017-11-30 09:42:39.064000\t最后交易日:\t2017-06-30\n",
|
|
|
|
|
"2017-11-30 09:42:39.064000\t总交易日:\t1328\n",
|
|
|
|
|
"2017-11-30 09:42:39.064000\t盈利交易日\t676\n",
|
|
|
|
|
"2017-11-30 09:42:39.064000\t亏损交易日:\t652\n",
|
|
|
|
|
"2017-11-30 09:42:39.064000\t起始资金:\t1000000\n",
|
|
|
|
|
"2017-11-30 09:42:39.064000\t结束资金:\t2,446,304.82\n",
|
|
|
|
|
"2017-11-30 09:42:39.064000\t总收益率:\t144.63\n",
|
|
|
|
|
"2017-11-30 09:42:39.064000\t总盈亏:\t1,446,304.82\n",
|
|
|
|
|
"2017-11-30 09:42:39.064000\t最大回撤: \t-155,832.39\n",
|
|
|
|
|
"2017-11-30 09:42:39.064000\t总手续费:\t216,395.18\n",
|
|
|
|
|
"2017-11-30 09:42:39.064000\t总滑点:\t555,500.0\n",
|
|
|
|
|
"2017-11-30 09:42:39.064000\t总成交金额:\t7,517,158,420.0\n",
|
|
|
|
|
"2017-11-30 09:42:39.064000\t总成交笔数:\t8,160.0\n",
|
|
|
|
|
"2017-11-30 09:42:39.064000\t日均盈亏:\t1,089.08\n",
|
|
|
|
|
"2017-11-30 09:42:39.064000\t日均手续费:\t162.95\n",
|
|
|
|
|
"2017-11-30 09:42:39.064000\t日均滑点:\t418.3\n",
|
|
|
|
|
"2017-11-30 09:42:39.064000\t日均成交金额:\t5,660,510.86\n",
|
|
|
|
|
"2017-11-30 09:42:39.064000\t日均成交笔数:\t6.14\n",
|
|
|
|
|
"2017-11-30 09:42:39.064000\t日均收益率:\t0.07%\n",
|
|
|
|
|
"2017-11-30 09:42:39.064000\t收益标准差:\t0.97%\n",
|
|
|
|
|
"2017-11-30 09:42:39.064000\tSharpe Ratio:\t1.09\n"
|
2017-10-07 15:25:59 +00:00
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"data": {
|
2017-11-30 01:44:09 +00:00
|
|
|
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAoEAAAOlCAYAAAASGT0sAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3Xlc1NX++PEXAuPCgICmyaVEc62gBFITxeXmNXPD/LmA\nUi7X1KIss9A0wt1M1G8XLI3b5kKiaWrX7m0V0rQUVOwqWZnKFUwRVGbUAZ3z++PIIK6I4Iz4fj4e\nPj6fOXM+nzmfOaJvzuqklFIIIYQQQog7SjV7F0AIIYQQQtx6EgQKIYQQQtyBJAgUQgghhLgDSRAo\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K\nKaVWrFih+vfvr+bMmaPWrVunlFKqqKhI9erVS3355ZdKKaWaN2+u8vPz1b///W81ffp0271iYmLU\ntGnTlFJKDRkyRL344otKKaVMJpPq0KGD+vHHH1Vubq4KDg5WmZmZSimlvvzySzVy5Eh18uRJ1a1b\nN3X48GGllFJ//vmn6tixo8rJySnXcxXfRzgWqRfHJPXimKReHFdZ62baNKV0Z61SVmtJelqaUiNG\nKLV7t1IdO5bk+eOPGy9L8bXe3koFBOjz+vWVOn1av//ss0oZDEo9+KBSR46UvtbeccsdOybwwQd1\nc21l/XnwwbKXpXnz5jx04VeSvn378vPPPzNmzBi8vLxITEwkNjaWY8eOYTabS13XrVs3+vbty9Kl\nS5kxYwY//fQTp0+ftr3fuXNnANzc3GjYsCEnT54kPT2dZs2a0bx5cwC6du3K4sWL2bFjB8eOHeO5\n554jLCyMZ555hmrVqvF
|
2017-10-07 15:25:59 +00:00
|
|
|
|
"text/plain": [
|
2017-11-30 01:44:09 +00:00
|
|
|
|
"<matplotlib.figure.Figure at 0x153df130>"
|
2017-10-07 15:25:59 +00:00
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
"metadata": {},
|
|
|
|
|
"output_type": "display_data"
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
"source": [
|
|
|
|
|
"# 合并获得组合回测结果\n",
|
|
|
|
|
"dfp = df1 + df2\n",
|
|
|
|
|
"\n",
|
|
|
|
|
"# 注意如果被抛弃的交易日位于回测的前后,即两者不重合的日期中,则不会影响组合曲线正确性\n",
|
|
|
|
|
"# 但是如果被抛弃的交易日位于回测的中部,即两者重合的日期中,组合曲线会出现错误(丢失交易日)\n",
|
|
|
|
|
"dfp = dfp.dropna() \n",
|
|
|
|
|
"\n",
|
|
|
|
|
"# 创建回测引擎,并设置组合回测初始资金后,显示结果\n",
|
|
|
|
|
"engine = BacktestingEngine()\n",
|
|
|
|
|
"engine.setCapital(1000000)\n",
|
2017-11-30 01:44:09 +00:00
|
|
|
|
"dfp, result = engine.calculateDailyStatistics(dfp)\n",
|
|
|
|
|
"engine.showDailyResult(dfp, result)"
|
2017-10-07 15:25:59 +00:00
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"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
|
|
|
|
|
}
|