vnpy/tests/backtesting/turtle.ipynb

145 lines
68 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=\"XBTUSD.BITMEX\",\n",
" interval=\"1m\",\n",
" start=datetime(2018, 1, 1),\n",
" end=datetime(2018, 4, 30),\n",
" rate=3.0/10000,\n",
" slippage=0.2,\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-02-15 16:04:41.398818\t开始加载历史数据\n",
"2019-02-15 16:04:52.954926\t历史数据加载完成数据量135570\n",
"2019-02-15 16:04:54.248921\t策略初始化完成\n",
"2019-02-15 16:04:54.248921\t开始回放历史数据\n",
"2019-02-15 16:05:07.653733\t历史数据回放结束\n",
"2019-02-15 16:05:07.654709\t开始计算逐日盯市盈亏\n",
"2019-02-15 16:05:07.765065\t逐日盯市盈亏计算完成\n",
"2019-02-15 16:05:07.766042\t开始计算策略统计指标\n",
"2019-02-15 16:05:07.781667\t------------------------------\n",
"2019-02-15 16:05:07.781667\t首个交易日\t2018-01-21\n",
"2019-02-15 16:05:07.781667\t最后交易日\t2018-04-05\n",
"2019-02-15 16:05:07.781667\t总交易日\t75\n",
"2019-02-15 16:05:07.781667\t盈利交易日\t28\n",
"2019-02-15 16:05:07.781667\t亏损交易日\t47\n",
"2019-02-15 16:05:07.781667\t起始资金\t1,000,000.00\n",
"2019-02-15 16:05:07.781667\t结束资金\t-4,996,529.76\n",
"2019-02-15 16:05:07.782644\t总收益率\t-599.65%\n",
"2019-02-15 16:05:07.782644\t年化收益\t-1,918.89%\n",
"2019-02-15 16:05:07.782644\t最大回撤: \t-7,982,815.30\n",
"2019-02-15 16:05:07.782644\t百分比最大回撤: -1,307.96%\n",
"2019-02-15 16:05:07.782644\t总盈亏\t-5,996,529.76\n",
"2019-02-15 16:05:07.782644\t总手续费\t26,268,309.76\n",
"2019-02-15 16:05:07.782644\t总滑点\t1,871,880.00\n",
"2019-02-15 16:05:07.782644\t总成交金额\t87,561,032,520.00\n",
"2019-02-15 16:05:07.782644\t总成交笔数\t20745\n",
"2019-02-15 16:05:07.782644\t日均盈亏\t-79,953.73\n",
"2019-02-15 16:05:07.782644\t日均手续费\t350,244.13\n",
"2019-02-15 16:05:07.782644\t日均滑点\t24,958.40\n",
"2019-02-15 16:05:07.782644\t日均成交金额\t1,167,480,433.60\n",
"2019-02-15 16:05:07.782644\t日均成交笔数\t276.6\n",
"2019-02-15 16:05:07.782644\t日均收益率\t32.92%\n",
"2019-02-15 16:05:07.782644\t收益标准差\t200.71%\n",
"2019-02-15 16:05:07.782644\tSharpe Ratio\t2.54\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": "iVBORw0KGgoAAAANSUhEUgAAAoUAAAOSCAYAAAACjapDAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzs3Xd81EX+x/HXbnogCSRAQoBACgyhQ+gdu4gNO54VG2e502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Akv/AsLCyM9Pd2nA6Wnw/f7V/DJ0h2MP6MHnROifWqnNrlcLp/7G8i2pWZojOoHjVPdpzGqHxriZ15mZmatHq++Tx9/CriNMfOBZwC/3kzwj+emEx0Rwr2frMTj0alvERERaTjq9ZlCa60HuK22jte8SSj3jU7nNx+u4J9LtnL1gPa1dWgRERERv6rvZwpr3dg+bRiUEscTU9eyp8Ad6HBEREREaoSSwlPkcDh4+OJuuEs8PPylLjwWERGRhkFJoQ9SWzZlwshUvlixk9nr9gY6HBEREZFqU1LoowkjU0lp0YT7P1uFu0R7F4qIiEj9pqTQR+EhQTx8cTe25hzmH99uCHQ4IiIiItWipLAaBqe2YGyfNrwyO4tN+w4FOhwRERERnykprKaJ53bG6XDw0kydLRQREZH6S0lhNbWKCueq/kl8snQH2w8cDnQ4IiIiIj5RUlgDbh2RgsMBr8zaGOhQRERERHyipLAGtI6J4NKMtnzw/TZ252tDaxEREal/lBTWkAkj0ijzeHl1ts4Wioi
"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
}