vnpy/tests/backtesting/turtle.ipynb

145 lines
53 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",
2019-03-27 06:44:48 +00:00
"from vnpy.app.cta_strategy.strategies.atr_rsi_strategy import (\n",
" AtrRsiStrategy,\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",
2019-03-27 06:44:48 +00:00
" start=datetime(2013, 1, 1),\n",
" end=datetime(2019, 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-03-27 06:44:48 +00:00
"2019-03-27 11:54:16.262535\t开始加载历史数据\n",
"2019-03-27 11:54:30.753671\t历史数据加载完成数据量147566\n",
"2019-03-27 11:54:31.390710\t策略初始化完成\n",
"2019-03-27 11:54:31.390710\t开始回放历史数据\n",
"2019-03-27 11:54:39.884536\t历史数据回放结束\n",
"2019-03-27 11:54:39.884536\t开始计算逐日盯市盈亏\n",
"2019-03-27 11:54:39.900121\t逐日盯市盈亏计算完成\n",
"2019-03-27 11:54:39.900121\t开始计算策略统计指标\n",
"2019-03-27 11:54:39.915706\t------------------------------\n",
"2019-03-27 11:54:39.915706\t首个交易日\t2018-01-11\n",
"2019-03-27 11:54:39.915706\t最后交易日\t2019-02-15\n",
"2019-03-27 11:54:39.915706\t总交易日\t94\n",
"2019-03-27 11:54:39.915706\t盈利交易日\t27\n",
"2019-03-27 11:54:39.915706\t亏损交易日\t67\n",
"2019-03-27 11:54:39.915706\t起始资金\t1,000,000.00\n",
"2019-03-27 11:54:39.915706\t结束资金\t-6,174,412.20\n",
"2019-03-27 11:54:39.915706\t总收益率\t-717.44%\n",
"2019-03-27 11:54:39.915706\t年化收益\t-1,831.76%\n",
"2019-03-27 11:54:39.915706\t最大回撤: \t-8,415,878.66\n",
"2019-03-27 11:54:39.915706\t百分比最大回撤: -702.44%\n",
"2019-03-27 11:54:39.915706\t总盈亏\t-7,174,412.20\n",
"2019-03-27 11:54:39.915706\t总手续费\t6,900,212.20\n",
"2019-03-27 11:54:39.915706\t总滑点\t477,180.00\n",
"2019-03-27 11:54:39.915706\t总成交金额\t23,000,707,320.00\n",
"2019-03-27 11:54:39.915706\t总成交笔数\t7953\n",
"2019-03-27 11:54:39.915706\t日均盈亏\t-76,323.53\n",
"2019-03-27 11:54:39.915706\t日均手续费\t73,406.51\n",
"2019-03-27 11:54:39.915706\t日均滑点\t5,076.38\n",
"2019-03-27 11:54:39.915706\t日均成交金额\t244,688,375.74\n",
"2019-03-27 11:54:39.915706\t日均成交笔数\t84.6063829787234\n",
"2019-03-27 11:54:39.915706\t日均收益率\t-0.52%\n",
"2019-03-27 11:54:39.915706\t收益标准差\t29.62%\n",
"2019-03-27 11:54:39.915706\tSharpe Ratio\t-0.27\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
2019-03-27 06:44:48 +00:00
"C:\\Github\\vnpy\\vnpy\\app\\cta_strategy\\backtesting.py:331: RuntimeWarning: invalid value encountered in log\n",
" df[\"return\"] = np.log(df[\"balance\"] / df[\"balance\"].shift(1)).fillna(0)\n"
2019-01-30 01:54:51 +00:00
]
},
{
"data": {
2019-03-27 06:44:48 +00:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAoYAAAOSCAYAAADpuhFAAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodH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ac2jT+1aXJ19mC4G8iJee4+WCgECAQCbb6tTl6fffzvK5vYF+hGScmAw6syRek2Q/GnNo0/tWniqG3jT20af5ncpqWlpUk/Z2cfSn4bOB3AGDMRWBKvA/fKzaJr0EfZxvJ4HVJEREQkpXX2HsNngJONMe8ALuDKeB3Y5XJR0juHpZt2x+uQIiIiIimtUwdDa209cG2ijj+sVy5PvL+O+vowbrcrUacRERERSQmdfSg5oUp651BRXcfa7RVOlyIiIiKScAqGh1DSOxeA+au2OVyJiIiISOIpGB7CsF65lPTO5Uf//Jg3Pt3idDkiIiIiCaVgeAh+r5vHrprAoO7Z3PjkYqpr650uSURERCRhFAxb0S3bz/dPNWwpr+LFjzc5XY6IiIhIwigYtsFxQ3vSLz/EX+atcboUERERkYRRMGwDj9vFVyb2Y8Gq7VrXUERERNKWgmEbXTCumCyfm4ffWe10KSIiIiIJoWDYRnkhP+eO7sszH3zGjr3VTpcjIiIiEncKhu1w+bEDqKyp5++l650uRURERCTuFAzbYVivXPoXhFi8fqfTpYiIiIjEnYJhOxV1C/LZzn1OlyEiIiISdwqG7dQ3L8j6HQq
"text/plain": [
"<Figure size 720x1152 with 4 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"#%%\n",
2019-03-27 06:44:48 +00:00
"engine.add_strategy(AtrRsiStrategy, {})\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
}