vnpy/tests/backtesting/turtle.ipynb

145 lines
78 KiB
Plaintext
Raw Normal View History

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"#%%\n",
"from vnpy.app.cta_strategy.backtesting import BacktestingEngine\n",
"from vnpy.app.cta_strategy.strategies.turtle_signal_strategy import (\n",
" TurtleSignalStrategy,\n",
")\n",
"from datetime import datetime"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"#%%\n",
"engine = BacktestingEngine()\n",
"engine.set_parameters(\n",
" vt_symbol=\"IF99.CFFEX\",\n",
" interval=\"1m\",\n",
" start=datetime(2013, 1, 1),\n",
" end=datetime(2019, 3, 30),\n",
" rate=0,\n",
" slippage=0,\n",
" size=300,\n",
" pricetick=0.2,\n",
" capital=1_000_000,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"scrolled": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2019-01-30 12:25:59.002493\t开始加载历史数据\n",
"2019-01-30 12:26:34.081621\t历史数据加载完成数据量376572\n",
"2019-01-30 12:26:34.332922\t策略初始化完成\n",
"2019-01-30 12:26:34.333896\t开始回放历史数据\n",
"2019-01-30 12:27:21.518437\t历史数据回放结束\n",
"2019-01-30 12:27:21.518437\t开始计算逐日盯市盈亏\n",
"2019-01-30 12:27:21.898312\t逐日盯市盈亏计算完成\n",
"2019-01-30 12:27:21.898312\t开始计算策略统计指标\n",
"2019-01-30 12:27:21.929482\t------------------------------\n",
"2019-01-30 12:27:21.929482\t首个交易日\t2013-02-01\n",
"2019-01-30 12:27:21.929482\t最后交易日\t2019-01-30\n",
"2019-01-30 12:27:21.929482\t总交易日\t1459\n",
"2019-01-30 12:27:21.929482\t盈利交易日\t805\n",
"2019-01-30 12:27:21.929482\t亏损交易日\t654\n",
"2019-01-30 12:27:21.929482\t起始资金\t1,000,000.00\n",
"2019-01-30 12:27:21.929482\t结束资金\t220,899,784.04\n",
"2019-01-30 12:27:21.929482\t总收益率\t21,989.98%\n",
"2019-01-30 12:27:21.929482\t年化收益\t3,617.27%\n",
"2019-01-30 12:27:21.929482\t最大回撤: \t-57,892,062.00%\n",
"2019-01-30 12:27:21.929482\t百分比最大回撤: -1,199.55%\n",
"2019-01-30 12:27:21.929482\t总盈亏\t219,899,784.04%\n",
"2019-01-30 12:27:21.929482\t总手续费\t0.00\n",
"2019-01-30 12:27:21.929482\t总滑点\t0.00\n",
"2019-01-30 12:27:21.929482\t总成交金额\t96,983,115,547.79\n",
"2019-01-30 12:27:21.929482\t总成交笔数\t68478\n",
"2019-01-30 12:27:21.929482\t日均盈亏\t150,719.52\n",
"2019-01-30 12:27:21.929482\t日均手续费\t0.00\n",
"2019-01-30 12:27:21.929482\t日均滑点\t0.00\n",
"2019-01-30 12:27:21.929482\t日均成交金额\t66,472,320.46\n",
"2019-01-30 12:27:21.929482\t日均成交笔数\t46.93488690884167\n",
"2019-01-30 12:27:21.929482\t日均收益率\t1,322.46%\n",
"2019-01-30 12:27:21.929482\t收益标准差\t828.67%\n",
"2019-01-30 12:27:21.929482\tSharpe Ratio\t24.72\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Github\\vnpy\\vnpy\\app\\cta_strategy\\backtesting.py:306: RuntimeWarning: invalid value encountered in log\n",
" df[\"return\"] = (np.log(df[\"balance\"] - np.log(df[\"balance\"].shift(1)))).fillna(\n"
2019-01-30 01:54:51 +00:00
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAokAAAOSCAYAAAAYsUrNAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzs3Xd4VFX6wPHvTMqk915IIISb0Jv0rogINlxde69rd+3lt7qKbV1dcW3Ye0HRVVF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C7v+SQgghxCnadKiQ8QMCcdK2noHjUH4lZ72y1qZs8sBANh4qshxnFFWTEOaNmltBoKcr8SGmwO9YWXOQ+FtyLq5OWjY8MpMQbzfUZ+ai1WDz3oUXDbN8dtJq+OXuKVy2eAuFlXV8/7dJBHvrCPLSMSLKly+3HeXLbUd59a8jLPMf/ziQz4UDwrlz6U4CPV0ZEOzJn+klvHHFaOYND++aBjtFEiQ6AL1ezzXXXEN1dTULFy7kq6++4qKLLsLDwwMfHx/y8/OJijIFrzExMbi7u7NgwQJcXV0JDg4mPz+/zWc//fTTPPbYY2i1WoKDg7nuuusIDw/n4YcfprHRtFx/4cKFdvmeQgghRGetO1jANR9sY0iED8vuntri/M97cnhs6V6bsuFRvvzrLyOobTCQX67nr4u3cNeXOy2B2vRBwYT5moaQs0tMaXBKq+v4dV8ul42LJsTbdM7VuXnRyZkJIaw6kE9CmO1c/oEh3vx6zzR+2p3DyGg/S8fN3GHh7M4qA+C+r3cD8NLFw3nouz08sNzUq1hUVUdRlWkEcOG
"text/plain": [
"<Figure size 720x1152 with 4 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"#%%\n",
"engine.add_strategy(TurtleSignalStrategy, {})\n",
"engine.load_data()\n",
"engine.run_backtesting()\n",
"df = engine.calculate_result()\n",
"engine.calculate_statistics()\n",
"engine.show_chart()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}