vnpy/examples/CtaBacktesting/.ipynb_checkpoints/backtesting_rb-checkpoint.ipynb

200 lines
110 KiB
Plaintext
Raw Normal View History

{
"cells": [
{
"cell_type": "code",
2017-10-09 05:21:17 +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.strategyBollChannel import BollChannelStrategy"
]
},
{
"cell_type": "code",
2017-10-09 05:21:17 +00:00
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# 创建回测引擎对象\n",
"engine = BacktestingEngine()"
]
},
{
"cell_type": "code",
2017-10-09 05:21:17 +00:00
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# 设置回测使用的数据\n",
"engine.setBacktestingMode(engine.BAR_MODE) # 设置引擎的回测模式为K线\n",
"engine.setDatabase(MINUTE_DB_NAME, 'rb0000') # 设置使用的历史数据库\n",
"engine.setStartDate('20110101') # 设置回测用的数据起始日期"
]
},
{
"cell_type": "code",
2017-10-09 05:21:17 +00:00
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# 配置回测引擎参数\n",
"engine.setSlippage(1) # 设置滑点为1跳\n",
"engine.setRate(1/10000) # 设置手续费万1\n",
"engine.setSize(10) # 设置合约大小 \n",
"engine.setPriceTick(1) # 设置最小价格变动 \n",
"engine.setCapital(30000) # 设置回测本金"
]
},
{
"cell_type": "code",
2017-10-09 05:21:17 +00:00
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# 在引擎中创建策略对象\n",
"d = {} # 策略参数配置\n",
"engine.initStrategy(BollChannelStrategy, d) # 创建策略对象"
]
},
{
"cell_type": "code",
2017-10-09 05:21:17 +00:00
"execution_count": 6,
"metadata": {
"collapsed": false
},
2017-10-09 05:21:17 +00:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2017-10-09 13:17:45.620000\t开始载入数据\n",
"2017-10-09 13:17:45.810000\t载入完成数据量448128\n",
"2017-10-09 13:17:45.810000\t开始回测\n",
"2017-10-09 13:17:45.817000\t策略初始化完成\n",
"2017-10-09 13:17:45.817000\t策略启动完成\n",
"2017-10-09 13:17:45.817000\t开始回放数据\n",
"2017-10-09 13:18:06.398000\t数据回放结束\n"
]
}
],
"source": [
"# 运行回测\n",
"engine.runBacktesting()"
]
},
{
"cell_type": "code",
2017-10-09 05:21:17 +00:00
"execution_count": 7,
"metadata": {
"collapsed": false
},
2017-10-09 05:21:17 +00:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2017-10-09 13:18:06.404000\t计算按日统计结果\n",
"2017-10-09 13:18:06.447000\t------------------------------\n",
"2017-10-09 13:18:06.447000\t首个交易日\t2011-01-11\n",
"2017-10-09 13:18:06.447000\t最后交易日\t2017-10-09\n",
"2017-10-09 13:18:06.447000\t总交易日\t1638\n",
"2017-10-09 13:18:06.447000\t盈利交易日\t702\n",
"2017-10-09 13:18:06.447000\t亏损交易日\t728\n",
"2017-10-09 13:18:06.447000\t起始资金\t30000\n",
"2017-10-09 13:18:06.447000\t结束资金\t76,760.0\n",
"2017-10-09 13:18:06.447000\t总收益率\t155.87\n",
"2017-10-09 13:18:06.447000\t总盈亏\t46,760.0\n",
"2017-10-09 13:18:06.447000\t最大回撤: \t-6,300.0\n",
"2017-10-09 13:18:06.447000\t总手续费\t0.0\n",
"2017-10-09 13:18:06.447000\t总滑点\t7,930.0\n",
"2017-10-09 13:18:06.447000\t总成交金额\t24,882,330.0\n",
"2017-10-09 13:18:06.447000\t总成交笔数\t793.0\n",
"2017-10-09 13:18:06.447000\t日均盈亏\t28.55\n",
"2017-10-09 13:18:06.447000\t日均手续费\t0.0\n",
"2017-10-09 13:18:06.447000\t日均滑点\t4.84\n",
"2017-10-09 13:18:06.447000\t日均成交金额\t15,190.68\n",
"2017-10-09 13:18:06.447000\t日均成交笔数\t0.48\n",
"2017-10-09 13:18:06.447000\t日均收益率\t0.06%\n",
"2017-10-09 13:18:06.447000\t收益标准差\t0.71%\n",
"2017-10-09 13:18:06.447000\tSharpe Ratio\t1.26\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAnYAAAOlCAYAAADgiarMAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3XlcVXX+x/EXiKhwUUmzNBq3tDSpHLBwiazsl02LaWMu\n7atatplri2K5paFTqZVjjeUWWNo201RW4hg5KklUho3ilpiKKxcVUM7vj0+XJTVQLlyW9/Px4HHu\n/Z5zz/2er2kfvsvn6+c4joOIiIiIVHr+vq6AiIiIiHiHAjsRERGRKkKBnYiIiEgVocBOREREpIpQ\nYCciIiJSRSiwExEREakiAnxdARGR4mzfvp1rrrmG888/H8dxOHbsGEFBQYwYMYI///nPJ/3cqFGj\naN26Nffcc0851lZExHcU2IlIpVC7dm2WLFmS//6TTz5h1KhRfPrppz6slYhIxaLATkQqpX379tGo\nUSMAxo0bx/fff09WVhaO4zBu3Djat29f5Pp3332X+Ph4jh49yv79+3nwwQfp27cvS5Ys4fPPP8ff\n358tW7ZQs2ZNJk+ezHnnnUdGRgZjxowhLS2NGjVq0KdPH+644w7cbjfjx4/n559/5ujRo3Ts2JHh\nw4fj76/ZLSLiWwrsRKRSOHLkCD179sRxHA4ePMju3buZOXMmycnJZGRkEBcXB8CsWbOYNWsWr776\nav5nDx06xLvvvsvf//536tWrx3fffcc999xD3759AVizZg0ff/wxjRo1Yty4cbzxxhtMnDiRmJgY\nmjdvzowZM3C73fTr14+uXbvy6quv0q5dOyZOnEheXh4jR47kzTff5P777/dJ24iIeCiwE5FK4fdD\nsWvXruWBBx7ggw8+4LHHHmPhwoVs3bqVVatW4XK5inw2KCiI1157ja+++ootW7bw008/cfjw4fzz\nF154YX7vX9u2bfn8888B+OabbxgxYgQALpeLjz76CIBly5bx/fffs2jRIgCys7Px8/Mru4cXESkh\nBXYiUim1b9+e5s2bs3r1ambOnMm9995Lt27daNGiRX4A5rFz50769OlDnz59iIyM5NprryUhISH/\nfK1atfJf+/n54dlCOyCg6D+R27ZtIzQ0lLy8PF566SVatGgBgNvtLqvHFBE5JZoQIiKVgifY8ti0\naRObN2/mk08+4aqrrqJv3760a9eOL774gry8vCLXfv/995xxxhkMGjSIzp0789VXX53wnr/XqVMn\nFi9eDEBmZiZ33303W7dupUuXLsyZMweAnJwcBg4cyPz58730pCIip089diJSKeTk5NCzZ0/AAjLP\nIonWrVvz5JNP0qNHD2rUqEFkZCSfffZZkc9efvnlvPfee1x77bUEBwcTHh7OGWecwZYtW/7wO599\n9lliYmK46aabcByHgQMH0rZtW55++mkmTJjAjTfeyNGjR+ncubPm14lIheDnFPcrq4iIiIhUCsX2\n2B09epQRI0awfft2AgICeP7556lRowYjR47E39+fVq1aMWbMGADi4+OJi4ujZs2aDBw4kK5du5Kd\nnc2wYcPYs2cPLpeLSZMmERoaSnJyMhMmTCAgIIBOnToxePDgMn9YERERkaqs2Dl2CQkJ5OXl8c47\n7/DQQw8xbdo0Jk6cyJAhQ5g3bx55eXksXbqUjIwM5s6dS1xcHLNnzyY2Npbc3FwWLlxI69atmT9/\nPj169GDmzJkAxMTEMHXqVBYsWEBKSgqpqall/rAiIiIiVVmxgV2zZs04duwYjuOQmZlJQEAA69at\nIzIyEoDo6GgSExNJSUkhIiKCgIAAXC4XzZo1IzU1laSkJKKjo/OvXblyJW63m9zcXMLCwgDo0qUL\niYmJZfiYIiIiIlVfsUOxwcHB/PLLL3Tv3p39+/fz2muvsWbNmiLn3W43WVlZhISE5JcHBQXll3ty\nSgUHB5OZmVmkrPB3iIiIiMjpKzawmzNnDpdffjlPPPEEO3fu5I477iA3Nzf/fFZWFnXr1sXlchXJ\n5VS4PCsrK78sJCQkPxj8/bUnkpSUdNoPJyIiIlLeIiIifPbdxQZ29erVy0/SG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eqwyocCuDKxatYobPf25JXTBBRewf//+MqqRiIhI5bVnjx2fffb4XjaPH36wQO6jj+z9G2/Y\n8euvoVcvmDEDfvwRbr7ZArFvv4Xrr/deHS+8ENau9d79TpcCuwrCz5MNUURERABbtJCcDIsWHX/u\nyBHo1s3OQ0FQ5Vn1+nvnnGPBX/36ZVPXikJz7MpIVlYWjz76KFu3bqVu3bo899xzADz33HMcOnSI\nXbt20aZNG6ZNm0ZgYCDOb32/hw8fJiYmhi1btrB//36Cg4OJjY2lWbNm3HHHHbRv355vv/2W9PR0\nIiMjmTx5MgBfffUVL730Eo7jUKdOHWJiYrjgggv44YcfGDZsGIcPH8bf35/BgwfTtaKkxxYRETmJ\nffvg4YdtQcPSpZZ/rm9fO5eXZz1vX3xhQ6ovvmiLHJo1g/fes2TDwcGWaDgjAzZvtnlz8+fbEG1V\nph67MrJz507uu+8+3n//fW644QaGDx/OokWL6NmzJ++88w6fffYZ27ZtIyEhocjnli9fTt26dXnn\nnXf497//Tbt27Zg3b17++W3btjFv3jw++ugjVq5cyapVq9izZw/Dhw/nhRde4IMPPuC+++5j6tSp\nHDx4kMmTJzNlyhQWL17MzJkziYmJ4ddffy3v5hARETkld95px2PHLGDr08cCOoAvv4Tvviu4duZM\ncLtte699+6BJE5uXt3q1XZucDFFRdq1ngURVVWV67Nq1s7HzsnLhhdaFW1Lnn38+F198MQA9e/Yk\nJiaGN998k+TkZGbPns3mzZvZvXs3WZ4Zob+59tprOffcc5k3bx5btmxh1apVtG/fPv/8lVdeCUBw\ncDBNmzblwIEDfPvtt7R
"text/plain": [
"<matplotlib.figure.Figure at 0x11f0e910>"
]
},
"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'])"
]
}
],
"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
}