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

223 lines
42 KiB
Plaintext
Raw Normal View History

2017-06-15 10:36:39 +00:00
{
"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) # 设置股指最小价格变动 "
]
},
{
"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-06-02 16:07:31.265000\t开始载入数据\n",
"2017-06-02 16:07:31.423000\t载入完成数据量331890\n",
"2017-06-02 16:07:31.423000\t开始回测\n",
"2017-06-02 16:07:31.441000\t策略初始化完成\n",
"2017-06-02 16:07:31.441000\t策略启动完成\n",
"2017-06-02 16:07:31.442000\t开始回放数据\n",
"2017-06-02 16:07:54.738000\t数据回放结束\n"
]
}
],
"source": [
"# 运行回测\n",
"engine.runBacktesting() # 运行回测"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2017-06-02 16:09:14.068000\t计算回测结果\n",
"2017-06-02 16:09:14.134000\t------------------------------\n",
"2017-06-02 16:09:14.134000\t第一笔交易\t2012-01-11 10:18:00\n",
"2017-06-02 16:09:14.134000\t最后一笔交易\t2017-03-20 09:31:00\n",
"2017-06-02 16:09:14.134000\t总交易次数\t3,749.0\n",
"2017-06-02 16:09:14.134000\t总盈亏\t683,717.31\n",
"2017-06-02 16:09:14.134000\t最大回撤: \t-215,542.46\n",
"2017-06-02 16:09:14.134000\t平均每笔盈利\t182.37\n",
"2017-06-02 16:09:14.134000\t平均每笔滑点\t120.0\n",
"2017-06-02 16:09:14.134000\t平均每笔佣金\t57.75\n",
"2017-06-02 16:09:14.134000\t胜率\t\t37.24%\n",
"2017-06-02 16:09:14.134000\t盈利交易平均值\t8,539.01\n",
"2017-06-02 16:09:14.134000\t亏损交易平均值\t-4,775.5\n",
"2017-06-02 16:09:14.134000\t盈亏比\t1.79\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAakAAAE/CAYAAAD8EzwFAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzsnXuYFMXVuN+zu4AIsqDioiALeEeNiBFJvCwSRdREk5go\niYlgks9EjRo1UVDjgjGJmHiLiRrjDS8RjJiIXxSRyPolPwUxiqByU+MFBLyCokFhOb8/qobpme2Z\nnd2dnu6dPe/z9DPV1dVVp6p7+nRVnzolqophGIZhJJGKuAUwDMMwjFyYkjIMwzASiyk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Nnnzkz6RSbN6sOHux6DJ9+qhk9l7q6pnXo\n3Dm8nFtuccerqpqec9hhTlmD632uXh1uNp10Fi9W/f73VX/+83TcRRe5en3ySXxyJYgkKKknvSJ5\nAPgx8DVgadGEgHOBuwNKyibzGslg40b3YP3KV9zfZexY3TLUN3OmC++6q5uguWBB5nyU7G8uYXkv\nW5aewzNtWjp9cJgu1xAiqI4YkRn3la+o/va3mWU//HDz9cwerho/PjOP3/wmdx0eeCAzLnuSamrL\nNxHXaNckQUkdCHTHeQq/HZiOnyDbZgFcno/hPJKnlJRN5jWSwWGHpR+ywYd/issuU121KvOcxkbV\nd95xCmjRItWttnK9o6uv1i29rbVrMz/Sp7YxY9zveeepHn98Ov6tt9I9o88+c72eMEWYrVg+/LDt\nbQCuF9lSUr2N3r1dr80oW6JUUoUazQ9Qt+rteuBUABH5pu+9tJVrgJ/hejQpMhY9FJHgoodPBdKl\nFircRIGLHoqILXpo5Gf
"text/plain": [
"<matplotlib.figure.Figure at 0x206d91f0>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# 显示回测结果\n",
"engine.showBacktestingResult()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'orderID': '1', 'direction': u'\\u591a', 'gatewayName': '', 'tradeID': '1', 'exchange': '', 'symbol': '', 'volume': 0, 'tradeTime': '2012-01-11 09:18:00', 'rawData': None, 'vtTradeID': '1', 'offset': u'\\u5f00\\u4ed3', 'vtOrderID': '1', 'dt': datetime.datetime(2012, 1, 11, 9, 18), 'price': 2464.4, 'vtSymbol': ''}\n",
"{'orderID': '2', 'direction': u'\\u7a7a', 'gatewayName': '', 'tradeID': '2', 'exchange': '', 'symbol': '', 'volume': 0, 'tradeTime': '2012-01-11 10:18:00', 'rawData': None, 'vtTradeID': '2', 'offset': u'\\u5e73\\u4ed3', 'vtOrderID': '2', 'dt': datetime.datetime(2012, 1, 11, 10, 18), 'price': 2448.8, 'vtSymbol': ''}\n",
"{'orderID': '3', 'direction': u'\\u7a7a', 'gatewayName': '', 'tradeID': '3', 'exchange': '', 'symbol': '', 'volume': 0, 'tradeTime': '2012-01-11 10:19:00', 'rawData': None, 'vtTradeID': '3', 'offset': u'\\u5f00\\u4ed3', 'vtOrderID': '3', 'dt': datetime.datetime(2012, 1, 11, 10, 19), 'price': 2448.4, 'vtSymbol': ''}\n",
"{'orderID': '4', 'direction': u'\\u591a', 'gatewayName': '', 'tradeID': '4', 'exchange': '', 'symbol': '', 'volume': 0, 'tradeTime': '2012-01-11 14:21:00', 'rawData': None, 'vtTradeID': '4', 'offset': u'\\u5e73\\u4ed3', 'vtOrderID': '4', 'dt': datetime.datetime(2012, 1, 11, 14, 21), 'price': 2456.2000000000003, 'vtSymbol': ''}\n",
"{'orderID': '5', 'direction': u'\\u591a', 'gatewayName': '', 'tradeID': '5', 'exchange': '', 'symbol': '', 'volume': 0, 'tradeTime': '2012-01-11 14:22:00', 'rawData': None, 'vtTradeID': '5', 'offset': u'\\u5f00\\u4ed3', 'vtOrderID': '5', 'dt': datetime.datetime(2012, 1, 11, 14, 22), 'price': 2455.6, 'vtSymbol': ''}\n",
"{'orderID': '6', 'direction': u'\\u7a7a', 'gatewayName': '', 'tradeID': '6', 'exchange': '', 'symbol': '', 'volume': 0, 'tradeTime': '2012-01-11 14:57:00', 'rawData': None, 'vtTradeID': '6', 'offset': u'\\u5e73\\u4ed3', 'vtOrderID': '6', 'dt': datetime.datetime(2012, 1, 11, 14, 57), 'price': 2444.0, 'vtSymbol': ''}\n",
"{'orderID': '7', 'direction': u'\\u7a7a', 'gatewayName': '', 'tradeID': '7', 'exchange': '', 'symbol': '', 'volume': 0, 'tradeTime': '2012-01-11 14:58:00', 'rawData': None, 'vtTradeID': '7', 'offset': u'\\u5f00\\u4ed3', 'vtOrderID': '7', 'dt': datetime.datetime(2012, 1, 11, 14, 58), 'price': 2445.8, 'vtSymbol': ''}\n",
"{'orderID': '8', 'direction': u'\\u591a', 'gatewayName': '', 'tradeID': '8', 'exchange': '', 'symbol': '', 'volume': 0, 'tradeTime': '2012-01-12 09:51:00', 'rawData': None, 'vtTradeID': '8', 'offset': u'\\u5e73\\u4ed3', 'vtOrderID': '8', 'dt': datetime.datetime(2012, 1, 12, 9, 51), 'price': 2452.8, 'vtSymbol': ''}\n",
"{'orderID': '9', 'direction': u'\\u591a', 'gatewayName': '', 'tradeID': '9', 'exchange': '', 'symbol': '', 'volume': 0, 'tradeTime': '2012-01-12 09:52:00', 'rawData': None, 'vtTradeID': '9', 'offset': u'\\u5f00\\u4ed3', 'vtOrderID': '9', 'dt': datetime.datetime(2012, 1, 12, 9, 52), 'price': 2452.0, 'vtSymbol': ''}\n",
"{'orderID': '10', 'direction': u'\\u7a7a', 'gatewayName': '', 'tradeID': '10', 'exchange': '', 'symbol': '', 'volume': 0, 'tradeTime': '2012-01-12 10:31:00', 'rawData': None, 'vtTradeID': '10', 'offset': u'\\u5e73\\u4ed3', 'vtOrderID': '10', 'dt': datetime.datetime(2012, 1, 12, 10, 31), 'price': 2455.2000000000003, 'vtSymbol': ''}\n"
]
}
],
"source": [
"# 显示前10条成交记录\n",
"for i in range(10):\n",
" print engine.tradeDict[str(i+1)].__dict__"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"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
}