vnpy/examples/CtaBacktesting/backtesting_portfolio.ipynb

189 lines
118 KiB
Plaintext
Raw Normal View History

{
"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-10-07 23:24:11.493000\t开始载入数据\n",
"2017-10-07 23:24:11.661000\t载入完成数据量348690\n",
"2017-10-07 23:24:11.661000\t开始回测\n",
"2017-10-07 23:24:11.703000\t策略初始化完成\n",
"2017-10-07 23:24:11.703000\t策略启动完成\n",
"2017-10-07 23:24:11.703000\t开始回放数据\n",
"2017-10-07 23:24:42.412000\t数据回放结束\n",
"2017-10-07 23:24:42.412000\t计算按日统计结果\n"
]
}
],
"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-10-07 23:24:43.174000\t开始载入数据\n",
"2017-10-07 23:24:43.340000\t载入完成数据量300224\n",
"2017-10-07 23:24:43.340000\t开始回测\n",
"2017-10-07 23:24:43.347000\t策略初始化完成\n",
"2017-10-07 23:24:43.347000\t策略启动完成\n",
"2017-10-07 23:24:43.347000\t开始回放数据\n",
"2017-10-07 23:24:58.453000\t数据回放结束\n",
"2017-10-07 23:24:58.453000\t计算按日统计结果\n"
]
}
],
"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",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2017-10-07 23:24:58.504000\t------------------------------\n",
"2017-10-07 23:24:58.504000\t首个交易日\t2012-01-11\n",
"2017-10-07 23:24:58.504000\t最后交易日\t2017-06-30\n",
"2017-10-07 23:24:58.504000\t总交易日\t1328\n",
"2017-10-07 23:24:58.504000\t盈利交易日\t679\n",
"2017-10-07 23:24:58.504000\t亏损交易日\t649\n",
"2017-10-07 23:24:58.504000\t起始资金\t1000000\n",
"2017-10-07 23:24:58.504000\t结束资金\t2,543,440.99\n",
"2017-10-07 23:24:58.504000\t总收益率\t154.34\n",
"2017-10-07 23:24:58.504000\t总盈亏\t1,543,440.99\n",
"2017-10-07 23:24:58.504000\t最大回撤: \t-155,832.39\n",
"2017-10-07 23:24:58.504000\t总手续费\t216,619.01\n",
"2017-10-07 23:24:58.505000\t总滑点\t535,820.0\n",
"2017-10-07 23:24:58.505000\t总成交金额\t7,476,566,620.0\n",
"2017-10-07 23:24:58.505000\t总成交笔数\t8,041.0\n",
"2017-10-07 23:24:58.505000\t日均盈亏\t1,162.23\n",
"2017-10-07 23:24:58.505000\t日均手续费\t163.12\n",
"2017-10-07 23:24:58.505000\t日均滑点\t403.48\n",
"2017-10-07 23:24:58.505000\t日均成交金额\t5,629,944.74\n",
"2017-10-07 23:24:58.505000\t日均成交笔数\t6.05\n",
"2017-10-07 23:24:58.505000\t日均收益率\t0.07%\n",
"2017-10-07 23:24:58.505000\t收益标准差\t0.96%\n",
"2017-10-07 23:24:58.505000\tSharpe Ratio\t1.15\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAoEAAAOlCAYAAAASGT0sAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3Xt8z3X/x/HHDuawL9soxbWyyClGbJUMqUiSC6kcIkWF\nmmQXRuQc6nK4uhqVS6WcciiX6uqAyhbywyRKy1UOuWxhDtl32MY+vz9e7eScsX1tz/vtttt338/n\n/fl835/v+8r12vvwens5juMgIiIiIsWKd2FXQEREREQKnoJAERERkWJIQaCIiIhIMaQgUERERKQY\nUhAoIiIiUgwpCBQREREphnwLuwIiIpfSnj17aNmyJTVr1sRxHE6ePEmZMmWIjo6mYcOGZ71u6NCh\n1KhRg8cff7wAaysiUngUBIpIkVOqVCmWLFmS/f7TTz9l6NChfP7554VYKxERz6IgUESKvEOHDlGx\nYkUAxo0bx5YtW0hNTcVxHMaNG0eDBg3ylF+8eDELFy7kxIkTHD58mKeeeorOnTuzZMkSli9fjre3\nN7t27aJEiRK8/PLL3HjjjSQnJzNy5Ei2b9+Oj48PnTp1onv37rjdbl588UW2bdvGiRMnuP322xk8\neDDe3pqNIyKFS0GgiBQ5x48fp0OHDjiOw5EjR9i/fz/Tp09n06ZNJCcns2DBAgBmzJjBjBkzeO21\n17KvPXr0KIsXL+Zf//oXAQEBfPfddzz++ON07twZgA0bNvDxxx9TsWJFxo0bx5tvvsmECRMYNWoU\nN9xwA9OmTcPtdtOlSxeaN2/Oa6+9Rt26dZkwYQK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9bXclHUEygiIiJX\njL179/Lwww/z73//m/vvv5/BgwezaNEiOnTowHvvvceyZcvYvXs3sbGxea6Li4ujXLlyvPfee3z2\n2WfUrVuXOXPmZJ/fvXs3c+bM4aOPPmLt2rWsW7eOAwcOMHjwYF566SWWLl1Kr169mDJlCkeOHOH5\n55/n73//Ox988AHTp09n1KhR/PbbbwX9deSLegJFRETknOrWhR9+uHz3r1MHvv/+wsrWrFmTm266\nCYAOHTowatQo3nrrLTZt2sTMmTPZuXMn+/fvJzU1Nc91rVq14rrrrmPOnDns2rWLdevW0aBBg+zz\nd955JwD+/v5UqVKF33//nY0bN1KjRg1q1qwJQMuWLWnZsiWxsbHs37+fZ555Jrun0dvbm59++olr\nr702v19HgVEQKCIiIud0oQFaQfD2zjuI6eXlxbBhw3Ach9atW3PnnXeSlJSU5zzAvHnzWLRoEd26\ndaNt27YEBASwZ8+e7HKlSpXKc1/HcfD1PT1M+umnn8jMzOTGG29kwYIF2cf37dtHhQoVLskzFhQN\nB4uIiMgVIyEhgV9++QWA9957j7CwML755hueeeYZWrdujeM4fPfdd5w8eRIgu6du9erVPPDAA3Ts\n2JGQkBC++uorMjMzz/lZ9evXZ/v27dmft2LFCgYPHszNN9/Mzp072bBhAwA//vgjrVq1Yt++fZfr\nsS8L9QSKiIjIFaNatWq88847TJo0iauuuoqXXnqJlStX8swzzxAYGEjp0qW59dZb+fXXX4GcnsCe\nPXsyYsQIPvjgA7y9valTpw7btm3LUyZL1vsKFSowadIkBg8eTGZmJi6Xi6lTpxIUFMSrr77Kyy+/\nTFpaGo7j8Pe//51KlSoV4DeRf15OVohcBMTHxxMWFlbY1RAgMTGRypUrF3Y15BRqF8+kdvFMahfP\nVVTaprDjFg0Hi4iIiBRDCgJFREREiiEFgSIiIiLFkIJAERERkWJIQaCIiIhIMaQgUERERKQYUhAo\nIiIiRV7RSYh36SgIFBE
"text/plain": [
"<matplotlib.figure.Figure at 0x1d4fe430>"
]
},
"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",
"engine.showDailyResult(dfp)"
]
},
{
"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
}