Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
fof
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.李
fof
Commits
e41f62d2
Commit
e41f62d2
authored
Feb 03, 2022
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update api
parent
2387388e
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
766 additions
and
643 deletions
+766
-643
010 style attribution.ipynb
attribution/010 style attribution.ipynb
+494
-542
020 brison attribution.ipynb
attribution/020 brison attribution.ipynb
+216
-74
utility.py
fof/utility.py
+56
-27
No files found.
attribution/010 style attribution.ipynb
View file @
e41f62d2
...
...
@@ -15,391 +15,27 @@
{
"cell_type": "code",
"execution_count": 2,
"id": "
b547e094-4e2a-4ce8-b6c3-16b84f7d7d43
",
"id": "
73399fb6-ea5c-48bb-872d-a35b555c1b00
",
"metadata": {},
"outputs": [],
"source": [
"
df = fd_skdetail(\"1030000006\", trade_dt=\"20210630\")
"
"
trade_dt = \"20210630\"
"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "5f3f9f46-6b93-4e1d-b941-3df4d0974662",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3384590848.7"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"total_value = df.HOLDMKTCAP.sum()\n",
"total_value"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "04f7ce63-8c83-4526-8893-2232eef6e097",
"metadata": {},
"outputs": [],
"source": [
"df[\"pct\"] = df.HOLDMKTCAP / total_value"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "37d007b9-c688-4f3c-8050-9f2c4bdff305",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>SECURITYID</th>\n",
" <th>SKCODE</th>\n",
" <th>SKNAME</th>\n",
" <th>HOLDMKTCAP</th>\n",
" <th>HOLDAMT</th>\n",
" <th>NAVRTO</th>\n",
" <th>pct</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1030000006</td>\n",
" <td>2010000045</td>\n",
" <td>海信视像</td>\n",
" <td>71468410.00</td>\n",
" <td>4228900.0</td>\n",
" <td>1.61</td>\n",
" <td>0.021116</td>\n",
" </tr>\n",
" <tr>\n",
" <th>85</th>\n",
" <td>1030000006</td>\n",
" <td>2010034181</td>\n",
" <td>宁波方正</td>\n",
" <td>4285.32</td>\n",
" <td>201.0</td>\n",
" <td>NaN</td>\n",
" <td>0.000001</td>\n",
" </tr>\n",
" <tr>\n",
" <th>84</th>\n",
" <td>1030000006</td>\n",
" <td>2010034138</td>\n",
" <td>玉马遮阳</td>\n",
" <td>7908.90</td>\n",
" <td>410.0</td>\n",
" <td>NaN</td>\n",
" <td>0.000002</td>\n",
" </tr>\n",
" <tr>\n",
" <th>83</th>\n",
" <td>1030000006</td>\n",
" <td>2010034132</td>\n",
" <td>泰福泵业</td>\n",
" <td>4064.00</td>\n",
" <td>200.0</td>\n",
" <td>NaN</td>\n",
" <td>0.000001</td>\n",
" </tr>\n",
" <tr>\n",
" <th>82</th>\n",
" <td>1030000006</td>\n",
" <td>2010034131</td>\n",
" <td>百川畅银</td>\n",
" <td>13285.02</td>\n",
" <td>329.0</td>\n",
" <td>NaN</td>\n",
" <td>0.000004</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>33</th>\n",
" <td>1030000006</td>\n",
" <td>2010029637</td>\n",
" <td>科沃斯</td>\n",
" <td>85084787.84</td>\n",
" <td>373048.0</td>\n",
" <td>1.92</td>\n",
" <td>0.025139</td>\n",
" </tr>\n",
" <tr>\n",
" <th>32</th>\n",
" <td>1030000006</td>\n",
" <td>2010028450</td>\n",
" <td>药石科技</td>\n",
" <td>83560757.67</td>\n",
" <td>525837.0</td>\n",
" <td>1.89</td>\n",
" <td>0.024689</td>\n",
" </tr>\n",
" <tr>\n",
" <th>31</th>\n",
" <td>1030000006</td>\n",
" <td>2010027040</td>\n",
" <td>英科医疗</td>\n",
" <td>10458240.00</td>\n",
" <td>83800.0</td>\n",
" <td>0.24</td>\n",
" <td>0.003090</td>\n",
" </tr>\n",
" <tr>\n",
" <th>43</th>\n",
" <td>1030000006</td>\n",
" <td>2010033620</td>\n",
" <td>江天化学</td>\n",
" <td>6531.70</td>\n",
" <td>217.0</td>\n",
" <td>NaN</td>\n",
" <td>0.000002</td>\n",
" </tr>\n",
" <tr>\n",
" <th>117</th>\n",
" <td>1030000006</td>\n",
" <td>2010034373</td>\n",
" <td>英科再生</td>\n",
" <td>54219.24</td>\n",
" <td>2469.0</td>\n",
" <td>NaN</td>\n",
" <td>0.000016</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>118 rows × 7 columns</p>\n",
"</div>"
],
"text/plain": [
" SECURITYID SKCODE SKNAME HOLDMKTCAP HOLDAMT NAVRTO pct\n",
"0 1030000006 2010000045 海信视像 71468410.00 4228900.0 1.61 0.021116\n",
"85 1030000006 2010034181 宁波方正 4285.32 201.0 NaN 0.000001\n",
"84 1030000006 2010034138 玉马遮阳 7908.90 410.0 NaN 0.000002\n",
"83 1030000006 2010034132 泰福泵业 4064.00 200.0 NaN 0.000001\n",
"82 1030000006 2010034131 百川畅银 13285.02 329.0 NaN 0.000004\n",
".. ... ... ... ... ... ... ...\n",
"33 1030000006 2010029637 科沃斯 85084787.84 373048.0 1.92 0.025139\n",
"32 1030000006 2010028450 药石科技 83560757.67 525837.0 1.89 0.024689\n",
"31 1030000006 2010027040 英科医疗 10458240.00 83800.0 0.24 0.003090\n",
"43 1030000006 2010033620 江天化学 6531.70 217.0 NaN 0.000002\n",
"117 1030000006 2010034373 英科再生 54219.24 2469.0 NaN 0.000016\n",
"\n",
"[118 rows x 7 columns]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "15ce5cca-312b-4ef5-8178-580ec9c440da",
"id": "b547e094-4e2a-4ce8-b6c3-16b84f7d7d43",
"metadata": {},
"outputs": [],
"source": [
"ddf = risk_exposure(df.SKCODE, \"20210630\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "a0e18d45-c6c7-45e5-bc83-9ae80575125e",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>trade_date</th>\n",
" <th>security_code</th>\n",
" <th>secShortName</th>\n",
" <th>symbol</th>\n",
" <th>SRISK</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>2021-06-30</td>\n",
" <td>2010000045</td>\n",
" <td>海信视像</td>\n",
" <td>600060.XSHG</td>\n",
" <td>43.597</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2021-06-30</td>\n",
" <td>2010000121</td>\n",
" <td>中青旅</td>\n",
" <td>600138.XSHG</td>\n",
" <td>20.325</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>2021-06-30</td>\n",
" <td>2010000171</td>\n",
" <td>复星医药</td>\n",
" <td>600196.XSHG</td>\n",
" <td>49.067</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>2021-06-30</td>\n",
" <td>2010000377</td>\n",
" <td>片仔癀</td>\n",
" <td>600436.XSHG</td>\n",
" <td>36.564</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2021-06-30</td>\n",
" <td>2010000438</td>\n",
" <td>贵州茅台</td>\n",
" <td>600519.XSHG</td>\n",
" <td>24.513</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>104</th>\n",
" <td>2021-06-30</td>\n",
" <td>2010034304</td>\n",
" <td>C利和兴</td>\n",
" <td>301013.XSHE</td>\n",
" <td>40.330</td>\n",
" </tr>\n",
" <tr>\n",
" <th>105</th>\n",
" <td>2021-06-30</td>\n",
" <td>2010034305</td>\n",
" <td>C嘉益</td>\n",
" <td>301004.XSHE</td>\n",
" <td>42.886</td>\n",
" </tr>\n",
" <tr>\n",
" <th>106</th>\n",
" <td>2021-06-30</td>\n",
" <td>2010034317</td>\n",
" <td>税友股份</td>\n",
" <td>603171.XSHG</td>\n",
" <td>40.035</td>\n",
" </tr>\n",
" <tr>\n",
" <th>107</th>\n",
" <td>2021-06-30</td>\n",
" <td>2010034318</td>\n",
" <td>杭州热电</td>\n",
" <td>605011.XSHG</td>\n",
" <td>35.420</td>\n",
" </tr>\n",
" <tr>\n",
" <th>108</th>\n",
" <td>2021-06-30</td>\n",
" <td>2010034324</td>\n",
" <td>C百洋</td>\n",
" <td>301015.XSHE</td>\n",
" <td>39.727</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>109 rows × 5 columns</p>\n",
"</div>"
],
"text/plain": [
" trade_date security_code secShortName symbol SRISK\n",
"0 2021-06-30 2010000045 海信视像 600060.XSHG 43.597\n",
"1 2021-06-30 2010000121 中青旅 600138.XSHG 20.325\n",
"2 2021-06-30 2010000171 复星医药 600196.XSHG 49.067\n",
"3 2021-06-30 2010000377 片仔癀 600436.XSHG 36.564\n",
"4 2021-06-30 2010000438 贵州茅台 600519.XSHG 24.513\n",
".. ... ... ... ... ...\n",
"104 2021-06-30 2010034304 C利和兴 301013.XSHE 40.330\n",
"105 2021-06-30 2010034305 C嘉益 301004.XSHE 42.886\n",
"106 2021-06-30 2010034317 税友股份 603171.XSHG 40.035\n",
"107 2021-06-30 2010034318 杭州热电 605011.XSHG 35.420\n",
"108 2021-06-30 2010034324 C百洋 301015.XSHE 39.727\n",
"\n",
"[109 rows x 5 columns]"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"special_risk(df.SKCODE, \"20210630\")"
"portfolio = fd_skdetail(\"1030000006\", trade_dt=trade_dt)"
]
},
{
"cell_type": "code",
"execution_count":
7
,
"id": "
90957b2b-fc4c-4e7f-b50b-b2189573f1d9
",
"execution_count":
4
,
"id": "
470f668b-3547-4b49-8597-9d001ae8f9a7
",
"metadata": {},
"outputs": [
{
...
...
@@ -567,211 +203,527 @@
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>104</th>\n",
" <td>2021-06-30</td>\n",
" <td>2010034304</td>\n",
" <td>-1.512</td>\n",
" <td>-1.342</td>\n",
" <td>-2.006</td>\n",
" <td>-0.971</td>\n",
" <td>0.109</td>\n",
" <td>0.762</td>\n",
" <td>-1.077</td>\n",
" <td>-0.979</td>\n",
" <td>...</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tbody>\n",
"</table>\n",
"<p>5 rows × 40 columns</p>\n",
"</div>"
],
"text/plain": [
" trade_date security_code BETA MOMENTUM SIZE EARNYILD RESVOL GROWTH \\\n",
"0 2021-06-30 2010000045 -0.017 -0.340 -0.841 -0.234 -0.041 -0.696 \n",
"1 2021-06-30 2010000121 -1.274 -0.758 -1.765 -0.103 -0.753 -0.247 \n",
"2 2021-06-30 2010000171 0.465 1.300 0.996 -0.862 1.511 -0.020 \n",
"3 2021-06-30 2010000377 2.436 1.730 1.325 -1.262 -0.079 0.305 \n",
"4 2021-06-30 2010000438 1.073 0.593 2.359 -0.937 -0.886 0.100 \n",
"\n",
" BTOP LEVERAGE ... Utilities Telecom AgriForest CHEM Media \\\n",
"0 0.755 -0.586 ... 0 0 0 0 0 \n",
"1 1.087 0.589 ... 0 0 0 0 0 \n",
"2 -0.778 -0.133 ... 0 0 0 0 0 \n",
"3 -1.297 -1.375 ... 0 0 0 0 0 \n",
"4 -1.187 -1.485 ... 0 0 0 0 0 \n",
"\n",
" IronSteel NonBankFinan ELECEQP AERODEF Conglomerates \n",
"0 0 0 0 0 0 \n",
"1 0 0 0 0 0 \n",
"2 0 0 0 0 0 \n",
"3 0 0 0 0 0 \n",
"4 0 0 0 0 0 \n",
"\n",
"[5 rows x 40 columns]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"risk_exp = risk_exposure(portfolio.SKCODE, trade_dt, trade_dt)\n",
"risk_exp.head()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "c75c33b2-c69e-412c-9151-8f6671c00cec",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>trade_date</th>\n",
" <th>BETA</th>\n",
" <th>MOMENTUM</th>\n",
" <th>SIZE</th>\n",
" <th>EARNYILD</th>\n",
" <th>RESVOL</th>\n",
" <th>GROWTH</th>\n",
" <th>BTOP</th>\n",
" <th>LEVERAGE</th>\n",
" <th>LIQUIDTY</th>\n",
" <th>...</th>\n",
" <th>Utilities</th>\n",
" <th>Telecom</th>\n",
" <th>AgriForest</th>\n",
" <th>CHEM</th>\n",
" <th>Media</th>\n",
" <th>IronSteel</th>\n",
" <th>NonBankFinan</th>\n",
" <th>ELECEQP</th>\n",
" <th>AERODEF</th>\n",
" <th>Conglomerates</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>
105
</th>\n",
" <th>
0
</th>\n",
" <td>2021-06-30</td>\n",
" <td>2010034305</td>\n",
" <td>-1.952</td>\n",
" <td>-1.374</td>\n",
" <td>-2.572</td>\n",
" <td>-0.499</td>\n",
" <td>0.242</td>\n",
" <td>-1.154</td>\n",
" <td>-0.990</td>\n",
" <td>-1.394</td>\n",
" <td>...</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0.00108</td>\n",
" <td>0.001188</td>\n",
" <td>-0.000703</td>\n",
" <td>-0.002</td>\n",
" <td>-0.002568</td>\n",
" <td>0.000804</td>\n",
" <td>0.000649</td>\n",
" <td>-0.000616</td>\n",
" <td>0.00265</td>\n",
" <td>...</td>\n",
" <td>-0.006972</td>\n",
" <td>0.006932</td>\n",
" <td>-0.012282</td>\n",
" <td>-0.001695</td>\n",
" <td>-0.006636</td>\n",
" <td>0.00891</td>\n",
" <td>0.000348</td>\n",
" <td>0.002113</td>\n",
" <td>0.002766</td>\n",
" <td>-0.002185</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>1 rows × 39 columns</p>\n",
"</div>"
],
"text/plain": [
" trade_date BETA MOMENTUM SIZE EARNYILD RESVOL GROWTH \\\n",
"0 2021-06-30 0.00108 0.001188 -0.000703 -0.002 -0.002568 0.000804 \n",
"\n",
" BTOP LEVERAGE LIQUIDTY ... Utilities Telecom AgriForest \\\n",
"0 0.000649 -0.000616 0.00265 ... -0.006972 0.006932 -0.012282 \n",
"\n",
" CHEM Media IronSteel NonBankFinan ELECEQP AERODEF \\\n",
"0 -0.001695 -0.006636 0.00891 0.000348 0.002113 0.002766 \n",
"\n",
" Conglomerates \n",
"0 -0.002185 \n",
"\n",
"[1 rows x 39 columns]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"risk_rtn = risk_return(trade_dt, trade_dt)\n",
"risk_rtn"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "e06d5836-1e39-47e7-9e1e-3ba77433aa05",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>trade_date</th>\n",
" <th>security_code</th>\n",
" <th>secShortName</th>\n",
" <th>symbol</th>\n",
" <th>spret</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
"Empty DataFrame\n",
"Columns: [trade_date, security_code, secShortName, symbol, spret]\n",
"Index: []"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"spec_rtn = specific_return(portfolio.SKCODE, trade_dt, trade_dt)\n",
"spec_rtn.head()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "48d5bd0f-d33b-4b5e-bc91-b622cba7c0be",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>trade_date</th>\n",
" <th>FactorID</th>\n",
" <th>BETA</th>\n",
" <th>MOMENTUM</th>\n",
" <th>SIZE</th>\n",
" <th>EARNYILD</th>\n",
" <th>RESVOL</th>\n",
" <th>GROWTH</th>\n",
" <th>BTOP</th>\n",
" <th>LEVERAGE</th>\n",
" <th>...</th>\n",
" <th>Utilities</th>\n",
" <th>Telecom</th>\n",
" <th>AgriForest</th>\n",
" <th>CHEM</th>\n",
" <th>Media</th>\n",
" <th>IronSteel</th>\n",
" <th>NonBankFinan</th>\n",
" <th>ELECEQP</th>\n",
" <th>AERODEF</th>\n",
" <th>Conglomerates</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>
106
</th>\n",
" <th>
0
</th>\n",
" <td>2021-06-30</td>\n",
" <td>2010034317</td>\n",
" <td>-1.587</td>\n",
" <td>-1.935</td>\n",
" <td>-1.743</td>\n",
" <td>-0.416</td>\n",
" <td>0.616</td>\n",
" <td>-0.501</td>\n",
" <td>-0.691</td>\n",
" <td>-1.083</td>\n",
" <td>...</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>17.41000</td>\n",
" <td>-0.36765</td>\n",
" <td>-1.245280</td>\n",
" <td>-1.010080</td>\n",
" <td>4.308910</td>\n",
" <td>0.573464</td>\n",
" <td>0.578040</td>\n",
" <td>-0.124983</td>\n",
" <td>...</td>\n",
" <td>-3.179720</td>\n",
" <td>4.888220</td>\n",
" <td>-8.581790</td>\n",
" <td>-0.200272</td>\n",
" <td>-3.514560</td>\n",
" <td>-17.73480</td>\n",
" <td>5.79121</td>\n",
" <td>6.31339</td>\n",
" <td>7.05734</td>\n",
" <td>-5.14934</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1
07
</th>\n",
" <th>1</th>\n",
" <td>2021-06-30</td>\n",
" <td>2010034318</td>\n",
" <td>-1.535</td>\n",
" <td>-1.314</td>\n",
" <td>-2.423</td>\n",
" <td>0.760</td>\n",
" <td>0.782</td>\n",
" <td>-0.533</td>\n",
" <td>-0.051</td>\n",
" <td>0.265</td>\n",
" <td>...</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>-0.36765</td>\n",
" <td>26.03980</td>\n",
" <td>11.082400</td>\n",
" <td>-2.756620</td>\n",
" <td>-6.939070</td>\n",
" <td>5.527240</td>\n",
" <td>-4.961540</td>\n",
" <td>-3.526840</td>\n",
" <td>...</td>\n",
" <td>6.439720</td>\n",
" <td>-14.841600</td>\n",
" <td>0.362207</td>\n",
" <td>-1.501860</td>\n",
" <td>-4.812920</td>\n",
" <td>-2.10750</td>\n",
" <td>-20.00980</td>\n",
" <td>10.43610</td>\n",
" <td>-6.98387</td>\n",
" <td>2.60997</td>\n",
" </tr>\n",
" <tr>\n",
" <th>
108
</th>\n",
" <th>
2
</th>\n",
" <td>2021-06-30</td>\n",
" <td>2010034324</td>\n",
" <td>-1.234</td>\n",
" <td>-0.869</td>\n",
" <td>-0.592</td>\n",
" <td>-0.728</td>\n",
" <td>0.346</td>\n",
" <td>0.457</td>\n",
" <td>-1.232</td>\n",
" <td>-0.630</td>\n",
" <td>...</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>2</td>\n",
" <td>-1.24528</td>\n",
" <td>11.08240</td>\n",
" <td>27.074300</td>\n",
" <td>0.108353</td>\n",
" <td>-6.563630</td>\n",
" <td>4.981810</td>\n",
" <td>-3.827610</td>\n",
" <td>-0.831560</td>\n",
" <td>...</td>\n",
" <td>-4.848280</td>\n",
" <td>-6.873530</td>\n",
" <td>-0.902493</td>\n",
" <td>1.415400</td>\n",
" <td>-2.572230</td>\n",
" <td>-5.72210</td>\n",
" <td>-9.31991</td>\n",
" <td>2.72661</td>\n",
" <td>-4.81947</td>\n",
" <td>3.90944</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>2021-06-30</td>\n",
" <td>3</td>\n",
" <td>-1.01008</td>\n",
" <td>-2.75662</td>\n",
" <td>0.108353</td>\n",
" <td>5.242660</td>\n",
" <td>-0.873673</td>\n",
" <td>-1.437590</td>\n",
" <td>0.176521</td>\n",
" <td>1.665720</td>\n",
" <td>...</td>\n",
" <td>0.312074</td>\n",
" <td>-0.008442</td>\n",
" <td>1.838370</td>\n",
" <td>5.241490</td>\n",
" <td>0.620696</td>\n",
" <td>10.86830</td>\n",
" <td>3.04039</td>\n",
" <td>-4.13551</td>\n",
" <td>-3.11186</td>\n",
" <td>-0.49132</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2021-06-30</td>\n",
" <td>4</td>\n",
" <td>4.30891</td>\n",
" <td>-6.93907</td>\n",
" <td>-6.563630</td>\n",
" <td>-0.873673</td>\n",
" <td>15.639900</td>\n",
" <td>-1.775660</td>\n",
" <td>5.010080</td>\n",
" <td>0.990544</td>\n",
" <td>...</td>\n",
" <td>0.580110</td>\n",
" <td>5.490300</td>\n",
" <td>-3.171420</td>\n",
" <td>3.719090</td>\n",
" <td>-0.874184</td>\n",
" <td>4.54887</td>\n",
" <td>1.97913</td>\n",
" <td>2.80706</td>\n",
" <td>10.57450</td>\n",
" <td>3.98674</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>
109
rows × 40 columns</p>\n",
"<p>
5
rows × 40 columns</p>\n",
"</div>"
],
"text/plain": [
" trade_date security_code BETA MOMENTUM SIZE EARNYILD RESVOL \\\n",
"0 2021-06-30 2010000045 -0.017 -0.340 -0.841 -0.234 -0.041 \n",
"1 2021-06-30 2010000121 -1.274 -0.758 -1.765 -0.103 -0.753 \n",
"2 2021-06-30 2010000171 0.465 1.300 0.996 -0.862 1.511 \n",
"3 2021-06-30 2010000377 2.436 1.730 1.325 -1.262 -0.079 \n",
"4 2021-06-30 2010000438 1.073 0.593 2.359 -0.937 -0.886 \n",
".. ... ... ... ... ... ... ... \n",
"104 2021-06-30 2010034304 -1.512 -1.342 -2.006 -0.971 0.109 \n",
"105 2021-06-30 2010034305 -1.952 -1.374 -2.572 -0.499 0.242 \n",
"106 2021-06-30 2010034317 -1.587 -1.935 -1.743 -0.416 0.616 \n",
"107 2021-06-30 2010034318 -1.535 -1.314 -2.423 0.760 0.782 \n",
"108 2021-06-30 2010034324 -1.234 -0.869 -0.592 -0.728 0.346 \n",
" trade_date FactorID BETA MOMENTUM SIZE EARNYILD RESVOL \\\n",
"0 2021-06-30 0 17.41000 -0.36765 -1.245280 -1.010080 4.308910 \n",
"1 2021-06-30 1 -0.36765 26.03980 11.082400 -2.756620 -6.939070 \n",
"2 2021-06-30 2 -1.24528 11.08240 27.074300 0.108353 -6.563630 \n",
"3 2021-06-30 3 -1.01008 -2.75662 0.108353 5.242660 -0.873673 \n",
"4 2021-06-30 4 4.30891 -6.93907 -6.563630 -0.873673 15.639900 \n",
"\n",
" GROWTH BTOP LEVERAGE ... Utilities Telecom AgriForest CHEM \\\n",
"0 -0.696 0.755 -0.586 ... 0 0 0 0 \n",
"1 -0.247 1.087 0.589 ... 0 0 0 0 \n",
"2 -0.020 -0.778 -0.133 ... 0 0 0 0 \n",
"3 0.305 -1.297 -1.375 ... 0 0 0 0 \n",
"4 0.100 -1.187 -1.485 ... 0 0 0 0 \n",
".. ... ... ... ... ... ... ... ... \n",
"104 0.762 -1.077 -0.979 ... 0 0 0 0 \n",
"105 -1.154 -0.990 -1.394 ... 0 0 0 0 \n",
"106 -0.501 -0.691 -1.083 ... 0 0 0 0 \n",
"107 -0.533 -0.051 0.265 ... 1 0 0 0 \n",
"108 0.457 -1.232 -0.630 ... 0 0 0 0 \n",
" GROWTH BTOP LEVERAGE ... Utilities Telecom AgriForest \\\n",
"0 0.573464 0.578040 -0.124983 ... -3.179720 4.888220 -8.581790 \n",
"1 5.527240 -4.961540 -3.526840 ... 6.439720 -14.841600 0.362207 \n",
"2 4.981810 -3.827610 -0.831560 ... -4.848280 -6.873530 -0.902493 \n",
"3 -1.437590 0.176521 1.665720 ... 0.312074 -0.008442 1.838370 \n",
"4 -1.775660 5.010080 0.990544 ... 0.580110 5.490300 -3.171420 \n",
"\n",
" CHEM Media IronSteel NonBankFinan ELECEQP AERODEF \\\n",
"0 -0.200272 -3.514560 -17.73480 5.79121 6.31339 7.05734 \n",
"1 -1.501860 -4.812920 -2.10750 -20.00980 10.43610 -6.98387 \n",
"2 1.415400 -2.572230 -5.72210 -9.31991 2.72661 -4.81947 \n",
"3 5.241490 0.620696 10.86830 3.04039 -4.13551 -3.11186 \n",
"4 3.719090 -0.874184 4.54887 1.97913 2.80706 10.57450 \n",
"\n",
" Conglomerates \n",
"0 -5.14934 \n",
"1 2.60997 \n",
"2 3.90944 \n",
"3 -0.49132 \n",
"4 3.98674 \n",
"\n",
"[5 rows x 40 columns]"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cov = risk_cov(trade_dt, trade_dt)\n",
"cov.head()"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "88854e5b-ad2e-4629-bb8c-9e58a851a298",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" Media IronSteel NonBankFinan ELECEQP AERODEF Conglomerates \n",
"0 0 0 0 0 0 0 \n",
"1 0 0 0 0 0 0 \n",
"2 0 0 0 0 0 0 \n",
"3 0 0 0 0 0 0 \n",
"4 0 0 0 0 0 0 \n",
".. ... ... ... ... ... ... \n",
"104 0 0 0 0 0 0 \n",
"105 0 0 0 0 0 0 \n",
"106 0 0 0 0 0 0 \n",
"107 0 0 0 0 0 0 \n",
"108 0 0 0 0 0 0 \n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
"[109 rows x 40 columns]"
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>trade_date</th>\n",
" <th>security_code</th>\n",
" <th>secShortName</th>\n",
" <th>symbol</th>\n",
" <th>SRISK</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>2021-06-30</td>\n",
" <td>2010000045</td>\n",
" <td>海信视像</td>\n",
" <td>600060.XSHG</td>\n",
" <td>43.597</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2021-06-30</td>\n",
" <td>2010000121</td>\n",
" <td>中青旅</td>\n",
" <td>600138.XSHG</td>\n",
" <td>20.325</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>2021-06-30</td>\n",
" <td>2010000171</td>\n",
" <td>复星医药</td>\n",
" <td>600196.XSHG</td>\n",
" <td>49.067</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>2021-06-30</td>\n",
" <td>2010000377</td>\n",
" <td>片仔癀</td>\n",
" <td>600436.XSHG</td>\n",
" <td>36.564</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2021-06-30</td>\n",
" <td>2010000438</td>\n",
" <td>贵州茅台</td>\n",
" <td>600519.XSHG</td>\n",
" <td>24.513</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" trade_date security_code secShortName symbol SRISK\n",
"0 2021-06-30 2010000045 海信视像 600060.XSHG 43.597\n",
"1 2021-06-30 2010000121 中青旅 600138.XSHG 20.325\n",
"2 2021-06-30 2010000171 复星医药 600196.XSHG 49.067\n",
"3 2021-06-30 2010000377 片仔癀 600436.XSHG 36.564\n",
"4 2021-06-30 2010000438 贵州茅台 600519.XSHG 24.513"
]
},
"execution_count":
7
,
"execution_count":
15
,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ddf"
"spec_risk = special_risk(portfolio.SKCODE, trade_dt, trade_dt)\n",
"spec_risk.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "
fd64eb4b-489e-46be-ba48-7d7c3c12a32a
",
"id": "
6102c448-5515-4f1d-ab82-91c0ce98a7ee
",
"metadata": {},
"outputs": [],
"source": []
...
...
attribution/020 brison attribution.ipynb
View file @
e41f62d2
...
...
@@ -14,17 +14,27 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 3,
"id": "1813212b-9027-4831-a245-1a19a44e787b",
"metadata": {},
"outputs": [],
"source": [
"trade_dt = \"20210630\""
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "21a0b455-1eb3-47f0-8a9c-15533dd12939",
"metadata": {},
"outputs": [],
"source": [
"
df = fd_skdetail(\"1030000006\", trade_dt=\"20210630\"
)"
"
portfolio = fd_skdetail(\"1030000006\", trade_dt=trade_dt
)"
]
},
{
"cell_type": "code",
"execution_count":
3
,
"execution_count":
6
,
"id": "82ca3a10-45e2-4b49-8be1-1e77104a1a52",
"metadata": {},
"outputs": [
...
...
@@ -91,133 +101,265 @@
" <td>华夏银行</td>\n",
" <td>0.18</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" trade_date security_code sname weighing\n",
"115 2021-06-30 2010000001 浦发银行 0.55\n",
"116 2021-06-30 2010000010 上海机场 0.22\n",
"117 2021-06-30 2010000011 包钢股份 0.17\n",
"118 2021-06-30 2010000012 华能国际 0.09\n",
"119 2021-06-30 2010000014 华夏银行 0.18"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"idx_comp = index_component(\"2070000060\", trade_dt, trade_dt)\n",
"idx_comp.head()"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "6d57d8f9-6c1c-4a41-ae64-87763648a22b",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>trade_date</th>\n",
" <th>security_code</th>\n",
" <th>industry_code</th>\n",
" <th>industry_name</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2
96
</th>\n",
" <th>2
0
</th>\n",
" <td>2021-06-30</td>\n",
" <td>20100
32227
</td>\n",
" <td>
沪硅产业
</td>\n",
" <td>
0.07
</td>\n",
" <td>20100
00045
</td>\n",
" <td>
330200
</td>\n",
" <td>
视听器材
</td>\n",
" </tr>\n",
" <tr>\n",
" <th>
278
</th>\n",
" <th>
79
</th>\n",
" <td>2021-06-30</td>\n",
" <td>20100
32240
</td>\n",
" <td>
万泰生物
</td>\n",
" <td>
0.08
</td>\n",
" <td>20100
00121
</td>\n",
" <td>
460300
</td>\n",
" <td>
旅游综合
</td>\n",
" </tr>\n",
" <tr>\n",
" <th>
191
</th>\n",
" <th>
62
</th>\n",
" <td>2021-06-30</td>\n",
" <td>20100
32306
</td>\n",
" <td>
中泰证券
</td>\n",
" <td>
0.04
</td>\n",
" <td>20100
00171
</td>\n",
" <td>
370300
</td>\n",
" <td>
生物制品
</td>\n",
" </tr>\n",
" <tr>\n",
" <th>
270
</th>\n",
" <th>
63
</th>\n",
" <td>2021-06-30</td>\n",
" <td>20100
32493
</td>\n",
" <td>
甘李药业
</td>\n",
" <td>
0.03
</td>\n",
" <td>20100
00377
</td>\n",
" <td>
370200
</td>\n",
" <td>
中药
</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2
67
</th>\n",
" <th>2
8
</th>\n",
" <td>2021-06-30</td>\n",
" <td>20100
33291
</td>\n",
" <td>
中金公司
</td>\n",
" <td>
0.08
</td>\n",
" <td>20100
00438
</td>\n",
" <td>
340300
</td>\n",
" <td>
饮料制造
</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>300 rows × 4 columns</p>\n",
"</div>"
],
"text/plain": [
" trade_date security_code sname weighing\n",
"115 2021-06-30 2010000001 浦发银行 0.55\n",
"116 2021-06-30 2010000010 上海机场 0.22\n",
"117 2021-06-30 2010000011 包钢股份 0.17\n",
"118 2021-06-30 2010000012 华能国际 0.09\n",
"119 2021-06-30 2010000014 华夏银行 0.18\n",
".. ... ... ... ...\n",
"296 2021-06-30 2010032227 沪硅产业 0.07\n",
"278 2021-06-30 2010032240 万泰生物 0.08\n",
"191 2021-06-30 2010032306 中泰证券 0.04\n",
"270 2021-06-30 2010032493 甘李药业 0.03\n",
"267 2021-06-30 2010033291 中金公司 0.08\n",
"\n",
"[300 rows x 4 columns]"
" trade_date security_code industry_code industry_name\n",
"20 2021-06-30 2010000045 330200 视听器材\n",
"79 2021-06-30 2010000121 460300 旅游综合\n",
"62 2021-06-30 2010000171 370300 生物制品\n",
"63 2021-06-30 2010000377 370200 中药\n",
"28 2021-06-30 2010000438 340300 饮料制造"
]
},
"execution_count":
3
,
"execution_count":
10
,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"index_component(\"2070000060\", trade_dt=\"20210630\")"
"ind = sw_industry_daily(df.SKCODE, trade_dt, trade_dt, 2)\n",
"ind.head()"
]
},
{
"cell_type": "code",
"execution_count":
4
,
"id": "
6d57d8f9-6c1c-4a41-ae64-87763648a22b
",
"execution_count":
13
,
"id": "
fa17cc0d-61ad-4958-bf01-7cdc1c17230d
",
"metadata": {},
"outputs": [],
"source": [
"r
es = sw_industry_daily(df.SKCODE, \"20210630\", 2
)"
"r
tns = stk_return(df.SKCODE, trade_dt, \"20211231\"
)"
]
},
{
"cell_type": "code",
"execution_count":
5
,
"id": "
6883105d-9a59-4e08-847c-a9c056826fd1
",
"execution_count":
14
,
"id": "
7743d37c-eb35-4294-95ef-da1007e3c175
",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>trade_date</th>\n",
" <th>security_code</th>\n",
" <th>change_pct / 100.</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>2021-06-30</td>\n",
" <td>2010000045</td>\n",
" <td>-0.009379</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2021-06-30</td>\n",
" <td>2010000121</td>\n",
" <td>-0.009434</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>2021-06-30</td>\n",
" <td>2010000171</td>\n",
" <td>0.000694</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>2021-06-30</td>\n",
" <td>2010000377</td>\n",
" <td>-0.002692</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2021-06-30</td>\n",
" <td>2010000438</td>\n",
" <td>-0.002570</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14830</th>\n",
" <td>2021-12-31</td>\n",
" <td>2010034355</td>\n",
" <td>-0.002386</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14831</th>\n",
" <td>2021-12-31</td>\n",
" <td>2010034356</td>\n",
" <td>-0.012759</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14763</th>\n",
" <td>2021-12-31</td>\n",
" <td>2010034358</td>\n",
" <td>0.006309</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14764</th>\n",
" <td>2021-12-31</td>\n",
" <td>2010034359</td>\n",
" <td>-0.018512</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14765</th>\n",
" <td>2021-12-31</td>\n",
" <td>2010034373</td>\n",
" <td>-0.015939</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>14832 rows × 3 columns</p>\n",
"</div>"
],
"text/plain": [
"39"
" trade_date security_code change_pct / 100.\n",
"0 2021-06-30 2010000045 -0.009379\n",
"1 2021-06-30 2010000121 -0.009434\n",
"2 2021-06-30 2010000171 0.000694\n",
"3 2021-06-30 2010000377 -0.002692\n",
"4 2021-06-30 2010000438 -0.002570\n",
"... ... ... ...\n",
"14830 2021-12-31 2010034355 -0.002386\n",
"14831 2021-12-31 2010034356 -0.012759\n",
"14763 2021-12-31 2010034358 0.006309\n",
"14764 2021-12-31 2010034359 -0.018512\n",
"14765 2021-12-31 2010034373 -0.015939\n",
"\n",
"[14832 rows x 3 columns]"
]
},
"execution_count":
5
,
"execution_count":
14
,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(res.industry_name.unique())"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "fa17cc0d-61ad-4958-bf01-7cdc1c17230d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Wall time: 44.6 s\n"
]
}
],
"source": [
"%%time\n",
"rtns = stk_return_range(df.SKCODE, \"20210630\", \"20211231\")"
"rtns"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "
7743d37c-eb35-4294-95ef-da1007e3c175
",
"id": "
41d6c18d-2ff7-4592-89cb-d19c4e8cc3e1
",
"metadata": {},
"outputs": [],
"source": []
...
...
fof/utility.py
View file @
e41f62d2
...
...
@@ -239,7 +239,10 @@ def fd_skdetail(security_ids, trade_dt):
ISVALID = 1 AND
SECODE in ({sec_id_strs})
"""
return
read_sql
(
query
)
.
sort_values
(
"SECURITYID"
)
df
=
read_sql
(
query
)
.
sort_values
(
"SECURITYID"
)
total_value
=
df
.
HOLDMKTCAP
.
sum
()
df
[
"pct"
]
=
df
.
HOLDMKTCAP
/
total_value
return
df
def
_to_yyyy_mm_dd
(
trade_dt
):
...
...
@@ -249,7 +252,7 @@ def _to_yyyy_mm_dd(trade_dt):
return
f
"{trade_dt[0:4]}-{trade_dt[4:6]}-{trade_dt[6:8]}"
def
risk_exposure
(
security_ids
,
trade_dt
):
def
risk_exposure
(
security_ids
,
start_date
,
end_date
):
if
isinstance
(
security_ids
,
str
):
sec_id_strs
=
security_ids
else
:
...
...
@@ -258,26 +261,54 @@ def risk_exposure(security_ids, trade_dt):
query
=
f
"""
SELECT trade_date, security_code, {','.join(RISK_STYLES)}, {','.join(INDUSTRY_STYLES)} FROM risk_exposure
WHERE
trade_date
= '{_to_yyyy_mm_dd(trade_dt
)}' AND
is_verify = 1 AND
trade_date
>= '{_to_yyyy_mm_dd(start_date
)}' AND
trade_date <= '{_to_yyyy_mm_dd(end_date)}' AND
flag = 1 AND
security_code IN ({sec_id_strs})
"""
return
read_sql
(
query
,
source
=
"mysql"
)
.
sort_values
(
"security_code"
)
return
read_sql
(
query
,
source
=
"mysql"
)
.
sort_values
(
[
"trade_date"
,
"security_code"
]
)
def
risk_
cov
(
trade_dt
,
model
=
"short"
):
def
risk_
return
(
start_date
,
end_date
):
query
=
f
"""
SELECT * FROM risk_cov_{model}
SELECT trade_date, {','.join(RISK_STYLES)}, {','.join(INDUSTRY_STYLES)} FROM risk_return
WHERE
trade_date = '{_to_yyyy_mm_dd(trade_dt
)}' AND
is_verify = 1 AND
trade_date >= '{_to_yyyy_mm_dd(start_date
)}' AND
trade_date <= '{_to_yyyy_mm_dd(end_date)}' AND
flag = 1
"""
return
read_sql
(
query
,
source
=
"mysql"
)
.
sort_values
(
"FactorID"
)
return
read_sql
(
query
,
source
=
"mysql"
)
.
sort_values
(
[
"trade_date"
]
)
def
special_risk
(
security_ids
,
trade_dt
,
model
=
"short"
):
def
specific_return
(
security_ids
,
start_date
,
end_date
):
if
isinstance
(
security_ids
,
str
):
sec_id_strs
=
security_ids
else
:
sec_id_strs
=
","
.
join
([
"'"
+
s
+
"'"
for
s
in
security_ids
])
query
=
f
"""
SELECT trade_date, security_code, secShortName, symbol, spret FROM specific_return
WHERE
trade_date >= '{_to_yyyy_mm_dd(start_date)}' AND
trade_date <= '{_to_yyyy_mm_dd(end_date)}' AND
flag = 1 AND
security_code IN ({sec_id_strs})
"""
return
read_sql
(
query
,
source
=
"mysql"
)
.
sort_values
([
"trade_date"
,
"security_code"
])
def
risk_cov
(
start_date
,
end_date
,
model
=
"short"
):
query
=
f
"""
SELECT trade_date, FactorID, {','.join(RISK_STYLES)}, {','.join(INDUSTRY_STYLES)} FROM risk_cov_{model}
WHERE
trade_date >= '{_to_yyyy_mm_dd(start_date)}' AND
trade_date <= '{_to_yyyy_mm_dd(end_date)}' AND
flag = 1
"""
return
read_sql
(
query
,
source
=
"mysql"
)
.
sort_values
([
"trade_date"
,
"FactorID"
])
def
special_risk
(
security_ids
,
start_date
,
end_date
,
model
=
"short"
):
if
isinstance
(
security_ids
,
str
):
sec_id_strs
=
security_ids
else
:
...
...
@@ -286,15 +317,15 @@ def special_risk(security_ids, trade_dt, model="short"):
query
=
f
"""
SELECT trade_date, security_code, secShortName, symbol, SRISK FROM specific_risk_{model}
WHERE
trade_date
= '{_to_yyyy_mm_dd(trade_dt
)}' AND
is_verify = 1 AND
trade_date
>= '{_to_yyyy_mm_dd(start_date
)}' AND
trade_date <= '{_to_yyyy_mm_dd(end_date)}' AND
flag = 1 AND
security_code IN ({sec_id_strs})
"""
return
read_sql
(
query
,
source
=
"mysql"
)
.
sort_values
(
"security_code"
)
return
read_sql
(
query
,
source
=
"mysql"
)
.
sort_values
(
[
"trade_date"
,
"security_code"
]
)
def
index_component
(
index_code
,
trade_dt
):
def
index_component
(
index_code
,
start_date
,
end_date
):
query
=
f
"""
SELECT index_component.trade_date, index_component.security_code, `index`.sname, `index`.weighing FROM `index`
JOIN index_component ON
...
...
@@ -302,17 +333,16 @@ def index_component(index_code, trade_dt):
`index`.isymbol = index_component.isymbol AND
`index`.symbol = index_component.symbol
WHERE
`index`.trade_date
= '{_to_yyyy_mm_dd(trade_dt
)}' AND
`index`.
is_verify = 1
AND
`index`.trade_date
>= '{_to_yyyy_mm_dd(start_date
)}' AND
`index`.
trade_date <= '{_to_yyyy_mm_dd(end_date)}'
AND
`index`.flag = 1 AND
index_component.is_verify = 1 AND
index_component.flag = 1 AND
index_component.isecurity_code = '{index_code}';
"""
return
read_sql
(
query
,
source
=
"mysql"
)
.
sort_values
(
"security_code"
)
return
read_sql
(
query
,
source
=
"mysql"
)
.
sort_values
(
[
"trade_date"
,
"security_code"
]
)
def
sw_industry_daily
(
security_ids
,
trade_dt
,
level
=
1
):
def
sw_industry_daily
(
security_ids
,
start_date
,
end_date
,
level
=
1
):
if
isinstance
(
security_ids
,
str
):
sec_id_strs
=
security_ids
else
:
...
...
@@ -321,27 +351,26 @@ def sw_industry_daily(security_ids, trade_dt, level=1):
query
=
f
"""
SELECT trade_date, security_code, industry_code{level} as industry_code, industry_name{level} as industry_name FROM sw_industry_daily
WHERE
trade_date
= '{_to_yyyy_mm_dd(trade_dt
)}' AND
is_verify = 1
AND
trade_date
>= '{_to_yyyy_mm_dd(start_date
)}' AND
trade_date <= '{_to_yyyy_mm_dd(end_date)}'
AND
flag = 1 AND
security_code IN ({sec_id_strs})
"""
return
read_sql
(
query
,
source
=
"mysql"
)
.
sort_values
(
"security_code"
)
return
read_sql
(
query
,
source
=
"mysql"
)
.
sort_values
(
[
"trade_date"
,
"security_code"
]
)
def
stk_return
_range
(
security_ids
,
start_date
,
end_date
):
def
stk_return
(
security_ids
,
start_date
,
end_date
):
if
isinstance
(
security_ids
,
str
):
sec_id_strs
=
security_ids
else
:
sec_id_strs
=
","
.
join
([
"'"
+
s
+
"'"
for
s
in
security_ids
])
query
=
f
"""
SELECT
security_code, min(trade_date) as start_date, max(trade_date) as end_date, exp(sum(log(change_pct / 100.0 + 1.0))) - 1.0 as rtn
FROM stk_daily_price_pro
SELECT
trade_date, security_code, change_pct / 100.
FROM stk_daily_price_pro
WHERE
trade_date >= '{_to_yyyy_mm_dd(start_date)}' AND
trade_date <= '{_to_yyyy_mm_dd(end_date)}' AND
security_code IN ({sec_id_strs}) AND
flag = 1
GROUP BY security_code;
"""
return
read_sql
(
query
,
source
=
"mysql"
)
.
sort_values
(
"security_code"
)
return
read_sql
(
query
,
source
=
"mysql"
)
.
sort_values
(
[
"trade_date"
,
"security_code"
]
)
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