Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
A
alpha-mind
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Dr.李
alpha-mind
Commits
c86610be
Commit
c86610be
authored
Feb 25, 2018
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update example
parent
265f1b2a
Changes
2
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
754 additions
and
63 deletions
+754
-63
Example 1 - factor IC analysis.ipynb
notebooks/Example 1 - factor IC analysis.ipynb
+41
-63
Example 2 - Strategy Analysis.ipynb
notebooks/Example 2 - Strategy Analysis.ipynb
+713
-0
No files found.
notebooks/Example 1 - factor IC analysis.ipynb
View file @
c86610be
...
@@ -2,7 +2,7 @@
...
@@ -2,7 +2,7 @@
"cells": [
"cells": [
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
null
,
"execution_count":
1
,
"metadata": {
"metadata": {
"collapsed": true
"collapsed": true
},
},
...
@@ -21,7 +21,7 @@
...
@@ -21,7 +21,7 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
null
,
"execution_count":
2
,
"metadata": {
"metadata": {
"collapsed": true
"collapsed": true
},
},
...
@@ -34,22 +34,23 @@
...
@@ -34,22 +34,23 @@
"start_date = '2010-01-01'\n",
"start_date = '2010-01-01'\n",
"end_date = '2018-02-14'\n",
"end_date = '2018-02-14'\n",
"\n",
"\n",
"frequency = '
2
0b'\n",
"frequency = '
1
0b'\n",
"method = 'risk_neutral'\n",
"method = 'risk_neutral'\n",
"neutralize_risk = industry_styles\n",
"neutralize_risk = industry_styles\n",
"turn_over_target_base = 2.0\n",
"industry_name = 'sw_adj'\n",
"benchmark_total_lower = 1.0\n",
"industry_level = 1\n",
"benchmark_total_lower = 0.8\n",
"benchmark_total_upper = 1.0\n",
"benchmark_total_upper = 1.0\n",
"horizon = map_freq(frequency)\n",
"horizon = map_freq(frequency)\n",
"weight_gap = 0.
300
\n",
"weight_gap = 0.
01
\n",
"benchmark_code = 905\n",
"benchmark_code = 905\n",
"universe_name = ['
hs3
00']\n",
"universe_name = ['
zz8
00']\n",
"universe = Universe('custom', universe_name)\n",
"universe = Universe('custom', universe_name)\n",
"ref_dates = makeSchedule(start_date, end_date, frequency, 'china.sse')\n",
"ref_dates = makeSchedule(start_date, end_date, frequency, 'china.sse')\n",
"\n",
"\n",
"executor = NaiveExecutor()\n",
"executor = NaiveExecutor()\n",
"
connect_str
= 'postgres+psycopg2://postgres:we083826@192.168.0.102/alpha'\n",
"
data_source
= 'postgres+psycopg2://postgres:we083826@192.168.0.102/alpha'\n",
"engine = SqlEngine(
connect_str
)"
"engine = SqlEngine(
data_source
)"
]
]
},
},
{
{
...
@@ -64,7 +65,8 @@
...
@@ -64,7 +65,8 @@
"Constraints settings\n",
"Constraints settings\n",
"\"\"\"\n",
"\"\"\"\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",
"total_risk_names = constraint_risk + ['benchmark', 'total']\n",
"\n",
"\n",
"b_type = []\n",
"b_type = []\n",
...
@@ -76,10 +78,6 @@
...
@@ -76,10 +78,6 @@
" b_type.append(BoundaryType.RELATIVE)\n",
" b_type.append(BoundaryType.RELATIVE)\n",
" l_val.append(benchmark_total_lower)\n",
" l_val.append(benchmark_total_lower)\n",
" u_val.append(benchmark_total_upper)\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",
" else:\n",
" b_type.append(BoundaryType.ABSOLUTE)\n",
" b_type.append(BoundaryType.ABSOLUTE)\n",
" l_val.append(0.0)\n",
" l_val.append(0.0)\n",
...
@@ -91,7 +89,9 @@
...
@@ -91,7 +89,9 @@
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": null,
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"outputs": [],
"source": [
"source": [
"\"\"\"\n",
"\"\"\"\n",
...
@@ -105,7 +105,10 @@
...
@@ -105,7 +105,10 @@
" dates=ref_dates,\n",
" dates=ref_dates,\n",
" horizon=horizon,\n",
" horizon=horizon,\n",
" offset=1)\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 @@
...
@@ -146,11 +149,12 @@
" date = value[0]\n",
" date = value[0]\n",
" data = value[1]\n",
" data = value[1]\n",
" index_dates.append(date)\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 = 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",
" alpha_logger.info('{0}: {1}'.format(date, len(total_data)))\n",
" risk_exp = total_data[neutralize_risk].values.astype(float)\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",
" benchmark_w = total_data.weight.values\n",
" is_in_benchmark = (benchmark_w > 0.).astype(float).reshape(-1, 1)\n",
" is_in_benchmark = (benchmark_w > 0.).astype(float).reshape(-1, 1)\n",
"\n",
"\n",
...
@@ -174,7 +178,7 @@
...
@@ -174,7 +178,7 @@
"\n",
"\n",
" alpha_logger.info('{0} full re-balance'.format(date))\n",
" alpha_logger.info('{0} full re-balance'.format(date))\n",
" target_pos, _ = er_portfolio_analysis(er,\n",
" target_pos, _ = er_portfolio_analysis(er,\n",
"
industry
,\n",
"
total_data.industry_name.values
,\n",
" None,\n",
" None,\n",
" constraints,\n",
" constraints,\n",
" False,\n",
" False,\n",
...
@@ -217,13 +221,13 @@
...
@@ -217,13 +221,13 @@
"\n",
"\n",
"def worker_func_positive(factor_name):\n",
"def worker_func_positive(factor_name):\n",
" from alphamind.api import SqlEngine\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",
" return factor_analysis(engine, factor_name, universe, benchmark_code, positive=True)\n",
"\n",
"\n",
"\n",
"\n",
"def worker_func_negative(factor_name):\n",
"def worker_func_negative(factor_name):\n",
" from alphamind.api import SqlEngine\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)"
" return factor_analysis(engine, factor_name, universe, benchmark_code, positive=False)"
]
]
},
},
...
@@ -237,7 +241,7 @@
...
@@ -237,7 +241,7 @@
"source": [
"source": [
"df = engine.fetch_factor_coverage(start_date='2011-01-01',\n",
"df = engine.fetch_factor_coverage(start_date='2011-01-01',\n",
" end_date='2018-02-12',\n",
" end_date='2018-02-12',\n",
" universe=
'hs300'
)\n",
" universe=
universe_name[0]
)\n",
"df = df[df.source != 'risk_exposure']\n",
"df = df[df.source != 'risk_exposure']\n",
"df = df.groupby('factor').mean()\n",
"df = df.groupby('factor').mean()\n",
"df = df[df.coverage >= 0.98]"
"df = df[df.coverage >= 0.98]"
...
@@ -246,40 +250,41 @@
...
@@ -246,40 +250,41 @@
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": null,
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"outputs": [],
"source": [
"source": [
"%%time\n",
"%%time\n",
"\n",
"\n",
"from dask.distributed import Client\n",
"from dask.distributed import Client\n",
"from dask.diagnostics import ProgressBar\n",
"\n",
"\n",
"client = Client('192.168.0.102:8786')\n",
"client = Client('192.168.0.102:8786')\n",
"\n",
"\n",
"tasks = client.map(worker_func_positive, df.index.tolist(), pure=False)\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",
"\n",
"tasks = client.map(worker_func_negative, df.index.tolist(), pure=False)\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",
"\n",
"factor_df = pd.DataFrame()\n",
"factor_df = pd.DataFrame()\n",
"ic_df = pd.DataFrame()\n",
"ic_df = pd.DataFrame()\n",
"\n",
"\n",
"for f_name,
df
in res1:\n",
"for f_name,
res
in res1:\n",
" factor_df[f_name] =
df
['returns']\n",
" factor_df[f_name] =
res
['returns']\n",
" ic_df[f_name] =
df
['IC']\n",
" ic_df[f_name] =
res
['IC']\n",
"\n",
"\n",
"for f_name,
df
in res2:\n",
"for f_name,
res
in res2:\n",
" factor_df[f_name] =
df
['returns']\n",
" factor_df[f_name] =
res
['returns']\n",
" ic_df[f_name] =
df
['IC']"
" ic_df[f_name] =
res
['IC']"
]
]
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": null,
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"outputs": [],
"source": [
"source": [
"writer = pd.ExcelWriter(f'{universe_name[0]}.xlsx', engine='xlsxwriter')\n",
"writer = pd.ExcelWriter(f'{universe_name[0]}.xlsx', engine='xlsxwriter')\n",
...
@@ -288,33 +293,6 @@
...
@@ -288,33 +293,6 @@
"writer.close()"
"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",
"cell_type": "code",
"execution_count": null,
"execution_count": null,
...
@@ -350,7 +328,7 @@
...
@@ -350,7 +328,7 @@
"name": "python",
"name": "python",
"nbconvert_exporter": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"pygments_lexer": "ipython3",
"version": "3.6.
4
"
"version": "3.6.
3
"
}
}
},
},
"nbformat": 4,
"nbformat": 4,
...
...
notebooks/Example 2 - Strategy Analysis.ipynb
0 → 100644
View file @
c86610be
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment