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

268 lines
94 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 09:54:42.212000\t开始载入数据\n",
"2018-01-09 09:54:42.331000\t载入完成数据量285480\n",
"2018-01-09 09:54:42.331000\t开始回测\n",
"2018-01-09 09:54:42.402000\t策略初始化完成\n",
"2018-01-09 09:54:42.402000\t策略启动完成\n",
"2018-01-09 09:54:42.402000\t开始回放数据\n",
"2018-01-09 09:55:10.816000\t数据回放结束\n"
]
}
],
"source": [
"# 运行回测\n",
"engine.runBacktesting() # 运行回测"
]
},
{
"cell_type": "code",
2018-01-11 08:03:42 +00:00
"execution_count": 7,
"metadata": {
"collapsed": false
},
2018-01-11 08:03:42 +00:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2018-01-09 09:55:10.822000\t计算按日统计结果\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Github\\vnpy\\vnpy\\vnpy\\trader\\app\\ctaStrategy\\ctaBacktesting.py:971: RuntimeWarning: invalid value encountered in log\n",
" df['return'] = (np.log(df['balance']) - np.log(df['balance'].shift(1))).fillna(0)\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"2018-01-09 09:55:11.113000\t------------------------------\n",
"2018-01-09 09:55:11.113000\t首个交易日\t2013-01-11\n",
"2018-01-09 09:55:11.114000\t最后交易日\t2017-07-14\n",
"2018-01-09 09:55:11.114000\t总交易日\t1095\n",
"2018-01-09 09:55:11.114000\t盈利交易日\t399\n",
"2018-01-09 09:55:11.114000\t亏损交易日\t696\n",
"2018-01-09 09:55:11.114000\t起始资金\t1000000\n",
"2018-01-09 09:55:11.114000\t结束资金\t-1,583,699.42\n",
"2018-01-09 09:55:11.114000\t总收益率\t-258.37\n",
"2018-01-09 09:55:11.114000\t年华收益率\t-56.63\n",
"2018-01-09 09:55:11.114000\t总盈亏\t-2,583,699.42\n",
"2018-01-09 09:55:11.114000\t最大回撤: \t-2,666,357.75\n",
"2018-01-09 09:55:11.114000\t百分比最大回撤: \t-246.28\n",
"2018-01-09 09:55:11.114000\t总手续费\t1,772,319.42\n",
"2018-01-09 09:55:11.114000\t总滑点\t3,900,600.0\n",
"2018-01-09 09:55:11.114000\t总成交金额\t59,077,313,940.0\n",
"2018-01-09 09:55:11.114000\t总成交笔数\t50,395.0\n",
"2018-01-09 09:55:11.114000\t日均盈亏\t-2,359.54\n",
"2018-01-09 09:55:11.114000\t日均手续费\t1,618.56\n",
"2018-01-09 09:55:11.114000\t日均滑点\t3,562.19\n",
"2018-01-09 09:55:11.114000\t日均成交金额\t53,951,884.88\n",
"2018-01-09 09:55:11.114000\t日均成交笔数\t46.02\n",
"2018-01-09 09:55:11.114000\t日均收益率\t-0.97%\n",
"2018-01-09 09:55:11.114000\t收益标准差\t24.5%\n",
"2018-01-09 09:55:11.114000\tSharpe Ratio\t-0.61\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAocAAAOlCAYAAAAfB01rAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3XlYlOX6wPEvO8KAuCaGae6paAqaidImpZ0sF1BwqyRT\nj7aRaVYmWoqZy68TanXMXFBxaTst1okySdNjYGouWGkKBbnhwqCyOO/vj7sZxHAHBpj7c11cMM+8\n8/LcA8Xts9yPk2EYBkoppZRSSgHO9u6AUkoppZSqODQ5VEoppZRSNpocKqWUUkopG00OlVJKKaWU\njSaHSimllFLKRpNDpZRSSill42rvDiilVHn4448/CAsLo0WLFhiGwblz5/Dy8mL8+PF06NDhoq+b\nMGECzZs359FHHy3H3iqllP1ocqiUchienp58+OGHtsdr165lwoQJfPnll3bslVJKVSyaHCqlHNbx\n48epW7cuAK+++io//fQTubm5GIbBq6++Svv27Ytdv2bNGlatWkVhYSEnTpzg8ccfJzIykg8//JCv\nvvoKZ2dnDh48iJubGzNmzKBp06YcPXqUSZMmsX//flxcXBgwYABDhgzBbDYzdepUfv75ZwoLC7n9\n9tsZN24czs662kcpZV+aHCqlHMbZs2fp06cPhmFw6tQpjhw5wrx589i2bRtHjx5l5cqVALzzzju8\n8847zJ8/3/ba06dPs2bNGv79739TvXp1tm/fzqOPPkpkZCQAKSkpfPrpp9StW5dXX32Vd999l7i4\nOGJjY7n55puZO3cuZrOZqKgo7rzzTubPn0+bNm2Ii4vDYrHw/PPPs3DhQh577DG7vDdKKWWlyaFS\nymFcOK38448/Mnz4cD7++GOeeuopVqxYQXp6Olu2bMFkMhV7rZeXF2+99Rbr1q3j4MGD7NmzhzNn\nztieb926tW0UslWrVnz11VcAbNq0ifHjxwNgMpn45JNPAPj222/56aefWL16NQB5eXk4OTmVXfBK\nKXWFNDlUSjms9u3bc/PNN/PDDz8wb948hg0bRvfu3WncuLEtibM6dOgQAwYMYMCAAQQHB3Pfffex\nfv162/MeHh62r52cnLAeW+/qWvx/sxkZGdSoUQOLxcIbb7xB48aNATCbzWUVplJKXRVd3KKUchjW\nhM3qt99+48CBA6xdu5a7776byMhI2rRpw9dff43FYil27U8//UTNmjUZNWoUISEhrFu3rsR7XqhL\nly588MEHAOTk5PDII4+Qnp5O165dWbRoEQD5+fmMHDmSZcuWlVKkSil17XTkUCnlMPLz8+nTpw8g\nSZ1140nz5s159tlneeihh3BxcSE4OJj//ve/xV7brVs33n//fe677z68vb0JDAykZs2aHDx48JLf\nc+LEicTGxvLggw9iGAYjR46kVatWvPjii0ybNo1evXpRWFhISEiIrjdUSlUITsbl/tmrlFJKKaUc\nxnVNK2/fvp0hQ4YAsGfPHkJDQxk6dChDhw5l7dq1AKxatYp+/foRGRnJt99+C8jC6yeffJJBgwYx\nYsQIjh8/DsC2bdvo378/AwcOJD4+3vZ94uPjiYiIICoqih07dgBSgiI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MPPsDZ2ZnWrVvz888/F7vG\nyvq4Vq1azJw5k3HjxmGxWDCZTMyZM4caNWrw5ptvMmPGDPLy8jAMg9dffx1/f/9yfCeun5NhTZ2r\nqNTUVIKCguzdjTJ3PUPxlZ0jxu6IMVs5YuyOGDNo3I7IEWO/MOaKkLfotLJSSimllLLR5FAppZRS\nStk4RHJ44gTs3WvvXiillFJKVXzXlRxu376dIUOGAJCens7AgQMZPHgwkydPtl2zatUq+vXrR2Rk\nJN9++y0AeXl5PPnkkww
"text/plain": [
"<matplotlib.figure.Figure at 0xf4353b0>"
]
},
"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",
"execution_count": null,
"metadata": {
2018-01-11 08:03:42 +00:00
"collapsed": false
},
"outputs": [],
"source": [
"# 显示优化的所有统计数据\n",
"for result in resultList:\n",
2018-01-11 08:03:42 +00:00
" 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)"
]
2018-01-11 08:03:42 +00:00
},
{
"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
}