vnpy/examples/CtaBacktesting/backtesting.ipynb

277 lines
174 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"
]
},
{
"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": true
},
"outputs": [],
"source": [
"# 在引擎中创建策略对象\n",
"d = {'atrLength': 11} # 策略参数配置\n",
"engine.initStrategy(AtrRsiStrategy, d) # 创建策略对象"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2017-08-23 22:46:29.700000\t开始载入数据\n",
"2017-08-23 22:46:29.790000\t载入完成数据量31455\n",
"2017-08-23 22:46:29.790000\t开始回测\n",
"2017-08-23 22:46:29.809000\t策略初始化完成\n",
"2017-08-23 22:46:29.809000\t策略启动完成\n",
"2017-08-23 22:46:29.809000\t开始回放数据\n",
"2017-08-23 22:46:31.487000\t数据回放结束\n"
]
}
],
"source": [
"# 运行回测\n",
"engine.runBacktesting() # 运行回测"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2017-08-23 22:46:33.876000\t计算按日统计结果\n",
"2017-08-23 22:46:33.907000\t------------------------------\n",
"2017-08-23 22:46:33.907000\t首个交易日\t2012-01-11\n",
"2017-08-23 22:46:33.907000\t最后交易日\t2012-06-29\n",
"2017-08-23 22:46:33.907000\t总交易日\t112\n",
"2017-08-23 22:46:33.907000\t盈利交易日\t48\n",
"2017-08-23 22:46:33.907000\t亏损交易日\t64\n",
"2017-08-23 22:46:33.907000\t起始资金\t1000000\n",
"2017-08-23 22:46:33.907000\t结束资金\t879,365.29\n",
"2017-08-23 22:46:33.907000\t总收益率\t-12.06\n",
"2017-08-23 22:46:33.907000\t总盈亏\t-120,634.71\n",
"2017-08-23 22:46:33.907000\t最大回撤: \t-138,853.38\n",
"2017-08-23 22:46:33.907000\t总手续费\t9,934.71\n",
"2017-08-23 22:46:33.907000\t总滑点\t25,980.0\n",
"2017-08-23 22:46:33.907000\t总成交金额\t331,156,920.0\n",
"2017-08-23 22:46:33.907000\t总成交笔数\t433.0\n",
"2017-08-23 22:46:33.907000\t日均盈亏\t-1,077.1\n",
"2017-08-23 22:46:33.907000\t日均手续费\t88.7\n",
"2017-08-23 22:46:33.907000\t日均滑点\t231.96\n",
"2017-08-23 22:46:33.907000\t日均成交金额\t2,956,758.21\n",
"2017-08-23 22:46:33.907000\t日均成交笔数\t3.87\n",
"2017-08-23 22:46:33.908000\t日均收益率\t-0.1%\n",
"2017-08-23 22:46:33.908000\t收益标准差\t0.98%\n",
"2017-08-23 22:46:33.908000\tSharpe Ratio\t-1.66\n"
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Gjfjjjz/Yv38/zZo1\no3nz5gD06NGDHj16EBcXR3p6Oq+99lpeZrFatWr8/PPPEvwJIYQQouIraWBWHqpVK7hcwcHBgUmT\nJqFpGr179+bJJ58kLS2twOsA69atIzY2liFDhtCnTx/c3Nw4e/Zs3nk1a9Ys0K6maTg53Rkm/fzz\nz5hMJh599FGio6Pzjp8/f5569epZ5DNamyz4uItJkyAry9a9EPZm82b45BNb90IIIaqu5ORkkpOT\nAVi/fj0+Pj7873//47XXXqN3795omsbBgwe5desWQF5mbteuXTz//PMMGDAAT09Ptm3bhslkuud7\ntWvXjpSUFI4fPw7Ali1bGD9+PO3bt+fkyZPEx8cD8NNPP9GzZ0/Onz9vrY9tUZL5K8LVqzBrFjzy\nCAwfbuveCHuxfz+89BJoGjz/PLi52bpHQghR9TRp0oTFixdz+vRp6tevz9y5c9m+fTuvvfYatWvX\nplatWnTq1InTp08DtzN/w4YNY8qUKXz66adUq1aN1q1b88svvxQ4J1fu1/Xq1WP+/PmMHz8ek8mE\nwWBg4cKF1KlThw8//JB58+aRlZWFpmn885//pGHDhuV4J8znoOWGxJVAQkICPj4+ZW7np5+gfXvw\n9ISjR8HRsex9s5XU1FTc3d1t3Y0KLy0NHnsMFi6Ezz+HFi3gnXfufY3ce/skz8U+yHOwT/Jcypel\n4pbSKvGw78GDBxk6dCgAp0+fJigoiCFDhjBt2rS8c2JiYhgwYACDBw9m+/btAGRlZTFmzBiCg4MZ\nNWoUly9fBiAxMZFBgwY
"text/plain": [
"<matplotlib.figure.Figure at 0xd28bc90>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# 显示逐日回测结果\n",
"df = engine.showDailyResult()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2017-08-23 22:46:39.856000\t计算回测结果\n",
"2017-08-23 22:46:39.863000\t------------------------------\n",
"2017-08-23 22:46:39.863000\t第一笔交易\t2012-01-11 10:18:00\n",
"2017-08-23 22:46:39.863000\t最后一笔交易\t2012-06-29 11:30:00\n",
"2017-08-23 22:46:39.863000\t总交易次数\t217.0\n",
"2017-08-23 22:46:39.863000\t总盈亏\t-120,716.82\n",
"2017-08-23 22:46:39.863000\t最大回撤: \t-135,300.01\n",
"2017-08-23 22:46:39.863000\t平均每笔盈利\t-556.3\n",
"2017-08-23 22:46:39.863000\t平均每笔滑点\t120.0\n",
"2017-08-23 22:46:39.863000\t平均每笔佣金\t45.88\n",
"2017-08-23 22:46:39.863000\t胜率\t\t33.18%\n",
"2017-08-23 22:46:39.863000\t盈利交易平均值\t6,203.36\n",
"2017-08-23 22:46:39.863000\t亏损交易平均值\t-3,912.82\n",
"2017-08-23 22:46:39.863000\t盈亏比\t1.59\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAsgAAASZCAYAAADGqegeAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3Xd4lFX6//H3kCopEASUEEVREBsoCRYSUHqXYgKh6FfD\nWnCxASvq+qOtCKuCZcG2KiiIQLC7a4FFiSAqBDECYgHBaCIIoWRGIQmZ3x/HIZM+SaYl83ldV66d\neeYpZ/JE9p4z97lvi91utyMiIiIiIgA08vUARERERET8iQJkEREREREnCpBFRERERJwoQBYRERER\ncaIAWURERETEiQJkEREREREnwb4eAEBRURH3338/v/zyC4WFhdx6662ce+653HvvvTRq1Ih27dox\nffp0AFauXMmKFSsICQnh1ltv5eqrr+b48eP87W9/4+DBg0RGRjJ37lxiYmLYunUrDz30EMHBwXTt\n2pWJEyf6+J2KiIiIiL/zixnkt99+m5iYGF555RWef/55/vGPfzBnzhwmTZrE0qVLKS4uZs2aNRw4\ncIAlS5awYsUKnn/+eebNm0dhYSGvvvoq7du355VXXmHo0KE89dRTAMyYMYP58+ezbNkysrKy2Llz\np4/fqYiIiIj4O78IkAcMGMCdd94JwIkTJwgKCmLHjh0kJCQA0L17dz799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aHDjUl09yhshQLKL9Qb+dOk3rhDhYLTJ4Ms2dX/PrKlSVB9Vdfmdnsstq2\nhVatyv9+c3MrrsDhbyr
"text/plain": [
"<matplotlib.figure.Figure at 0xbf31950>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# 显示逐笔回测结果\n",
"engine.showBacktestingResult()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'orderID': '1', 'direction': u'\\u591a', 'gatewayName': '', 'tradeID': '1', 'exchange': '', 'symbol': '', 'volume': 1, 'tradeTime': '09:18:00', 'rawData': None, 'vtTradeID': '1', 'offset': u'\\u5f00\\u4ed3', 'vtOrderID': '1', 'dt': datetime.datetime(2012, 1, 11, 9, 18), 'price': 2464.4, 'vtSymbol': ''}\n",
"{'orderID': '2', 'direction': u'\\u7a7a', 'gatewayName': '', 'tradeID': '2', 'exchange': '', 'symbol': '', 'volume': 1, 'tradeTime': '10:18:00', 'rawData': None, 'vtTradeID': '2', 'offset': u'\\u5e73\\u4ed3', 'vtOrderID': '2', 'dt': datetime.datetime(2012, 1, 11, 10, 18), 'price': 2448.8, 'vtSymbol': ''}\n",
"{'orderID': '3', 'direction': u'\\u7a7a', 'gatewayName': '', 'tradeID': '3', 'exchange': '', 'symbol': '', 'volume': 1, 'tradeTime': '10:19:00', 'rawData': None, 'vtTradeID': '3', 'offset': u'\\u5f00\\u4ed3', 'vtOrderID': '3', 'dt': datetime.datetime(2012, 1, 11, 10, 19), 'price': 2448.4, 'vtSymbol': ''}\n",
"{'orderID': '4', 'direction': u'\\u591a', 'gatewayName': '', 'tradeID': '4', 'exchange': '', 'symbol': '', 'volume': 1, 'tradeTime': '14:21:00', 'rawData': None, 'vtTradeID': '4', 'offset': u'\\u5e73\\u4ed3', 'vtOrderID': '4', 'dt': datetime.datetime(2012, 1, 11, 14, 21), 'price': 2456.2000000000003, 'vtSymbol': ''}\n",
"{'orderID': '5', 'direction': u'\\u591a', 'gatewayName': '', 'tradeID': '5', 'exchange': '', 'symbol': '', 'volume': 1, 'tradeTime': '14:22:00', 'rawData': None, 'vtTradeID': '5', 'offset': u'\\u5f00\\u4ed3', 'vtOrderID': '5', 'dt': datetime.datetime(2012, 1, 11, 14, 22), 'price': 2455.6, 'vtSymbol': ''}\n",
"{'orderID': '6', 'direction': u'\\u7a7a', 'gatewayName': '', 'tradeID': '6', 'exchange': '', 'symbol': '', 'volume': 1, 'tradeTime': '14:57:00', 'rawData': None, 'vtTradeID': '6', 'offset': u'\\u5e73\\u4ed3', 'vtOrderID': '6', 'dt': datetime.datetime(2012, 1, 11, 14, 57), 'price': 2444.0, 'vtSymbol': ''}\n",
"{'orderID': '7', 'direction': u'\\u7a7a', 'gatewayName': '', 'tradeID': '7', 'exchange': '', 'symbol': '', 'volume': 1, 'tradeTime': '14:58:00', 'rawData': None, 'vtTradeID': '7', 'offset': u'\\u5f00\\u4ed3', 'vtOrderID': '7', 'dt': datetime.datetime(2012, 1, 11, 14, 58), 'price': 2445.8, 'vtSymbol': ''}\n",
"{'orderID': '8', 'direction': u'\\u591a', 'gatewayName': '', 'tradeID': '8', 'exchange': '', 'symbol': '', 'volume': 1, 'tradeTime': '09:51:00', 'rawData': None, 'vtTradeID': '8', 'offset': u'\\u5e73\\u4ed3', 'vtOrderID': '8', 'dt': datetime.datetime(2012, 1, 12, 9, 51), 'price': 2452.8, 'vtSymbol': ''}\n",
"{'orderID': '9', 'direction': u'\\u591a', 'gatewayName': '', 'tradeID': '9', 'exchange': '', 'symbol': '', 'volume': 1, 'tradeTime': '09:52:00', 'rawData': None, 'vtTradeID': '9', 'offset': u'\\u5f00\\u4ed3', 'vtOrderID': '9', 'dt': datetime.datetime(2012, 1, 12, 9, 52), 'price': 2452.0, 'vtSymbol': ''}\n",
"{'orderID': '10', 'direction': u'\\u7a7a', 'gatewayName': '', 'tradeID': '10', 'exchange': '', 'symbol': '', 'volume': 1, 'tradeTime': '10:31:00', 'rawData': None, 'vtTradeID': '10', 'offset': u'\\u5e73\\u4ed3', 'vtOrderID': '10', 'dt': datetime.datetime(2012, 1, 12, 10, 31), 'price': 2455.2000000000003, 'vtSymbol': ''}\n"
]
}
],
"source": [
"# 显示前10条成交记录\n",
"for i in range(10):\n",
" print engine.tradeDict[str(i+1)].__dict__"
]
},
{
"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",
"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
}