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",
|
|
|
|
|
"execution_count": 2,
|
|
|
|
|
"metadata": {},
|
2018-11-11 07:25:59 +00:00
|
|
|
|
"outputs": [],
|
2018-11-11 03:45:27 +00:00
|
|
|
|
"source": [
|
2018-11-11 06:57:51 +00:00
|
|
|
|
"engine = BacktestingEngine()\n",
|
|
|
|
|
"engine.setPeriod(datetime(2015, 1, 1), datetime(2018, 11, 9))\n",
|
2018-11-11 07:25:59 +00:00
|
|
|
|
"engine.initPortfolio('setting.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 03:45:27 +00:00
|
|
|
|
"metadata": {
|
|
|
|
|
"scrolled": false
|
|
|
|
|
},
|
2018-11-11 07:25:59 +00:00
|
|
|
|
"outputs": [
|
|
|
|
|
{
|
|
|
|
|
"name": "stdout",
|
|
|
|
|
"output_type": "stream",
|
|
|
|
|
"text": [
|
|
|
|
|
"15:22:46.074000:000300.XSHG数据加载完成,总数据量:940\n",
|
|
|
|
|
"15:22:46.074000:全部数据加载完成\n"
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
],
|
2018-11-11 03:45:27 +00:00
|
|
|
|
"source": [
|
2018-11-11 06:57:51 +00:00
|
|
|
|
"engine.loadData()\n",
|
|
|
|
|
"engine.runBacktesting()\n",
|
|
|
|
|
"engine.calculateResult()"
|
2018-11-11 03:45:27 +00:00
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"execution_count": null,
|
2018-11-11 06:57:51 +00:00
|
|
|
|
"metadata": {
|
|
|
|
|
"scrolled": false
|
|
|
|
|
},
|
2018-11-11 03:45:27 +00:00
|
|
|
|
"outputs": [],
|
2018-11-11 06:57:51 +00:00
|
|
|
|
"source": [
|
|
|
|
|
"for dt, l in engine.tradeDict.items():\n",
|
|
|
|
|
" print dt\n",
|
|
|
|
|
" for trade in l:\n",
|
|
|
|
|
" print trade.vtSymbol, trade.direction, trade.offset, trade.price, trade.volume"
|
|
|
|
|
]
|
2018-11-11 03:45:27 +00:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"execution_count": null,
|
2018-11-11 06:57:51 +00:00
|
|
|
|
"metadata": {
|
|
|
|
|
"scrolled": false
|
|
|
|
|
},
|
2018-11-11 03:45:27 +00:00
|
|
|
|
"outputs": [],
|
|
|
|
|
"source": [
|
2018-11-11 06:57:51 +00:00
|
|
|
|
"for result in engine.resultList:\n",
|
|
|
|
|
" if result.totalPnl:\n",
|
|
|
|
|
" print result.date, result.totalPnl"
|
2018-11-11 03:45:27 +00:00
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
2018-11-11 07:25:59 +00:00
|
|
|
|
"execution_count": 4,
|
2018-11-11 06:57:51 +00:00
|
|
|
|
"metadata": {
|
|
|
|
|
"scrolled": true
|
|
|
|
|
},
|
2018-11-11 07:25:59 +00:00
|
|
|
|
"outputs": [
|
|
|
|
|
{
|
|
|
|
|
"data": {
|
|
|
|
|
"text/plain": [
|
|
|
|
|
"[<matplotlib.lines.Line2D at 0x8190eb0>]"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
"execution_count": 4,
|
|
|
|
|
"metadata": {},
|
|
|
|
|
"output_type": "execute_result"
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"data": {
|
|
|
|
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYAAAAEDCAYAAAA849PJAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvNQv5yAAAIABJREFUeJzt3Xd8m+W99/HPz7Lkve0QZzh7FggEEwi0NIQwCgVaSimhD4dOSgstLeWU0T6l6zylk1HSlnScAx1AS+FAKauBsMNIIEASyF7OtB3He/t6/pCseCa2JUuW9H2/Xnrlvi9d0n1JkfXTtc05h4iIJJ6kaBdARESiQwFARCRBKQCIiCQoBQARkQSlACAikqAUAEREEtSIDwBm9kcz229mawaQ9zYzWx24bTCzg5Eoo4hILLKRPg/AzE4D6oB7nXNHD+JxXwWOd859btgKJyISw0Z8DcA59wJwoGuamU0xsyfNbJWZvWhmM/t46GLgvogUUkQkBiVHuwBDtBS4yjm30cxOAn4NLOy808wmAJOAZ6NUPhGRES/mAoCZZQKnAH83s87klB7ZLgUedM61R7JsIiKxJOYCAP5mq4POueMOk+dS4OoIlUdEJCaN+D6AnpxzNcBWM/skgPnN6bzfzGYAecCKKBVRRCQmjPgAYGb34f8yn2FmZWb2eeDTwOfN7G1gLXBhl4csBu53I314k4hIlI34YaAiIjI8RnwNQEREhseI7gQuLCx0EydOjHYxRERixqpVqyqcc0UDyTuiA8DEiRNZuXJltIshIhIzzGz7QPOqCUhEJEEpAIiIJCgFABGRBKUAICKSoBQAREQSlAKAiEiCUgAQEUlQCgBDsGzdPnYfbIx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
|
|
]
|
|
|
|
|
},
|
|
|
|
|
"metadata": {},
|
|
|
|
|
"output_type": "display_data"
|
|
|
|
|
}
|
|
|
|
|
],
|
2018-11-11 03:45:27 +00:00
|
|
|
|
"source": [
|
|
|
|
|
"l = [result.totalPnl for result in engine.resultList]\n",
|
|
|
|
|
"equity = np.cumsum(l)\n",
|
|
|
|
|
"plt.plot(equity)"
|
|
|
|
|
]
|
|
|
|
|
},
|
2018-11-11 07:25:59 +00:00
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"execution_count": null,
|
|
|
|
|
"metadata": {},
|
|
|
|
|
"outputs": [],
|
|
|
|
|
"source": []
|
|
|
|
|
},
|
2018-11-11 03:45:27 +00:00
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"execution_count": null,
|
|
|
|
|
"metadata": {},
|
|
|
|
|
"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.14"
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
"nbformat": 4,
|
|
|
|
|
"nbformat_minor": 2
|
|
|
|
|
}
|