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": {},
|
|
|
|
"outputs": [
|
|
|
|
{
|
2018-11-11 06:57:51 +00:00
|
|
|
"ename": "TypeError",
|
|
|
|
"evalue": "coercing to Unicode: need string or buffer, int found",
|
2018-11-11 03:45:27 +00:00
|
|
|
"output_type": "error",
|
|
|
|
"traceback": [
|
|
|
|
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
|
2018-11-11 06:57:51 +00:00
|
|
|
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
|
|
|
|
"\u001b[1;32m<ipython-input-2-a69b1c674549>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mengine\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mBacktestingEngine\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[0mengine\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msetPeriod\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdatetime\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m2015\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdatetime\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m2018\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m11\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m9\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mengine\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0minitPortfolio\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m10000000\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'setting.csv'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
|
|
|
|
"\u001b[1;32mC:\\Github\\vnpy\\examples\\TurtleStrategy\\turtleEngine.py\u001b[0m in \u001b[0;36minitPortfolio\u001b[1;34m(self, filename, portfolioValue)\u001b[0m\n\u001b[0;32m 60\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0minitPortfolio\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfilename\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mportfolioValue\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m10000000\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 61\u001b[0m \u001b[1;34m\"\"\"\"\"\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 62\u001b[1;33m \u001b[1;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfilename\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 63\u001b[0m \u001b[0mr\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mDictReader\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mf\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 64\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0md\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mr\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
|
|
|
|
"\u001b[1;31mTypeError\u001b[0m: coercing to Unicode: need string or buffer, int found"
|
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",
|
|
|
|
"engine.initPortfolio('setting.csv')"
|
2018-11-11 03:45:27 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {
|
|
|
|
"scrolled": false
|
|
|
|
},
|
|
|
|
"outputs": [],
|
|
|
|
"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",
|
|
|
|
"execution_count": null,
|
2018-11-11 06:57:51 +00:00
|
|
|
"metadata": {
|
|
|
|
"scrolled": true
|
|
|
|
},
|
2018-11-11 03:45:27 +00:00
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"l = [result.totalPnl for result in engine.resultList]\n",
|
|
|
|
"equity = np.cumsum(l)\n",
|
|
|
|
"plt.plot(equity)"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"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
|
|
|
|
}
|