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
636e498d
Commit
636e498d
authored
Mar 18, 2018
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update example
parent
09dc52e9
Changes
2
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
524 additions
and
34 deletions
+524
-34
Example 6 - Target Volatility Builder.ipynb
notebooks/Example 6 - Target Volatility Builder.ipynb
+82
-34
Example 7 - Portfolio Optimizer Performance.ipynb
notebooks/Example 7 - Portfolio Optimizer Performance.ipynb
+442
-0
No files found.
notebooks/Example 6 - Target Volatility Builder.ipynb
View file @
636e498d
...
...
@@ -3,7 +3,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%matplotlib inline\n",
...
...
@@ -28,10 +30,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"ref_date = '2018-0
2
-08'\n",
"ref_date = '2018-0
1
-08'\n",
"engine = SqlEngine('postgres+psycopg2://postgres:we083826@192.168.0.102/alpha')\n",
"universe = Universe('custom', ['zz800'])"
]
...
...
@@ -39,24 +43,28 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"codes = engine.fetch_codes(ref_date, universe)\n",
"total_data = engine.fetch_data(ref_date, 'ep_q', codes, 90
5, industry='sw_adj
')\n",
"total_data = engine.fetch_data(ref_date, 'ep_q', codes, 90
6, industry='sw_adj', risk_model='day
')\n",
"all_styles = risk_styles + industry_styles + ['COUNTRY']"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"risk_cov = total_data['risk_cov'][all_styles].values\n",
"factor = total_data['factor']\n",
"risk_exposure = factor[all_styles].values\n",
"special_risk = factor['
s
_srisk'].values"
"special_risk = factor['
d
_srisk'].values"
]
},
{
...
...
@@ -84,7 +92,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"er = factor['ep_q'].values\n",
...
...
@@ -107,7 +117,7 @@
"source": [
"# check the result\n",
"print(f\"total weight is {p_weight.sum(): .4f}\")\n",
"print(f\"portfolio activate weight forecasting vol is {np.sqrt((p_weight - bm) @ sec_cov @ (p_weight - bm)):.
2
f}\")\n",
"print(f\"portfolio activate weight forecasting vol is {np.sqrt((p_weight - bm) @ sec_cov @ (p_weight - bm)):.
4
f}\")\n",
"print(f\"portfolio expected return is {p_weight @ er:.4f} comparing with benchmark er {bm @ er:.4f}\")"
]
},
...
...
@@ -122,29 +132,37 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"\"\"\"\n",
"Back test parameter settings\n",
"\"\"\"\n",
"\n",
"start_date = '201
1
-01-01'\n",
"end_date = '2018-0
2-14
'\n",
"start_date = '201
0
-01-01'\n",
"end_date = '2018-0
3-08
'\n",
"\n",
"freq = '10b'\n",
"industry_lower = 1.0\n",
"industry_upper = 1.0\n",
"neutralized_risk = ['SIZE'] + industry_styles\n",
"neutralized_risk = industry_styles\n",
"industry_name = 'sw_adj'\n",
"industry_level = 1\n",
"risk_model = 'short'\n",
"batch = 0\n",
"horizon = map_freq(freq)\n",
"universe = Universe(\"custom\", ['zz
5
00'])\n",
"universe = Universe(\"custom\", ['zz
8
00'])\n",
"data_source = 'postgres+psycopg2://postgres:we083826@192.168.0.102/alpha'\n",
"benchmark_code = 90
5
\n",
"benchmark_code = 90
6
\n",
"target_vol = 0.05\n",
"\n",
"if risk_model == 'day':\n",
" risk_model_name = 'd_srisk'\n",
"elif risk_model == 'short':\n",
" risk_model_name = 's_srisk'\n",
"else:\n",
" risk_model_name = 'l_srisk'\n",
"\n",
"executor = NaiveExecutor()\n",
"ref_dates = makeSchedule(start_date, end_date, freq, 'china.sse')\n",
"engine = SqlEngine(data_source)"
...
...
@@ -153,14 +171,16 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"\"\"\"\n",
"Factor Model\n",
"\"\"\"\n",
"\n",
"alpha_factors = {'f01':
LAST('ep_q'
)}\n",
"alpha_factors = {'f01':
CSRank(LAST('ep_q')
)}\n",
"\n",
"weights = dict(f01=1.)\n",
"\n",
...
...
@@ -203,7 +223,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"\"\"\"\n",
...
...
@@ -225,14 +247,16 @@
"bounds = create_box_bounds(total_risk_names, b_type, l_val, u_val)\n",
"industry_total = engine.fetch_industry_matrix_range(universe, dates=ref_dates, category=industry_name, level=industry_level)\n",
"benchmark_total = engine.fetch_benchmark_range(dates=ref_dates, benchmark=benchmark_code)\n",
"risk_cov_total, risk_exposure_total = engine.fetch_risk_model_range(universe, dates=ref_dates)\n",
"risk_cov_total, risk_exposure_total = engine.fetch_risk_model_range(universe, dates=ref_dates
, risk_model=risk_model
)\n",
"index_return = engine.fetch_dx_return_index_range(benchmark_code, start_date, end_date, horizon=horizon, offset=1).set_index('trade_date')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# rebalance\n",
...
...
@@ -260,7 +284,7 @@
" \n",
" risk_exposure = total_data[all_styles].values\n",
" risk_cov = risk_cov[all_styles].values\n",
" special_risk = total_data[
's_srisk'
].values\n",
" special_risk = total_data[
risk_model_name
].values\n",
" sec_cov = risk_exposure @ risk_cov @ risk_exposure.T / 10000 + np.diag(special_risk ** 2) / 10000\n",
"\n",
" benchmark_w = total_data.weight.values\n",
...
...
@@ -272,7 +296,7 @@
" constraints = LinearConstraints(bounds, total_risk_exp, benchmark_w)\n",
" \n",
" lbound = np.zeros(len(total_data))\n",
" ubound =
0.05 + benchmark_w
\n",
" ubound =
np.ones(len(total_data)) * 0.1
\n",
"\n",
" er = predicts[i].loc[codes].values.flatten()\n",
" cons_mat = np.ones((len(er), 1))\n",
...
...
@@ -280,7 +304,7 @@
" \n",
" try:\n",
" target_pos, _ = er_portfolio_analysis(er,\n",
"
industry_matrix
.industry_name.values,\n",
"
total_data
.industry_name.values,\n",
" None,\n",
" constraints,\n",
" False,\n",
...
...
@@ -348,14 +372,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"metadata": {
"collapsed": true
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
...
...
@@ -376,7 +395,36 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.4"
"version": "3.6.3"
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,
...
...
notebooks/Example 7 - Portfolio Optimizer Performance.ipynb
0 → 100644
View file @
636e498d
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