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": [
|
2018-01-11 08:03:42 +00:00
|
|
|
|
"2018-01-07 15:25:16.799000\t开始载入数据\n",
|
|
|
|
|
"2018-01-07 15:25:16.805000\t载入完成,数据量:0\n",
|
|
|
|
|
"2018-01-07 15:25:16.805000\t开始回测\n",
|
|
|
|
|
"2018-01-07 15:25:16.805000\t策略初始化完成\n",
|
|
|
|
|
"2018-01-07 15:25:16.805000\t策略启动完成\n",
|
|
|
|
|
"2018-01-07 15:25:16.805000\t开始回放数据\n",
|
|
|
|
|
"2018-01-07 15:25:16.806000\t数据回放结束\n"
|
2017-10-08 14:32:07 +00:00
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
],
|
2017-10-07 02:58:37 +00:00
|
|
|
|
"source": [
|
|
|
|
|
"# 运行回测\n",
|
|
|
|
|
"engine.runBacktesting()"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
2018-01-11 08:03:42 +00:00
|
|
|
|
"execution_count": null,
|
2017-10-07 02:58:37 +00:00
|
|
|
|
"metadata": {
|
|
|
|
|
"collapsed": false
|
|
|
|
|
},
|
2018-01-11 08:03:42 +00:00
|
|
|
|
"outputs": [],
|
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
|
|
|
|
|
}
|