vnpy/examples/cta_backtesting/portfolio.ipynb

224 lines
79 KiB
Plaintext
Raw Normal View History

2019-06-10 15:44:46 +00:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from vnpy.app.cta_strategy.backtesting import BacktestingEngine, OptimizationSetting\n",
"from vnpy.app.cta_strategy.strategies.atr_rsi_strategy import AtrRsiStrategy\n",
"from vnpy.app.cta_strategy.strategies.boll_channel_strategy import BollChannelStrategy\n",
"from datetime import datetime"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def run_backtesting(strategy_class, setting, vt_symbol, interval, start, end, rate, slippage, size, pricetick, capital):\n",
" engine = BacktestingEngine()\n",
" engine.set_parameters(\n",
" vt_symbol=vt_symbol,\n",
" interval=interval,\n",
" start=start,\n",
" end=end,\n",
" rate=rate,\n",
" slippage=slippage,\n",
" size=size,\n",
" pricetick=pricetick,\n",
" capital=capital \n",
" )\n",
" engine.add_strategy(strategy_class, setting)\n",
" engine.load_data()\n",
" engine.run_backtesting()\n",
" df = engine.calculate_result()\n",
" return df\n",
"\n",
"def show_portafolio(df):\n",
" engine = BacktestingEngine()\n",
" engine.calculate_statistics(df)\n",
" engine.show_chart(df)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2019-06-10 23:37:47.321851\t开始加载历史数据\n",
"2019-06-10 23:37:47.736877\t加载进度## [25%]\n",
"2019-06-10 23:37:47.990762\t加载进度##### [50%]\n",
"2019-06-10 23:37:48.298379\t加载进度####### [76%]\n",
"2019-06-10 23:37:48.518127\t加载进度########## [100%]\n",
"2019-06-10 23:37:48.518127\t历史数据加载完成数据量17280\n",
"2019-06-10 23:37:48.584513\t策略初始化完成\n",
"2019-06-10 23:37:48.584513\t开始回放历史数据\n",
"2019-06-10 23:37:49.319839\t历史数据回放结束\n",
"2019-06-10 23:37:49.319839\t开始计算逐日盯市盈亏\n",
"2019-06-10 23:37:49.324736\t逐日盯市盈亏计算完成\n"
]
}
],
"source": [
"df1 = run_backtesting(\n",
" strategy_class=AtrRsiStrategy, \n",
" setting={}, \n",
" vt_symbol=\"IF88.CFFEX\",\n",
" interval=\"1m\", \n",
" start=datetime(2019, 1, 1), \n",
" end=datetime(2019, 4, 30),\n",
" rate=0.3/10000,\n",
" slippage=0.2,\n",
" size=300,\n",
" pricetick=0.2,\n",
" capital=1_000_000,\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2019-06-10 23:37:54.953582\t开始加载历史数据\n",
"2019-06-10 23:37:55.452611\t加载进度## [25%]\n",
"2019-06-10 23:37:55.905706\t加载进度##### [50%]\n",
"2019-06-10 23:37:56.352015\t加载进度####### [76%]\n",
"2019-06-10 23:37:56.885187\t加载进度########## [100%]\n",
"2019-06-10 23:37:56.885187\t历史数据加载完成数据量27168\n",
"2019-06-10 23:37:56.903729\t策略初始化完成\n",
"2019-06-10 23:37:56.903729\t开始回放历史数据\n",
"2019-06-10 23:37:57.144939\t历史数据回放结束\n",
"2019-06-10 23:37:57.144939\t开始计算逐日盯市盈亏\n",
"2019-06-10 23:37:57.150828\t逐日盯市盈亏计算完成\n"
]
}
],
"source": [
"df2 = run_backtesting(\n",
" strategy_class=BollChannelStrategy, \n",
" setting={'fixed_size': 16}, \n",
" vt_symbol=\"RB88.SHFE\",\n",
" interval=\"1m\", \n",
" start=datetime(2019, 1, 1), \n",
" end=datetime(2019, 4, 30),\n",
" rate=1/10000,\n",
" slippage=1,\n",
" size=10,\n",
" pricetick=1,\n",
" capital=1_000_000,\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2019-06-10 23:38:00.683004\t开始计算策略统计指标\n",
"2019-06-10 23:38:00.799241\t------------------------------\n",
"2019-06-10 23:38:00.799241\t首个交易日\t2019-01-16\n",
"2019-06-10 23:38:00.799241\t最后交易日\t2019-04-19\n",
"2019-06-10 23:38:00.799241\t总交易日\t62\n",
"2019-06-10 23:38:00.799241\t盈利交易日\t31\n",
"2019-06-10 23:38:00.799241\t亏损交易日\t31\n",
"2019-06-10 23:38:00.799241\t起始资金\t1,000,000.00\n",
"2019-06-10 23:38:00.799241\t结束资金\t1,094,312.31\n",
"2019-06-10 23:38:00.799241\t总收益率\t9.43%\n",
"2019-06-10 23:38:00.799241\t年化收益\t36.51%\n",
"2019-06-10 23:38:00.799241\t最大回撤: \t-119,944.93\n",
"2019-06-10 23:38:00.799241\t百分比最大回撤: -10.84%\n",
"2019-06-10 23:38:00.799241\t总盈亏\t94,312.31\n",
"2019-06-10 23:38:00.799241\t总手续费\t11,787.69\n",
"2019-06-10 23:38:00.799241\t总滑点\t23,500.00\n",
"2019-06-10 23:38:00.799241\t总成交金额\t333,377,980.00\n",
"2019-06-10 23:38:00.799241\t总成交笔数\t320.0\n",
"2019-06-10 23:38:00.799241\t日均盈亏\t1,521.17\n",
"2019-06-10 23:38:00.799241\t日均手续费\t190.12\n",
"2019-06-10 23:38:00.799241\t日均滑点\t379.03\n",
"2019-06-10 23:38:00.799241\t日均成交金额\t5,377,064.19\n",
"2019-06-10 23:38:00.799241\t日均成交笔数\t5.161290322580645\n",
"2019-06-10 23:38:00.800195\t日均收益率\t0.14%\n",
"2019-06-10 23:38:00.800195\t收益标准差\t1.86%\n",
"2019-06-10 23:38:00.800195\tSharpe Ratio\t1.20\n",
"2019-06-10 23:38:00.800195\t收益回撤比\t0.87\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAn8AAAOSCAYAAAAF47YTAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREF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qWgusG3ft5+O+fxx4fNy1VuDKCe43o7ZCCBEotSYzTd0WvnZR2hmPbVxg4A+lJyiv62RD0ZmPCxGO9Ho9JpMJg8EwYZmUcGS32zGZTOj1+mB3RYo8CyHEdHY4l3U3ThDcrZ1nIFKrYUeVUYI/IZzmzJlDQ0NDQOvcDQ8PExUVFbDX84Zerx8tLRNMEvwJIcQ0So8byU2OocBw5kkecbpIzslLkX1/QowRFRUV8NMtzuYEmUDzZcKHEELMOlabnZ3VRjYUTb58taEojUON3XT2DwW4d0IIfzppMtPUFfrlYzwlwZ8QQkyhorGbHsvIlEu6GxekYbdDWY0pgD0TQvjbt/+yj//cfjTY3fA5Cf6EEGIKrv1+582fPPhbPieJBF3kaFshxOzQ2DlAjPNEn9lEgj8hhJhCaZWRRVkJpCdMfoJHZISWdfMN7DguwZ8Qs4XNZsfUP0RaQnSwu+JzEvwJIcQkBoas7K3tnDDLd7yNRWmc7DBz0mQOQM+EEP7WNTCM1WYnLX72Hd0owZ8QQkxib10HQ1YbGxZMH/ytLkgB4GDjlKdPCiHOEsa+QYApZ/3PVhL8CSFC1rDVhs0
"text/plain": [
"<Figure size 720x1152 with 4 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"dfp = df1 + df2\n",
"dfp =dfp.dropna() \n",
"show_portafolio(dfp)"
]
},
{
"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"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}