2018-11-11 03:45:27 +00:00
|
|
|
|
{
|
|
|
|
|
"cells": [
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"execution_count": 1,
|
|
|
|
|
"metadata": {},
|
|
|
|
|
"outputs": [],
|
|
|
|
|
"source": [
|
|
|
|
|
"%matplotlib inline\n",
|
|
|
|
|
"\n",
|
|
|
|
|
"from datetime import datetime\n",
|
2018-11-11 06:57:51 +00:00
|
|
|
|
"\n",
|
|
|
|
|
"import numpy as np\n",
|
2018-11-11 03:45:27 +00:00
|
|
|
|
"import matplotlib.pyplot as plt\n",
|
|
|
|
|
"\n",
|
|
|
|
|
"from turtleEngine import BacktestingEngine"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
2018-11-11 07:25:59 +00:00
|
|
|
|
"execution_count": 2,
|
2018-11-11 03:45:27 +00:00
|
|
|
|
"metadata": {},
|
2018-11-11 08:04:25 +00:00
|
|
|
|
"outputs": [
|
|
|
|
|
{
|
|
|
|
|
"name": "stdout",
|
|
|
|
|
"output_type": "stream",
|
|
|
|
|
"text": [
|
2018-11-13 08:11:35 +00:00
|
|
|
|
"16:07:40.112000:\t投资组合的合约代码['000300', '000905']\n",
|
|
|
|
|
"16:07:40.112000:\t投资组合的初始价值10000000\n"
|
2018-11-11 08:04:25 +00:00
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
],
|
2018-11-11 03:45:27 +00:00
|
|
|
|
"source": [
|
|
|
|
|
"engine = BacktestingEngine()\n",
|
2018-11-13 08:11:35 +00:00
|
|
|
|
"engine.setPeriod(datetime(2014, 1, 1), datetime(2018, 12, 30))\n",
|
2018-11-11 08:04:25 +00:00
|
|
|
|
"engine.initPortfolio('test.csv', 10000000)"
|
2018-11-11 03:45:27 +00:00
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
2018-11-11 07:25:59 +00:00
|
|
|
|
"execution_count": 3,
|
2018-11-11 06:57:51 +00:00
|
|
|
|
"metadata": {
|
|
|
|
|
"scrolled": false
|
|
|
|
|
},
|
2018-11-11 07:25:59 +00:00
|
|
|
|
"outputs": [
|
|
|
|
|
{
|
|
|
|
|
"name": "stdout",
|
|
|
|
|
"output_type": "stream",
|
|
|
|
|
"text": [
|
2018-11-13 08:11:35 +00:00
|
|
|
|
"16:07:40.182000:\t000300数据加载完成,总数据量:1185\n",
|
|
|
|
|
"16:07:40.221000:\t000905数据加载完成,总数据量:1185\n",
|
|
|
|
|
"16:07:40.221000:\t全部数据加载完成\n",
|
|
|
|
|
"16:07:40.221000:\t开始回放K线数据\n",
|
|
|
|
|
"16:07:40.355000:\tK线数据回放结束\n",
|
|
|
|
|
"16:07:40.355000:\t开始统计回测结果\n",
|
|
|
|
|
"16:07:40.362000:\t回测结果统计结束\n"
|
2018-11-11 07:25:59 +00:00
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
],
|
2018-11-11 03:45:27 +00:00
|
|
|
|
"source": [
|
|
|
|
|
"engine.loadData()\n",
|
|
|
|
|
"engine.runBacktesting()\n",
|
|
|
|
|
"engine.calculateResult()"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
2018-11-11 07:25:59 +00:00
|
|
|
|
"execution_count": 4,
|
2018-11-11 08:04:25 +00:00
|
|
|
|
"metadata": {},
|
2018-11-11 07:25:59 +00:00
|
|
|
|
"outputs": [
|
|
|
|
|
{
|
|
|
|
|
"data": {
|
|
|
|
|
"text/plain": [
|
2018-11-13 08:11:35 +00:00
|
|
|
|
"[<matplotlib.lines.Line2D at 0x8238270>]"
|
2018-11-11 07:25:59 +00:00
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
"execution_count": 4,
|
|
|
|
|
"metadata": {},
|
|
|
|
|
"output_type": "execute_result"
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"data": {
|
2018-11-13 08:11:35 +00:00
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B++s3NvDtKQNJirdZFle02dbO38RfLxnPmH6ZnPuvT4DmZrwvdx1lTP9MEj3t8vG2OE4+Ri29yd2LNvOHtzd7v1R+9vLaFol+1d6yFuWz0xIprWkkLyMJgKE57kS/o6SGyUP64HIZXMZgi5Mu/WqrrLMzpl9mQGXnjOnLffMm8vrq/U3PBiDB1vy6a33+7l4qLOKlwqKQrEGrc93EKLvTRaItjvg4Cajt8MOth73b9y7e7O3i5svpMvzhrU3s8/xcveaZldz55kbrggZvkgeI99ToUxJtPHT5JKDllAjXzRrWZl+Trsxb8tzyPbz45V4+2HKIu/67CXC3TT+7bA8VtdZ18QsFu9PFe5sOedubW4+t2H73OTz/o5O4eNIAxvTP5H8/mQm4+5OfePd77CipISWh81+cVx6jEuByGZ76fHeLfa9dN4Mnv+/+xQbuppZEWxy/eGUdBbe9zdBfLmL4HYu5/DF/Q306VllnJzMl8PrvN8b354mrTuSJq05k973nUZCdylLPZyWcenxpoo9RdqeLeJtgs0lAbfS2uOY/lRW7y3h91f42ZQp3H+WxT3e1+ald3eDgoQ93+J0sqjMq6lomU98mp7PH5nPNqUNZfPMp3n1ZqYmcMqJlrfJGT7c537b7VXvLmPfIsjbHb+2O1zdw22vr+cGThTzx2S4Athys4ldvbODGF1d37aRCxO50ccPzq9h0wF0LfXDpDq5+upAPPW3t2w5VkZZoY/zAXoC7hj5jWI63lpyV6p5v5mBlvfeC7A2zR3Q6jp+cMZJ3bjmlxb53Nhzko20lPLt8D18druZX543m8pMG88Xtsxm
|
2018-11-11 07:25:59 +00:00
|
|
|
|
"text/plain": [
|
2018-11-13 08:11:35 +00:00
|
|
|
|
"<matplotlib.figure.Figure at 0x81b79d0>"
|
2018-11-11 07:25:59 +00:00
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
"metadata": {},
|
|
|
|
|
"output_type": "display_data"
|
|
|
|
|
}
|
|
|
|
|
],
|
2018-11-11 03:45:27 +00:00
|
|
|
|
"source": [
|
2018-11-11 08:04:25 +00:00
|
|
|
|
"# 汇总每日盈亏,绘制简单资金曲线\n",
|
2018-11-11 03:45:27 +00:00
|
|
|
|
"l = [result.totalPnl for result in engine.resultList]\n",
|
2018-11-13 08:11:35 +00:00
|
|
|
|
"dateList = [result.date for result in engine.resultList]\n",
|
2018-11-11 03:45:27 +00:00
|
|
|
|
"equity = np.cumsum(l)\n",
|
2018-11-13 08:11:35 +00:00
|
|
|
|
"plt.plot(dateList, equity)"
|
2018-11-11 03:45:27 +00:00
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
2018-11-13 08:11:35 +00:00
|
|
|
|
"execution_count": null,
|
2018-11-11 08:04:25 +00:00
|
|
|
|
"metadata": {
|
|
|
|
|
"scrolled": false
|
|
|
|
|
},
|
2018-11-13 08:11:35 +00:00
|
|
|
|
"outputs": [],
|
2018-11-11 08:04:25 +00:00
|
|
|
|
"source": [
|
|
|
|
|
"# 查看逐笔的交易记录\n",
|
|
|
|
|
"for dt, l in engine.tradeDict.items():\n",
|
|
|
|
|
" for trade in l:\n",
|
2018-11-13 08:11:35 +00:00
|
|
|
|
" print dt, trade.vtSymbol, trade.direction, trade.offset, trade.price, trade.volume"
|
2018-11-11 08:04:25 +00:00
|
|
|
|
]
|
2018-11-11 03:45:27 +00:00
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
"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.14"
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
"nbformat": 4,
|
|
|
|
|
"nbformat_minor": 2
|
|
|
|
|
}
|