Commit 636e498d authored by Dr.李's avatar Dr.李

update example

parent 09dc52e9
......@@ -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-02-08'\n",
"ref_date = '2018-01-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, 905, industry='sw_adj')\n",
"total_data = engine.fetch_data(ref_date, 'ep_q', codes, 906, 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)):.2f}\")\n",
"print(f\"portfolio activate weight forecasting vol is {np.sqrt((p_weight - bm) @ sec_cov @ (p_weight - bm)):.4f}\")\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 = '2011-01-01'\n",
"end_date = '2018-02-14'\n",
"start_date = '2010-01-01'\n",
"end_date = '2018-03-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\", ['zz500'])\n",
"universe = Universe(\"custom\", ['zz800'])\n",
"data_source = 'postgres+psycopg2://postgres:we083826@192.168.0.102/alpha'\n",
"benchmark_code = 905\n",
"benchmark_code = 906\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,
......
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