Commit c86610be authored by Dr.李's avatar Dr.李

update example

parent 265f1b2a
......@@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {
"collapsed": true
},
......@@ -21,7 +21,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {
"collapsed": true
},
......@@ -34,22 +34,23 @@
"start_date = '2010-01-01'\n",
"end_date = '2018-02-14'\n",
"\n",
"frequency = '20b'\n",
"frequency = '10b'\n",
"method = 'risk_neutral'\n",
"neutralize_risk = industry_styles\n",
"turn_over_target_base = 2.0\n",
"benchmark_total_lower = 1.0\n",
"industry_name = 'sw_adj'\n",
"industry_level = 1\n",
"benchmark_total_lower = 0.8\n",
"benchmark_total_upper = 1.0\n",
"horizon = map_freq(frequency)\n",
"weight_gap = 0.300\n",
"weight_gap = 0.01\n",
"benchmark_code = 905\n",
"universe_name = ['hs300']\n",
"universe_name = ['zz800']\n",
"universe = Universe('custom', universe_name)\n",
"ref_dates = makeSchedule(start_date, end_date, frequency, 'china.sse')\n",
"\n",
"executor = NaiveExecutor()\n",
"connect_str = 'postgres+psycopg2://postgres:we083826@192.168.0.102/alpha'\n",
"engine = SqlEngine(connect_str)"
"data_source = 'postgres+psycopg2://postgres:we083826@192.168.0.102/alpha'\n",
"engine = SqlEngine(data_source)"
]
},
{
......@@ -64,7 +65,8 @@
"Constraints settings\n",
"\"\"\"\n",
"\n",
"constraint_risk = ['SIZE', 'SIZENL', 'BETA']\n",
"industry_names = industry_list(industry_name, industry_level)\n",
"constraint_risk = ['SIZE', 'SIZENL', 'BETA'] + industry_names\n",
"total_risk_names = constraint_risk + ['benchmark', 'total']\n",
"\n",
"b_type = []\n",
......@@ -76,10 +78,6 @@
" b_type.append(BoundaryType.RELATIVE)\n",
" l_val.append(benchmark_total_lower)\n",
" u_val.append(benchmark_total_upper)\n",
" elif name == 'total':\n",
" b_type.append(BoundaryType.RELATIVE)\n",
" l_val.append(1.0)\n",
" u_val.append(1.0)\n",
" else:\n",
" b_type.append(BoundaryType.ABSOLUTE)\n",
" l_val.append(0.0)\n",
......@@ -91,7 +89,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"\"\"\"\n",
......@@ -105,7 +105,10 @@
" dates=ref_dates,\n",
" horizon=horizon,\n",
" offset=1)\n",
"return_groups = codes_return.groupby('trade_date')"
"\n",
"return_groups = codes_return.groupby('trade_date')\n",
"industry_total = engine.fetch_industry_matrix_range(universe, dates=ref_dates, category=industry_name, level=industry_level)\n",
"industry_groups = industry_total.groupby('trade_date')"
]
},
{
......@@ -146,11 +149,12 @@
" date = value[0]\n",
" data = value[1]\n",
" index_dates.append(date)\n",
"\n",
" \n",
" industry_matrix = industry_groups.get_group(date)\n",
" total_data = data.fillna(data[alpha_name].median())\n",
" total_data = pd.merge(total_data, industry_matrix, on=['code'])\n",
" alpha_logger.info('{0}: {1}'.format(date, len(total_data)))\n",
" risk_exp = total_data[neutralize_risk].values.astype(float)\n",
" industry = total_data.industry_code.values\n",
" benchmark_w = total_data.weight.values\n",
" is_in_benchmark = (benchmark_w > 0.).astype(float).reshape(-1, 1)\n",
"\n",
......@@ -174,7 +178,7 @@
"\n",
" alpha_logger.info('{0} full re-balance'.format(date))\n",
" target_pos, _ = er_portfolio_analysis(er,\n",
" industry,\n",
" total_data.industry_name.values,\n",
" None,\n",
" constraints,\n",
" False,\n",
......@@ -217,13 +221,13 @@
"\n",
"def worker_func_positive(factor_name):\n",
" from alphamind.api import SqlEngine\n",
" engine = SqlEngine(connect_str)\n",
" engine = SqlEngine(data_source)\n",
" return factor_analysis(engine, factor_name, universe, benchmark_code, positive=True)\n",
"\n",
"\n",
"def worker_func_negative(factor_name):\n",
" from alphamind.api import SqlEngine\n",
" engine = SqlEngine(connect_str)\n",
" engine = SqlEngine(data_source)\n",
" return factor_analysis(engine, factor_name, universe, benchmark_code, positive=False)"
]
},
......@@ -237,7 +241,7 @@
"source": [
"df = engine.fetch_factor_coverage(start_date='2011-01-01',\n",
" end_date='2018-02-12',\n",
" universe='hs300')\n",
" universe=universe_name[0])\n",
"df = df[df.source != 'risk_exposure']\n",
"df = df.groupby('factor').mean()\n",
"df = df[df.coverage >= 0.98]"
......@@ -246,40 +250,41 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%%time\n",
"\n",
"from dask.distributed import Client\n",
"from dask.diagnostics import ProgressBar\n",
"\n",
"client = Client('192.168.0.102:8786')\n",
"\n",
"tasks = client.map(worker_func_positive, df.index.tolist(), pure=False)\n",
"with ProgressBar():\n",
" res1 = client.gather(tasks)\n",
"res1 = client.gather(tasks)\n",
"\n",
"tasks = client.map(worker_func_negative, df.index.tolist(), pure=False)\n",
"with ProgressBar():\n",
" res2 = client.gather(tasks)\n",
"res2 = client.gather(tasks)\n",
"\n",
"factor_df = pd.DataFrame()\n",
"ic_df = pd.DataFrame()\n",
"\n",
"for f_name, df in res1:\n",
" factor_df[f_name] = df['returns']\n",
" ic_df[f_name] = df['IC']\n",
"for f_name, res in res1:\n",
" factor_df[f_name] = res['returns']\n",
" ic_df[f_name] = res['IC']\n",
"\n",
"for f_name, df in res2:\n",
" factor_df[f_name] = df['returns']\n",
" ic_df[f_name] = df['IC']"
"for f_name, res in res2:\n",
" factor_df[f_name] = res['returns']\n",
" ic_df[f_name] = res['IC']"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"writer = pd.ExcelWriter(f'{universe_name[0]}.xlsx', engine='xlsxwriter')\n",
......@@ -288,33 +293,6 @@
"writer.close()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ic_df.shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
......@@ -350,7 +328,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.4"
"version": "3.6.3"
}
},
"nbformat": 4,
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment