vnpy/examples/CtaBacktesting/backtesting_IF.ipynb

255 lines
113 KiB
Plaintext
Raw Normal View History

{
"cells": [
{
"cell_type": "code",
2018-01-11 08:03:42 +00:00
"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\n",
"from vnpy.trader.app.ctaStrategy.strategy.strategyMultiSignal import MultiSignalStrategy"
]
},
{
"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('20130101') # 设置回测用的数据起始日期"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"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",
2018-01-11 08:03:42 +00:00
"engine.initStrategy(AtrRsiStrategy, d) # 创建策略对象\n",
"#ngine.initStrategy(MultiTimeframeStrategy, d) \n",
2018-01-11 08:03:42 +00:00
"#engine.initStrategy(MultiSignalStrategy, {}) "
]
},
{
"cell_type": "code",
2018-01-11 08:03:42 +00:00
"execution_count": 6,
"metadata": {
"collapsed": false
},
2018-01-11 08:03:42 +00:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2018-01-09 10:00:06.236000\t开始载入数据\n",
"2018-01-09 10:00:06.408000\t载入完成数据量285480\n",
"2018-01-09 10:00:06.408000\t开始回测\n",
"2018-01-09 10:00:06.457000\t策略初始化完成\n",
"2018-01-09 10:00:06.458000\t策略启动完成\n",
"2018-01-09 10:00:06.458000\t开始回放数据\n",
"2018-01-09 10:00:33.335000\t数据回放结束\n"
]
}
],
"source": [
"# 运行回测\n",
"engine.runBacktesting() # 运行回测"
]
},
{
"cell_type": "code",
2018-01-11 08:03:42 +00:00
"execution_count": 7,
"metadata": {
2018-01-11 08:03:42 +00:00
"collapsed": false,
"scrolled": true
},
2018-01-11 08:03:42 +00:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2018-01-09 10:00:33.341000\t计算按日统计结果\n",
"2018-01-09 10:00:33.425000\t------------------------------\n",
"2018-01-09 10:00:33.425000\t首个交易日\t2013-01-11\n",
"2018-01-09 10:00:33.425000\t最后交易日\t2017-07-14\n",
"2018-01-09 10:00:33.425000\t总交易日\t1095\n",
"2018-01-09 10:00:33.425000\t盈利交易日\t541\n",
"2018-01-09 10:00:33.425000\t亏损交易日\t554\n",
"2018-01-09 10:00:33.425000\t起始资金\t1000000\n",
"2018-01-09 10:00:33.425000\t结束资金\t1,696,649.0\n",
"2018-01-09 10:00:33.425000\t总收益率\t69.66%\n",
"2018-01-09 10:00:33.425000\t年化收益\t15.27%\n",
"2018-01-09 10:00:33.425000\t总盈亏\t696,649.0\n",
"2018-01-09 10:00:33.425000\t最大回撤: \t-185,949.45\n",
"2018-01-09 10:00:33.425000\t百分比最大回撤: \t-10.7%\n",
"2018-01-09 10:00:33.425000\t总手续费\t201,671.0\n",
"2018-01-09 10:00:33.425000\t总滑点\t406,020.0\n",
"2018-01-09 10:00:33.425000\t总成交金额\t6,722,366,580.0\n",
"2018-01-09 10:00:33.425000\t总成交笔数\t6,767.0\n",
"2018-01-09 10:00:33.425000\t日均盈亏\t636.21\n",
"2018-01-09 10:00:33.425000\t日均手续费\t184.17\n",
"2018-01-09 10:00:33.425000\t日均滑点\t370.79\n",
"2018-01-09 10:00:33.425000\t日均成交金额\t6,139,147.56\n",
"2018-01-09 10:00:33.425000\t日均成交笔数\t6.18\n",
"2018-01-09 10:00:33.425000\t日均收益率\t0.05%\n",
"2018-01-09 10:00:33.425000\t收益标准差\t1.03%\n",
"2018-01-09 10:00:33.425000\tSharpe Ratio\t0.72\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAoEAAAOlCAYAAAASGT0sAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3XlcVPX+x/EXi7gwbO4iKe5L4gaZipp6teWWqZkLpP1M\n27yalZnU1RStbHNpESuj5eaubWZp17wppmUppliKlbuCC4LKgAI68/vj42EGRAVEZmA+z8fDx8wc\nzpw5X7B88/lublar1YpSSimllHIp7o6+AaWUUkopVfo0BCqllFJKuSANgUoppZRSLkhDoFJKKaWU\nC9IQqJRSSinlgjQEKqWUUkq5IE9H34BSSpWko0eP0rt3b5o1a4bVauXixYtUqVKFqKgo2rdvf8X3\nPf/88zRt2pSHHnqoFO9WKaUcR0OgUqrcqVSpEl9++WXu69WrV/P888/z3//+14F3pZRSzkVDoFKq\n3EtLS6NmzZoAvPTSS+zcuZOMjAysVisvvfQS7dq1y3P+Z599xrJly7hw4QKnT5/m0UcfZciQIXz5\n5Zd8//33uLu7c/DgQSpUqMDrr79O48aNSUlJYcqUKezbtw8PDw8GDx7MsGHDMJvNvPzyy/z5559c\nuHCBTp06MWHCBNzddTSOUsqxNAQqpcqd8+fP079/f6xWK2fPnuXkyZPMnTuX7du3k5KSwtKlSwGY\nN28e8+bN49133819b2ZmJp999hkffPABfn5+7Nixg4ceeoghQ4YAsHXrVr755htq1qzJSy+9xIcf\nfsgrr7xCdHQ0DRo0ICYmBrPZTEREBN27d+fdd9+lVatWvPLKK1gsFp577jk++ugjHn74YYd8b5RS\nyqAhUClV7uTvDv7tt9945JFHWLFiBU8++SSLFy/m0KFD/Prrr5hMpjzvrVKlCu+99x7r1q3j4MGD\n7N69m3PnzuV+/eabb86tKrZs2ZLvv/8egJ9//pmoqCgATCYTK1euBGD9+vXs3LmT5cuXA5CVlYWb\nm9uNa7xSShWShkClVLnXrl07GjRowJYtW5g7dy4jRoygV69eNGzYMDesGY4fP87gwYMZPHgwYWFh\n3HHHHcTFxeV+vWLFirnP3dzcMLZf9/TM+7/Tw4cPExAQgMVi4a233qJhw4YAmM3mG9VMpZQqEh2U\nopQqd4xgZti/fz8HDhxg9erV9OzZkyFDhtCqVSv+97//YbFY8py7c+dOqlatyqhRowgPD2fdunUF\nXjO/zp0788UXXwCQnp7O8OHDOXToEF26dOGTTz4BIDs7m8cff5yFCxeWUEuVUqr4tBKolCp3srOz\n6d+/PyDhzZgA0rRpU5555hn69u2Lh4cHYWFhrFmzJs97u3btyueff84dd9yBt7c3ISEhVK1alYMH\nD171M1944QWio6O59957sVqtPP7447Rs2ZKJEycyffp0+vTpw4ULFwgPD9fxgEopp+Bmvdavt0op\npZRSqtzR7mCllFJKKRekIVAppZRSygVpCFRKKaWUckEaApVSSimlXFC5mh0cHx/v6FtQSimllCq0\n0NBQh312uQqB4NhvZmlJSkoiMDDQ0bdR6rTdrsUV2+2KbTa4atu13a6joDY7unil3cFKKaWUUi5I\nQ6BSSimllAvSEKiUUkop5YI0BCqllFJKuSANgUoppZRSLkhDoFJKKaWUC9IQqJRSSinlgjQEKqWU\nUkq5IA2BSimllFIuSEOgg/3666/06dOnSO/p2bMnp0+fvkF3pJRSSilXoCGwDHJzc3P0LSillFKq\njCt3eweXRRkZGYwdO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Mk8mEl5cXhw8fJigoiI0bNzJm\nzBg8PDyYMWMGI0aMIDk
"text/plain": [
"<matplotlib.figure.Figure at 0xb085ab0>"
]
},
"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",
2018-01-11 08:03:42 +00:00
"execution_count": null,
"metadata": {
"collapsed": false
},
2018-01-11 08:03:42 +00:00
"outputs": [],
"source": [
"# 优化配置\n",
"setting = OptimizationSetting() # 新建一个优化任务设置对象\n",
"setting.setOptimizeTarget('totalNetPnl') # 设置优化排序的目标是策略净盈利\n",
"setting.addParameter('atrLength', 12, 16, 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",
"resultList = engine.runParallelOptimization(AtrRsiStrategy, setting)\n",
"print u'耗时:%s' %(time.time()-start)"
]
},
{
"cell_type": "code",
2018-01-11 08:03:42 +00:00
"execution_count": null,
"metadata": {
"collapsed": false
},
2018-01-11 08:03:42 +00:00
"outputs": [],
"source": [
"# 显示优化的所有统计数据\n",
"for result in resultList:\n",
" print '-' * 30\n",
" print u'参数:%s目标%s' %(result[0], result[1])\n",
" print u'统计数据:'\n",
" for k, v in result[2].items():\n",
" print u'%s%s' %(k, v)"
]
},
{
"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
}