2019-01-29 07:35:37 +00:00
|
|
|
|
{
|
|
|
|
|
"cells": [
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"execution_count": 1,
|
|
|
|
|
"metadata": {},
|
2019-01-29 20:39:04 +00:00
|
|
|
|
"outputs": [],
|
|
|
|
|
"source": [
|
|
|
|
|
"#%%\n",
|
2019-04-15 15:32:04 +00:00
|
|
|
|
"from vnpy.app.cta_strategy.backtesting import BacktestingEngine, OptimizationSetting\n",
|
2019-03-27 06:44:48 +00:00
|
|
|
|
"from vnpy.app.cta_strategy.strategies.atr_rsi_strategy import (\n",
|
|
|
|
|
" AtrRsiStrategy,\n",
|
2019-01-29 20:39:04 +00:00
|
|
|
|
")\n",
|
|
|
|
|
"from datetime import datetime"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
2019-04-15 15:32:04 +00:00
|
|
|
|
"execution_count": 4,
|
2019-01-29 20:39:04 +00:00
|
|
|
|
"metadata": {},
|
|
|
|
|
"outputs": [],
|
|
|
|
|
"source": [
|
|
|
|
|
"#%%\n",
|
|
|
|
|
"engine = BacktestingEngine()\n",
|
|
|
|
|
"engine.set_parameters(\n",
|
2019-04-15 15:32:04 +00:00
|
|
|
|
" vt_symbol=\"IF88.CFFEX\",\n",
|
2019-01-29 20:39:04 +00:00
|
|
|
|
" interval=\"1m\",\n",
|
2019-04-15 15:32:04 +00:00
|
|
|
|
" start=datetime(2019, 1, 1),\n",
|
2019-03-27 06:44:48 +00:00
|
|
|
|
" end=datetime(2019, 4, 30),\n",
|
2019-04-15 15:32:04 +00:00
|
|
|
|
" rate=0.3/10000,\n",
|
2019-02-16 02:13:22 +00:00
|
|
|
|
" slippage=0.2,\n",
|
2019-01-29 20:39:04 +00:00
|
|
|
|
" size=300,\n",
|
|
|
|
|
" pricetick=0.2,\n",
|
|
|
|
|
" capital=1_000_000,\n",
|
2019-04-15 15:32:04 +00:00
|
|
|
|
")\n",
|
|
|
|
|
"engine.add_strategy(AtrRsiStrategy, {})"
|
2019-01-29 20:39:04 +00:00
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
2019-04-15 15:32:04 +00:00
|
|
|
|
"execution_count": null,
|
2019-01-29 20:39:04 +00:00
|
|
|
|
"metadata": {
|
|
|
|
|
"scrolled": false
|
|
|
|
|
},
|
2019-04-15 15:32:04 +00:00
|
|
|
|
"outputs": [],
|
|
|
|
|
"source": [
|
|
|
|
|
"#%%\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": 5,
|
|
|
|
|
"metadata": {},
|
2019-01-29 07:35:37 +00:00
|
|
|
|
"outputs": [
|
|
|
|
|
{
|
2019-01-29 20:39:04 +00:00
|
|
|
|
"name": "stdout",
|
|
|
|
|
"output_type": "stream",
|
|
|
|
|
"text": [
|
2019-04-15 15:32:04 +00:00
|
|
|
|
"2019-04-15 22:19:49.696835\t参数:{'atr_length': 22}, 目标:121.19996051999999\n",
|
|
|
|
|
"2019-04-15 22:19:49.709531\t参数:{'atr_length': 23}, 目标:116.54901966000013\n",
|
|
|
|
|
"2019-04-15 22:19:49.710507\t参数:{'atr_length': 24}, 目标:113.29820520000014\n"
|
2019-01-30 01:54:51 +00:00
|
|
|
|
]
|
2019-01-30 04:40:49 +00:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"data": {
|
|
|
|
|
"text/plain": [
|
2019-04-15 15:32:04 +00:00
|
|
|
|
"[(\"{'atr_length': 22}\",\n",
|
|
|
|
|
" 121.19996051999999,\n",
|
|
|
|
|
" {'start_date': datetime.date(2013, 1, 18),\n",
|
|
|
|
|
" 'end_date': datetime.date(2019, 4, 11),\n",
|
|
|
|
|
" 'total_days': 1514,\n",
|
|
|
|
|
" 'profit_days': 763,\n",
|
|
|
|
|
" 'loss_days': 750,\n",
|
|
|
|
|
" 'capital': 1000000,\n",
|
|
|
|
|
" 'end_balance': 2211999.6052,\n",
|
|
|
|
|
" 'max_drawdown': -248787.6971999996,\n",
|
|
|
|
|
" 'max_ddpercent': -12.636908338002794,\n",
|
|
|
|
|
" 'total_net_pnl': 1211999.6052000003,\n",
|
|
|
|
|
" 'daily_net_pnl': 800.5281408190227,\n",
|
|
|
|
|
" 'total_commission': 242400.39479999998,\n",
|
|
|
|
|
" 'daily_commission': 160.10594108322323,\n",
|
|
|
|
|
" 'total_slippage': 481860.0,\n",
|
|
|
|
|
" 'daily_slippage': 318.2694848084544,\n",
|
|
|
|
|
" 'total_turnover': 8080013160.0,\n",
|
|
|
|
|
" 'daily_turnover': 5336864.702774108,\n",
|
|
|
|
|
" 'total_trade_count': 8031,\n",
|
|
|
|
|
" 'daily_trade_count': 5.30449141347424,\n",
|
|
|
|
|
" 'total_return': 121.19996051999999,\n",
|
|
|
|
|
" 'annual_return': 19.212675379656538,\n",
|
|
|
|
|
" 'daily_return': 0.052348808029058974,\n",
|
|
|
|
|
" 'return_std': 0.9487639654919149,\n",
|
|
|
|
|
" 'sharpe_ratio': 0.854779772691872,\n",
|
|
|
|
|
" 'return_drawdown_ratio': 9.590950355754112}),\n",
|
|
|
|
|
" (\"{'atr_length': 23}\",\n",
|
|
|
|
|
" 116.54901966000013,\n",
|
|
|
|
|
" {'start_date': datetime.date(2013, 1, 18),\n",
|
|
|
|
|
" 'end_date': datetime.date(2019, 4, 11),\n",
|
|
|
|
|
" 'total_days': 1514,\n",
|
|
|
|
|
" 'profit_days': 759,\n",
|
|
|
|
|
" 'loss_days': 754,\n",
|
|
|
|
|
" 'capital': 1000000,\n",
|
|
|
|
|
" 'end_balance': 2165490.1966000013,\n",
|
|
|
|
|
" 'max_drawdown': -232904.1239999996,\n",
|
|
|
|
|
" 'max_ddpercent': -13.536251422505968,\n",
|
|
|
|
|
" 'total_net_pnl': 1165490.1966000004,\n",
|
|
|
|
|
" 'daily_net_pnl': 769.8085842800531,\n",
|
|
|
|
|
" 'total_commission': 242769.80339999998,\n",
|
|
|
|
|
" 'daily_commission': 160.34993619550858,\n",
|
|
|
|
|
" 'total_slippage': 482700.0,\n",
|
|
|
|
|
" 'daily_slippage': 318.82430647291943,\n",
|
|
|
|
|
" 'total_turnover': 8092326780.0,\n",
|
|
|
|
|
" 'daily_turnover': 5344997.873183619,\n",
|
|
|
|
|
" 'total_trade_count': 8045,\n",
|
|
|
|
|
" 'daily_trade_count': 5.313738441215324,\n",
|
|
|
|
|
" 'total_return': 116.54901966000013,\n",
|
|
|
|
|
" 'annual_return': 18.475406022721288,\n",
|
|
|
|
|
" 'daily_return': 0.0509452313711608,\n",
|
|
|
|
|
" 'return_std': 0.961380153488665,\n",
|
|
|
|
|
" 'sharpe_ratio': 0.8209448965768181,\n",
|
|
|
|
|
" 'return_drawdown_ratio': 8.610139987960078}),\n",
|
|
|
|
|
" (\"{'atr_length': 24}\",\n",
|
|
|
|
|
" 113.29820520000014,\n",
|
|
|
|
|
" {'start_date': datetime.date(2013, 1, 18),\n",
|
|
|
|
|
" 'end_date': datetime.date(2019, 4, 11),\n",
|
|
|
|
|
" 'total_days': 1514,\n",
|
|
|
|
|
" 'profit_days': 760,\n",
|
|
|
|
|
" 'loss_days': 753,\n",
|
|
|
|
|
" 'capital': 1000000,\n",
|
|
|
|
|
" 'end_balance': 2132982.0520000015,\n",
|
|
|
|
|
" 'max_drawdown': -236503.9475999996,\n",
|
|
|
|
|
" 'max_ddpercent': -13.23872340727957,\n",
|
|
|
|
|
" 'total_net_pnl': 1132982.0520000013,\n",
|
|
|
|
|
" 'daily_net_pnl': 748.3368903566719,\n",
|
|
|
|
|
" 'total_commission': 242817.948,\n",
|
|
|
|
|
" 'daily_commission': 160.3817357992074,\n",
|
|
|
|
|
" 'total_slippage': 482700.0,\n",
|
|
|
|
|
" 'daily_slippage': 318.82430647291943,\n",
|
|
|
|
|
" 'total_turnover': 8093931600.0,\n",
|
|
|
|
|
" 'daily_turnover': 5346057.85997358,\n",
|
|
|
|
|
" 'total_trade_count': 8045,\n",
|
|
|
|
|
" 'daily_trade_count': 5.313738441215324,\n",
|
|
|
|
|
" 'total_return': 113.29820520000014,\n",
|
|
|
|
|
" 'annual_return': 17.96008536856013,\n",
|
|
|
|
|
" 'daily_return': 0.049946173936258026,\n",
|
|
|
|
|
" 'return_std': 0.959328411709829,\n",
|
|
|
|
|
" 'sharpe_ratio': 0.8065671672003681,\n",
|
|
|
|
|
" 'return_drawdown_ratio': 8.558091419728651})]"
|
2019-01-30 04:40:49 +00:00
|
|
|
|
]
|
|
|
|
|
},
|
2019-04-15 15:32:04 +00:00
|
|
|
|
"execution_count": 5,
|
|
|
|
|
"metadata": {},
|
|
|
|
|
"output_type": "execute_result"
|
2019-01-29 07:35:37 +00:00
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
"source": [
|
2019-04-15 15:32:04 +00:00
|
|
|
|
"setting = OptimizationSetting()\n",
|
|
|
|
|
"setting.set_target(\"total_return\")\n",
|
|
|
|
|
"setting.add_parameter(\"atr_length\", 22, 24, 1)\n",
|
|
|
|
|
"\n",
|
|
|
|
|
"engine.run_optimization(setting)"
|
2019-01-29 07:35:37 +00:00
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"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
|
|
|
|
|
}
|