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

256 lines
110 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-02-16 14:17:58 +00:00
"2018-02-16 22:10:48.332000\t开始载入数据\n",
"2018-02-16 22:10:48.417000\t载入完成数据量91381\n",
"2018-02-16 22:10:48.418000\t开始回测\n",
"2018-02-16 22:10:48.463000\t策略初始化完成\n",
"2018-02-16 22:10:48.463000\t策略启动完成\n",
"2018-02-16 22:10:48.463000\t开始回放数据\n",
"2018-02-16 22:10:56.057000\t数据回放结束\n"
2018-01-11 08:03:42 +00:00
]
}
],
"source": [
"# 运行回测\n",
"engine.runBacktesting() # 运行回测"
]
},
{
"cell_type": "code",
2018-01-11 08:03:42 +00:00
"execution_count": 7,
"metadata": {
2018-02-16 14:17:58 +00:00
"collapsed": false,
"scrolled": false
},
2018-01-11 08:03:42 +00:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2018-02-16 14:17:58 +00:00
"2018-02-16 22:10:56.062000\t计算按日统计结果\n",
"2018-02-16 22:10:56.304000\t------------------------------\n",
"2018-02-16 22:10:56.305000\t首个交易日\t2013-01-11\n",
"2018-02-16 22:10:56.305000\t最后交易日\t2014-06-04\n",
"2018-02-16 22:10:56.305000\t总交易日\t334\n",
"2018-02-16 22:10:56.305000\t盈利交易日\t167\n",
"2018-02-16 22:10:56.305000\t亏损交易日\t167\n",
"2018-02-16 22:10:56.305000\t起始资金\t1000000\n",
"2018-02-16 22:10:56.305000\t结束资金\t1,283,473.32\n",
"2018-02-16 22:10:56.305000\t总收益率\t28.35%\n",
"2018-02-16 22:10:56.305000\t年化收益\t20.37%\n",
"2018-02-16 22:10:56.305000\t总盈亏\t283,473.32\n",
"2018-02-16 22:10:56.305000\t最大回撤: \t-101,191.41\n",
"2018-02-16 22:10:56.305000\t百分比最大回撤: -9.23%\n",
"2018-02-16 22:10:56.305000\t总手续费\t33,266.68\n",
"2018-02-16 22:10:56.305000\t总滑点\t94,260.0\n",
"2018-02-16 22:10:56.305000\t总成交金额\t1,108,889,460.0\n",
"2018-02-16 22:10:56.305000\t总成交笔数\t1,571.0\n",
"2018-02-16 22:10:56.305000\t日均盈亏\t848.72\n",
"2018-02-16 22:10:56.305000\t日均手续费\t99.6\n",
"2018-02-16 22:10:56.305000\t日均滑点\t282.22\n",
"2018-02-16 22:10:56.305000\t日均成交金额\t3,320,028.32\n",
"2018-02-16 22:10:56.305000\t日均成交笔数\t4.7\n",
"2018-02-16 22:10:56.305000\t日均收益率\t0.07%\n",
"2018-02-16 22:10:56.305000\t收益标准差\t0.87%\n",
"2018-02-16 22:10:56.305000\tSharpe Ratio\t1.3\n"
2018-01-11 08:03:42 +00:00
]
},
{
"data": {
2018-02-16 14:17:58 +00:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAoEAAAOlCAYAAAASGT0sAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3Xt8zuX/wPHXjg67t5kQs0LkTNiSGjounYR8HTamUKFQ\nSZZv8kMOJYf6ZpLo4LRsIdW3g4gtItm+rGLIcWyOm+weZnN/fn+83bt3dm929n4+Hh52f+7P53Nf\nn8tq713X9X5fDoZhGCillFJKqRuKY1k3QCmllFJKlT4NApVSSimlbkAaBCqllFJK3YA0CFRKKaWU\nugFpEKiUUkopdQPSIFAppZRS6gbkXNYNUEqp4nT8+HECAgJo1qwZhmFw5coVqlevTkhICB06dMj3\nuvHjx9O0aVMGDx5ciq1VSqmyo0GgUqrSqVq1KmvWrMl8/f333zN+/Hh+/PHHMmyVUkqVLxoEKqUq\nveTkZOrUqQPA1KlT+eOPP0hNTcUwDKZOnUr79u2znf/ll18SHh5ORkYG586d4/nnn6d///6sWbOG\nn376CUdHR44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SUhcnvXAcHeOQR+QMSeMXHSxbxX3/JKODzz4Ofn6wXTEqSgs3F5dNP\n4d13Zcp2+XLb8aNHoVs3CT579rT/fu3bw3vvyTNXqyb3WLdORjnLeOtgMCqRHTt2lNi9jx8/XiL3\n/e2334yWLVsaO3fuNAzDMFauXGn06dPHmDlzpvH1118bhmEY6enpRvfu3Y1169YZhmEYzZo1M5KT\nk40ffvjBmDp1aua9Jk6
2018-01-11 08:03:42 +00:00
"text/plain": [
2018-02-16 14:17:58 +00:00
"<matplotlib.figure.Figure at 0xccfd390>"
2018-01-11 08:03:42 +00:00
]
},
"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",
2018-02-16 14:17:58 +00:00
"#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",
2018-02-16 14:17:58 +00:00
"#resultList = engine.runParallelOptimization(AtrRsiStrategy, setting)\n",
"resultList = engine.runOptimization(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
}