2017-10-07 02:58:37 +00:00
|
|
|
|
{
|
|
|
|
|
"cells": [
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
2017-10-08 14:32:07 +00:00
|
|
|
|
"execution_count": 1,
|
2017-10-07 02:58:37 +00:00
|
|
|
|
"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-08 14:32:07 +00:00
|
|
|
|
"execution_count": 2,
|
2017-10-07 02:58:37 +00:00
|
|
|
|
"metadata": {
|
|
|
|
|
"collapsed": false
|
|
|
|
|
},
|
|
|
|
|
"outputs": [],
|
|
|
|
|
"source": [
|
|
|
|
|
"# 创建回测引擎对象\n",
|
|
|
|
|
"engine = BacktestingEngine()"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
2017-10-08 14:32:07 +00:00
|
|
|
|
"execution_count": 3,
|
2017-10-07 02:58:37 +00:00
|
|
|
|
"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-08 14:32:07 +00:00
|
|
|
|
"execution_count": 4,
|
2017-10-07 02:58:37 +00:00
|
|
|
|
"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-08 14:32:07 +00:00
|
|
|
|
"execution_count": 5,
|
2017-10-07 02:58:37 +00:00
|
|
|
|
"metadata": {
|
|
|
|
|
"collapsed": true
|
|
|
|
|
},
|
|
|
|
|
"outputs": [],
|
|
|
|
|
"source": [
|
|
|
|
|
"# 在引擎中创建策略对象\n",
|
|
|
|
|
"d = {} # 策略参数配置\n",
|
|
|
|
|
"engine.initStrategy(BollChannelStrategy, d) # 创建策略对象"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
2017-10-08 14:32:07 +00:00
|
|
|
|
"execution_count": 6,
|
2017-10-07 02:58:37 +00:00
|
|
|
|
"metadata": {
|
|
|
|
|
"collapsed": false
|
|
|
|
|
},
|
2017-10-08 14:32:07 +00:00
|
|
|
|
"outputs": [
|
|
|
|
|
{
|
|
|
|
|
"name": "stdout",
|
|
|
|
|
"output_type": "stream",
|
|
|
|
|
"text": [
|
|
|
|
|
"2017-10-08 22:23:52.427000\t开始载入数据\n",
|
|
|
|
|
"2017-10-08 22:23:52.548000\t载入完成,数据量:332899\n",
|
|
|
|
|
"2017-10-08 22:23:52.548000\t开始回测\n",
|
|
|
|
|
"2017-10-08 22:23:52.548000\t策略初始化完成\n",
|
|
|
|
|
"2017-10-08 22:23:52.548000\t策略启动完成\n",
|
|
|
|
|
"2017-10-08 22:23:52.548000\t开始回放数据\n",
|
|
|
|
|
"2017-10-08 22:24:07.417000\t数据回放结束\n"
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
],
|
2017-10-07 02:58:37 +00:00
|
|
|
|
"source": [
|
|
|
|
|
"# 运行回测\n",
|
|
|
|
|
"engine.runBacktesting()"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
2017-10-08 14:32:07 +00:00
|
|
|
|
"execution_count": 7,
|
2017-10-07 02:58:37 +00:00
|
|
|
|
"metadata": {
|
|
|
|
|
"collapsed": false
|
|
|
|
|
},
|
2017-10-08 14:32:07 +00:00
|
|
|
|
"outputs": [
|
|
|
|
|
{
|
|
|
|
|
"name": "stdout",
|
|
|
|
|
"output_type": "stream",
|
|
|
|
|
"text": [
|
|
|
|
|
"2017-10-08 22:24:07.423000\t计算按日统计结果\n",
|
|
|
|
|
"2017-10-08 22:24:07.451000\t------------------------------\n",
|
|
|
|
|
"2017-10-08 22:24:07.451000\t首个交易日:\t2011-09-02\n",
|
|
|
|
|
"2017-10-08 22:24:07.451000\t最后交易日:\t2017-09-29\n",
|
|
|
|
|
"2017-10-08 22:24:07.451000\t总交易日:\t1478\n",
|
|
|
|
|
"2017-10-08 22:24:07.451000\t盈利交易日\t640\n",
|
|
|
|
|
"2017-10-08 22:24:07.451000\t亏损交易日:\t601\n",
|
|
|
|
|
"2017-10-08 22:24:07.451000\t起始资金:\t30000\n",
|
|
|
|
|
"2017-10-08 22:24:07.451000\t结束资金:\t82,010.0\n",
|
|
|
|
|
"2017-10-08 22:24:07.451000\t总收益率:\t173.37\n",
|
|
|
|
|
"2017-10-08 22:24:07.451000\t总盈亏:\t52,010.0\n",
|
|
|
|
|
"2017-10-08 22:24:07.451000\t最大回撤: \t-3,790.0\n",
|
|
|
|
|
"2017-10-08 22:24:07.451000\t总手续费:\t0.0\n",
|
|
|
|
|
"2017-10-08 22:24:07.451000\t总滑点:\t5,890.0\n",
|
|
|
|
|
"2017-10-08 22:24:07.451000\t总成交金额:\t18,246,990.0\n",
|
|
|
|
|
"2017-10-08 22:24:07.451000\t总成交笔数:\t589.0\n",
|
|
|
|
|
"2017-10-08 22:24:07.451000\t日均盈亏:\t35.19\n",
|
|
|
|
|
"2017-10-08 22:24:07.451000\t日均手续费:\t0.0\n",
|
|
|
|
|
"2017-10-08 22:24:07.451000\t日均滑点:\t3.99\n",
|
|
|
|
|
"2017-10-08 22:24:07.451000\t日均成交金额:\t12,345.73\n",
|
|
|
|
|
"2017-10-08 22:24:07.451000\t日均成交笔数:\t0.4\n",
|
|
|
|
|
"2017-10-08 22:24:07.451000\t日均收益率:\t0.07%\n",
|
|
|
|
|
"2017-10-08 22:24:07.451000\t收益标准差:\t0.73%\n",
|
|
|
|
|
"2017-10-08 22:24:07.451000\tSharpe Ratio:\t1.45\n"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"data": {
|
|
|
|
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAnYAAAOlCAYAAADgiarMAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3Xt8z3X/x/HHZhbb11BSadUcNoqJNnJq6SrFr4NUKrpc\nqIjSaTlTmytGXSHXhaRyyZymotPVUWnSKppEaSpnJodFtmEb+/z+eLXNDBvbvt8dnvfbbbfP9/v+\nfL7fz/vzthsv78Pr7eU4joOIiIiIlHvenq6AiIiIiJQMBXYiIiIiFYQCOxEREZEKQoGdiIiISAWh\nwE5ERESkglBgJyIiIlJB+Hi6AiIihdm5cyedOnWicePGOI7DsWPH8PPzY9iwYVx11VWn/NyIESMI\nCQmhb9++bqytiIjnKLATkXKhWrVqLFmyJPf9hx9+yIgRI/j44489WCsRkbJFgZ2IlEv79++nbt26\nAIwdO5Z169aRnp6O4ziMHTuWli1b5rv+zTffZNGiRRw9epQDBw7Qv39/7r33XpYsWcKnn36Kt7c3\nW7dupWrVqjz//PM0atSIffv2ERUVxaZNm6hSpQr33HMPvXr1Ii0tjXHjxvHLL79w9OhR2rZty9Ch\nQ/H21uwWEfEsBXYiUi4cOXKEbt264TgOBw8eZO/evUyfPp01a9awb98+4uLiAJg5cyYzZ87kpZde\nyv3soUOHePPNN3nllVeoWbMmP/zwA3379uXee+8F4LvvvuP999+nbt26jB07ltdee43x48cTHR1N\n/fr1mTZtGmlpafTo0YOOHTvy0ksv0axZM8aPH092djbDhw9n1qxZPPjggx5pGxGRHArsRKRcOHEo\n9vvvv6dfv3688847PP744yxYsIBt27axcuVKXC5Xvs/6+fkxY8YMli1bxtatW/n55585fPhw7vmm\nTZvm9v5dccUVfPrppwB8/fXXDBs2DACXy8V7770HwBdffMG6det44403AMjIyMDLy6v0Hl5EpIgU\n2IlIudSyZUvq16/PqlWrmD59Ovfffz833HADDRo0yA3AcuzevZt77rmHe+65h/DwcG666Sbi4+Nz\nz59zzjm5r728vMjZQtvHJ/9fkdu3b6d27dpkZ2czZcoUGjRoAEBaWlppPaaIyBnRhBARKRdygq0c\nmzdvZsuWLXz44Yf87W9/495776VZs2Z89tlnZGdn57t23bp1nHvuuQwcOJD27duzbNmyk37nidq1\na8fixYsBSE1NpU+fPmzbto0OHTowe/ZsADIzMxkwYADz5s0roScVETl76rETkXIhMzOTbt26ARaQ\n5SySCAkJ4amnnqJr165UqVKF8PBwPvnkk3yfveaaa3jrrbe46aab8Pf3JzQ0lHPPPZetW7ee9p5P\nP/000dHR3HbbbTiOw4ABA7jiiisYNWoUMTEx3HrrrRw9epT27dtrfp2IlAleTmH/ZRURERGRcqHQ\nHrvMzExGjBjBjh07cLlcREVFATB8+HC8vb0JDg7OLVu0aBFxcXFUrVqVAQMG0LFjRzIyMhgyZAgp\nKSm4XC4mTJhA7dq1WbNmDTEx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Vi2bBlTpkzBcRyqV69OdHQ0\nTZo0YfXq1UycOJHDhw/j7e3NoEGD6Nixo6eaRUREREqRArtSsnv3biZPnsyVV17JokWLGDp0KK1a\ntaJbt27ceuutHD16lDvuuIP4+Hg6deqU+7nly5cTEBDAwoULAYiKimLu3LmMHj0agO3btzN37lzS\n09Pp0qULK1eupGHDhgwdOpS5c+fSuHFjPv30UyZNmsQLL7zAyJEjmTVrFvXq1WPPnj3cfffdLFy4\nkAsvvNAj7SIiIiKlp8IEds2awU8/leQ35t/7rmlT+PHHon+6cePGXHnllQB069aN6OhoZs2axZo1\na3j11VfZsmULe/fuJT09Pd/nbrrpJi655BLmzp3L1q1bWblyJS1btsw9f9111wHg7+/PZZddxp9/\n/snq1asJCQmhcePGAHTq1IlOnToRHx/P3r17eeSRR3J7BL29vdmwYYMCOxERkQqowgR2ZxJ0FUVx\nNzb29s4/fdHLy4tRo0bhOA5dunThuuuuY9euXfnOA8yfP5833niDv//979x6663UrFmTnTt35l5X\nrVq1fN/rOA4+PgX/GDds2EB2djaNGjUiLi4ut3zPnj2cd955Z/1cIiIiUnZp8UQpSUpKIikpCYCF\nCxcSFhbG119/zSOPPEKXLl1wHIcffviBY8eOAeT2qH311Vfccccd3HnnnQQFBbFs2TKys7NPe68r\nr7ySTZs2sXHjRgCWLl3K0KFDadGiBVu2bOG7774D4Oeff+amm25iz549pfXYIiIi4kEVpseurGnY\nsCHTpk1j27Zt1KlTh+eee44vvviCRx55hFq1alG9enVat27Ntm3bgLweu/vvv59nnnmGxYsX4+3t\nTdOmTfnll1/yXZMj5/15553HCy+8wNChQ8nOzsblcjF58mRq167Nf/7zH55//nkyMjJwHId//etf\nXHTRRW5sCREREXEXLyenq6iMSkxMJCwszO33Le5QrJwZtbd7qb3dS+3tPmpr91J7F+SpuCWHhmJF\nREREKggFdiIiIiIVhAI7ERERkQpCgZ2IiIjIafz8M2RknPr8P/4Bf+0r4HEK7EREREROwnEgJgau\nuAImTz71dbGx0KOHXe9pCuxERERETmLPHhg1yl6npJz6uiZN7NivX+nXqTAK7ERERESOs2IFXHst\n/LXPAAAn7ACaz/79dnzttdKtV1EosBMRERE5zhtvwPLleYFdQACkpZ38Wsex3rznn3df/U6n0J0n\njh49yrBhw9i5cyc+Pj48++yzVKlSheHDh+Pt7U1wcDBRUVEALFq0iLi4OKpWrcqAAQPo2LEjGRkZ\nDBkyhJSUFFwuFxMmTKB
|
|
|
|
|
"text/plain": [
|
|
|
|
|
"<matplotlib.figure.Figure at 0x11af54b0>"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
"metadata": {},
|
|
|
|
|
"output_type": "display_data"
|
|
|
|
|
}
|
|
|
|
|
],
|
2017-10-07 02:58:37 +00:00
|
|
|
|
"source": [
|
|
|
|
|
"# 显示逐日回测结果\n",
|
2017-10-07 15:25:59 +00:00
|
|
|
|
"engine.showDailyResult()"
|
2017-10-07 02:58:37 +00:00
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"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
|
|
|
|
|
}
|