[Add]genetic optimization of strategy parameters

This commit is contained in:
vn.py 2019-05-02 22:32:05 +08:00
parent ed4c71f820
commit 4385dd871a
3 changed files with 653 additions and 95 deletions

View File

@ -17,3 +17,4 @@ tigeropen
rqdatac
ta-lib
ibapi
deap

View File

@ -16,7 +16,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
@ -54,115 +54,526 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"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-05-02 22:29:22.289010\t开始运行遗传算法每代族群总数100, 优良品种筛选个数80迭代次数300交叉概率0.95突变概率0.05\n",
"2019-05-02 22:29:22.289010\t开始加载历史数据\n",
"2019-05-02 22:29:24.103532\t历史数据加载完成数据量18240\n",
"2019-05-02 22:29:24.173848\t策略初始化完成\n",
"2019-05-02 22:29:24.173848\t开始回放历史数据\n",
"2019-05-02 22:29:24.788129\t历史数据回放结束\n",
"2019-05-02 22:29:24.789106\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:29:24.789106\t成交记录为空无法计算\n",
"2019-05-02 22:29:24.789106\t开始计算策略统计指标\n",
"2019-05-02 22:29:24.789106\t开始加载历史数据\n",
"2019-05-02 22:29:24.789106\t历史数据加载完成数据量18240\n",
"2019-05-02 22:29:24.867234\t策略初始化完成\n",
"2019-05-02 22:29:24.868210\t开始回放历史数据\n",
"2019-05-02 22:29:25.834068\t历史数据回放结束\n",
"2019-05-02 22:29:25.835044\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:29:25.839927\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:29:25.840904\t开始计算策略统计指标\n",
"2019-05-02 22:29:25.856529\t开始加载历史数据\n",
"2019-05-02 22:29:25.857506\t历史数据加载完成数据量18240\n",
"2019-05-02 22:29:25.939540\t策略初始化完成\n",
"2019-05-02 22:29:25.939540\t开始回放历史数据\n",
"2019-05-02 22:29:27.055794\t历史数据回放结束\n",
"2019-05-02 22:29:27.056771\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:29:27.062630\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:29:27.062630\t开始计算策略统计指标\n",
"2019-05-02 22:29:27.074350\t开始加载历史数据\n",
"2019-05-02 22:29:27.074350\t历史数据加载完成数据量18240\n",
"2019-05-02 22:29:27.156384\t策略初始化完成\n",
"2019-05-02 22:29:27.156384\t开始回放历史数据\n",
"2019-05-02 22:29:28.159352\t历史数据回放结束\n",
"2019-05-02 22:29:28.160329\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:29:28.165212\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:29:28.165212\t开始计算策略统计指标\n",
"2019-05-02 22:29:28.176931\t开始加载历史数据\n",
"2019-05-02 22:29:28.176931\t历史数据加载完成数据量18240\n",
"2019-05-02 22:29:28.260919\t策略初始化完成\n",
"2019-05-02 22:29:28.260919\t开始回放历史数据\n",
"2019-05-02 22:29:29.418190\t历史数据回放结束\n",
"2019-05-02 22:29:29.418190\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:29:29.424049\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:29:29.424049\t开始计算策略统计指标\n",
"2019-05-02 22:29:29.436745\t开始加载历史数据\n",
"2019-05-02 22:29:29.436745\t历史数据加载完成数据量18240\n",
"2019-05-02 22:29:29.521709\t策略初始化完成\n",
"2019-05-02 22:29:29.522686\t开始回放历史数据\n",
"2019-05-02 22:29:30.513935\t历史数据回放结束\n",
"2019-05-02 22:29:30.514911\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:29:30.519794\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:29:30.519794\t开始计算策略统计指标\n",
"2019-05-02 22:29:30.531514\t开始加载历史数据\n",
"2019-05-02 22:29:30.531514\t历史数据加载完成数据量18240\n",
"2019-05-02 22:29:30.611595\t策略初始化完成\n",
"2019-05-02 22:29:30.611595\t开始回放历史数据\n",
"2019-05-02 22:29:31.729802\t历史数据回放结束\n",
"2019-05-02 22:29:31.730778\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:29:31.735661\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:29:31.735661\t开始计算策略统计指标\n",
"2019-05-02 22:29:31.747381\t开始加载历史数据\n",
"2019-05-02 22:29:31.747381\t历史数据加载完成数据量18240\n",
"2019-05-02 22:29:31.825509\t策略初始化完成\n",
"2019-05-02 22:29:31.826485\t开始回放历史数据\n",
"2019-05-02 22:29:32.840196\t历史数据回放结束\n",
"2019-05-02 22:29:32.840196\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:29:32.846056\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:29:32.847032\t开始计算策略统计指标\n",
"2019-05-02 22:29:32.858751\t开始加载历史数据\n",
"2019-05-02 22:29:32.858751\t历史数据加载完成数据量18240\n",
"2019-05-02 22:29:32.936879\t策略初始化完成\n",
"2019-05-02 22:29:32.936879\t开始回放历史数据\n",
"2019-05-02 22:29:34.065829\t历史数据回放结束\n",
"2019-05-02 22:29:34.066806\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:29:34.071689\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:29:34.072665\t开始计算策略统计指标\n",
"2019-05-02 22:29:34.085361\t开始加载历史数据\n",
"2019-05-02 22:29:34.085361\t历史数据加载完成数据量18240\n",
"2019-05-02 22:29:34.161536\t策略初始化完成\n",
"2019-05-02 22:29:34.162512\t开始回放历史数据\n",
"2019-05-02 22:29:35.174270\t历史数据回放结束\n",
"2019-05-02 22:29:35.175247\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:29:35.180130\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:29:35.180130\t开始计算策略统计指标\n",
"2019-05-02 22:29:35.192825\t开始加载历史数据\n",
"2019-05-02 22:29:35.192825\t历史数据加载完成数据量18240\n",
"2019-05-02 22:29:35.274860\t策略初始化完成\n",
"2019-05-02 22:29:35.274860\t开始回放历史数据\n",
"2019-05-02 22:29:35.918439\t历史数据回放结束\n",
"2019-05-02 22:29:35.918439\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:29:35.918439\t成交记录为空无法计算\n",
"2019-05-02 22:29:35.918439\t开始计算策略统计指标\n",
"2019-05-02 22:29:35.918439\t开始加载历史数据\n",
"2019-05-02 22:29:35.918439\t历史数据加载完成数据量18240\n",
"2019-05-02 22:29:35.999497\t策略初始化完成\n",
"2019-05-02 22:29:35.999497\t开始回放历史数据\n",
"2019-05-02 22:29:36.671398\t历史数据回放结束\n",
"2019-05-02 22:29:36.671398\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:29:36.672374\t成交记录为空无法计算\n",
"2019-05-02 22:29:36.672374\t开始计算策略统计指标\n",
"2019-05-02 22:29:36.673351\t开始加载历史数据\n",
"2019-05-02 22:29:36.673351\t历史数据加载完成数据量18240\n",
"2019-05-02 22:29:36.761245\t策略初始化完成\n",
"2019-05-02 22:29:36.762222\t开始回放历史数据\n",
"2019-05-02 22:29:37.830622\t历史数据回放结束\n",
"2019-05-02 22:29:37.831599\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:29:37.837458\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:29:37.837458\t开始计算策略统计指标\n",
"2019-05-02 22:29:37.849177\t开始加载历史数据\n",
"2019-05-02 22:29:37.849177\t历史数据加载完成数据量18240\n",
"2019-05-02 22:29:37.926329\t策略初始化完成\n",
"2019-05-02 22:29:37.927305\t开始回放历史数据\n",
"2019-05-02 22:29:38.877537\t历史数据回放结束\n",
"2019-05-02 22:29:38.878514\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:29:38.884373\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:29:38.884373\t开始计算策略统计指标\n",
"2019-05-02 22:29:38.895116\t开始加载历史数据\n",
"2019-05-02 22:29:38.896093\t历史数据加载完成数据量18240\n",
"2019-05-02 22:29:38.974221\t策略初始化完成\n",
"2019-05-02 22:29:38.974221\t开始回放历史数据\n",
"2019-05-02 22:29:40.131492\t历史数据回放结束\n",
"2019-05-02 22:29:40.131492\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:29:40.137351\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:29:40.137351\t开始计算策略统计指标\n",
"2019-05-02 22:29:40.149070\t开始加载历史数据\n",
"2019-05-02 22:29:40.149070\t历史数据加载完成数据量18240\n",
"2019-05-02 22:29:40.234035\t策略初始化完成\n",
"2019-05-02 22:29:40.235011\t开始回放历史数据\n",
"2019-05-02 22:29:41.257511\t历史数据回放结束\n",
"2019-05-02 22:29:41.258488\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:29:41.263371\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:29:41.263371\t开始计算策略统计指标\n",
"2019-05-02 22:29:41.275090\t开始加载历史数据\n",
"2019-05-02 22:29:41.275090\t历史数据加载完成数据量18240\n",
"2019-05-02 22:29:41.353218\t策略初始化完成\n",
"2019-05-02 22:29:41.353218\t开始回放历史数据\n",
"2019-05-02 22:29:42.819095\t历史数据回放结束\n",
"2019-05-02 22:29:42.819095\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:29:42.823978\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:29:42.823978\t开始计算策略统计指标\n",
"2019-05-02 22:29:42.835697\t开始加载历史数据\n",
"2019-05-02 22:29:42.836674\t历史数据加载完成数据量18240\n",
"2019-05-02 22:29:42.917731\t策略初始化完成\n",
"2019-05-02 22:29:42.917731\t开始回放历史数据\n",
"2019-05-02 22:29:43.901168\t历史数据回放结束\n",
"2019-05-02 22:29:43.901168\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:29:43.907027\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:29:43.908004\t开始计算策略统计指标\n",
"2019-05-02 22:29:43.920700\t开始加载历史数据\n",
"2019-05-02 22:29:43.921676\t历史数据加载完成数据量18240\n",
"2019-05-02 22:29:44.004687\t策略初始化完成\n",
"2019-05-02 22:29:44.005664\t开始回放历史数据\n",
"2019-05-02 22:29:45.120941\t历史数据回放结束\n",
"2019-05-02 22:29:45.121918\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:29:45.126801\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:29:45.126801\t开始计算策略统计指标\n",
"2019-05-02 22:29:45.137543\t开始加载历史数据\n",
"2019-05-02 22:29:45.138520\t历史数据加载完成数据量18240\n",
"2019-05-02 22:29:45.216648\t策略初始化完成\n",
"2019-05-02 22:29:45.217624\t开始回放历史数据\n",
"2019-05-02 22:29:46.272352\t历史数据回放结束\n",
"2019-05-02 22:29:46.272352\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:29:46.277235\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:29:46.278212\t开始计算策略统计指标\n",
"2019-05-02 22:29:46.289931\t开始加载历史数据\n",
"2019-05-02 22:29:46.289931\t历史数据加载完成数据量18240\n",
"2019-05-02 22:29:46.372942\t策略初始化完成\n",
"2019-05-02 22:29:46.372942\t开始回放历史数据\n",
"2019-05-02 22:29:47.500915\t历史数据回放结束\n",
"2019-05-02 22:29:47.501892\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:29:47.507751\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:29:47.507751\t开始计算策略统计指标\n",
"2019-05-02 22:29:47.519471\t开始加载历史数据\n",
"2019-05-02 22:29:47.519471\t历史数据加载完成数据量18240\n",
"2019-05-02 22:29:47.590762\t策略初始化完成\n",
"2019-05-02 22:29:47.590762\t开始回放历史数据\n",
"2019-05-02 22:29:48.230435\t历史数据回放结束\n",
"2019-05-02 22:29:48.231412\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:29:48.231412\t成交记录为空无法计算\n",
"2019-05-02 22:29:48.231412\t开始计算策略统计指标\n",
"2019-05-02 22:29:48.231412\t开始加载历史数据\n",
"2019-05-02 22:29:48.231412\t历史数据加载完成数据量18240\n",
"2019-05-02 22:29:48.315400\t策略初始化完成\n",
"2019-05-02 22:29:48.315400\t开始回放历史数据\n",
"2019-05-02 22:29:49.317391\t历史数据回放结束\n",
"2019-05-02 22:29:49.317391\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:29:49.323251\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:29:49.323251\t开始计算策略统计指标\n",
"2019-05-02 22:29:49.335947\t开始加载历史数据\n",
"2019-05-02 22:29:49.335947\t历史数据加载完成数据量18240\n",
"2019-05-02 22:29:49.416028\t策略初始化完成\n",
"2019-05-02 22:29:49.416028\t开始回放历史数据\n",
"2019-05-02 22:29:50.513726\t历史数据回放结束\n",
"2019-05-02 22:29:50.513726\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:29:50.519586\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:29:50.519586\t开始计算策略统计指标\n",
"2019-05-02 22:29:50.531305\t开始加载历史数据\n",
"2019-05-02 22:29:50.531305\t历史数据加载完成数据量18240\n",
"2019-05-02 22:29:50.610410\t策略初始化完成\n",
"2019-05-02 22:29:50.611386\t开始回放历史数据\n",
"2019-05-02 22:29:51.688576\t历史数据回放结束\n",
"2019-05-02 22:29:51.689553\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:29:51.694436\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:29:51.695412\t开始计算策略统计指标\n",
"2019-05-02 22:29:51.707131\t开始加载历史数据\n",
"2019-05-02 22:29:51.708108\t历史数据加载完成数据量18240\n",
"2019-05-02 22:29:51.804791\t策略初始化完成\n",
"2019-05-02 22:29:51.807721\t开始回放历史数据\n",
"2019-05-02 22:29:53.077301\t历史数据回放结束\n",
"2019-05-02 22:29:53.078278\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:29:53.083161\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:29:53.083161\t开始计算策略统计指标\n",
"2019-05-02 22:29:53.094880\t开始加载历史数据\n",
"2019-05-02 22:29:53.094880\t历史数据加载完成数据量18240\n",
"2019-05-02 22:29:53.182774\t策略初始化完成\n",
"2019-05-02 22:29:53.183751\t开始回放历史数据\n",
"2019-05-02 22:29:54.420126\t历史数据回放结束\n",
"2019-05-02 22:29:54.421103\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:29:54.425986\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:29:54.425986\t开始计算策略统计指标\n",
"2019-05-02 22:29:54.437705\t开始加载历史数据\n",
"2019-05-02 22:29:54.437705\t历史数据加载完成数据量18240\n",
"2019-05-02 22:29:54.517786\t策略初始化完成\n",
"2019-05-02 22:29:54.517786\t开始回放历史数据\n"
]
},
{
"data": {
"text/plain": [
"[(\"{'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})]"
"name": "stdout",
"output_type": "stream",
"text": [
"2019-05-02 22:29:55.671151\t历史数据回放结束\n",
"2019-05-02 22:29:55.672127\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:29:55.677010\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:29:55.677987\t开始计算策略统计指标\n",
"2019-05-02 22:29:55.689706\t开始加载历史数据\n",
"2019-05-02 22:29:55.689706\t历史数据加载完成数据量18240\n",
"2019-05-02 22:29:55.767834\t策略初始化完成\n",
"2019-05-02 22:29:55.767834\t开始回放历史数据\n",
"2019-05-02 22:29:56.790334\t历史数据回放结束\n",
"2019-05-02 22:29:56.790334\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:29:56.796194\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:29:56.796194\t开始计算策略统计指标\n",
"2019-05-02 22:29:56.806937\t开始加载历史数据\n",
"2019-05-02 22:29:56.807913\t历史数据加载完成数据量18240\n",
"2019-05-02 22:29:56.889948\t策略初始化完成\n",
"2019-05-02 22:29:56.890924\t开始回放历史数据\n",
"2019-05-02 22:29:57.607749\t历史数据回放结束\n",
"2019-05-02 22:29:57.608725\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:29:57.608725\t成交记录为空无法计算\n",
"2019-05-02 22:29:57.608725\t开始计算策略统计指标\n",
"2019-05-02 22:29:57.608725\t开始加载历史数据\n",
"2019-05-02 22:29:57.609702\t历史数据加载完成数据量18240\n",
"2019-05-02 22:29:57.698572\t策略初始化完成\n",
"2019-05-02 22:29:57.699549\t开始回放历史数据\n",
"2019-05-02 22:29:58.882212\t历史数据回放结束\n",
"2019-05-02 22:29:58.882212\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:29:58.888071\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:29:58.888071\t开始计算策略统计指标\n",
"2019-05-02 22:29:58.898814\t开始加载历史数据\n",
"2019-05-02 22:29:58.898814\t历史数据加载完成数据量18240\n",
"2019-05-02 22:29:58.986708\t策略初始化完成\n",
"2019-05-02 22:29:58.987684\t开始回放历史数据\n",
"2019-05-02 22:30:00.071710\t历史数据回放结束\n",
"2019-05-02 22:30:00.072687\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:30:00.077570\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:30:00.081476\t开始计算策略统计指标\n",
"2019-05-02 22:30:00.092219\t开始加载历史数据\n",
"2019-05-02 22:30:00.095149\t历史数据加载完成数据量18240\n",
"2019-05-02 22:30:00.173277\t策略初始化完成\n",
"2019-05-02 22:30:00.174253\t开始回放历史数据\n",
"2019-05-02 22:30:00.906703\t历史数据回放结束\n",
"2019-05-02 22:30:00.907680\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:30:00.907680\t成交记录为空无法计算\n",
"2019-05-02 22:30:00.908657\t开始计算策略统计指标\n",
"2019-05-02 22:30:00.908657\t开始加载历史数据\n",
"2019-05-02 22:30:00.908657\t历史数据加载完成数据量18240\n",
"2019-05-02 22:30:01.018036\t策略初始化完成\n",
"2019-05-02 22:30:01.019012\t开始回放历史数据\n",
"2019-05-02 22:30:02.128430\t历史数据回放结束\n",
"2019-05-02 22:30:02.135266\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:30:02.141126\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:30:02.141126\t开始计算策略统计指标\n",
"2019-05-02 22:30:02.151868\t开始加载历史数据\n",
"2019-05-02 22:30:02.152845\t历史数据加载完成数据量18240\n",
"2019-05-02 22:30:02.229020\t策略初始化完成\n",
"2019-05-02 22:30:02.229020\t开始回放历史数据\n",
"2019-05-02 22:30:03.410706\t历史数据回放结束\n",
"2019-05-02 22:30:03.411682\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:30:03.416565\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:30:03.416565\t开始计算策略统计指标\n",
"2019-05-02 22:30:03.428285\t开始加载历史数据\n",
"2019-05-02 22:30:03.429261\t历史数据加载完成数据量18240\n",
"2019-05-02 22:30:03.533757\t策略初始化完成\n",
"2019-05-02 22:30:03.533757\t开始回放历史数据\n",
"2019-05-02 22:30:04.638292\t历史数据回放结束\n",
"2019-05-02 22:30:04.638292\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:30:04.643175\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:30:04.644152\t开始计算策略统计指标\n",
"2019-05-02 22:30:04.654894\t开始加载历史数据\n",
"2019-05-02 22:30:04.654894\t历史数据加载完成数据量18240\n",
"2019-05-02 22:30:04.738882\t策略初始化完成\n",
"2019-05-02 22:30:04.738882\t开始回放历史数据\n",
"2019-05-02 22:30:05.721341\t历史数据回放结束\n",
"2019-05-02 22:30:05.722318\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:30:05.728178\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:30:05.728178\t开始计算策略统计指标\n",
"2019-05-02 22:30:05.739897\t开始加载历史数据\n",
"2019-05-02 22:30:05.739897\t历史数据加载完成数据量18240\n",
"2019-05-02 22:30:05.824861\t策略初始化完成\n",
"2019-05-02 22:30:05.825838\t开始回放历史数据\n",
"2019-05-02 22:30:06.928419\t历史数据回放结束\n",
"2019-05-02 22:30:06.928419\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:30:06.933302\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:30:06.934279\t开始计算策略统计指标\n",
"2019-05-02 22:30:06.945998\t开始加载历史数据\n",
"2019-05-02 22:30:06.945998\t历史数据加载完成数据量18240\n",
"2019-05-02 22:30:07.024126\t策略初始化完成\n",
"2019-05-02 22:30:07.025102\t开始回放历史数据\n",
"2019-05-02 22:30:08.041743\t历史数据回放结束\n",
"2019-05-02 22:30:08.042720\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:30:08.048579\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:30:08.048579\t开始计算策略统计指标\n",
"2019-05-02 22:30:08.062252\t开始加载历史数据\n",
"2019-05-02 22:30:08.062252\t历史数据加载完成数据量18240\n",
"2019-05-02 22:30:08.145263\t策略初始化完成\n",
"2019-05-02 22:30:08.145263\t开始回放历史数据\n",
"2019-05-02 22:30:09.292768\t历史数据回放结束\n",
"2019-05-02 22:30:09.293744\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:30:09.298627\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:30:09.298627\t开始计算策略统计指标\n",
"2019-05-02 22:30:09.311323\t开始加载历史数据\n",
"2019-05-02 22:30:09.311323\t历史数据加载完成数据量18240\n",
"2019-05-02 22:30:09.393357\t策略初始化完成\n",
"2019-05-02 22:30:09.393357\t开始回放历史数据\n",
"2019-05-02 22:30:10.363121\t历史数据回放结束\n",
"2019-05-02 22:30:10.363121\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:30:10.368981\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:30:10.369957\t开始计算策略统计指标\n",
"2019-05-02 22:30:10.380700\t开始加载历史数据\n",
"2019-05-02 22:30:10.381677\t历史数据加载完成数据量18240\n",
"2019-05-02 22:30:10.459805\t策略初始化完成\n",
"2019-05-02 22:30:10.459805\t开始回放历史数据\n",
"2019-05-02 22:30:11.536994\t历史数据回放结束\n",
"2019-05-02 22:30:11.536994\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:30:11.542854\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:30:11.542854\t开始计算策略统计指标\n",
"2019-05-02 22:30:11.554573\t开始加载历史数据\n",
"2019-05-02 22:30:11.554573\t历史数据加载完成数据量18240\n",
"2019-05-02 22:30:11.638561\t策略初始化完成\n",
"2019-05-02 22:30:11.639537\t开始回放历史数据\n",
"2019-05-02 22:30:12.632740\t历史数据回放结束\n",
"2019-05-02 22:30:12.633716\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:30:12.638599\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:30:12.639576\t开始计算策略统计指标\n",
"2019-05-02 22:30:12.650318\t开始加载历史数据\n",
"2019-05-02 22:30:12.651295\t历史数据加载完成数据量18240\n",
"2019-05-02 22:30:12.733329\t策略初始化完成\n",
"2019-05-02 22:30:12.734306\t开始回放历史数据\n",
"2019-05-02 22:30:13.352494\t历史数据回放结束\n",
"2019-05-02 22:30:13.353470\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:30:13.353470\t成交记录为空无法计算\n",
"2019-05-02 22:30:13.353470\t开始计算策略统计指标\n",
"2019-05-02 22:30:13.354447\t开始加载历史数据\n",
"2019-05-02 22:30:13.354447\t历史数据加载完成数据量18240\n",
"2019-05-02 22:30:13.436481\t策略初始化完成\n",
"2019-05-02 22:30:13.436481\t开始回放历史数据\n",
"2019-05-02 22:30:14.540039\t历史数据回放结束\n",
"2019-05-02 22:30:14.541016\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:30:14.546876\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:30:14.546876\t开始计算策略统计指标\n",
"2019-05-02 22:30:14.558595\t开始加载历史数据\n",
"2019-05-02 22:30:14.558595\t历史数据加载完成数据量18240\n",
"2019-05-02 22:30:14.650395\t策略初始化完成\n",
"2019-05-02 22:30:14.650395\t开始回放历史数据\n",
"2019-05-02 22:30:15.294951\t历史数据回放结束\n",
"2019-05-02 22:30:15.294951\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:30:15.294951\t成交记录为空无法计算\n",
"2019-05-02 22:30:15.294951\t开始计算策略统计指标\n",
"2019-05-02 22:30:15.294951\t开始加载历史数据\n",
"2019-05-02 22:30:15.294951\t历史数据加载完成数据量18240\n",
"2019-05-02 22:30:15.377962\t策略初始化完成\n",
"2019-05-02 22:30:15.377962\t开始回放历史数据\n",
"2019-05-02 22:30:16.346749\t历史数据回放结束\n",
"2019-05-02 22:30:16.346749\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:30:16.352609\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:30:16.352609\t开始计算策略统计指标\n",
"2019-05-02 22:30:16.365305\t开始加载历史数据\n",
"2019-05-02 22:30:16.365305\t历史数据加载完成数据量18240\n",
"2019-05-02 22:30:16.445386\t策略初始化完成\n",
"2019-05-02 22:30:16.446363\t开始回放历史数据\n",
"2019-05-02 22:30:17.544061\t历史数据回放结束\n",
"2019-05-02 22:30:17.545038\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:30:17.550897\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:30:17.550897\t开始计算策略统计指标\n",
"2019-05-02 22:30:17.563593\t开始加载历史数据\n",
"2019-05-02 22:30:17.563593\t历史数据加载完成数据量18240\n",
"2019-05-02 22:30:17.640744\t策略初始化完成\n",
"2019-05-02 22:30:17.640744\t开始回放历史数据\n",
"2019-05-02 22:30:18.641759\t历史数据回放结束\n",
"2019-05-02 22:30:18.642736\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:30:18.647619\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:30:18.647619\t开始计算策略统计指标\n",
"2019-05-02 22:30:18.659338\t开始加载历史数据\n",
"2019-05-02 22:30:18.660315\t历史数据加载完成数据量18240\n",
"2019-05-02 22:30:18.736490\t策略初始化完成\n",
"2019-05-02 22:30:18.736490\t开始回放历史数据\n",
"2019-05-02 22:30:19.827352\t历史数据回放结束\n",
"2019-05-02 22:30:19.828328\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:30:19.833211\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:30:19.833211\t开始计算策略统计指标\n",
"2019-05-02 22:30:19.844931\t开始加载历史数据\n",
"2019-05-02 22:30:19.845907\t历史数据加载完成数据量18240\n",
"2019-05-02 22:30:19.927942\t策略初始化完成\n",
"2019-05-02 22:30:19.928918\t开始回放历史数据\n",
"2019-05-02 22:30:20.916261\t历史数据回放结束\n",
"2019-05-02 22:30:20.916261\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:30:20.922120\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:30:20.922120\t开始计算策略统计指标\n",
"2019-05-02 22:30:20.934816\t开始加载历史数据\n",
"2019-05-02 22:30:20.935793\t历史数据加载完成数据量18240\n",
"2019-05-02 22:30:21.010991\t策略初始化完成\n",
"2019-05-02 22:30:21.011968\t开始回放历史数据\n",
"2019-05-02 22:30:22.131151\t历史数据回放结束\n",
"2019-05-02 22:30:22.131151\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:30:22.137011\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:30:22.137011\t开始计算策略统计指标\n",
"2019-05-02 22:30:22.149707\t开始加载历史数据\n",
"2019-05-02 22:30:22.149707\t历史数据加载完成数据量18240\n",
"2019-05-02 22:30:22.227835\t策略初始化完成\n",
"2019-05-02 22:30:22.228811\t开始回放历史数据\n",
"2019-05-02 22:30:23.201505\t历史数据回放结束\n",
"2019-05-02 22:30:23.202481\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:30:23.207364\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:30:23.207364\t开始计算策略统计指标\n",
"2019-05-02 22:30:23.219084\t开始加载历史数据\n",
"2019-05-02 22:30:23.219084\t历史数据加载完成数据量18240\n",
"2019-05-02 22:30:23.295258\t策略初始化完成\n",
"2019-05-02 22:30:23.295258\t开始回放历史数据\n",
"2019-05-02 22:30:24.418348\t历史数据回放结束\n",
"2019-05-02 22:30:24.418348\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:30:24.423231\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:30:24.424208\t开始计算策略统计指标\n",
"2019-05-02 22:30:24.437880\t开始加载历史数据\n",
"2019-05-02 22:30:24.437880\t历史数据加载完成数据量18240\n",
"2019-05-02 22:30:24.517962\t策略初始化完成\n",
"2019-05-02 22:30:24.517962\t开始回放历史数据\n",
"2019-05-02 22:30:25.200605\t历史数据回放结束\n",
"2019-05-02 22:30:25.201582\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:30:25.201582\t成交记录为空无法计算\n",
"2019-05-02 22:30:25.202558\t开始计算策略统计指标\n",
"2019-05-02 22:30:25.202558\t开始加载历史数据\n",
"2019-05-02 22:30:25.203535\t历史数据加载完成数据量18240\n",
"2019-05-02 22:30:25.275803\t策略初始化完成\n",
"2019-05-02 22:30:25.276780\t开始回放历史数据\n"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
{
"name": "stdout",
"output_type": "stream",
"text": [
"2019-05-02 22:30:25.947704\t历史数据回放结束\n",
"2019-05-02 22:30:25.948681\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:30:25.948681\t成交记录为空无法计算\n",
"2019-05-02 22:30:25.949657\t开始计算策略统计指标\n",
"2019-05-02 22:30:25.949657\t开始加载历史数据\n",
"2019-05-02 22:30:25.949657\t历史数据加载完成数据量18240\n",
"2019-05-02 22:30:26.027785\t策略初始化完成\n",
"2019-05-02 22:30:26.027785\t开始回放历史数据\n",
"2019-05-02 22:30:27.024894\t历史数据回放结束\n",
"2019-05-02 22:30:27.025870\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:30:27.030753\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:30:27.030753\t开始计算策略统计指标\n",
"2019-05-02 22:30:27.042473\t开始加载历史数据\n",
"2019-05-02 22:30:27.042473\t历史数据加载完成数据量18240\n",
"2019-05-02 22:30:27.127437\t策略初始化完成\n",
"2019-05-02 22:30:27.128413\t开始回放历史数据\n",
"2019-05-02 22:30:28.317912\t历史数据回放结束\n",
"2019-05-02 22:30:28.317912\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:30:28.323772\t逐日盯市盈亏计算完成\n",
"2019-05-02 22:30:28.324748\t开始计算策略统计指标\n",
"2019-05-02 22:30:28.335491\t开始加载历史数据\n",
"2019-05-02 22:30:28.336468\t历史数据加载完成数据量18240\n",
"2019-05-02 22:30:28.412642\t策略初始化完成\n",
"2019-05-02 22:30:28.413619\t开始回放历史数据\n",
"2019-05-02 22:30:29.072824\t历史数据回放结束\n",
"2019-05-02 22:30:29.072824\t开始计算逐日盯市盈亏\n",
"2019-05-02 22:30:29.072824\t成交记录为空无法计算\n",
"2019-05-02 22:30:29.072824\t开始计算策略统计指标\n",
"gen\tnevals\tmean \tstd \tmin \tmax \n",
"0 \t100 \t[1.24452619]\t[2.90495733]\t[-3.24204978]\t[8.88922512]\n"
]
},
{
"ename": "ValueError",
"evalue": "empty range for randrange() (1,1, 0)",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-3-4726e35b67fb>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0msetting\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0madd_parameter\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"atr_length\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m105\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m \u001b[0mengine\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrun_ga_optimization\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msetting\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;32mC:\\Github\\vnpy\\vnpy\\app\\cta_strategy\\backtesting.py\u001b[0m in \u001b[0;36mrun_ga_optimization\u001b[1;34m(self, optimization_setting, output)\u001b[0m\n\u001b[0;32m 602\u001b[0m \u001b[0mNGEN\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 603\u001b[0m \u001b[0mstats\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 604\u001b[1;33m \u001b[0mhalloffame\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mhof\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 605\u001b[0m ) \n\u001b[0;32m 606\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32mc:\\miniconda3\\lib\\site-packages\\deap\\algorithms.py\u001b[0m in \u001b[0;36meaMuPlusLambda\u001b[1;34m(population, toolbox, mu, lambda_, cxpb, mutpb, ngen, stats, halloffame, verbose)\u001b[0m\n\u001b[0;32m 316\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mgen\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mngen\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 317\u001b[0m \u001b[1;31m# Vary the population\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 318\u001b[1;33m \u001b[0moffspring\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mvarOr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpopulation\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtoolbox\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlambda_\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcxpb\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmutpb\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 319\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 320\u001b[0m \u001b[1;31m# Evaluate the individuals with an invalid fitness\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32mc:\\miniconda3\\lib\\site-packages\\deap\\algorithms.py\u001b[0m in \u001b[0;36mvarOr\u001b[1;34m(population, toolbox, lambda_, cxpb, mutpb)\u001b[0m\n\u001b[0;32m 234\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mop_choice\u001b[0m \u001b[1;33m<\u001b[0m \u001b[0mcxpb\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;31m# Apply crossover\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 235\u001b[0m \u001b[0mind1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mind2\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mlist\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmap\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtoolbox\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mclone\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mrandom\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msample\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpopulation\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 236\u001b[1;33m \u001b[0mind1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mind2\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtoolbox\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mind1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mind2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 237\u001b[0m \u001b[1;32mdel\u001b[0m \u001b[0mind1\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfitness\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mvalues\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 238\u001b[0m \u001b[0moffspring\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mind1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32mc:\\miniconda3\\lib\\site-packages\\deap\\tools\\crossover.py\u001b[0m in \u001b[0;36mcxTwoPoint\u001b[1;34m(ind1, ind2)\u001b[0m\n\u001b[0;32m 42\u001b[0m \u001b[0msize\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mmin\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mind1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mind2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 43\u001b[0m \u001b[0mcxpoint1\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mrandom\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrandint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msize\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 44\u001b[1;33m \u001b[0mcxpoint2\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mrandom\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrandint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msize\u001b[0m \u001b[1;33m-\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 45\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mcxpoint2\u001b[0m \u001b[1;33m>=\u001b[0m \u001b[0mcxpoint1\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 46\u001b[0m \u001b[0mcxpoint2\u001b[0m \u001b[1;33m+=\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32mc:\\miniconda3\\lib\\random.py\u001b[0m in \u001b[0;36mrandint\u001b[1;34m(self, a, b)\u001b[0m\n\u001b[0;32m 220\u001b[0m \"\"\"\n\u001b[0;32m 221\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 222\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrandrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mb\u001b[0m\u001b[1;33m+\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 223\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 224\u001b[0m def _randbelow(self, n, int=int, maxsize=1<<BPF, type=type,\n",
"\u001b[1;32mc:\\miniconda3\\lib\\random.py\u001b[0m in \u001b[0;36mrandrange\u001b[1;34m(self, start, stop, step, _int)\u001b[0m\n\u001b[0;32m 198\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mistart\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_randbelow\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mwidth\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 199\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mstep\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 200\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"empty range for randrange() (%d,%d, %d)\"\u001b[0m \u001b[1;33m%\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mistart\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mistop\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mwidth\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 201\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 202\u001b[0m \u001b[1;31m# Non-unit step argument supplied.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mValueError\u001b[0m: empty range for randrange() (1,1, 0)"
]
}
],
"source": [
"setting = OptimizationSetting()\n",
"setting.set_target(\"total_return\")\n",
"setting.add_parameter(\"atr_length\", 22, 24, 1)\n",
"setting.add_parameter(\"atr_length\", 3, 105, 1)\n",
"\n",
"engine.run_optimization(setting)"
"engine.run_ga_optimization(setting)"
]
},
{

View File

@ -3,12 +3,15 @@ from datetime import date, datetime
from typing import Callable
from itertools import product
from functools import lru_cache
from time import time
import multiprocessing
import random
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from pandas import DataFrame
from deap import creator, base, tools, algorithms
from vnpy.trader.constant import (Direction, Offset, Exchange,
Interval, Status)
@ -514,6 +517,101 @@ class BacktestingEngine:
return result_values
def run_ga_optimization(self, optimization_setting: OptimizationSetting, output=True):
""""""
# Get optimization setting and target
settings = optimization_setting.generate_setting()
target_name = optimization_setting.target_name
if not settings:
self.output("优化参数组合为空,请检查")
return
if not target_name:
self.output("优化目标未设置,请检查")
return
# Define parameter generation function
def generate_parameter():
""""""
return list(random.choice(settings).values())
# Create ga object function
object_func = create_ga_optimize(
target_name,
self.strategy_class,
settings[0],
self.vt_symbol,
self.interval,
self.start,
self.rate,
self.slippage,
self.size,
self.pricetick,
self.capital,
self.end,
self.mode
)
# Set up genetic algorithem
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", list, fitness=creator.FitnessMax)
toolbox = base.Toolbox()
toolbox.register("individual", tools.initIterate, creator.Individual, generate_parameter)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("mate", tools.cxTwoPoint)
toolbox.register("mutate", tools.mutUniformInt, low=4, up=40, indpb=1)
toolbox.register("evaluate", object_func)
toolbox.register("select", tools.selNSGA2)
# pool = multiprocessing.Pool(multiprocessing.cpu_count())
# toolbox.register("map", pool.map)
MU = 80 # 设置每一代选择的个体数
LAMBDA = 100 # 设置每一代产生的子女数
POP = 100
CXPB = 0.95 # 交叉概率
MUTPB = 0.05 # 变异概率
NGEN = 300 # 种群代数
pop = toolbox.population(POP) # 设置族群里面的个体数量
hof = tools.ParetoFront() # 解的集合:帕累托前沿(非占优最优集)
stats = tools.Statistics(lambda ind: ind.fitness.values)
np.set_printoptions(suppress=True) # 对numpy默认输出的科学计数法转换
stats.register("mean", np.mean, axis=0) # 统计目标优化函数结果的平均值
stats.register("std", np.std, axis=0) # 统计目标优化函数结果的标准差
stats.register("min", np.min, axis=0) # 统计目标优化函数结果的最小值
stats.register("max", np.max, axis=0) # 统计目标优化函数结果的最大值
msg = "开始运行遗传算法,每代族群总数:%s, 优良品种筛选个数:%s,迭代次数:%s,交叉概率:%s,突变概率:%s" %(POP,MU,NGEN,CXPB,MUTPB)
self.output(msg)
# Run ga optimization
# esMuPlusLambda是一种基于(μ+λ)选择策略的多目标优化分段遗传算法
start = time()
algorithms.eaMuPlusLambda(
pop,
toolbox,
MU,
LAMBDA,
CXPB,
MUTPB,
NGEN,
stats,
halloffame=hof
)
end = time()
cost = int((end - start))
self.output(f"遗传算法优化完成,耗时{cost}")
self.output("输出帕累托前沿解集:")
return hof
def update_daily_close(self, price: float):
""""""
d = self.datetime.date()
@ -968,6 +1066,54 @@ def optimize(
return (str(setting), target_value, statistics)
def create_ga_optimize(
target_name: str,
strategy_class: CtaTemplate,
setting: dict,
vt_symbol: str,
interval: Interval,
start: datetime,
rate: float,
slippage: float,
size: float,
pricetick: float,
capital: int,
end: datetime,
mode: BacktestingMode,
):
"""
Function for running in multiprocessing.pool
"""
parameter_keys = list(setting.keys())
@lru_cache(maxsize=1000000)
def _optimizae(parameter_values: tuple):
""""""
setting = dict(zip(parameter_keys, parameter_values))
result = optimize(
target_name,
strategy_class,
setting,
vt_symbol,
interval,
start,
rate,
slippage,
size,
pricetick,
capital,
end,
mode
)
return (result[1],)
def ga_optimize(parameter_values: list):
""""""
return _optimizae(tuple(parameter_values))
return ga_optimize
@lru_cache(maxsize=10)
def load_bar_data(
symbol: str,