vnpy/examples/CtaBacktesting/backtesting.ipynb

230 lines
112 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-09-20 15:25:04.452000\t开始载入数据\n",
"2017-09-20 15:25:04.597000\t载入完成数据量351090\n",
"2017-09-20 15:25:04.597000\t开始回测\n",
"2017-09-20 15:25:04.615000\t策略初始化完成\n",
"2017-09-20 15:25:04.615000\t策略启动完成\n",
"2017-09-20 15:25:04.615000\t开始回放数据\n",
"2017-09-20 15:25:28.260000\t数据回放结束\n"
]
}
],
"source": [
"# 运行回测\n",
"engine.runBacktesting() # 运行回测"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2017-09-20 15:25:28.265000\t计算按日统计结果\n",
"2017-09-20 15:25:28.375000\t------------------------------\n",
"2017-09-20 15:25:28.375000\t首个交易日\t2012-01-11\n",
"2017-09-20 15:25:28.375000\t最后交易日\t2017-07-14\n",
"2017-09-20 15:25:28.375000\t总交易日\t1338\n",
"2017-09-20 15:25:28.375000\t盈利交易日\t651\n",
"2017-09-20 15:25:28.375000\t亏损交易日\t686\n",
"2017-09-20 15:25:28.375000\t起始资金\t1000000\n",
"2017-09-20 15:25:28.375000\t结束资金\t1,601,119.34\n",
"2017-09-20 15:25:28.375000\t总收益率\t60.11\n",
"2017-09-20 15:25:28.375000\t总盈亏\t601,119.34\n",
"2017-09-20 15:25:28.375000\t最大回撤: \t-185,949.45\n",
"2017-09-20 15:25:28.376000\t总手续费\t220,760.66\n",
"2017-09-20 15:25:28.376000\t总滑点\t457,980.0\n",
"2017-09-20 15:25:28.376000\t总成交金额\t7,358,688,660.0\n",
"2017-09-20 15:25:28.376000\t总成交笔数\t7,632.0\n",
"2017-09-20 15:25:28.376000\t日均盈亏\t449.27\n",
"2017-09-20 15:25:28.376000\t日均手续费\t164.99\n",
"2017-09-20 15:25:28.376000\t日均滑点\t342.29\n",
"2017-09-20 15:25:28.376000\t日均成交金额\t5,499,767.31\n",
"2017-09-20 15:25:28.376000\t日均成交笔数\t5.7\n",
"2017-09-20 15:25:28.376000\t日均收益率\t0.04%\n",
"2017-09-20 15:25:28.376000\t收益标准差\t1.07%\n",
"2017-09-20 15:25:28.376000\tSharpe Ratio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rnp5e5HV9+/alQYMGhIeHc/ToUbZs2ULHjh0Lnu/duzcArq6u\nNGrUiHPnzrF9+3ZatmxJq1atAOjTpw99+vQhKiqKU6dO8cwzzxRkGqtVq8b+/fs1CFRK3XBOntQg\nUFV9N2wQWNoArTJUq1Z0aKadnR1Tp07FMAz69etH7969SUxMLPI8wMqVK1m9ejXDhw+nf//+eHp6\ncvz48YLzqlevXuS6hmHg4HDpLd+/fz/5+fk0b96cVatWFRw/efIkNWvWLJfPqJRS15PLZQInT4Y7\n74R+/Up3rSZN4KWX4Mkny699SpUHnRhiA+Li4oiLiwPgk08+ISAggN9++41nnnmGfv36YRgGu3bt\nIi8vD6AgU7dp0yYeeughBg0aROPGjVm/fj35+fmXfa8OHTpw6NAhDh48CMCPP/7IlClTuOWWWzhy\n5Ajbtm0DYN++ffTt25eTJ09W1MdWSimbFBUFq1eXHAQuXAhLl5b+ekeOwMcfl0vTlCpXN2wm0JY0\na9aMxYsXc+zYMWrVqsX8+fPZsGEDzzzzDF5eXtSoUYPOnTtz7NgxwJIJHDVqFNOnT+fzzz+nWrVq\ntG3blgMHDhQ5x2Tu16xZk4ULFzJlyhTy8/Nxc3Nj0aJFeHt7884777BgwQKysrIwDIM33ngDX1/f\nSvwmlFLK+qZOlW3hIDA/H269Ffr2lf1jxyAvD+zti742LQ3eestyjQu/s/PHH/JYizsoW2JnmGml\nKiAmJoaAgABrN0MBCQk
"text/plain": [
"<matplotlib.figure.Figure at 0xc1ef430>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# 显示逐日回测结果\n",
"df = 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",
" 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
}