2019-01-29 07:35:37 +00:00
|
|
|
|
{
|
|
|
|
|
"cells": [
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
2019-05-03 09:01:47 +00:00
|
|
|
|
"execution_count": 1,
|
2019-01-29 07:35:37 +00:00
|
|
|
|
"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-05-03 09:01:47 +00:00
|
|
|
|
"execution_count": 2,
|
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",
|
2019-05-03 09:01:47 +00:00
|
|
|
|
"execution_count": 3,
|
2019-05-03 08:10:11 +00:00
|
|
|
|
"metadata": {
|
|
|
|
|
"scrolled": true
|
|
|
|
|
},
|
2019-05-03 09:01:47 +00:00
|
|
|
|
"outputs": [
|
|
|
|
|
{
|
|
|
|
|
"name": "stdout",
|
|
|
|
|
"output_type": "stream",
|
|
|
|
|
"text": [
|
|
|
|
|
"2019-05-03 16:19:04.193703\t开始运行遗传算法,每代族群总数:11, 优良品种筛选个数:8,迭代次数:30,交叉概率:0.95,突变概率:0.050000000000000044\n",
|
|
|
|
|
"gen\tnevals\tmean \tstd \tmin \tmax \n",
|
|
|
|
|
"0 \t11 \t[0.58423524]\t[0.30377007]\t[0.13231977]\t[1.2382818]\n",
|
|
|
|
|
"1 \t11 \t[0.90248989]\t[0.15747112]\t[0.68707859]\t[1.2382818]\n",
|
|
|
|
|
"2 \t11 \t[1.09406088]\t[0.18860523]\t[0.86284921]\t[1.46762684]\n",
|
|
|
|
|
"3 \t11 \t[1.21413386]\t[0.12138014]\t[1.02072108]\t[1.46762684]\n",
|
|
|
|
|
"4 \t11 \t[1.29561806]\t[0.09930932]\t[1.2382818] \t[1.46762684]\n",
|
|
|
|
|
"5 \t11 \t[1.41029058]\t[0.09930932]\t[1.2382818] \t[1.46762684]\n",
|
|
|
|
|
"6 \t11 \t[1.46762684]\t[0.] \t[1.46762684]\t[1.46762684]\n",
|
|
|
|
|
"7 \t11 \t[1.46762684]\t[0.] \t[1.46762684]\t[1.46762684]\n",
|
|
|
|
|
"8 \t11 \t[1.46762684]\t[0.] \t[1.46762684]\t[1.46762684]\n",
|
|
|
|
|
"9 \t11 \t[1.46762684]\t[0.] \t[1.46762684]\t[1.46762684]\n",
|
|
|
|
|
"10 \t11 \t[1.46762684]\t[0.] \t[1.46762684]\t[1.46762684]\n",
|
|
|
|
|
"11 \t11 \t[1.46762684]\t[0.] \t[1.46762684]\t[1.46762684]\n",
|
|
|
|
|
"12 \t11 \t[1.46762684]\t[0.] \t[1.46762684]\t[1.46762684]\n",
|
|
|
|
|
"13 \t11 \t[1.46762684]\t[0.] \t[1.46762684]\t[1.46762684]\n",
|
|
|
|
|
"14 \t11 \t[1.46762684]\t[0.] \t[1.46762684]\t[1.46762684]\n",
|
|
|
|
|
"15 \t11 \t[1.46762684]\t[0.] \t[1.46762684]\t[1.46762684]\n",
|
|
|
|
|
"16 \t11 \t[1.46762684]\t[0.] \t[1.46762684]\t[1.46762684]\n",
|
|
|
|
|
"17 \t11 \t[1.46762684]\t[0.] \t[1.46762684]\t[1.46762684]\n",
|
|
|
|
|
"18 \t11 \t[1.46762684]\t[0.] \t[1.46762684]\t[1.46762684]\n",
|
|
|
|
|
"19 \t11 \t[1.46762684]\t[0.] \t[1.46762684]\t[1.46762684]\n",
|
|
|
|
|
"20 \t11 \t[1.46762684]\t[0.] \t[1.46762684]\t[1.46762684]\n",
|
|
|
|
|
"21 \t11 \t[1.46762684]\t[0.] \t[1.46762684]\t[1.46762684]\n",
|
|
|
|
|
"22 \t11 \t[1.46762684]\t[0.] \t[1.46762684]\t[1.46762684]\n",
|
|
|
|
|
"23 \t11 \t[1.46762684]\t[0.] \t[1.46762684]\t[1.46762684]\n",
|
|
|
|
|
"24 \t11 \t[1.46762684]\t[0.] \t[1.46762684]\t[1.46762684]\n",
|
|
|
|
|
"25 \t11 \t[1.46762684]\t[0.] \t[1.46762684]\t[1.46762684]\n",
|
|
|
|
|
"26 \t11 \t[1.46762684]\t[0.] \t[1.46762684]\t[1.46762684]\n",
|
|
|
|
|
"27 \t11 \t[1.46762684]\t[0.] \t[1.46762684]\t[1.46762684]\n",
|
|
|
|
|
"28 \t11 \t[1.46762684]\t[0.] \t[1.46762684]\t[1.46762684]\n",
|
|
|
|
|
"29 \t11 \t[1.46762684]\t[0.] \t[1.46762684]\t[1.46762684]\n",
|
|
|
|
|
"30 \t11 \t[1.46762684]\t[0.] \t[1.46762684]\t[1.46762684]\n",
|
|
|
|
|
"2019-05-03 16:19:58.256354\t遗传算法优化完成,耗时54秒\n"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"data": {
|
|
|
|
|
"text/plain": [
|
|
|
|
|
"[({'atr_length': 38, 'atr_ma_length': 25}, 1.4676268402266743)]"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
"execution_count": 3,
|
|
|
|
|
"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",
|
2019-05-03 07:17:32 +00:00
|
|
|
|
"setting.set_target(\"sharpe_ratio\")\n",
|
2019-05-03 08:10:11 +00:00
|
|
|
|
"setting.add_parameter(\"atr_length\", 3, 39, 1)\n",
|
|
|
|
|
"setting.add_parameter(\"atr_ma_length\", 10, 30, 1)\n",
|
2019-04-15 15:32:04 +00:00
|
|
|
|
"\n",
|
2019-05-02 14:32:05 +00:00
|
|
|
|
"engine.run_ga_optimization(setting)"
|
2019-01-29 07:35:37 +00:00
|
|
|
|
]
|
|
|
|
|
},
|
2019-05-03 08:10:11 +00:00
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"execution_count": null,
|
|
|
|
|
"metadata": {},
|
|
|
|
|
"outputs": [],
|
|
|
|
|
"source": [
|
|
|
|
|
"result = _"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"execution_count": null,
|
|
|
|
|
"metadata": {},
|
|
|
|
|
"outputs": [],
|
|
|
|
|
"source": [
|
|
|
|
|
"print(result)"
|
|
|
|
|
]
|
|
|
|
|
},
|
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
|
|
|
|
|
}
|