Commit 2843e07c authored by Dr.李's avatar Dr.李

update example

parent 40d4840c
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
"from cvxopt import *\n", "from cvxopt import *\n",
"from alphamind.data.neutralize import neutralize\n", "from alphamind.data.neutralize import neutralize\n",
"\n", "\n",
"engine = sa.create_engine('mysql+pymysql://sa:We051253524522@rm-bp1psdz5615icqc0yo.mysql.rds.aliyuncs.com/uqer?charset=utf8')" "engine = sa.create_engine('mysql+mysqldb://sa:We051253524522@rm-bp1psdz5615icqc0yo.mysql.rds.aliyuncs.com/uqer?charset=utf8')"
] ]
}, },
{ {
...@@ -30,8 +30,7 @@ ...@@ -30,8 +30,7 @@
"factor_list = [\"DebtEquityRatio\",\"EBITToTOR\"]\n", "factor_list = [\"DebtEquityRatio\",\"EBITToTOR\"]\n",
"ref_date = '2017-05-23'\n", "ref_date = '2017-05-23'\n",
"weights = np.array([0.3,0.7])\n", "weights = np.array([0.3,0.7])\n",
"risk_penlty = 0.5\n", "risk_penlty = 0.5"
"n = len(codes_list)"
] ]
}, },
{ {
...@@ -61,7 +60,15 @@ ...@@ -61,7 +60,15 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"%%time\n", "%%time\n",
"\n",
"risk_exposure_data = pd.read_sql(\"select * from risk_exposure where Date = '{0}'\".format(ref_date), engine).sort_values('Code')\n",
"risk_exposure_data.drop(['Date', 'exchangeCD', 'secShortName'], axis=1, inplace=True)\n",
"risk_exposure_data.dropna(inplace=True)\n",
"codes_list = risk_exposure_data.Code.astype(str).tolist()\n",
"codes = ','.join(codes_list)\n",
"\n",
"factor_data = pd.read_sql(\"select Code, {1} from factors where Date = '{0}' and Code in ({2})\".format(ref_date, factors, codes), engine).sort_values('Code')\n", "factor_data = pd.read_sql(\"select Code, {1} from factors where Date = '{0}' and Code in ({2})\".format(ref_date, factors, codes), engine).sort_values('Code')\n",
"\n",
"risk_exposure_data = pd.read_sql(\"select * from risk_exposure where Date = '{0}' and Code in ({1})\".format(ref_date, codes), engine).sort_values('Code')\n", "risk_exposure_data = pd.read_sql(\"select * from risk_exposure where Date = '{0}' and Code in ({1})\".format(ref_date, codes), engine).sort_values('Code')\n",
"risk_exposure_data.drop(['Date', 'exchangeCD', 'secShortName'], axis=1, inplace=True)\n", "risk_exposure_data.drop(['Date', 'exchangeCD', 'secShortName'], axis=1, inplace=True)\n",
"risk_cov_data = pd.read_sql(\"select * from risk_cov_day where Date = '{0}'\".format(ref_date), engine).sort_values('FactorID')\n", "risk_cov_data = pd.read_sql(\"select * from risk_cov_day where Date = '{0}'\".format(ref_date), engine).sort_values('FactorID')\n",
...@@ -70,6 +77,17 @@ ...@@ -70,6 +77,17 @@
"specific_risk = pd.read_sql(\"select Code, SRISK from specific_risk_day where Date = '{0}' and Code in ({1})\".format(ref_date, codes), engine).sort_values('Code')" "specific_risk = pd.read_sql(\"select Code, SRISK from specific_risk_day where Date = '{0}' and Code in ({1})\".format(ref_date, codes), engine).sort_values('Code')"
] ]
}, },
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"n = len(codes_list)"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
...@@ -93,6 +111,15 @@ ...@@ -93,6 +111,15 @@
"signal = ne_factor_values @ weights" "signal = ne_factor_values @ weights"
] ]
}, },
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"risk_exposure_values.shape"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
...@@ -206,13 +233,44 @@ ...@@ -206,13 +233,44 @@
"A = matrix(A)\n", "A = matrix(A)\n",
"b = matrix(b)\n", "b = matrix(b)\n",
"\n", "\n",
"sol = solvers.qp(P, q, G, h, A, b, solver=None)" "sol = solvers.qp(P, q, G, h, A, b)"
] ]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"sec_cov_values.tofile('sec_cov_values.dat')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"signal.tofile('signal.dat')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
} }
], ],
"metadata": { "metadata": {
"kernelspec": { "kernelspec": {
"display_name": "Python 3", "display_name": "Python [default]",
"language": "python", "language": "python",
"name": "python3" "name": "python3"
}, },
......
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np\n",
"from cvxopt import *"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"sec_cov_values = np.fromfile('sec_cov_values.dat')\n",
"signal = np.fromfile('signal.dat')\n",
"sec_cov_values = sec_cov_values.reshape((len(signal), -1))\n",
"n = len(sec_cov_values)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"P = matrix(sec_cov_values)\n",
"q = -matrix(signal)\n",
"\n",
"G = np.zeros((2*n, n))\n",
"h = np.zeros(2*n)\n",
"for i in range(n):\n",
" G[i, i] = 1.\n",
" h[i] = 0.01\n",
" G[i+n, i] = -1.\n",
" h[i+n] = 0.\n",
" \n",
"G = matrix(G)\n",
"h = matrix(h)\n",
" \n",
"A = np.ones((1, n))\n",
"b = np.ones(1)\n",
"\n",
"A = matrix(A)\n",
"b = matrix(b)\n",
"\n",
"sol = solvers.qp(P, q, G, h, A, b)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:py2]",
"language": "python",
"name": "conda-env-py2-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
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