vnpy/examples/CtaBacktesting/backtesting_IF.ipynb

233 lines
102 KiB
Plaintext
Raw Normal View History

{
"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\n",
"from vnpy.trader.app.ctaStrategy.strategy.strategyMultiTimeframe import MultiTimeframeStrategy"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# 创建回测引擎对象\n",
"engine = BacktestingEngine()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# 设置回测使用的数据\n",
"engine.setBacktestingMode(engine.BAR_MODE) # 设置引擎的回测模式为K线\n",
"engine.setDatabase(MINUTE_DB_NAME, 'IF0000') # 设置使用的历史数据库\n",
"engine.setStartDate('20120101') # 设置回测用的数据起始日期"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# 配置回测引擎参数\n",
"engine.setSlippage(0.2) # 设置滑点为股指1跳\n",
"engine.setRate(0.3/10000) # 设置手续费万0.3\n",
"engine.setSize(300) # 设置股指合约大小 \n",
"engine.setPriceTick(0.2) # 设置股指最小价格变动 \n",
"engine.setCapital(1000000) # 设置回测本金"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# 在引擎中创建策略对象\n",
"d = {'atrLength': 11} # 策略参数配置\n",
"#engine.initStrategy(AtrRsiStrategy, d) # 创建策略对象\n",
"engine.initStrategy(MultiTimeframeStrategy, d) # 创建策略对象"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2017-12-04 15:35:51.714000\t开始载入数据\n",
"2017-12-04 15:35:52.312000\t载入完成数据量351090\n",
"2017-12-04 15:35:52.312000\t开始回测\n",
"2017-12-04 15:35:52.333000\t策略初始化完成\n",
"2017-12-04 15:35:52.333000\t策略启动完成\n",
"2017-12-04 15:35:52.334000\t开始回放数据\n",
"2017-12-04 15:36:11.673000\t数据回放结束\n"
]
}
],
"source": [
"# 运行回测\n",
"engine.runBacktesting() # 运行回测"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2017-12-04 15:36:11.682000\t计算按日统计结果\n",
"2017-12-04 15:36:11.743000\t------------------------------\n",
"2017-12-04 15:36:11.743000\t首个交易日\t2012-01-11\n",
"2017-12-04 15:36:11.743000\t最后交易日\t2017-07-14\n",
"2017-12-04 15:36:11.743000\t总交易日\t1338\n",
"2017-12-04 15:36:11.743000\t盈利交易日\t442\n",
"2017-12-04 15:36:11.743000\t亏损交易日\t632\n",
"2017-12-04 15:36:11.743000\t起始资金\t1000000\n",
"2017-12-04 15:36:11.743000\t结束资金\t1,415,037.22\n",
"2017-12-04 15:36:11.743000\t总收益率\t41.5\n",
"2017-12-04 15:36:11.743000\t总盈亏\t415,037.22\n",
"2017-12-04 15:36:11.743000\t最大回撤: \t-292,237.65\n",
"2017-12-04 15:36:11.743000\t总手续费\t49,722.78\n",
"2017-12-04 15:36:11.743000\t总滑点\t113,580.0\n",
"2017-12-04 15:36:11.743000\t总成交金额\t1,657,426,020.0\n",
"2017-12-04 15:36:11.743000\t总成交笔数\t1,893.0\n",
"2017-12-04 15:36:11.743000\t日均盈亏\t310.19\n",
"2017-12-04 15:36:11.743000\t日均手续费\t37.16\n",
"2017-12-04 15:36:11.743000\t日均滑点\t84.89\n",
"2017-12-04 15:36:11.743000\t日均成交金额\t1,238,733.95\n",
"2017-12-04 15:36:11.743000\t日均成交笔数\t1.41\n",
"2017-12-04 15:36:11.743000\t日均收益率\t0.03%\n",
"2017-12-04 15:36:11.743000\t收益标准差\t0.87%\n",
"2017-12-04 15:36:11.743000\tSharpe Ratio\t0.46\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAoEAAAOlCAYAAAASGT0sAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3XlcVPX+x/EXiyj7opkSuYtbaAjZgpJl3q6laXldQC3T\nSk2zm3kly0xtsWuidUO7GdW9P3dss83KSiEt64oppaKpuSSoEKCAyDbn98fHwwwICgiyfZ6Ph4+Z\nOXPmzJk5om++y+drZxiGgVJKKaWUalDsa/oElFJKKaXUlachUCmllFKqAdIQqJRSSinVAGkIVEop\npZRqgDQEKqWUUko1QBoClVJKKaUaIMeaPgGllKpKx48fp3///nTq1AnDMCgsLMTFxYWIiAh69uxZ\n5utmzpyJv78/Dz744BU8W6WUqjkaApVS9U6TJk348MMPix5v2LCBmTNn8uWXX9bgWSmlVO2iIVAp\nVe+lp6fTvHlzAF544QV++eUXsrOzMQyDF154gcDAwGL7v/fee8TExFBQUEBGRgaPPPIII0eO5MMP\nP2Tjxo3Y29tz5MgRGjVqxIIFC+jQoQOpqak899xzHDp0CAcHB0aMGMGYMWPIysrixRdfZP/+/RQU\nFHDzzTczY8YM7O11NI5SqmZpCFRK1Tvnzp3j3nvvxTAMzpw5Q0pKCkuXLmXnzp2kpqaydu1aAJYt\nW8ayZct44403il579uxZ3nvvPd566y08PT3ZtWsXDz74ICNHjgRg+/btfPrppzRv3pwXXniBt99+\nm/nz5zNnzhzatm3LkiVLyMrKIiwsjL59+/LGG29w3XXXMX/+fCwWC0899RTvvPMODz30UI18N0op\nZdIQqJSqd0p2B//88888/PDDrF+/nscff5zVq1dz9OhRfvrpJ9zc3Iq91sXFhX//+99s2rSJI0eO\nsHfvXnJycoqe79atW1GrYteuXdm4cSMAP/zwAxEREQC4ubnxySefALB582Z++eUX1q1bB0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Gv/5avn07depEjx49ALj33nuZM2cO77zzDjt37iQ6OprDhw+TkpJCdnZ2sdfd\neeedXHvttaxYsYIjR47w008/ERgYWPT8bbfdBoCrqyutW7fm9OnT7NixA39/fzp16gRA//796d+/\nP7GxsaSkpDB58uSilkZ7e3v27dunIVAppZRS9Ud5A9qVYG9ffDqDnZ0dzzzzDIZhMGDAAG677TaS\nk5OLPQ+watUq1q1bx+jRoxk0aBCenp4cP368aL8mTZoUO65hGDg6XhiT9u3bh8VioUOHDqxdu7Zo\n+6lTp2jatGmVfMYrRSeGKKWUUqrOSExMJDExEYA1a9YQFBTEDz/8wOTJkxkwYACGYbBr1y4KCwsB\nilrqtm7dyn333cfQoUNp06YNmzZtwmKxXPS9evTowaFDhzh48CAAX3/9NTNmzOD666/n8OHDbN++\nHYC9e/dy5513curUqer62NVCWwKVUkopVWe0b9+eJUuWcPToUZo1a8Y///lPNm/ezOTJk/Hy8sLZ\n2ZlevXpx9OhRwNoSOG7cOGbPns0HH3yAvb093bp1Y//+/cX2MZmPmzZtysKFC5kxYwYWiwU3NzcW\nL16Mt7c3r7/+OgsWLCA3NxfDMHjllVdo2bLlFfwmLp+dYUbkeiA+Pp6goKCaPg0FJCUl4evrW9On\noWzoNal99JrUPnpNaqf6el1qOrdod7BSSimlVAOkIVAppZRSqgHSEKiUUkop1QCVOwTu2rWLMWPG\nADILJjQ0lPvvv5/777+fDRs2ABATE8PQoUMZOXIkmzdvBiA3N5epU6cyatQoJkyYQHp6OgA7d+5k\n+PDhhIeHExUVVfQ+UVF
"text/plain": [
"<matplotlib.figure.Figure at 0xece8650>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# 显示逐日回测结果\n",
"engine.showDailyResult()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# 显示逐笔回测结果\n",
"engine.showBacktestingResult()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# 显示前10条成交记录\n",
"for i in range(10):\n",
" 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'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"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",
"start = time.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
}