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 06:57:51 +00:00
|
|
|
"execution_count": null,
|
2018-11-11 03:45:27 +00:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"engine = BacktestingEngine()\n",
|
|
|
|
"engine.setPeriod(datetime(2015, 1, 1), datetime(2018, 11, 9))\n",
|
2018-11-11 06:57:51 +00:00
|
|
|
"engine.initPortfolio('setting.csv', 10000000)"
|
2018-11-11 03:45:27 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2018-11-11 06:57:51 +00:00
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {
|
|
|
|
"scrolled": false
|
|
|
|
},
|
|
|
|
"outputs": [],
|
2018-11-11 03:45:27 +00:00
|
|
|
"source": [
|
|
|
|
"engine.loadData()\n",
|
|
|
|
"engine.runBacktesting()\n",
|
|
|
|
"engine.calculateResult()"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2018-11-11 06:57:51 +00:00
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {
|
|
|
|
"scrolled": false
|
|
|
|
},
|
|
|
|
"outputs": [],
|
2018-11-11 03:45:27 +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"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {
|
|
|
|
"scrolled": false
|
|
|
|
},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"for result in engine.resultList:\n",
|
2018-11-11 06:57:51 +00:00
|
|
|
" 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": [],
|
2018-11-11 06:57:51 +00:00
|
|
|
"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
|
|
|
|
}
|