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
5b940f54
Commit
5b940f54
authored
May 23, 2018
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update examples
parent
ec08d8a0
Changes
10
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
605 additions
and
397 deletions
+605
-397
Example 1 - Factor IC analysis.ipynb
notebooks/Example 1 - Factor IC analysis.ipynb
+495
-16
Example 10 - Quadratic Optimizer Comparison with CVXOPT.ipynb
...ple 10 - Quadratic Optimizer Comparison with CVXOPT.ipynb
+1
-1
Example 11 - Long Short Strategy Model.ipynb
notebooks/Example 11 - Long Short Strategy Model.ipynb
+2
-2
Example 12 - Machine Learning Model Prediction.ipynb
...ooks/Example 12 - Machine Learning Model Prediction.ipynb
+30
-324
Example 13 - Evaluation within Industry Groups.ipynb
...ooks/Example 13 - Evaluation within Industry Groups.ipynb
+3
-3
Example 2 - Strategy Analysis.ipynb
notebooks/Example 2 - Strategy Analysis.ipynb
+11
-8
Example 3 - Multi Weight Gap Comparison.ipynb
notebooks/Example 3 - Multi Weight Gap Comparison.ipynb
+51
-33
Example 6 - Target Volatility Builder.ipynb
notebooks/Example 6 - Target Volatility Builder.ipynb
+10
-8
Example 7 - Portfolio Optimizer Performance.ipynb
notebooks/Example 7 - Portfolio Optimizer Performance.ipynb
+1
-1
Example 8 - Neutralize Algorithm Comparison.ipynb
notebooks/Example 8 - Neutralize Algorithm Comparison.ipynb
+1
-1
No files found.
notebooks/Example 1 - Factor IC analysis.ipynb
View file @
5b940f54
This diff is collapsed.
Click to expand it.
notebooks/Example 10 - Quadratic Optimizer Comparison with CVXOPT.ipynb
View file @
5b940f54
...
@@ -40,7 +40,7 @@
...
@@ -40,7 +40,7 @@
"ref_date = '2018-02-08'\n",
"ref_date = '2018-02-08'\n",
"\n",
"\n",
"engine = SqlEngine(os.environ['DB_URI'])\n",
"engine = SqlEngine(os.environ['DB_URI'])\n",
"universe = Universe('
custom', ['ashare_ex']
)\n",
"universe = Universe('
ashare_ex'
)\n",
"codes = engine.fetch_codes(ref_date, universe)\n",
"codes = engine.fetch_codes(ref_date, universe)\n",
"\n",
"\n",
"risk_cov, risk_exposure = engine.fetch_risk_model(ref_date, codes)\n",
"risk_cov, risk_exposure = engine.fetch_risk_model(ref_date, codes)\n",
...
...
notebooks/Example 11 - Long Short Strategy Model.ipynb
View file @
5b940f54
...
@@ -38,7 +38,7 @@
...
@@ -38,7 +38,7 @@
"freq = '5b'\n",
"freq = '5b'\n",
"horizon = map_freq(freq)\n",
"horizon = map_freq(freq)\n",
"neutralized_risk = risk_styles + industry_styles\n",
"neutralized_risk = risk_styles + industry_styles\n",
"universe = Universe(
\"custom\", ['zz500']
)\n",
"universe = Universe(
'zz500'
)\n",
"data_source = os.environ['DB_URI']\n",
"data_source = os.environ['DB_URI']\n",
"offset = 1\n",
"offset = 1\n",
"method = 'ls'\n",
"method = 'ls'\n",
...
@@ -185,7 +185,7 @@
...
@@ -185,7 +185,7 @@
"name": "python",
"name": "python",
"nbconvert_exporter": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"pygments_lexer": "ipython3",
"version": "3.6.
3
"
"version": "3.6.
5
"
},
},
"varInspector": {
"varInspector": {
"cols": {
"cols": {
...
...
notebooks/Example 12 - Machine Learning Model Prediction.ipynb
View file @
5b940f54
This diff is collapsed.
Click to expand it.
notebooks/Example 13 - Evaluation within Industry Groups.ipynb
View file @
5b940f54
...
@@ -30,7 +30,7 @@
...
@@ -30,7 +30,7 @@
"from alphamind.api import *\n",
"from alphamind.api import *\n",
"\n",
"\n",
"factor = 'CFO2EV'\n",
"factor = 'CFO2EV'\n",
"universe = Universe('
custom', ['zz800']
)\n",
"universe = Universe('
zz800'
)\n",
"start_date = '2010-01-01'\n",
"start_date = '2010-01-01'\n",
"end_date = '2018-04-26'\n",
"end_date = '2018-04-26'\n",
"freq = '20b'\n",
"freq = '20b'\n",
...
@@ -185,7 +185,7 @@
...
@@ -185,7 +185,7 @@
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
"sample_factor3 = sample_factor3[['code', '
isOpen', '
f3', 'industry']]\n",
"sample_factor3 = sample_factor3[['code', 'f3', 'industry']]\n",
"sample_factor3.sort_values('f3', ascending=False).head(15)"
"sample_factor3.sort_values('f3', ascending=False).head(15)"
]
]
},
},
...
@@ -306,7 +306,7 @@
...
@@ -306,7 +306,7 @@
"name": "python",
"name": "python",
"nbconvert_exporter": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"pygments_lexer": "ipython3",
"version": "3.6.
3
"
"version": "3.6.
5
"
},
},
"varInspector": {
"varInspector": {
"cols": {
"cols": {
...
...
notebooks/Example 2 - Strategy Analysis.ipynb
View file @
5b940f54
...
@@ -116,12 +116,7 @@
...
@@ -116,12 +116,7 @@
"outputs": [],
"outputs": [],
"source": [
"source": [
"# Running settings\n",
"# Running settings\n",
"running_setting = RunningSetting(universe,\n",
"running_setting = RunningSetting(weights_bandwidth=weights_bandwidth,\n",
" start_date,\n",
" end_date,\n",
" freq,\n",
" benchmark=benchmark_code,\n",
" weights_bandwidth=weights_bandwidth,\n",
" rebalance_method=method,\n",
" rebalance_method=method,\n",
" bounds=bounds,\n",
" bounds=bounds,\n",
" turn_over_target=turn_over_target)"
" turn_over_target=turn_over_target)"
...
@@ -134,8 +129,16 @@
...
@@ -134,8 +129,16 @@
"outputs": [],
"outputs": [],
"source": [
"source": [
"# Strategy\n",
"# Strategy\n",
"strategy = Strategy(alpha_model, data_meta, running_setting)\n",
"strategy = Strategy(alpha_model,\n",
"ret_df, positions = strategy.run()"
" data_meta,\n",
" universe=universe,\n",
" start_date=start_date,\n",
" end_date=end_date,\n",
" freq=freq,\n",
" benchmark=benchmark_code)\n",
"\n",
"strategy.prepare_backtest_data()\n",
"ret_df, positions = strategy.run(running_setting=running_setting)"
]
]
},
},
{
{
...
...
notebooks/Example 3 - Multi Weight Gap Comparison.ipynb
View file @
5b940f54
...
@@ -42,32 +42,42 @@
...
@@ -42,32 +42,42 @@
"benchmark_code = 905\n",
"benchmark_code = 905\n",
"universe = Universe('zz800') + Universe('cyb')\n",
"universe = Universe('zz800') + Universe('cyb')\n",
"\n",
"\n",
"def create_scenario(weights_bandwidth=0.02, target_vol=0.01, method='risk_neutral'):\n",
"start_date = '2017-01-01'\n",
" start_date = '2017-01-01'\n",
"end_date = '2018-04-27'\n",
" end_date = '2018-04-27'\n",
"freq = '10b'\n",
" freq = '10b'\n",
"neutralized_risk = None\n",
" neutralized_risk = None\n",
"\n",
"\n",
"alpha_factors = {\n",
" alpha_factors = {\n",
" 'f01': CSQuantiles(LAST('EPS'), groups='sw1'),\n",
" 'f01': CSQuantiles(LAST('EPS'), groups='sw1'),\n",
" 'f02': CSQuantiles(LAST('ROE'), groups='sw1'),\n",
" 'f02': CSQuantiles(LAST('ROE'), groups='sw1'),\n",
"}\n",
" }\n",
"\n",
"\n",
"weights = dict(\n",
" weights = dict(\n",
" f01=1.,\n",
" f01=1.,\n",
" f02=1.\n",
" f02=1.\n",
" )\n",
" )\n",
"\n",
"\n",
"alpha_model = ConstLinearModel(features=alpha_factors, weights=weights)\n",
" alpha_model = ConstLinearModel(features=alpha_factors, weights=weights)\n",
"\n",
"\n",
"data_meta = DataMeta(freq=freq,\n",
" data_meta = DataMeta(freq=freq,\n",
" universe=universe,\n",
" universe=universe,\n",
" batch=1,\n",
" batch=1,\n",
" neutralized_risk=None,\n",
" neutralized_risk=None,\n",
" pre_process=None,\n",
" pre_process=None,\n",
" post_process=None,\n",
" post_process=None,\n",
" data_source=os.environ['DB_URI'])\n",
" data_source=os.environ['DB_URI'])\n",
"\n",
"strategy = Strategy(alpha_model,\n",
" data_meta,\n",
" universe=universe,\n",
" start_date=start_date,\n",
" end_date=end_date,\n",
" freq=freq,\n",
" benchmark=benchmark_code)\n",
"\n",
"strategy.prepare_backtest_data()\n",
"\n",
"\n",
"def create_scenario(weights_bandwidth=0.02, target_vol=0.01, method='risk_neutral'):\n",
" industry_names = industry_list('sw', 1)\n",
" industry_names = industry_list('sw', 1)\n",
" constraint_risk = ['SIZE', 'SIZENL', 'BETA']\n",
" constraint_risk = ['SIZE', 'SIZENL', 'BETA']\n",
" total_risk_names = constraint_risk + ['benchmark', 'total']\n",
" total_risk_names = constraint_risk + ['benchmark', 'total']\n",
...
@@ -93,19 +103,13 @@
...
@@ -93,19 +103,13 @@
" u_val.append(0.0)\n",
" u_val.append(0.0)\n",
"\n",
"\n",
" bounds = create_box_bounds(total_risk_names, b_type, l_val, u_val)\n",
" bounds = create_box_bounds(total_risk_names, b_type, l_val, u_val)\n",
" running_setting = RunningSetting(universe,\n",
" running_setting = RunningSetting(weights_bandwidth=weights_bandwidth,\n",
" start_date,\n",
" end_date,\n",
" freq,\n",
" benchmark=benchmark_code,\n",
" weights_bandwidth=weights_bandwidth,\n",
" rebalance_method=method,\n",
" rebalance_method=method,\n",
" bounds=bounds,\n",
" bounds=bounds,\n",
" target_vol=target_vol,\n",
" target_vol=target_vol,\n",
" turn_over_target=0.4)\n",
" turn_over_target=0.4)\n",
"\n",
"\n",
" strategy = Strategy(alpha_model, data_meta, running_setting)\n",
" ret_df, positions = strategy.run(running_setting)\n",
" ret_df, positions = strategy.run()\n",
" return ret_df"
" return ret_df"
]
]
},
},
...
@@ -150,6 +154,7 @@
...
@@ -150,6 +154,7 @@
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
"%%time\n",
"weight_gaps = [0.005, 0.010, 0.015, 0.020]\n",
"weight_gaps = [0.005, 0.010, 0.015, 0.020]\n",
"\n",
"\n",
"with pd.ExcelWriter(f'zz800_cyb_{benchmark_code}_gap.xlsx', engine='xlsxwriter') as writer:\n",
"with pd.ExcelWriter(f'zz800_cyb_{benchmark_code}_gap.xlsx', engine='xlsxwriter') as writer:\n",
...
@@ -166,6 +171,7 @@
...
@@ -166,6 +171,7 @@
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
"%%time\n",
"target_vols = [0.015, 0.030, 0.045, 0.060]\n",
"target_vols = [0.015, 0.030, 0.045, 0.060]\n",
"\n",
"\n",
"with pd.ExcelWriter(f'zz800_cyb_{benchmark_code}_tv.xlsx', engine='xlsxwriter') as writer:\n",
"with pd.ExcelWriter(f'zz800_cyb_{benchmark_code}_tv.xlsx', engine='xlsxwriter') as writer:\n",
...
@@ -190,6 +196,18 @@
...
@@ -190,6 +196,18 @@
"language": "python",
"language": "python",
"name": "python3"
"name": "python3"
},
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
},
"varInspector": {
"varInspector": {
"cols": {
"cols": {
"lenName": 16,
"lenName": 16,
...
...
notebooks/Example 6 - Target Volatility Builder.ipynb
View file @
5b940f54
...
@@ -224,12 +224,7 @@
...
@@ -224,12 +224,7 @@
"\"\"\"\n",
"\"\"\"\n",
"Running Settings\n",
"Running Settings\n",
"\"\"\"\n",
"\"\"\"\n",
"running_setting = RunningSetting(universe,\n",
"running_setting = RunningSetting(weights_bandwidth=weights_bandwidth,\n",
" start_date,\n",
" end_date,\n",
" freq,\n",
" benchmark=benchmark_code,\n",
" weights_bandwidth=weights_bandwidth,\n",
" rebalance_method='tv',\n",
" rebalance_method='tv',\n",
" bounds=bounds,\n",
" bounds=bounds,\n",
" target_vol=target_vol)"
" target_vol=target_vol)"
...
@@ -244,8 +239,15 @@
...
@@ -244,8 +239,15 @@
"\"\"\"\n",
"\"\"\"\n",
"Strategy run\n",
"Strategy run\n",
"\"\"\"\n",
"\"\"\"\n",
"strategy = Strategy(alpha_model, data_meta, running_setting)\n",
"strategy = Strategy(alpha_model,\n",
"ret_df, positions = strategy.run()"
" data_meta,\n",
" universe=universe,\n",
" start_date=start_date,\n",
" end_date=end_date,\n",
" freq=freq,\n",
" benchmark=benchmark_code)\n",
"strategy.prepare_backtest_data()\n",
"ret_df, positions = strategy.run(running_setting)"
]
]
},
},
{
{
...
...
notebooks/Example 7 - Portfolio Optimizer Performance.ipynb
View file @
5b940f54
...
@@ -63,7 +63,7 @@
...
@@ -63,7 +63,7 @@
"data_source = os.environ['DB_URI']\n",
"data_source = os.environ['DB_URI']\n",
"engine = SqlEngine(data_source)\n",
"engine = SqlEngine(data_source)\n",
"\n",
"\n",
"universes = [Universe(
'custom', [u_name]
) for u_name in u_names]\n",
"universes = [Universe(
u_name
) for u_name in u_names]\n",
"codes_set = [engine.fetch_codes(ref_date, universe=universe) for universe in universes]\n",
"codes_set = [engine.fetch_codes(ref_date, universe=universe) for universe in universes]\n",
"data_set = [engine.fetch_data(ref_date, factor, codes, benchmark=b_code, risk_model=risk_model) for codes, b_code in zip(codes_set, b_codes)]"
"data_set = [engine.fetch_data(ref_date, factor, codes, benchmark=b_code, risk_model=risk_model) for codes, b_code in zip(codes_set, b_codes)]"
]
]
...
...
notebooks/Example 8 - Neutralize Algorithm Comparison.ipynb
View file @
5b940f54
...
@@ -46,7 +46,7 @@
...
@@ -46,7 +46,7 @@
"factor = 'EPS'\n",
"factor = 'EPS'\n",
"\n",
"\n",
"engine = SqlEngine(os.environ['DB_URI'])\n",
"engine = SqlEngine(os.environ['DB_URI'])\n",
"universe = Universe('
custom', ['zz800']
)"
"universe = Universe('
zz800'
)"
]
]
},
},
{
{
...
...
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