From 4385dd871a53c3250d97c84a52fad1d5e058e6dc Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Thu, 2 May 2019 22:32:05 +0800 Subject: [PATCH 1/4] [Add]genetic optimization of strategy parameters --- requirements.txt | 1 + tests/backtesting/turtle.ipynb | 601 ++++++++++++++++++++++----- vnpy/app/cta_strategy/backtesting.py | 146 +++++++ 3 files changed, 653 insertions(+), 95 deletions(-) diff --git a/requirements.txt b/requirements.txt index 0706dba5..8eaa8cf4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -17,3 +17,4 @@ tigeropen rqdatac ta-lib ibapi +deap \ No newline at end of file diff --git a/tests/backtesting/turtle.ipynb b/tests/backtesting/turtle.ipynb index 9807424b..8a4f75f4 100644 --- a/tests/backtesting/turtle.ipynb +++ b/tests/backtesting/turtle.ipynb @@ -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})]" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" + "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" + ] + }, + { + "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\u001b[0m in \u001b[0;36m\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< 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)" ] }, { diff --git a/vnpy/app/cta_strategy/backtesting.py b/vnpy/app/cta_strategy/backtesting.py index e4aa25c7..083cb2d0 100644 --- a/vnpy/app/cta_strategy/backtesting.py +++ b/vnpy/app/cta_strategy/backtesting.py @@ -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, From 6e3b7faae3f8fa23187525ee3fc45cbe09b29a26 Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Fri, 3 May 2019 15:17:32 +0800 Subject: [PATCH 2/4] [Add]change variable names --- tests/backtesting/GA_Pre_Final.ipynb | 71 ++-- tests/backtesting/turtle.ipynb | 514 +-------------------------- vnpy/app/cta_strategy/backtesting.py | 59 +-- 3 files changed, 76 insertions(+), 568 deletions(-) diff --git a/tests/backtesting/GA_Pre_Final.ipynb b/tests/backtesting/GA_Pre_Final.ipynb index 8519ad3a..0d99a169 100644 --- a/tests/backtesting/GA_Pre_Final.ipynb +++ b/tests/backtesting/GA_Pre_Final.ipynb @@ -10,12 +10,11 @@ "import multiprocessing\n", "import numpy as np\n", "from deap import creator, base, tools, algorithms\n", - "from backtesting import BacktestingEngine,OptimizationSetting\n", - "from boll_channel_strategy import BollChannelStrategy\n", - "from atr_rsi_strategy import AtrRsiStrategy\n", + "from vnpy.app.cta_strategy.backtesting import BacktestingEngine,OptimizationSetting\n", + "from vnpy.app.cta_strategy.strategies.boll_channel_strategy import BollChannelStrategy\n", + "from vnpy.app.cta_strategy.strategies.atr_rsi_strategy import AtrRsiStrategy\n", "from datetime import datetime\n", "import multiprocessing #多进程\n", - "from scoop import futures #多进程\n", "from functools import lru_cache" ] }, @@ -89,7 +88,7 @@ { "data": { "text/plain": [ - "[22, 28, 22]" + "[48, 6, 26]" ] }, "execution_count": 5, @@ -109,7 +108,7 @@ { "data": { "text/plain": [ - "{'boll_window': 24, 'cci_window': 14, 'atr_window': 28}" + "{'boll_window': 16, 'cci_window': 48, 'atr_window': 6}" ] }, "execution_count": 6, @@ -171,10 +170,24 @@ "execution_count": 8, "metadata": {}, "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2019-05-03 15:02:07.528909\t开始加载历史数据\n", + "2019-05-03 15:02:34.854177\t历史数据加载完成,数据量:175440\n", + "2019-05-03 15:02:34.877616\t策略初始化完成\n", + "2019-05-03 15:02:34.877616\t开始回放历史数据\n", + "2019-05-03 15:02:37.000744\t历史数据回放结束\n", + "2019-05-03 15:02:37.000744\t开始计算逐日盯市盈亏\n", + "2019-05-03 15:02:37.012463\t逐日盯市盈亏计算完成\n", + "2019-05-03 15:02:37.012463\t开始计算策略统计指标\n" + ] + }, { "data": { "text/plain": [ - "(-0.51, -0.28)" + "(0.96, 0.38)" ] }, "execution_count": 8, @@ -224,8 +237,8 @@ " toolbox.register(\"mutate\", tools.mutUniformInt,low = 4,up = 40,indpb=1) \n", " toolbox.register(\"evaluate\", object_func) \n", " toolbox.register(\"select\", tools.selNSGA2) \n", - " #pool = multiprocessing.Pool()\n", - " #toolbox.register(\"map\", pool.map)\n", + " pool = multiprocessing.Pool(multiprocessing.cpu_count())\n", + " toolbox.register(\"map\", pool.map)\n", " #toolbox.register(\"map\", futures.map)\n", " \n", " \n", @@ -266,7 +279,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "metadata": { "scrolled": false }, @@ -275,43 +288,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "开始运行遗传算法,每代族群总数:34, 优良品种筛选个数:27,迭代次数:30,交叉概率:0.95,突变概率:0.05\n", - "gen\tnevals\tmean \tstd \tmin \tmax \n", - "0 \t34 \t[0.08852941 0.00352941]\t[0.5373362 0.29107188]\t[-0.7 -0.63]\t[1.51 0.5 ]\n", - "1 \t34 \t[0.60148148 0.27518519]\t[0.31013383 0.08573734]\t[0.32 0.18] \t[1.51 0.5 ]\n", - "2 \t34 \t[0.79333333 0.33851852]\t[0.27758215 0.06742369]\t[0.47 0.25] \t[1.54 0.5 ]\n", - "3 \t34 \t[1.00888889 0.39777778]\t[0.3147525 0.06214281]\t[0.7 0.33] \t[1.54 0.5 ]\n", - "4 \t34 \t[1.41074074 0.47444444]\t[0.22881217 0.04661373]\t[0.96 0.36] \t[1.92 0.57]\n", - "5 \t34 \t[1.59666667 0.51222222]\t[0.14714568 0.0255797 ]\t[1.51 0.49] \t[1.92 0.57]\n", - "6 \t34 \t[1.66259259 0.52185185]\t[0.16585564 0.02981884]\t[1.52 0.49] \t[1.92 0.57]\n", - "7 \t34 \t[1.8737037 0.55666667]\t[0.07713135 0.01763834]\t[1.75 0.53] \t[1.95 0.57]\n", - "8 \t34 \t[1.93666667 0.57 ]\t[0.01490712 0. ]\t[1.92 0.57] \t[1.95 0.57]\n", - "9 \t34 \t[1.95 0.57] \t[0. 0.] \t[1.95 0.57] \t[1.95 0.57]\n", - "10 \t34 \t[1.95 0.57] \t[0. 0.] \t[1.95 0.57] \t[1.95 0.57]\n", - "11 \t34 \t[1.95 0.57] \t[0. 0.] \t[1.95 0.57] \t[1.95 0.57]\n", - "12 \t34 \t[1.95 0.57] \t[0. 0.] \t[1.95 0.57] \t[1.95 0.57]\n", - "13 \t34 \t[1.95 0.57] \t[0. 0.] \t[1.95 0.57] \t[1.95 0.57]\n", - "14 \t34 \t[1.95 0.57] \t[0. 0.] \t[1.95 0.57] \t[1.95 0.57]\n", - "15 \t34 \t[1.95 0.57] \t[0. 0.] \t[1.95 0.57] \t[1.95 0.57]\n", - "16 \t34 \t[1.95 0.57] \t[0. 0.] \t[1.95 0.57] \t[1.95 0.57]\n", - "17 \t34 \t[1.95 0.57] \t[0. 0.] \t[1.95 0.57] \t[1.95 0.57]\n", - "18 \t34 \t[1.95 0.57] \t[0. 0.] \t[1.95 0.57] \t[1.95 0.57]\n", - "19 \t34 \t[1.95 0.57] \t[0. 0.] \t[1.95 0.57] \t[1.95 0.57]\n", - "20 \t34 \t[1.95 0.57] \t[0. 0.] \t[1.95 0.57] \t[1.95 0.57]\n", - "21 \t34 \t[1.95 0.57] \t[0. 0.] \t[1.95 0.57] \t[1.95 0.57]\n", - "22 \t34 \t[1.95 0.57] \t[0. 0.] \t[1.95 0.57] \t[1.95 0.57]\n", - "23 \t34 \t[1.95 0.57] \t[0. 0.] \t[1.95 0.57] \t[1.95 0.57]\n", - "24 \t34 \t[1.95 0.57] \t[0. 0.] \t[1.95 0.57] \t[1.95 0.57]\n", - "25 \t34 \t[1.95 0.57] \t[0. 0.] \t[1.95 0.57] \t[1.95 0.57]\n", - "26 \t34 \t[1.95 0.57] \t[0. 0.] \t[1.95 0.57] \t[1.95 0.57]\n", - "27 \t34 \t[1.95 0.57] \t[0. 0.] \t[1.95 0.57] \t[1.95 0.57]\n", - "28 \t34 \t[1.95 0.57] \t[0. 0.] \t[1.95 0.57] \t[1.95 0.57]\n", - "29 \t34 \t[1.95 0.57] \t[0. 0.] \t[1.95 0.57] \t[1.95 0.57]\n", - "30 \t34 \t[1.95 0.57] \t[0. 0.] \t[1.95 0.57] \t[1.95 0.57]\n", - "遗传算法优化完成,耗时309秒\n", - "输出帕累托前沿解集:\n", - "{'boll_window': 48, 'cci_window': 40, 'atr_window': 22, 'return_drawdown_ratio': 1.95, 'sharpe_ratio': 0.57}\n", - "{'boll_window': 48, 'cci_window': 50, 'atr_window': 22, 'return_drawdown_ratio': 1.95, 'sharpe_ratio': 0.57}\n" + "开始运行遗传算法,每代族群总数:34, 优良品种筛选个数:27,迭代次数:30,交叉概率:0.95,突变概率:0.05\n" ] } ], diff --git a/tests/backtesting/turtle.ipynb b/tests/backtesting/turtle.ipynb index 8a4f75f4..fc872235 100644 --- a/tests/backtesting/turtle.ipynb +++ b/tests/backtesting/turtle.ipynb @@ -61,517 +61,33 @@ "name": "stdout", "output_type": "stream", "text": [ - "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" + "2019-05-03 14:58:44.510371\t开始运行遗传算法,每代族群总数:20, 优良品种筛选个数:16,迭代次数:300,交叉概率:0.95,突变概率:0.05\n" ] }, { - "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" - ] - }, - { - "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)", + "ename": "AttributeError", + "evalue": "Can't pickle local object 'create_ga_optimize..ga_optimize'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\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< 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)" + "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 4\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_ma_length\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m10\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m80\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 5\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\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 601\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 602\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--> 603\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 604\u001b[0m ) \n\u001b[0;32m 605\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 301\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[0;32m 302\u001b[0m \u001b[0minvalid_ind\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[0mind\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mind\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mpopulation\u001b[0m \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mind\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfitness\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mvalid\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 303\u001b[1;33m \u001b[0mfitnesses\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtoolbox\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[0mevaluate\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0minvalid_ind\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 304\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mind\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfit\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mzip\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minvalid_ind\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfitnesses\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 305\u001b[0m \u001b[0mind\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[0mfit\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32mc:\\miniconda3\\lib\\multiprocessing\\pool.py\u001b[0m in \u001b[0;36mmap\u001b[1;34m(self, func, iterable, chunksize)\u001b[0m\n\u001b[0;32m 288\u001b[0m \u001b[1;32min\u001b[0m \u001b[0ma\u001b[0m \u001b[0mlist\u001b[0m \u001b[0mthat\u001b[0m \u001b[1;32mis\u001b[0m \u001b[0mreturned\u001b[0m\u001b[1;33m.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 289\u001b[0m '''\n\u001b[1;32m--> 290\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_map_async\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfunc\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0miterable\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmapstar\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mchunksize\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget\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 291\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 292\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mstarmap\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfunc\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0miterable\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mchunksize\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\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;32mc:\\miniconda3\\lib\\multiprocessing\\pool.py\u001b[0m in \u001b[0;36mget\u001b[1;34m(self, timeout)\u001b[0m\n\u001b[0;32m 681\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_value\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 682\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 683\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_value\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 684\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 685\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0m_set\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mi\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mobj\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;32mc:\\miniconda3\\lib\\multiprocessing\\pool.py\u001b[0m in \u001b[0;36m_handle_tasks\u001b[1;34m(taskqueue, put, outqueue, pool, cache)\u001b[0m\n\u001b[0;32m 455\u001b[0m \u001b[1;32mbreak\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 456\u001b[0m \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 457\u001b[1;33m \u001b[0mput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtask\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 458\u001b[0m \u001b[1;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 459\u001b[0m \u001b[0mjob\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0midx\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtask\u001b[0m\u001b[1;33m[\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[0m\n", + "\u001b[1;32mc:\\miniconda3\\lib\\multiprocessing\\connection.py\u001b[0m in \u001b[0;36msend\u001b[1;34m(self, obj)\u001b[0m\n\u001b[0;32m 204\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_check_closed\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 205\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_check_writable\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--> 206\u001b[1;33m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_send_bytes\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0m_ForkingPickler\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdumps\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mobj\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 207\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 208\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mrecv_bytes\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmaxlength\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\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;32mc:\\miniconda3\\lib\\multiprocessing\\reduction.py\u001b[0m in \u001b[0;36mdumps\u001b[1;34m(cls, obj, protocol)\u001b[0m\n\u001b[0;32m 49\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mdumps\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcls\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mobj\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mprotocol\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\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 50\u001b[0m \u001b[0mbuf\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mio\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mBytesIO\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---> 51\u001b[1;33m \u001b[0mcls\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mbuf\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mprotocol\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdump\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mobj\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 52\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mbuf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgetbuffer\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 53\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mAttributeError\u001b[0m: Can't pickle local object 'create_ga_optimize..ga_optimize'" ] } ], "source": [ "setting = OptimizationSetting()\n", - "setting.set_target(\"total_return\")\n", + "setting.set_target(\"sharpe_ratio\")\n", "setting.add_parameter(\"atr_length\", 3, 105, 1)\n", + "setting.add_parameter(\"atr_ma_length\", 10, 80, 1)\n", "\n", "engine.run_ga_optimization(setting)" ] diff --git a/vnpy/app/cta_strategy/backtesting.py b/vnpy/app/cta_strategy/backtesting.py index 083cb2d0..004bdda5 100644 --- a/vnpy/app/cta_strategy/backtesting.py +++ b/vnpy/app/cta_strategy/backtesting.py @@ -565,41 +565,40 @@ class BacktestingEngine: toolbox.register("evaluate", object_func) toolbox.register("select", tools.selNSGA2) - # pool = multiprocessing.Pool(multiprocessing.cpu_count()) - # toolbox.register("map", pool.map) + 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 # 种群代数 + mu = 16 # number of individuals to select for the next generation + lambda_ = 20 # number of children to produce at each generation + cxpb = 0.95 # probability that an offspring is produced by crossover + mutpb = 0.05 # probability that an offspring is produced by mutation + ngen = 300 # number of generation - pop = toolbox.population(POP) # 设置族群里面的个体数量 - hof = tools.ParetoFront() # 解的集合:帕累托前沿(非占优最优集) + pop_size = 20 # number of individuals in each generation + pop = toolbox.population(pop_size) + hof = tools.ParetoFront() # end result of pareto front 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) # 统计目标优化函数结果的最大值 + np.set_printoptions(suppress=True) + 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) + msg = "开始运行遗传算法,每代族群总数:%s, 优良品种筛选个数:%s,迭代次数:%s,交叉概率:%s,突变概率:%s" %(pop_size, mu, ngen, cxpb, mutpb) self.output(msg) # Run ga optimization - # esMuPlusLambda是一种基于(μ+λ)选择策略的多目标优化分段遗传算法 start = time() algorithms.eaMuPlusLambda( pop, toolbox, - MU, - LAMBDA, - CXPB, - MUTPB, - NGEN, + mu, + lambda_, + cxpb, + mutpb, + ngen, stats, halloffame=hof ) @@ -1139,3 +1138,19 @@ def load_tick_data( return database_manager.load_tick_data( symbol, exchange, start, end ) + + +# GA related global value +ga_target_name = None +ga_strategy_class = None +ga_setting = None +ga_vt_symbol = None +ga_interval = None +ga_start = None +ga_rate = None +ga_slippage = None +ga_size = None +ga_pricetick = None +ga_capital = None +ga_end = None +ga_mode = None \ No newline at end of file From 4cd84b45a5aa91b69e0e3cb0a83a6bf3ff0c8ccc Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Fri, 3 May 2019 16:10:11 +0800 Subject: [PATCH 3/4] [Mod]add return value of run_ga_optimization --- tests/backtesting/GA_Pre_Final.ipynb | 104 +++-------------- tests/backtesting/run_ga.py | 27 +++++ tests/backtesting/turtle.ipynb | 60 +++++----- vnpy/app/cta_strategy/backtesting.py | 164 ++++++++++++++------------- 4 files changed, 153 insertions(+), 202 deletions(-) create mode 100644 tests/backtesting/run_ga.py diff --git a/tests/backtesting/GA_Pre_Final.ipynb b/tests/backtesting/GA_Pre_Final.ipynb index 0d99a169..de3b6d46 100644 --- a/tests/backtesting/GA_Pre_Final.ipynb +++ b/tests/backtesting/GA_Pre_Final.ipynb @@ -20,17 +20,9 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "数据总体: 13824\n" - ] - } - ], + "outputs": [], "source": [ "setting = OptimizationSetting()\n", "#setting.add_parameter('atr_length', 10, 50, 2)\n", @@ -50,20 +42,9 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "dict_keys(['boll_window', 'cci_window', 'atr_window'])" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "setting_names = random.choice(local_setting).keys()\n", "setting_names" @@ -71,7 +52,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -82,40 +63,18 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "[48, 6, 26]" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "parameter_generate()" ] }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'boll_window': 16, 'cci_window': 48, 'atr_window': 6}" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "setting=dict(zip(setting_names,parameter_generate()))\n", "setting" @@ -123,7 +82,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -167,41 +126,16 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2019-05-03 15:02:07.528909\t开始加载历史数据\n", - "2019-05-03 15:02:34.854177\t历史数据加载完成,数据量:175440\n", - "2019-05-03 15:02:34.877616\t策略初始化完成\n", - "2019-05-03 15:02:34.877616\t开始回放历史数据\n", - "2019-05-03 15:02:37.000744\t历史数据回放结束\n", - "2019-05-03 15:02:37.000744\t开始计算逐日盯市盈亏\n", - "2019-05-03 15:02:37.012463\t逐日盯市盈亏计算完成\n", - "2019-05-03 15:02:37.012463\t开始计算策略统计指标\n" - ] - }, - { - "data": { - "text/plain": [ - "(0.96, 0.38)" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "object_func(parameter_generate())" ] }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -216,7 +150,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -283,15 +217,7 @@ "metadata": { "scrolled": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "开始运行遗传算法,每代族群总数:34, 优良品种筛选个数:27,迭代次数:30,交叉概率:0.95,突变概率:0.05\n" - ] - } - ], + "outputs": [], "source": [ "optimize()" ] diff --git a/tests/backtesting/run_ga.py b/tests/backtesting/run_ga.py new file mode 100644 index 00000000..4ce1c6b3 --- /dev/null +++ b/tests/backtesting/run_ga.py @@ -0,0 +1,27 @@ +from vnpy.app.cta_strategy.backtesting import BacktestingEngine, OptimizationSetting +from vnpy.app.cta_strategy.strategies.atr_rsi_strategy import ( + AtrRsiStrategy, +) +from datetime import datetime + +if __name__ == "__main__": + engine = BacktestingEngine() + engine.set_parameters( + vt_symbol="IF88.CFFEX", + interval="1m", + start=datetime(2019, 1, 1), + end=datetime(2019, 4, 30), + rate=0.3 / 10000, + slippage=0.2, + size=300, + pricetick=0.2, + capital=1_000_000, + ) + engine.add_strategy(AtrRsiStrategy, {}) + + setting = OptimizationSetting() + setting.set_target("sharpe_ratio") + setting.add_parameter("atr_length", 3, 39, 1) + setting.add_parameter("atr_ma_length", 10, 30, 1) + + engine.run_ga_optimization(setting) diff --git a/tests/backtesting/turtle.ipynb b/tests/backtesting/turtle.ipynb index fc872235..822614f8 100644 --- a/tests/backtesting/turtle.ipynb +++ b/tests/backtesting/turtle.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -16,7 +16,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -54,44 +54,38 @@ }, { "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2019-05-03 14:58:44.510371\t开始运行遗传算法,每代族群总数:20, 优良品种筛选个数:16,迭代次数:300,交叉概率:0.95,突变概率:0.05\n" - ] - }, - { - "ename": "AttributeError", - "evalue": "Can't pickle local object 'create_ga_optimize..ga_optimize'", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 4\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_ma_length\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m10\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m80\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 5\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\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 601\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 602\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--> 603\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 604\u001b[0m ) \n\u001b[0;32m 605\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 301\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[0;32m 302\u001b[0m \u001b[0minvalid_ind\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[0mind\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mind\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mpopulation\u001b[0m \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mind\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfitness\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mvalid\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 303\u001b[1;33m \u001b[0mfitnesses\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtoolbox\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[0mevaluate\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0minvalid_ind\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 304\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mind\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfit\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mzip\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minvalid_ind\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfitnesses\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 305\u001b[0m \u001b[0mind\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[0mfit\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", - "\u001b[1;32mc:\\miniconda3\\lib\\multiprocessing\\pool.py\u001b[0m in \u001b[0;36mmap\u001b[1;34m(self, func, iterable, chunksize)\u001b[0m\n\u001b[0;32m 288\u001b[0m \u001b[1;32min\u001b[0m \u001b[0ma\u001b[0m \u001b[0mlist\u001b[0m \u001b[0mthat\u001b[0m \u001b[1;32mis\u001b[0m \u001b[0mreturned\u001b[0m\u001b[1;33m.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 289\u001b[0m '''\n\u001b[1;32m--> 290\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_map_async\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfunc\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0miterable\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmapstar\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mchunksize\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget\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 291\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 292\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mstarmap\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfunc\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0miterable\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mchunksize\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\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;32mc:\\miniconda3\\lib\\multiprocessing\\pool.py\u001b[0m in \u001b[0;36mget\u001b[1;34m(self, timeout)\u001b[0m\n\u001b[0;32m 681\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_value\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 682\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 683\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_value\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 684\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 685\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0m_set\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mi\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mobj\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;32mc:\\miniconda3\\lib\\multiprocessing\\pool.py\u001b[0m in \u001b[0;36m_handle_tasks\u001b[1;34m(taskqueue, put, outqueue, pool, cache)\u001b[0m\n\u001b[0;32m 455\u001b[0m \u001b[1;32mbreak\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 456\u001b[0m \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 457\u001b[1;33m \u001b[0mput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtask\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 458\u001b[0m \u001b[1;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 459\u001b[0m \u001b[0mjob\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0midx\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtask\u001b[0m\u001b[1;33m[\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[0m\n", - "\u001b[1;32mc:\\miniconda3\\lib\\multiprocessing\\connection.py\u001b[0m in \u001b[0;36msend\u001b[1;34m(self, obj)\u001b[0m\n\u001b[0;32m 204\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_check_closed\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 205\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_check_writable\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--> 206\u001b[1;33m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_send_bytes\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0m_ForkingPickler\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdumps\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mobj\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 207\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 208\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mrecv_bytes\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmaxlength\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\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;32mc:\\miniconda3\\lib\\multiprocessing\\reduction.py\u001b[0m in \u001b[0;36mdumps\u001b[1;34m(cls, obj, protocol)\u001b[0m\n\u001b[0;32m 49\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mdumps\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcls\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mobj\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mprotocol\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\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 50\u001b[0m \u001b[0mbuf\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mio\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mBytesIO\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---> 51\u001b[1;33m \u001b[0mcls\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mbuf\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mprotocol\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdump\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mobj\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 52\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mbuf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgetbuffer\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 53\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", - "\u001b[1;31mAttributeError\u001b[0m: Can't pickle local object 'create_ga_optimize..ga_optimize'" - ] - } - ], + "execution_count": null, + "metadata": { + "scrolled": true + }, + "outputs": [], "source": [ "setting = OptimizationSetting()\n", "setting.set_target(\"sharpe_ratio\")\n", - "setting.add_parameter(\"atr_length\", 3, 105, 1)\n", - "setting.add_parameter(\"atr_ma_length\", 10, 80, 1)\n", + "setting.add_parameter(\"atr_length\", 3, 39, 1)\n", + "setting.add_parameter(\"atr_ma_length\", 10, 30, 1)\n", "\n", "engine.run_ga_optimization(setting)" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "result = _" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(result)" + ] + }, { "cell_type": "code", "execution_count": null, diff --git a/vnpy/app/cta_strategy/backtesting.py b/vnpy/app/cta_strategy/backtesting.py index 004bdda5..bb670872 100644 --- a/vnpy/app/cta_strategy/backtesting.py +++ b/vnpy/app/cta_strategy/backtesting.py @@ -6,6 +6,7 @@ from functools import lru_cache from time import time import multiprocessing import random +import math import numpy as np import matplotlib.pyplot as plt @@ -29,6 +30,8 @@ from .base import ( from .template import CtaTemplate sns.set_style("whitegrid") +creator.create("FitnessMax", base.Fitness, weights=(1.0,)) +creator.create("Individual", list, fitness=creator.FitnessMax) class OptimizationSetting: @@ -537,44 +540,52 @@ class BacktestingEngine: 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 - ) + global ga_target_name + global ga_strategy_class + global ga_setting + global ga_vt_symbol + global ga_interval + global ga_start + global ga_rate + global ga_slippage + global ga_size + global ga_pricetick + global ga_capital + global ga_end + global ga_mode + + ga_target_name = target_name + ga_strategy_class = self.strategy_class + ga_setting = settings[0] + ga_vt_symbol = self.vt_symbol + ga_interval = self.interval + ga_start = self.start + ga_rate = self.rate + ga_slippage = self.slippage + ga_size = self.size + ga_pricetick = self.pricetick + ga_capital = self.capital + ga_end = self.end + ga_mode = 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("evaluate", ga_optimize) toolbox.register("select", tools.selNSGA2) - pool = multiprocessing.Pool(multiprocessing.cpu_count()) - toolbox.register("map", pool.map) - - mu = 16 # number of individuals to select for the next generation - lambda_ = 20 # number of children to produce at each generation - cxpb = 0.95 # probability that an offspring is produced by crossover - mutpb = 0.05 # probability that an offspring is produced by mutation - ngen = 300 # number of generation - - pop_size = 20 # number of individuals in each generation + total_size = len(settings) + pop_size = int(pow(total_size, 1 / math.e)) # number of individuals in each generation + lambda_ = pop_size # number of children to produce at each generation + mu = int(pop_size * 0.8) # number of individuals to select for the next generation + + cxpb = 0.95 # probability that an offspring is produced by crossover + mutpb = 1 - cxpb # probability that an offspring is produced by mutation + ngen = 30 # number of generation + pop = toolbox.population(pop_size) hof = tools.ParetoFront() # end result of pareto front @@ -584,11 +595,15 @@ class BacktestingEngine: 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_size, mu, ngen, cxpb, mutpb) - self.output(msg) + + # Multiprocessing is not supported yet. + # pool = multiprocessing.Pool(multiprocessing.cpu_count()) + # toolbox.register("map", pool.map) # Run ga optimization + msg = "开始运行遗传算法,每代族群总数:%s, 优良品种筛选个数:%s,迭代次数:%s,交叉概率:%s,突变概率:%s" % (pop_size, mu, ngen, cxpb, mutpb) + self.output(msg) + start = time() algorithms.eaMuPlusLambda( @@ -607,9 +622,17 @@ class BacktestingEngine: cost = int((end - start)) self.output(f"遗传算法优化完成,耗时{cost}秒") - self.output("输出帕累托前沿解集:") - return hof + # Return result list + results = [] + parameter_keys = list(ga_setting.keys()) + + for parameter_values in hof: + setting = dict(zip(parameter_keys, parameter_values)) + target_value = ga_optimize(parameter_values)[0] + results.append((setting, target_value)) + + return results def update_daily_close(self, price: float): """""" @@ -1065,52 +1088,33 @@ 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 _ga_optimizae(parameter_values: tuple): + """""" + parameter_keys = list(ga_setting.keys()) + setting = dict(zip(parameter_keys, parameter_values)) - @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],) + result = optimize( + ga_target_name, + ga_strategy_class, + setting, + ga_vt_symbol, + ga_interval, + ga_start, + ga_rate, + ga_slippage, + ga_size, + ga_pricetick, + ga_capital, + ga_end, + ga_mode + ) + return (result[1],) - def ga_optimize(parameter_values: list): - """""" - return _optimizae(tuple(parameter_values)) - return ga_optimize +def ga_optimize(parameter_values: list): + """""" + return _ga_optimizae(tuple(parameter_values)) @lru_cache(maxsize=10) @@ -1141,6 +1145,8 @@ def load_tick_data( # GA related global value +ga_end = None +ga_mode = None ga_target_name = None ga_strategy_class = None ga_setting = None @@ -1151,6 +1157,4 @@ ga_rate = None ga_slippage = None ga_size = None ga_pricetick = None -ga_capital = None -ga_end = None -ga_mode = None \ No newline at end of file +ga_capital = None \ No newline at end of file From a11e6be3ce8448b81abfbfa34681afc30eef8902 Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Fri, 3 May 2019 17:01:47 +0800 Subject: [PATCH 4/4] [Add]run ga optimization in CtaBacktester --- .flake8 | 1 + tests/backtesting/turtle.ipynb | 59 ++++++++++++++++++++++++++-- vnpy/app/cta_backtester/engine.py | 51 +++++++++++++++--------- vnpy/app/cta_backtester/ui/widget.py | 29 +++++++++++--- vnpy/app/cta_strategy/backtesting.py | 15 ++++--- 5 files changed, 121 insertions(+), 34 deletions(-) diff --git a/.flake8 b/.flake8 index ab63c931..10742c16 100644 --- a/.flake8 +++ b/.flake8 @@ -5,3 +5,4 @@ ignore = W503 line break before binary operator W293 blank line contains whitespace W291 trailing whitespace + W391 blank line at end of file diff --git a/tests/backtesting/turtle.ipynb b/tests/backtesting/turtle.ipynb index 822614f8..a3a4db10 100644 --- a/tests/backtesting/turtle.ipynb +++ b/tests/backtesting/turtle.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -16,7 +16,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -54,11 +54,62 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": { "scrolled": true }, - "outputs": [], + "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" + } + ], "source": [ "setting = OptimizationSetting()\n", "setting.set_target(\"sharpe_ratio\")\n", diff --git a/vnpy/app/cta_backtester/engine.py b/vnpy/app/cta_backtester/engine.py index 9da8e6f7..573feefe 100644 --- a/vnpy/app/cta_backtester/engine.py +++ b/vnpy/app/cta_backtester/engine.py @@ -222,20 +222,25 @@ class BacktesterEngine(BaseEngine): return strategy_class.get_class_parameters() def run_optimization( - self, - class_name: str, - vt_symbol: str, - interval: str, - start: datetime, - end: datetime, - rate: float, - slippage: float, - size: int, - pricetick: float, - capital: int, - optimization_setting: OptimizationSetting): + self, + class_name: str, + vt_symbol: str, + interval: str, + start: datetime, + end: datetime, + rate: float, + slippage: float, + size: int, + pricetick: float, + capital: int, + optimization_setting: OptimizationSetting, + use_ga: bool + ): """""" - self.write_log("开始多进程参数优化") + if use_ga: + self.write_log("开始遗传算法参数优化") + else: + self.write_log("开始多进程参数优化") self.result_values = None @@ -260,10 +265,16 @@ class BacktesterEngine(BaseEngine): {} ) - self.result_values = engine.run_optimization( - optimization_setting, - output=False - ) + if use_ga: + self.result_values = engine.run_ga_optimization( + optimization_setting, + output=False + ) + else: + self.result_values = engine.run_optimization( + optimization_setting, + output=False + ) # Clear thread object handler. self.thread = None @@ -285,7 +296,8 @@ class BacktesterEngine(BaseEngine): size: int, pricetick: float, capital: int, - optimization_setting: OptimizationSetting + optimization_setting: OptimizationSetting, + use_ga: bool ): if self.thread: self.write_log("已有任务在运行中,请等待完成") @@ -305,7 +317,8 @@ class BacktesterEngine(BaseEngine): size, pricetick, capital, - optimization_setting + optimization_setting, + use_ga ) ) self.thread.start() diff --git a/vnpy/app/cta_backtester/ui/widget.py b/vnpy/app/cta_backtester/ui/widget.py index 74eddac9..2198c11d 100644 --- a/vnpy/app/cta_backtester/ui/widget.py +++ b/vnpy/app/cta_backtester/ui/widget.py @@ -240,7 +240,7 @@ class BacktesterManager(QtWidgets.QWidget): if i != dialog.Accepted: return - optimization_setting = dialog.get_setting() + optimization_setting, use_ga = dialog.get_setting() self.target_display = dialog.target_display self.backtester_engine.start_optimization( @@ -254,7 +254,8 @@ class BacktesterManager(QtWidgets.QWidget): size, pricetick, capital, - optimization_setting + optimization_setting, + use_ga ) self.result_button.setEnabled(False) @@ -592,6 +593,7 @@ class OptimizationSettingEditor(QtWidgets.QDialog): self.edits = {} self.optimization_setting = None + self.use_ga = False self.init_ui() @@ -642,12 +644,27 @@ class OptimizationSettingEditor(QtWidgets.QDialog): row += 1 - button = QtWidgets.QPushButton("确定") - button.clicked.connect(self.generate_setting) - grid.addWidget(button, row, 0, 1, 4) + parallel_button = QtWidgets.QPushButton("多进程优化") + parallel_button.clicked.connect(self.generate_parallel_setting) + grid.addWidget(parallel_button, row, 0, 1, 4) + + row += 1 + ga_button = QtWidgets.QPushButton("遗传算法优化") + ga_button.clicked.connect(self.generate_ga_setting) + grid.addWidget(ga_button, row, 0, 1, 4) self.setLayout(grid) + def generate_ga_setting(self): + """""" + self.use_ga = True + self.generate_setting() + + def generate_parallel_setting(self): + """""" + self.use_ga = False + self.generate_setting() + def generate_setting(self): """""" self.optimization_setting = OptimizationSetting() @@ -676,7 +693,7 @@ class OptimizationSettingEditor(QtWidgets.QDialog): def get_setting(self): """""" - return self.optimization_setting + return self.optimization_setting, self.use_ga class OptimizationResultMonitor(QtWidgets.QDialog): diff --git a/vnpy/app/cta_strategy/backtesting.py b/vnpy/app/cta_strategy/backtesting.py index bb670872..fc4875ce 100644 --- a/vnpy/app/cta_strategy/backtesting.py +++ b/vnpy/app/cta_strategy/backtesting.py @@ -601,8 +601,12 @@ class BacktestingEngine: # toolbox.register("map", pool.map) # Run ga optimization - msg = "开始运行遗传算法,每代族群总数:%s, 优良品种筛选个数:%s,迭代次数:%s,交叉概率:%s,突变概率:%s" % (pop_size, mu, ngen, cxpb, mutpb) - self.output(msg) + self.output(f"参数优化空间:{total_size}") + self.output(f"每代族群总数:{pop_size}") + self.output(f"优良筛选个数:{mu}") + self.output(f"迭代次数:{ngen}") + self.output(f"交叉概率:{cxpb:.0%}") + self.output(f"突变概率:{mutpb:.0%}") start = time() @@ -617,7 +621,7 @@ class BacktestingEngine: stats, halloffame=hof ) - + end = time() cost = int((end - start)) @@ -630,7 +634,7 @@ class BacktestingEngine: for parameter_values in hof: setting = dict(zip(parameter_keys, parameter_values)) target_value = ga_optimize(parameter_values)[0] - results.append((setting, target_value)) + results.append((setting, target_value, {})) return results @@ -1059,12 +1063,13 @@ def optimize( pricetick: float, capital: int, end: datetime, - mode: BacktestingMode, + mode: BacktestingMode ): """ Function for running in multiprocessing.pool """ engine = BacktestingEngine() + engine.set_parameters( vt_symbol=vt_symbol, interval=interval,