132 lines
4.1 KiB
Plaintext
132 lines
4.1 KiB
Plaintext
{
|
||
"cells": [
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 1,
|
||
"metadata": {
|
||
"collapsed": true
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"%matplotlib inline\n",
|
||
"\n",
|
||
"from vnpy.trader.app.ctaStrategy.ctaBacktesting import BacktestingEngine, MINUTE_DB_NAME\n",
|
||
"\n",
|
||
"def runBacktesting(strategyClass, settingDict, symbol, \n",
|
||
" startDate, endDate, slippage, \n",
|
||
" rate, size, priceTick):\n",
|
||
" \"\"\"运行单标的回测\"\"\"\n",
|
||
" engine = BacktestingEngine()\n",
|
||
" engine.setBacktestingMode(engine.BAR_MODE)\n",
|
||
" engine.setDatabase(MINUTE_DB_NAME, symbol)\n",
|
||
" engine.setStartDate(startDate)\n",
|
||
" engine.setEndDate(endDate)\n",
|
||
" engine.setSlippage(slippage)\n",
|
||
" engine.setRate(rate) \n",
|
||
" engine.setSize(size) \n",
|
||
" engine.setPriceTick(priceTick)\n",
|
||
" \n",
|
||
" engine.initStrategy(strategyClass, settingDict)\n",
|
||
" engine.runBacktesting()\n",
|
||
" df = engine.calculateDailyResult()\n",
|
||
" return df"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 2,
|
||
"metadata": {
|
||
"collapsed": false
|
||
},
|
||
"outputs": [
|
||
{
|
||
"ename": "SyntaxError",
|
||
"evalue": "EOL while scanning string literal (<ipython-input-2-50e7df42c47d>, line 4)",
|
||
"output_type": "error",
|
||
"traceback": [
|
||
"\u001b[1;36m File \u001b[1;32m\"<ipython-input-2-50e7df42c47d>\"\u001b[1;36m, line \u001b[1;32m4\u001b[0m\n\u001b[1;33m '20120101', '20170630, 0.2,\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m EOL while scanning string literal\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"# 运行IF回测,交易1手\n",
|
||
"from vnpy.trader.app.ctaStrategy.strategy.strategyAtrRsi import AtrRsiStrategy\n",
|
||
"df1 = runBacktesting(AtrRsiStrategy, {}, 'IF0000', \n",
|
||
" '20120101', '20170630', 0.2, \n",
|
||
" 0.3/10000, 300, 0.2)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {
|
||
"collapsed": false
|
||
},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"2017-10-07 23:23:57.497000\t开始载入数据\n",
|
||
"2017-10-07 23:23:57.617000\t载入完成,数据量:300224\n",
|
||
"2017-10-07 23:23:57.617000\t开始回测\n",
|
||
"2017-10-07 23:23:57.625000\t策略初始化完成\n",
|
||
"2017-10-07 23:23:57.625000\t策略启动完成\n",
|
||
"2017-10-07 23:23:57.625000\t开始回放数据\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"# 运行rb回测,交易16手\n",
|
||
"from vnpy.trader.app.ctaStrategy.strategy.strategyBollChannel import BollChannelStrategy\n",
|
||
"settingDict = {'fixedSize': 16}\n",
|
||
"df2 = runBacktesting(BollChannelStrategy, settingDict, 'rb0000', \n",
|
||
" '20120101', '20170630', 1, \n",
|
||
" 1/10000, 10, 1)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {
|
||
"collapsed": false
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"# 合并获得组合回测结果\n",
|
||
"dfp = df1 + df2\n",
|
||
"\n",
|
||
"# 注意如果被抛弃的交易日位于回测的前后,即两者不重合的日期中,则不会影响组合曲线正确性\n",
|
||
"# 但是如果被抛弃的交易日位于回测的中部,即两者重合的日期中,组合曲线会出现错误(丢失交易日)\n",
|
||
"dfp = dfp.dropna() \n",
|
||
"\n",
|
||
"# 创建回测引擎,并设置组合回测初始资金后,显示结果\n",
|
||
"engine = BacktestingEngine()\n",
|
||
"engine.setCapital(1000000)\n",
|
||
"engine.showDailyResult(dfp)"
|
||
]
|
||
}
|
||
],
|
||
"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
|
||
}
|