Commit 64406a81 authored by Dr.李's avatar Dr.李

added manager more info

parent 52149dd6
...@@ -52,6 +52,9 @@ INDUSTRY_STYLES = [ ...@@ -52,6 +52,9 @@ INDUSTRY_STYLES = [
] ]
BOND_CLASSES = ["990301", "990302", "990303", "990304", "990305", "990306", "990399", "990501", "990502", "990205", "990402", "990603"]
def read_sql(query, source="mssql"): def read_sql(query, source="mssql"):
if source == "mssql": if source == "mssql":
engine = sa.create_engine(MSSQL_CONN, encoding="utf8", connect_args={"charset": "GBK"}) engine = sa.create_engine(MSSQL_CONN, encoding="utf8", connect_args={"charset": "GBK"})
...@@ -60,7 +63,7 @@ def read_sql(query, source="mssql"): ...@@ -60,7 +63,7 @@ def read_sql(query, source="mssql"):
return pd.read_sql(query, con=engine) return pd.read_sql(query, con=engine)
def nearest_report_date(date): def nearest_report_date(date, freq="Q"):
if len(date) == 10: if len(date) == 10:
date = date.replace("-", "") date = date.replace("-", "")
...@@ -68,7 +71,14 @@ def nearest_report_date(date): ...@@ -68,7 +71,14 @@ def nearest_report_date(date):
year = int(date[0:4]) year = int(date[0:4])
last_year = year last_year = year
q = (mm // 3 + 1) * 3 if freq == "Q":
q = (mm // 3 + 1) * 3 - 3
else:
q = (mm // 6 + 1) * 6 - 6
if q <= 0:
q += 12
last_year -= 1
last = calendar.monthrange(last_year, q)[1] last = calendar.monthrange(last_year, q)[1]
return dt.date(last_year, q, last).strftime("%Y%m%d") return dt.date(last_year, q, last).strftime("%Y%m%d")
...@@ -497,3 +507,16 @@ def fd_rl_classification(security_ids, trade_dt): ...@@ -497,3 +507,16 @@ def fd_rl_classification(security_ids, trade_dt):
flag = 1 flag = 1
""" """
return read_sql(query, source="mysql").sort_values("SECURITYID").reset_index(drop=True) return read_sql(query, source="mysql").sort_values("SECURITYID").reset_index(drop=True)
def read_factors(security_ids, trade_dt, factors):
sec_id_strs = _join_ids(security_ids)
query = f"""
SELECT security_code, trade_date, {','.join(factors)} FROM factor_valuation_estimation
WHERE
trade_date = '{_to_yyyymmdd(trade_dt)}' AND
security_code in ({sec_id_strs}) AND
flag = 1
"""
return read_sql(query, source="mysql").sort_values("security_code").reset_index(drop=True)
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 3,
"id": "ca389ee5-b800-4682-bfab-a22a6247bd67", "id": "ca389ee5-b800-4682-bfab-a22a6247bd67",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -13,7 +13,8 @@ ...@@ -13,7 +13,8 @@
"from fof.utility import (\n", "from fof.utility import (\n",
" fd_manager,\n", " fd_manager,\n",
" fd_fund_mktv,\n", " fd_fund_mktv,\n",
" fd_rl_classification\n", " fd_rl_classification,\n",
" BOND_CLASSES\n",
")\n", ")\n",
"import pandas as pd\n", "import pandas as pd\n",
"from PyFin.api import *" "from PyFin.api import *"
...@@ -30,7 +31,7 @@ ...@@ -30,7 +31,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 4,
"id": "008213ed-f569-4535-8519-7a5b1b06503c", "id": "008213ed-f569-4535-8519-7a5b1b06503c",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -40,7 +41,7 @@ ...@@ -40,7 +41,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 5,
"id": "be10d317-bca6-41db-9b54-56573cd564a6", "id": "be10d317-bca6-41db-9b54-56573cd564a6",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
...@@ -145,7 +146,7 @@ ...@@ -145,7 +146,7 @@
"4 19000101 " "4 19000101 "
] ]
}, },
"execution_count": 3, "execution_count": 5,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
...@@ -158,7 +159,7 @@ ...@@ -158,7 +159,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 6,
"id": "ffbac3af-39d6-4cd3-95cb-705c32380985", "id": "ffbac3af-39d6-4cd3-95cb-705c32380985",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
...@@ -196,40 +197,40 @@ ...@@ -196,40 +197,40 @@
" <td>1030000867</td>\n", " <td>1030000867</td>\n",
" <td>1.9650</td>\n", " <td>1.9650</td>\n",
" <td>1.708075</td>\n", " <td>1.708075</td>\n",
" <td>1.385083e+08</td>\n", " <td>1.940913e+08</td>\n",
" <td>2.721688e+08</td>\n", " <td>3.813894e+08</td>\n",
" </tr>\n", " </tr>\n",
" <tr>\n", " <tr>\n",
" <th>1</th>\n", " <th>1</th>\n",
" <td>1030004356</td>\n", " <td>1030004356</td>\n",
" <td>1.5530</td>\n", " <td>1.5530</td>\n",
" <td>1.569653</td>\n", " <td>1.569653</td>\n",
" <td>1.991231e+08</td>\n", " <td>3.329090e+08</td>\n",
" <td>3.092382e+08</td>\n", " <td>5.170077e+08</td>\n",
" </tr>\n", " </tr>\n",
" <tr>\n", " <tr>\n",
" <th>2</th>\n", " <th>2</th>\n",
" <td>1030005207</td>\n", " <td>1030005207</td>\n",
" <td>1.8904</td>\n", " <td>1.8904</td>\n",
" <td>1.689080</td>\n", " <td>1.689080</td>\n",
" <td>2.636247e+08</td>\n", " <td>8.860713e+07</td>\n",
" <td>4.983561e+08</td>\n", " <td>1.675029e+08</td>\n",
" </tr>\n", " </tr>\n",
" <tr>\n", " <tr>\n",
" <th>3</th>\n", " <th>3</th>\n",
" <td>1030007006</td>\n", " <td>1030007006</td>\n",
" <td>1.1860</td>\n", " <td>1.1860</td>\n",
" <td>0.168919</td>\n", " <td>0.168919</td>\n",
" <td>5.582928e+08</td>\n", " <td>4.971115e+08</td>\n",
" <td>6.621352e+08</td>\n", " <td>5.895742e+08</td>\n",
" </tr>\n", " </tr>\n",
" <tr>\n", " <tr>\n",
" <th>4</th>\n", " <th>4</th>\n",
" <td>1030008874</td>\n", " <td>1030008874</td>\n",
" <td>1.7560</td>\n", " <td>1.7560</td>\n",
" <td>1.679213</td>\n", " <td>1.679213</td>\n",
" <td>3.007749e+06</td>\n", " <td>1.152421e+07</td>\n",
" <td>5.281608e+06</td>\n", " <td>2.023652e+07</td>\n",
" </tr>\n", " </tr>\n",
" </tbody>\n", " </tbody>\n",
"</table>\n", "</table>\n",
...@@ -237,14 +238,14 @@ ...@@ -237,14 +238,14 @@
], ],
"text/plain": [ "text/plain": [
" SECURITYID UNITNAV NAVGRTD ENDFDSHARE MKTV\n", " SECURITYID UNITNAV NAVGRTD ENDFDSHARE MKTV\n",
"0 1030000867 1.9650 1.708075 1.385083e+08 2.721688e+08\n", "0 1030000867 1.9650 1.708075 1.940913e+08 3.813894e+08\n",
"1 1030004356 1.5530 1.569653 1.991231e+08 3.092382e+08\n", "1 1030004356 1.5530 1.569653 3.329090e+08 5.170077e+08\n",
"2 1030005207 1.8904 1.689080 2.636247e+08 4.983561e+08\n", "2 1030005207 1.8904 1.689080 8.860713e+07 1.675029e+08\n",
"3 1030007006 1.1860 0.168919 5.582928e+08 6.621352e+08\n", "3 1030007006 1.1860 0.168919 4.971115e+08 5.895742e+08\n",
"4 1030008874 1.7560 1.679213 3.007749e+06 5.281608e+06" "4 1030008874 1.7560 1.679213 1.152421e+07 2.023652e+07"
] ]
}, },
"execution_count": 4, "execution_count": 6,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
...@@ -257,7 +258,7 @@ ...@@ -257,7 +258,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": 7,
"id": "3dc7be9d-e8c2-4566-bf14-d617ea230c14", "id": "3dc7be9d-e8c2-4566-bf14-d617ea230c14",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
...@@ -343,21 +344,20 @@ ...@@ -343,21 +344,20 @@
"4 1030008874 9902 混合型基金 990202 高权益仓位混合型基金" "4 1030008874 9902 混合型基金 990202 高权益仓位混合型基金"
] ]
}, },
"execution_count": 5, "execution_count": 7,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
], ],
"source": [ "source": [
"# 获取基金分类信息\n", "# 获取基金分类信息\n",
"bonds_class_codes = [\"990301\", \"990302\", \"990303\", \"990304\", \"990305\", \"990306\", \"990399\", \"990501\", \"990502\", \"990205\", \"990402\", \"990603\"]\n",
"funds_rl_classification = fd_rl_classification(funds_mktv.SECURITYID, trade_dt)\n", "funds_rl_classification = fd_rl_classification(funds_mktv.SECURITYID, trade_dt)\n",
"funds_rl_classification.head()" "funds_rl_classification.head()"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 6, "execution_count": 8,
"id": "1055c68c-3875-489b-acaa-a9046d98a2c1", "id": "1055c68c-3875-489b-acaa-a9046d98a2c1",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -368,13 +368,13 @@ ...@@ -368,13 +368,13 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 7, "execution_count": 9,
"id": "00d6c888-682e-43c5-8c57-420a279effa3", "id": "00d6c888-682e-43c5-8c57-420a279effa3",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"market_manager_index_ret = total_df.MKTV @ total_df.NAVGRTD / (1e-10 + total_df.MKTV.sum()) / 100.0\n", "market_manager_index_ret = total_df.MKTV @ total_df.NAVGRTD / (1e-10 + total_df.MKTV.sum()) / 100.0\n",
"flags = total_df.rl_sub_class.isin(bonds_class_codes)\n", "flags = total_df.rl_sub_class.isin(BOND_CLASSES)\n",
"bond_market_manager_index_ret = total_df[flags].MKTV @ total_df[flags].NAVGRTD / (1e-10 + total_df[flags].MKTV.sum()) / 100.0\n", "bond_market_manager_index_ret = total_df[flags].MKTV @ total_df[flags].NAVGRTD / (1e-10 + total_df[flags].MKTV.sum()) / 100.0\n",
"equity_market_manager_index_ret = total_df[~flags].MKTV @ total_df[~flags].NAVGRTD / (1e-10 + total_df[~flags].MKTV.sum()) / 100.0" "equity_market_manager_index_ret = total_df[~flags].MKTV @ total_df[~flags].NAVGRTD / (1e-10 + total_df[~flags].MKTV.sum()) / 100.0"
] ]
...@@ -389,7 +389,7 @@ ...@@ -389,7 +389,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 8, "execution_count": 10,
"id": "e1817df4-ce3f-48ca-b656-2a17abc18f8e", "id": "e1817df4-ce3f-48ca-b656-2a17abc18f8e",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -398,7 +398,6 @@ ...@@ -398,7 +398,6 @@
" funds = fd_manager(manager_ids, start_date=trade_dt, end_date=trade_dt)\n", " funds = fd_manager(manager_ids, start_date=trade_dt, end_date=trade_dt)\n",
" funds_mktv = fd_fund_mktv(funds.SECURITYID.unique(), trade_dt=trade_dt)\n", " funds_mktv = fd_fund_mktv(funds.SECURITYID.unique(), trade_dt=trade_dt)\n",
" \n", " \n",
" bonds_class_codes = [\"990301\", \"990302\", \"990303\", \"990304\", \"990305\", \"990306\", \"990399\", \"990501\", \"990502\", \"990205\", \"990402\", \"990603\"]\n",
" funds_rl_classification = fd_rl_classification(funds_mktv.SECURITYID, trade_dt)\n", " funds_rl_classification = fd_rl_classification(funds_mktv.SECURITYID, trade_dt)\n",
" \n", " \n",
" total_df = pd.merge(funds, funds_mktv, on=\"SECURITYID\")\n", " total_df = pd.merge(funds, funds_mktv, on=\"SECURITYID\")\n",
...@@ -406,7 +405,7 @@ ...@@ -406,7 +405,7 @@
" \n", " \n",
" def calc(g):\n", " def calc(g):\n",
" market_manager_index_ret = g.MKTV @ g.NAVGRTD / (1e-10 + g.MKTV.sum()) / 100.0\n", " market_manager_index_ret = g.MKTV @ g.NAVGRTD / (1e-10 + g.MKTV.sum()) / 100.0\n",
" flags = g.rl_sub_class.isin(bonds_class_codes)\n", " flags = g.rl_sub_class.isin(BOND_CLASSES)\n",
" bond_market_manager_index_ret = g[flags].MKTV @ g[flags].NAVGRTD / (1e-10 + g[flags].MKTV.sum()) / 100.0\n", " bond_market_manager_index_ret = g[flags].MKTV @ g[flags].NAVGRTD / (1e-10 + g[flags].MKTV.sum()) / 100.0\n",
" equity_market_manager_index_ret = g[~flags].MKTV @ g[~flags].NAVGRTD / (1e-10 + g[~flags].MKTV.sum()) / 100.0\n", " equity_market_manager_index_ret = g[~flags].MKTV @ g[~flags].NAVGRTD / (1e-10 + g[~flags].MKTV.sum()) / 100.0\n",
" return pd.Series(\n", " return pd.Series(\n",
...@@ -417,7 +416,7 @@ ...@@ -417,7 +416,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 9, "execution_count": 11,
"id": "3225d437-56a8-4fc6-80bc-22456a356754", "id": "3225d437-56a8-4fc6-80bc-22456a356754",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
...@@ -464,9 +463,9 @@ ...@@ -464,9 +463,9 @@
" <th>1</th>\n", " <th>1</th>\n",
" <td>30322345</td>\n", " <td>30322345</td>\n",
" <td>左金保</td>\n", " <td>左金保</td>\n",
" <td>0.009627</td>\n", " <td>0.011596</td>\n",
" <td>0.0</td>\n", " <td>0.0</td>\n",
" <td>0.009627</td>\n", " <td>0.011596</td>\n",
" <td>20220207</td>\n", " <td>20220207</td>\n",
" </tr>\n", " </tr>\n",
" </tbody>\n", " </tbody>\n",
...@@ -476,10 +475,10 @@ ...@@ -476,10 +475,10 @@
"text/plain": [ "text/plain": [
" MANAGERCODE MANAGERNAME all bond equity trade_date\n", " MANAGERCODE MANAGERNAME all bond equity trade_date\n",
"0 30189744 张坤 0.039522 0.0 0.039522 20220207\n", "0 30189744 张坤 0.039522 0.0 0.039522 20220207\n",
"1 30322345 左金保 0.009627 0.0 0.009627 20220207" "1 30322345 左金保 0.011596 0.0 0.011596 20220207"
] ]
}, },
"execution_count": 9, "execution_count": 11,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
......
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "f000cc3d-6327-41bf-9bcf-a2c1aa682378",
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"sys.path.append(\"../\")\n",
"import datetime as dt\n",
"from fof.utility import (\n",
" fd_manager,\n",
" fd_fund_mktv,\n",
" fd_skdetail,\n",
" fd_rl_classification,\n",
" nearest_report_date,\n",
" risk_exposure,\n",
" read_factors,\n",
" sw_industry_daily,\n",
" RISK_STYLES,\n",
" BOND_CLASSES\n",
")\n",
"import pandas as pd\n",
"from PyFin.api import *"
]
},
{
"cell_type": "markdown",
"id": "50439166-7c11-43c3-87c6-1b83b68c5695",
"metadata": {},
"source": [
"# 1. Prepare Data\n",
"-----------------------"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "f67845f8-7d22-4c1e-a0cb-8025487c102e",
"metadata": {},
"outputs": [],
"source": [
"trade_dt = adjustDateByCalendar(\"china.sse\", \"2022-02-01\").strftime(\"%Y%m%d\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "eca3cbc4-46d2-4290-b059-9264115d4935",
"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>MANAGERCODE</th>\n",
" <th>MANAGERNAME</th>\n",
" <th>COMPCODE</th>\n",
" <th>COMPNAME</th>\n",
" <th>SECURITYID</th>\n",
" <th>BEGINDATE</th>\n",
" <th>ENDDATE</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>30322345</td>\n",
" <td>左金保</td>\n",
" <td>80000243</td>\n",
" <td>长信基金管理有限责任公司</td>\n",
" <td>1030008874</td>\n",
" <td>20170109</td>\n",
" <td>19000101</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>30322345</td>\n",
" <td>左金保</td>\n",
" <td>80000243</td>\n",
" <td>长信基金管理有限责任公司</td>\n",
" <td>1030010453</td>\n",
" <td>20171109</td>\n",
" <td>19000101</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>30322345</td>\n",
" <td>左金保</td>\n",
" <td>80000243</td>\n",
" <td>长信基金管理有限责任公司</td>\n",
" <td>1030011585</td>\n",
" <td>20180809</td>\n",
" <td>19000101</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>30322345</td>\n",
" <td>左金保</td>\n",
" <td>80000243</td>\n",
" <td>长信基金管理有限责任公司</td>\n",
" <td>1030010116</td>\n",
" <td>20180830</td>\n",
" <td>19000101</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>30322345</td>\n",
" <td>左金保</td>\n",
" <td>80000243</td>\n",
" <td>长信基金管理有限责任公司</td>\n",
" <td>2030000115</td>\n",
" <td>20150313</td>\n",
" <td>19000101</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" MANAGERCODE MANAGERNAME COMPCODE COMPNAME SECURITYID BEGINDATE \\\n",
"0 30322345 左金保 80000243 长信基金管理有限责任公司 1030008874 20170109 \n",
"1 30322345 左金保 80000243 长信基金管理有限责任公司 1030010453 20171109 \n",
"2 30322345 左金保 80000243 长信基金管理有限责任公司 1030011585 20180809 \n",
"3 30322345 左金保 80000243 长信基金管理有限责任公司 1030010116 20180830 \n",
"4 30322345 左金保 80000243 长信基金管理有限责任公司 2030000115 20150313 \n",
"\n",
" ENDDATE \n",
"0 19000101 \n",
"1 19000101 \n",
"2 19000101 \n",
"3 19000101 \n",
"4 19000101 "
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 获取指定日,基金经理在管基金\n",
"funds = fd_manager(\"30322345\", start_date=trade_dt, end_date=trade_dt)\n",
"funds.head()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "b6e971ac-57ca-4b78-8a4a-7fab42cbcbff",
"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>UNITNAV</th>\n",
" <th>NAVGRTD</th>\n",
" <th>ENDFDSHARE</th>\n",
" <th>MKTV</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1030000867</td>\n",
" <td>1.9650</td>\n",
" <td>1.708075</td>\n",
" <td>1.277449e+08</td>\n",
" <td>2.510187e+08</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1030004356</td>\n",
" <td>1.5530</td>\n",
" <td>1.569653</td>\n",
" <td>2.049993e+08</td>\n",
" <td>3.183639e+08</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1030005207</td>\n",
" <td>1.8904</td>\n",
" <td>1.689080</td>\n",
" <td>1.169028e+08</td>\n",
" <td>2.209931e+08</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1030007006</td>\n",
" <td>1.1860</td>\n",
" <td>0.168919</td>\n",
" <td>1.723610e+08</td>\n",
" <td>2.044201e+08</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>1030008874</td>\n",
" <td>1.7560</td>\n",
" <td>1.679213</td>\n",
" <td>1.152421e+07</td>\n",
" <td>2.023652e+07</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" SECURITYID UNITNAV NAVGRTD ENDFDSHARE MKTV\n",
"0 1030000867 1.9650 1.708075 1.277449e+08 2.510187e+08\n",
"1 1030004356 1.5530 1.569653 2.049993e+08 3.183639e+08\n",
"2 1030005207 1.8904 1.689080 1.169028e+08 2.209931e+08\n",
"3 1030007006 1.1860 0.168919 1.723610e+08 2.044201e+08\n",
"4 1030008874 1.7560 1.679213 1.152421e+07 2.023652e+07"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 在管基金规模\n",
"funds_mktv = fd_fund_mktv(funds.SECURITYID.unique(), trade_dt=trade_dt)\n",
"funds_mktv.head()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "81b9647d-b257-4d61-ac7c-5529cdc26c39",
"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>pid</th>\n",
" <th>security_code</th>\n",
" <th>SKNAME</th>\n",
" <th>HOLDMKTCAP</th>\n",
" <th>HOLDAMT</th>\n",
" <th>NAVRTO</th>\n",
" <th>weight</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1030016920</td>\n",
" <td>2010000026</td>\n",
" <td>中信证券</td>\n",
" <td>242972.00</td>\n",
" <td>9200.0</td>\n",
" <td>2.36</td>\n",
" <td>0.000122</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1030005207</td>\n",
" <td>2010000026</td>\n",
" <td>中信证券</td>\n",
" <td>5514408.00</td>\n",
" <td>208800.0</td>\n",
" <td>2.67</td>\n",
" <td>0.002774</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1030010116</td>\n",
" <td>2010000026</td>\n",
" <td>中信证券</td>\n",
" <td>5514408.00</td>\n",
" <td>208800.0</td>\n",
" <td>2.67</td>\n",
" <td>0.002774</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1030011585</td>\n",
" <td>2010000026</td>\n",
" <td>中信证券</td>\n",
" <td>242972.00</td>\n",
" <td>9200.0</td>\n",
" <td>2.36</td>\n",
" <td>0.000122</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>1030016920</td>\n",
" <td>2010000030</td>\n",
" <td>招商银行</td>\n",
" <td>401808.79</td>\n",
" <td>8249.0</td>\n",
" <td>3.90</td>\n",
" <td>0.000202</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" pid security_code SKNAME HOLDMKTCAP HOLDAMT NAVRTO weight\n",
"0 1030016920 2010000026 中信证券 242972.00 9200.0 2.36 0.000122\n",
"1 1030005207 2010000026 中信证券 5514408.00 208800.0 2.67 0.002774\n",
"2 1030010116 2010000026 中信证券 5514408.00 208800.0 2.67 0.002774\n",
"3 1030011585 2010000026 中信证券 242972.00 9200.0 2.36 0.000122\n",
"4 1030016920 2010000030 招商银行 401808.79 8249.0 3.90 0.000202"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 获取在管基金所有组合(半年报/年报)\n",
"manager_portfolio = fd_skdetail(funds.SECURITYID.unique().tolist(), nearest_report_date(trade_dt, freq=\"S\"))\n",
"manager_portfolio.head()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "f3a678fe-31a3-4d95-a110-6eb40841c007",
"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>rl_class</th>\n",
" <th>rl_class_name</th>\n",
" <th>rl_sub_class</th>\n",
" <th>rl_sub_class_name</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1030000867</td>\n",
" <td>9902</td>\n",
" <td>混合型基金</td>\n",
" <td>990202</td>\n",
" <td>高权益仓位混合型基金</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1030004356</td>\n",
" <td>9901</td>\n",
" <td>股票型基金</td>\n",
" <td>990102</td>\n",
" <td>普通股票型基金</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1030005207</td>\n",
" <td>9901</td>\n",
" <td>股票型基金</td>\n",
" <td>990102</td>\n",
" <td>普通股票型基金</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1030007006</td>\n",
" <td>9902</td>\n",
" <td>混合型基金</td>\n",
" <td>990202</td>\n",
" <td>高权益仓位混合型基金</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>1030008874</td>\n",
" <td>9902</td>\n",
" <td>混合型基金</td>\n",
" <td>990202</td>\n",
" <td>高权益仓位混合型基金</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" SECURITYID rl_class rl_class_name rl_sub_class rl_sub_class_name\n",
"0 1030000867 9902 混合型基金 990202 高权益仓位混合型基金\n",
"1 1030004356 9901 股票型基金 990102 普通股票型基金\n",
"2 1030005207 9901 股票型基金 990102 普通股票型基金\n",
"3 1030007006 9902 混合型基金 990202 高权益仓位混合型基金\n",
"4 1030008874 9902 混合型基金 990202 高权益仓位混合型基金"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 获取基金分类信息\n",
"funds_rl_classification = fd_rl_classification(funds.SECURITYID.unique().tolist(), trade_dt)\n",
"funds_rl_classification.head()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "98fb2367-cf06-4ab5-9eb2-c9e20d3be17e",
"metadata": {},
"outputs": [],
"source": [
"# 获取风险暴露\n",
"risk_exp = risk_exposure(trade_dt, trade_dt, manager_portfolio.security_code.unique().tolist())"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "71a55496-ba7b-4948-89e8-a84fe9f40a53",
"metadata": {},
"outputs": [],
"source": [
"# 获取行业分类\n",
"industry = sw_industry_daily(trade_dt, trade_dt, 1, manager_portfolio.security_code.unique().tolist())"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "609292b2-4e07-4672-bf1a-103cfcb4d9f7",
"metadata": {},
"outputs": [],
"source": [
"# 获取因子值\n",
"factor_names = [\"PETTM\"]\n",
"factors = read_factors(manager_portfolio.security_code.unique().tolist(), trade_dt, factor_names)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "07dad3d3-29fc-437b-97da-84be03e55215",
"metadata": {},
"outputs": [],
"source": [
"# 计算分类\n",
"total_df = pd.merge(funds, manager_portfolio[[\"pid\", \"security_code\", \"HOLDMKTCAP\"]], left_on=\"SECURITYID\", right_on=\"pid\")\n",
"total_df = pd.merge(total_df, funds_rl_classification, on=\"SECURITYID\")\n",
"total_df = pd.merge(total_df, funds_mktv, on=\"SECURITYID\")\n",
"total_df = pd.merge(total_df, industry, on=\"security_code\")\n",
"total_df = pd.merge(total_df, risk_exp[[\"security_code\"] + RISK_STYLES], on=\"security_code\")\n",
"total_df = pd.merge(total_df, factors, on=[\"security_code\", \"trade_date\"])"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "54bff949-0399-484b-a509-ebbd94060d59",
"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>MANAGERCODE</th>\n",
" <th>MANAGERNAME</th>\n",
" <th>COMPCODE</th>\n",
" <th>COMPNAME</th>\n",
" <th>SECURITYID</th>\n",
" <th>BEGINDATE</th>\n",
" <th>ENDDATE</th>\n",
" <th>pid</th>\n",
" <th>security_code</th>\n",
" <th>HOLDMKTCAP</th>\n",
" <th>...</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>SIZENL</th>\n",
" <th>PETTM</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>30322345</td>\n",
" <td>左金保</td>\n",
" <td>80000243</td>\n",
" <td>长信基金管理有限责任公司</td>\n",
" <td>1030008874</td>\n",
" <td>20170109</td>\n",
" <td>19000101</td>\n",
" <td>1030008874</td>\n",
" <td>2010000042</td>\n",
" <td>15282000.0</td>\n",
" <td>...</td>\n",
" <td>0.674</td>\n",
" <td>-0.954</td>\n",
" <td>2.010</td>\n",
" <td>-0.437</td>\n",
" <td>0.427</td>\n",
" <td>0.618</td>\n",
" <td>1.448</td>\n",
" <td>-0.408</td>\n",
" <td>0.589</td>\n",
" <td>8.819104</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>30322345</td>\n",
" <td>左金保</td>\n",
" <td>80000243</td>\n",
" <td>长信基金管理有限责任公司</td>\n",
" <td>1030000867</td>\n",
" <td>20150313</td>\n",
" <td>19000101</td>\n",
" <td>1030000867</td>\n",
" <td>2010000042</td>\n",
" <td>15282000.0</td>\n",
" <td>...</td>\n",
" <td>0.674</td>\n",
" <td>-0.954</td>\n",
" <td>2.010</td>\n",
" <td>-0.437</td>\n",
" <td>0.427</td>\n",
" <td>0.618</td>\n",
" <td>1.448</td>\n",
" <td>-0.408</td>\n",
" <td>0.589</td>\n",
" <td>8.819104</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>30322345</td>\n",
" <td>左金保</td>\n",
" <td>80000243</td>\n",
" <td>长信基金管理有限责任公司</td>\n",
" <td>1030004356</td>\n",
" <td>20150313</td>\n",
" <td>19000101</td>\n",
" <td>1030004356</td>\n",
" <td>2010000042</td>\n",
" <td>8591880.0</td>\n",
" <td>...</td>\n",
" <td>0.674</td>\n",
" <td>-0.954</td>\n",
" <td>2.010</td>\n",
" <td>-0.437</td>\n",
" <td>0.427</td>\n",
" <td>0.618</td>\n",
" <td>1.448</td>\n",
" <td>-0.408</td>\n",
" <td>0.589</td>\n",
" <td>8.819104</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>30322345</td>\n",
" <td>左金保</td>\n",
" <td>80000243</td>\n",
" <td>长信基金管理有限责任公司</td>\n",
" <td>1030008874</td>\n",
" <td>20170109</td>\n",
" <td>19000101</td>\n",
" <td>1030008874</td>\n",
" <td>2010000152</td>\n",
" <td>15029378.0</td>\n",
" <td>...</td>\n",
" <td>0.350</td>\n",
" <td>0.232</td>\n",
" <td>0.717</td>\n",
" <td>-0.999</td>\n",
" <td>0.083</td>\n",
" <td>-0.506</td>\n",
" <td>-0.171</td>\n",
" <td>0.337</td>\n",
" <td>-0.047</td>\n",
" <td>12.198357</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>30322345</td>\n",
" <td>左金保</td>\n",
" <td>80000243</td>\n",
" <td>长信基金管理有限责任公司</td>\n",
" <td>1030000867</td>\n",
" <td>20150313</td>\n",
" <td>19000101</td>\n",
" <td>1030000867</td>\n",
" <td>2010000152</td>\n",
" <td>15029378.0</td>\n",
" <td>...</td>\n",
" <td>0.350</td>\n",
" <td>0.232</td>\n",
" <td>0.717</td>\n",
" <td>-0.999</td>\n",
" <td>0.083</td>\n",
" <td>-0.506</td>\n",
" <td>-0.171</td>\n",
" <td>0.337</td>\n",
" <td>-0.047</td>\n",
" <td>12.198357</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>75</th>\n",
" <td>30322345</td>\n",
" <td>左金保</td>\n",
" <td>80000243</td>\n",
" <td>长信基金管理有限责任公司</td>\n",
" <td>1030007006</td>\n",
" <td>20160727</td>\n",
" <td>19000101</td>\n",
" <td>1030007006</td>\n",
" <td>2010025110</td>\n",
" <td>5184990.0</td>\n",
" <td>...</td>\n",
" <td>-1.961</td>\n",
" <td>-1.154</td>\n",
" <td>-0.902</td>\n",
" <td>0.419</td>\n",
" <td>0.678</td>\n",
" <td>-1.148</td>\n",
" <td>-0.560</td>\n",
" <td>0.353</td>\n",
" <td>0.600</td>\n",
" <td>45.277531</td>\n",
" </tr>\n",
" <tr>\n",
" <th>76</th>\n",
" <td>30322345</td>\n",
" <td>左金保</td>\n",
" <td>80000243</td>\n",
" <td>长信基金管理有限责任公司</td>\n",
" <td>1030007006</td>\n",
" <td>20160727</td>\n",
" <td>19000101</td>\n",
" <td>1030007006</td>\n",
" <td>2010031328</td>\n",
" <td>6292354.0</td>\n",
" <td>...</td>\n",
" <td>0.711</td>\n",
" <td>-0.588</td>\n",
" <td>-1.336</td>\n",
" <td>-0.202</td>\n",
" <td>1.745</td>\n",
" <td>-1.141</td>\n",
" <td>-0.556</td>\n",
" <td>0.233</td>\n",
" <td>0.468</td>\n",
" <td>88.885583</td>\n",
" </tr>\n",
" <tr>\n",
" <th>77</th>\n",
" <td>30322345</td>\n",
" <td>左金保</td>\n",
" <td>80000243</td>\n",
" <td>长信基金管理有限责任公司</td>\n",
" <td>1030007006</td>\n",
" <td>20160727</td>\n",
" <td>19000101</td>\n",
" <td>1030007006</td>\n",
" <td>2010031624</td>\n",
" <td>5697780.0</td>\n",
" <td>...</td>\n",
" <td>0.026</td>\n",
" <td>-1.134</td>\n",
" <td>-0.261</td>\n",
" <td>0.620</td>\n",
" <td>0.493</td>\n",
" <td>-1.100</td>\n",
" <td>-1.131</td>\n",
" <td>1.317</td>\n",
" <td>0.601</td>\n",
" <td>23.858731</td>\n",
" </tr>\n",
" <tr>\n",
" <th>78</th>\n",
" <td>30322345</td>\n",
" <td>左金保</td>\n",
" <td>80000243</td>\n",
" <td>长信基金管理有限责任公司</td>\n",
" <td>1030007006</td>\n",
" <td>20160727</td>\n",
" <td>19000101</td>\n",
" <td>1030007006</td>\n",
" <td>2010032091</td>\n",
" <td>5784470.0</td>\n",
" <td>...</td>\n",
" <td>-0.113</td>\n",
" <td>-0.696</td>\n",
" <td>-1.049</td>\n",
" <td>0.798</td>\n",
" <td>1.155</td>\n",
" <td>-1.185</td>\n",
" <td>-1.668</td>\n",
" <td>0.574</td>\n",
" <td>0.515</td>\n",
" <td>63.522086</td>\n",
" </tr>\n",
" <tr>\n",
" <th>79</th>\n",
" <td>30322345</td>\n",
" <td>左金保</td>\n",
" <td>80000243</td>\n",
" <td>长信基金管理有限责任公司</td>\n",
" <td>1030007006</td>\n",
" <td>20160727</td>\n",
" <td>19000101</td>\n",
" <td>1030007006</td>\n",
" <td>2010033132</td>\n",
" <td>5663580.0</td>\n",
" <td>...</td>\n",
" <td>0.649</td>\n",
" <td>-0.791</td>\n",
" <td>-1.106</td>\n",
" <td>1.133</td>\n",
" <td>0.794</td>\n",
" <td>-1.301</td>\n",
" <td>-1.529</td>\n",
" <td>1.741</td>\n",
" <td>0.549</td>\n",
" <td>61.212585</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>80 rows × 32 columns</p>\n",
"</div>"
],
"text/plain": [
" MANAGERCODE MANAGERNAME COMPCODE COMPNAME SECURITYID BEGINDATE \\\n",
"0 30322345 左金保 80000243 长信基金管理有限责任公司 1030008874 20170109 \n",
"1 30322345 左金保 80000243 长信基金管理有限责任公司 1030000867 20150313 \n",
"2 30322345 左金保 80000243 长信基金管理有限责任公司 1030004356 20150313 \n",
"3 30322345 左金保 80000243 长信基金管理有限责任公司 1030008874 20170109 \n",
"4 30322345 左金保 80000243 长信基金管理有限责任公司 1030000867 20150313 \n",
".. ... ... ... ... ... ... \n",
"75 30322345 左金保 80000243 长信基金管理有限责任公司 1030007006 20160727 \n",
"76 30322345 左金保 80000243 长信基金管理有限责任公司 1030007006 20160727 \n",
"77 30322345 左金保 80000243 长信基金管理有限责任公司 1030007006 20160727 \n",
"78 30322345 左金保 80000243 长信基金管理有限责任公司 1030007006 20160727 \n",
"79 30322345 左金保 80000243 长信基金管理有限责任公司 1030007006 20160727 \n",
"\n",
" ENDDATE pid security_code HOLDMKTCAP ... MOMENTUM SIZE \\\n",
"0 19000101 1030008874 2010000042 15282000.0 ... 0.674 -0.954 \n",
"1 19000101 1030000867 2010000042 15282000.0 ... 0.674 -0.954 \n",
"2 19000101 1030004356 2010000042 8591880.0 ... 0.674 -0.954 \n",
"3 19000101 1030008874 2010000152 15029378.0 ... 0.350 0.232 \n",
"4 19000101 1030000867 2010000152 15029378.0 ... 0.350 0.232 \n",
".. ... ... ... ... ... ... ... \n",
"75 19000101 1030007006 2010025110 5184990.0 ... -1.961 -1.154 \n",
"76 19000101 1030007006 2010031328 6292354.0 ... 0.711 -0.588 \n",
"77 19000101 1030007006 2010031624 5697780.0 ... 0.026 -1.134 \n",
"78 19000101 1030007006 2010032091 5784470.0 ... -0.113 -0.696 \n",
"79 19000101 1030007006 2010033132 5663580.0 ... 0.649 -0.791 \n",
"\n",
" EARNYILD RESVOL GROWTH BTOP LEVERAGE LIQUIDTY SIZENL PETTM \n",
"0 2.010 -0.437 0.427 0.618 1.448 -0.408 0.589 8.819104 \n",
"1 2.010 -0.437 0.427 0.618 1.448 -0.408 0.589 8.819104 \n",
"2 2.010 -0.437 0.427 0.618 1.448 -0.408 0.589 8.819104 \n",
"3 0.717 -0.999 0.083 -0.506 -0.171 0.337 -0.047 12.198357 \n",
"4 0.717 -0.999 0.083 -0.506 -0.171 0.337 -0.047 12.198357 \n",
".. ... ... ... ... ... ... ... ... \n",
"75 -0.902 0.419 0.678 -1.148 -0.560 0.353 0.600 45.277531 \n",
"76 -1.336 -0.202 1.745 -1.141 -0.556 0.233 0.468 88.885583 \n",
"77 -0.261 0.620 0.493 -1.100 -1.131 1.317 0.601 23.858731 \n",
"78 -1.049 0.798 1.155 -1.185 -1.668 0.574 0.515 63.522086 \n",
"79 -1.106 1.133 0.794 -1.301 -1.529 1.741 0.549 61.212585 \n",
"\n",
"[80 rows x 32 columns]"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"total_df"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "e3c82655-730a-4fc2-bd59-45e4469f600a",
"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>MANAGERCODE</th>\n",
" <th>MANAGERNAME</th>\n",
" <th>COMPCODE</th>\n",
" <th>COMPNAME</th>\n",
" <th>SECURITYID</th>\n",
" <th>BEGINDATE</th>\n",
" <th>ENDDATE</th>\n",
" <th>pid</th>\n",
" <th>security_code</th>\n",
" <th>HOLDMKTCAP</th>\n",
" <th>...</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>SIZENL</th>\n",
" <th>PETTM</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>30322345</td>\n",
" <td>左金保</td>\n",
" <td>80000243</td>\n",
" <td>长信基金管理有限责任公司</td>\n",
" <td>1030008874</td>\n",
" <td>20170109</td>\n",
" <td>19000101</td>\n",
" <td>1030008874</td>\n",
" <td>2010000042</td>\n",
" <td>15282000.0</td>\n",
" <td>...</td>\n",
" <td>0.674</td>\n",
" <td>-0.954</td>\n",
" <td>2.010</td>\n",
" <td>-0.437</td>\n",
" <td>0.427</td>\n",
" <td>0.618</td>\n",
" <td>1.448</td>\n",
" <td>-0.408</td>\n",
" <td>0.589</td>\n",
" <td>8.819104</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>30322345</td>\n",
" <td>左金保</td>\n",
" <td>80000243</td>\n",
" <td>长信基金管理有限责任公司</td>\n",
" <td>1030000867</td>\n",
" <td>20150313</td>\n",
" <td>19000101</td>\n",
" <td>1030000867</td>\n",
" <td>2010000042</td>\n",
" <td>15282000.0</td>\n",
" <td>...</td>\n",
" <td>0.674</td>\n",
" <td>-0.954</td>\n",
" <td>2.010</td>\n",
" <td>-0.437</td>\n",
" <td>0.427</td>\n",
" <td>0.618</td>\n",
" <td>1.448</td>\n",
" <td>-0.408</td>\n",
" <td>0.589</td>\n",
" <td>8.819104</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>30322345</td>\n",
" <td>左金保</td>\n",
" <td>80000243</td>\n",
" <td>长信基金管理有限责任公司</td>\n",
" <td>1030004356</td>\n",
" <td>20150313</td>\n",
" <td>19000101</td>\n",
" <td>1030004356</td>\n",
" <td>2010000042</td>\n",
" <td>8591880.0</td>\n",
" <td>...</td>\n",
" <td>0.674</td>\n",
" <td>-0.954</td>\n",
" <td>2.010</td>\n",
" <td>-0.437</td>\n",
" <td>0.427</td>\n",
" <td>0.618</td>\n",
" <td>1.448</td>\n",
" <td>-0.408</td>\n",
" <td>0.589</td>\n",
" <td>8.819104</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>30322345</td>\n",
" <td>左金保</td>\n",
" <td>80000243</td>\n",
" <td>长信基金管理有限责任公司</td>\n",
" <td>1030008874</td>\n",
" <td>20170109</td>\n",
" <td>19000101</td>\n",
" <td>1030008874</td>\n",
" <td>2010000152</td>\n",
" <td>15029378.0</td>\n",
" <td>...</td>\n",
" <td>0.350</td>\n",
" <td>0.232</td>\n",
" <td>0.717</td>\n",
" <td>-0.999</td>\n",
" <td>0.083</td>\n",
" <td>-0.506</td>\n",
" <td>-0.171</td>\n",
" <td>0.337</td>\n",
" <td>-0.047</td>\n",
" <td>12.198357</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>30322345</td>\n",
" <td>左金保</td>\n",
" <td>80000243</td>\n",
" <td>长信基金管理有限责任公司</td>\n",
" <td>1030000867</td>\n",
" <td>20150313</td>\n",
" <td>19000101</td>\n",
" <td>1030000867</td>\n",
" <td>2010000152</td>\n",
" <td>15029378.0</td>\n",
" <td>...</td>\n",
" <td>0.350</td>\n",
" <td>0.232</td>\n",
" <td>0.717</td>\n",
" <td>-0.999</td>\n",
" <td>0.083</td>\n",
" <td>-0.506</td>\n",
" <td>-0.171</td>\n",
" <td>0.337</td>\n",
" <td>-0.047</td>\n",
" <td>12.198357</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>5 rows × 32 columns</p>\n",
"</div>"
],
"text/plain": [
" MANAGERCODE MANAGERNAME COMPCODE COMPNAME SECURITYID BEGINDATE \\\n",
"0 30322345 左金保 80000243 长信基金管理有限责任公司 1030008874 20170109 \n",
"1 30322345 左金保 80000243 长信基金管理有限责任公司 1030000867 20150313 \n",
"2 30322345 左金保 80000243 长信基金管理有限责任公司 1030004356 20150313 \n",
"3 30322345 左金保 80000243 长信基金管理有限责任公司 1030008874 20170109 \n",
"4 30322345 左金保 80000243 长信基金管理有限责任公司 1030000867 20150313 \n",
"\n",
" ENDDATE pid security_code HOLDMKTCAP ... MOMENTUM SIZE \\\n",
"0 19000101 1030008874 2010000042 15282000.0 ... 0.674 -0.954 \n",
"1 19000101 1030000867 2010000042 15282000.0 ... 0.674 -0.954 \n",
"2 19000101 1030004356 2010000042 8591880.0 ... 0.674 -0.954 \n",
"3 19000101 1030008874 2010000152 15029378.0 ... 0.350 0.232 \n",
"4 19000101 1030000867 2010000152 15029378.0 ... 0.350 0.232 \n",
"\n",
" EARNYILD RESVOL GROWTH BTOP LEVERAGE LIQUIDTY SIZENL PETTM \n",
"0 2.010 -0.437 0.427 0.618 1.448 -0.408 0.589 8.819104 \n",
"1 2.010 -0.437 0.427 0.618 1.448 -0.408 0.589 8.819104 \n",
"2 2.010 -0.437 0.427 0.618 1.448 -0.408 0.589 8.819104 \n",
"3 0.717 -0.999 0.083 -0.506 -0.171 0.337 -0.047 12.198357 \n",
"4 0.717 -0.999 0.083 -0.506 -0.171 0.337 -0.047 12.198357 \n",
"\n",
"[5 rows x 32 columns]"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"total_df.head()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "a6e1f341-7448-4134-aeb9-6eed0a7ffede",
"metadata": {},
"outputs": [],
"source": [
"# 计算风格暴露\n",
"\n",
"flags = total_df.rl_sub_class.isin(BOND_CLASSES)\n",
"all_risk_styles = total_df.MKTV @ total_df[RISK_STYLES]/ (1e-10 + total_df.MKTV.sum())\n",
"bond_risk_styles = total_df[flags].MKTV @ total_df[flags][RISK_STYLES][RISK_STYLES] / (1e-10 + total_df[flags].MKTV.sum())\n",
"equity_risk_styles = total_df[~flags].MKTV @ total_df[~flags][RISK_STYLES] / (1e-10 + total_df[~flags].MKTV.sum())"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "25de6b2e-6ec1-49bf-b77b-3d4cc0fad1e2",
"metadata": {},
"outputs": [],
"source": [
"# 计算行业暴露\n",
"\n",
"all_industry_group = total_df.groupby(\"industry_name\")[\"MKTV\"].sum()\n",
"all_industry_group = all_industry_group / all_industry_group.sum()\n",
"\n",
"bond_industry_group = total_df[flags].groupby(\"industry_name\")[\"MKTV\"].sum()\n",
"bond_industry_group = bond_industry_group / bond_industry_group.sum()\n",
"\n",
"equity_industry_group = total_df[~flags].groupby(\"industry_name\")[\"MKTV\"].sum()\n",
"equity_industry_group = equity_industry_group / equity_industry_group.sum()"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "3425ffb4-0be8-4a90-a0f3-07cbdc9d662d",
"metadata": {},
"outputs": [],
"source": [
"# 计算因子暴露\n",
"\n",
"all_factors_styles = total_df.MKTV @ total_df[factor_names]/ (1e-10 + total_df.MKTV.sum())\n",
"bond_factors_styles = total_df[flags].MKTV @ total_df[flags][factor_names] / (1e-10 + total_df[flags].MKTV.sum())\n",
"equity_factors_styles = total_df[~flags].MKTV @ total_df[~flags][factor_names] / (1e-10 + total_df[~flags].MKTV.sum())"
]
},
{
"cell_type": "markdown",
"id": "2348e82d-b05a-4367-80d4-a4c505c6f44d",
"metadata": {},
"source": [
"## 1.1 Put it all together"
]
},
{
"cell_type": "code",
"execution_count": 55,
"id": "bf5df7a8-22ab-4302-be8e-754b94588266",
"metadata": {},
"outputs": [],
"source": [
"def calc_manager_style_exposure(manager_ids, trade_dt, factor_names):\n",
" funds = fd_manager(manager_ids, start_date=trade_dt, end_date=trade_dt)\n",
" funds_mktv = fd_fund_mktv(funds.SECURITYID.unique(), trade_dt=trade_dt)\n",
" manager_portfolio = fd_skdetail(funds.SECURITYID.unique().tolist(), nearest_report_date(trade_dt, freq=\"S\"))\n",
" funds_rl_classification = fd_rl_classification(funds.SECURITYID.unique().tolist(), trade_dt)\n",
" risk_exp = risk_exposure(trade_dt, trade_dt, manager_portfolio.security_code.unique().tolist())\n",
" industry = sw_industry_daily(trade_dt, trade_dt, 1, manager_portfolio.security_code.unique().tolist())\n",
" factors = read_factors(manager_portfolio.security_code.unique().tolist(), trade_dt, factor_names)\n",
" \n",
" # 计算分类\n",
" total_df = pd.merge(funds, manager_portfolio[[\"pid\", \"security_code\", \"HOLDMKTCAP\"]], left_on=\"SECURITYID\", right_on=\"pid\")\n",
" total_df = pd.merge(total_df, funds_rl_classification, on=\"SECURITYID\")\n",
" total_df = pd.merge(total_df, funds_mktv, on=\"SECURITYID\")\n",
" total_df = pd.merge(total_df, industry, on=\"security_code\")\n",
" total_df = pd.merge(total_df, risk_exp[[\"security_code\"] + RISK_STYLES], on=\"security_code\")\n",
" total_df = pd.merge(total_df, factors, on=[\"security_code\", \"trade_date\"])\n",
" \n",
" def reformat(series, asset, category):\n",
" series.name = \"value\"\n",
" series.index = pd.MultiIndex.from_product([[asset], [category], series.index])\n",
" return series\n",
" \n",
" def calc(g):\n",
" flags = g.rl_sub_class.isin(BOND_CLASSES)\n",
" \n",
" # 计算风格暴露\n",
" all_risk_styles = reformat(g.MKTV @ g[RISK_STYLES]/ (1e-10 + g.MKTV.sum()), \"all\", \"risk\")\n",
" bond_risk_styles = reformat(g[flags].MKTV @ g[flags][RISK_STYLES][RISK_STYLES] / (1e-10 + g[flags].MKTV.sum()), \"bond\", \"risk\")\n",
" equity_risk_styles = reformat(g[~flags].MKTV @ g[~flags][RISK_STYLES] / (1e-10 + g[~flags].MKTV.sum()), \"equity\", \"risk\")\n",
" \n",
" # 计算行业暴露\n",
" all_industry_group = g.groupby(\"industry_name\")[\"MKTV\"].sum()\n",
" all_industry_group = reformat(all_industry_group / all_industry_group.sum(), \"all\", \"industry\")\n",
" bond_industry_group = g[flags].groupby(\"industry_name\")[\"MKTV\"].sum()\n",
" bond_industry_group = reformat(bond_industry_group / bond_industry_group.sum(), \"bond\", \"industry\")\n",
" equity_industry_group = g[~flags].groupby(\"industry_name\")[\"MKTV\"].sum()\n",
" equity_industry_group = reformat(equity_industry_group / equity_industry_group.sum(), \"equity\", \"industry\")\n",
" \n",
" # 计算因子暴露\n",
" all_factors_styles = reformat(g.MKTV @ g[factor_names]/ (1e-10 + g.MKTV.sum()), \"all\", \"factor\")\n",
" bond_factors_styles = reformat(g[flags].MKTV @ g[flags][factor_names] / (1e-10 + g[flags].MKTV.sum()), \"bond\", \"factor\")\n",
" equity_factors_styles = reformat(g[~flags].MKTV @ g[~flags][factor_names] / (1e-10 + g[~flags].MKTV.sum()), \"equity\", \"factor\")\n",
"\n",
" sd = pd.concat([bond_risk_styles, bond_risk_styles, equity_risk_styles, all_industry_group, bond_industry_group, equity_industry_group, all_factors_styles, bond_factors_styles, equity_factors_styles])\n",
" return sd\n",
" dfs = []\n",
" for k, g in total_df.groupby(\"MANAGERCODE\"):\n",
" df = calc(g).reset_index().rename(columns=dict(level_0=\"asset\", level_1=\"category\", level_2=\"name\")).drop_duplicates([\"asset\", \"category\", \"name\"])\n",
" df[\"MANAGERCODE\"] = k\n",
" dfs.append(df)\n",
" \n",
" res = pd.concat(dfs)\n",
" df = pd.merge(res, funds[[\"MANAGERCODE\", \"MANAGERNAME\"]].drop_duplicates([\"MANAGERCODE\"]), on=\"MANAGERCODE\", how=\"left\")\n",
" return df"
]
},
{
"cell_type": "code",
"execution_count": 56,
"id": "48864c00-836c-4609-8900-6bf4f0d6d447",
"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>asset</th>\n",
" <th>category</th>\n",
" <th>name</th>\n",
" <th>value</th>\n",
" <th>MANAGERCODE</th>\n",
" <th>MANAGERNAME</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>56</th>\n",
" <td>equity</td>\n",
" <td>factor</td>\n",
" <td>PETTM</td>\n",
" <td>56.222581</td>\n",
" <td>30322345</td>\n",
" <td>左金保</td>\n",
" </tr>\n",
" <tr>\n",
" <th>54</th>\n",
" <td>all</td>\n",
" <td>factor</td>\n",
" <td>PETTM</td>\n",
" <td>56.222581</td>\n",
" <td>30322345</td>\n",
" <td>左金保</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>equity</td>\n",
" <td>risk</td>\n",
" <td>MOMENTUM</td>\n",
" <td>0.888092</td>\n",
" <td>30322345</td>\n",
" <td>左金保</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>equity</td>\n",
" <td>risk</td>\n",
" <td>BETA</td>\n",
" <td>0.854896</td>\n",
" <td>30322345</td>\n",
" <td>左金保</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15</th>\n",
" <td>equity</td>\n",
" <td>risk</td>\n",
" <td>GROWTH</td>\n",
" <td>0.568967</td>\n",
" <td>30322345</td>\n",
" <td>左金保</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" asset category name value MANAGERCODE MANAGERNAME\n",
"56 equity factor PETTM 56.222581 30322345 左金保\n",
"54 all factor PETTM 56.222581 30322345 左金保\n",
"11 equity risk MOMENTUM 0.888092 30322345 左金保\n",
"10 equity risk BETA 0.854896 30322345 左金保\n",
"15 equity risk GROWTH 0.568967 30322345 左金保"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"res = calc_manager_style_exposure([\"30322345\"], trade_dt, [\"PETTM\"]).sort_index()\n",
"res.sort_values(\"value\", ascending=False).head()"
]
},
{
"cell_type": "markdown",
"id": "ef292aeb-3a1a-4d02-9afa-e79a4cd2bbc1",
"metadata": {},
"source": [
"# 2. 基金经理穿透\n",
"------------------"
]
},
{
"cell_type": "code",
"execution_count": 58,
"id": "6f7cbb5d-23ca-44cb-af55-82a97381bb76",
"metadata": {},
"outputs": [],
"source": [
"start_dt = \"2022-01-01\"\n",
"end_dt = \"2022-01-07\"\n",
"\n",
"trade_dates = [d.strftime(\"%Y%m%d\") for d in bizDatesList(\"china.sse\", start_dt, end_dt)]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "29af1c8b-fd50-494f-a909-1d43d8e57249",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2022-02-27 18:34:54.539120 : 20220104\n",
"2022-02-27 18:36:42.175333 : 20220105\n",
"2022-02-27 18:38:38.340255 : 20220106\n",
"2022-02-27 18:40:15.933234 : 20220107\n"
]
}
],
"source": [
"dfs = []\n",
"for trade_dt in trade_dates:\n",
" print(dt.datetime.now(), \":\", trade_dt)\n",
" manager_ids = fd_manager(start_date=trade_dt, end_date=trade_dt).MANAGERCODE.unique().tolist()\n",
" res = calc_manager_style_exposure(manager_ids, trade_dt, [\"PETTM\"])\n",
" res[\"trade_date\"] = trade_dt\n",
" dfs.append(res)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "64768bba-5727-41e7-9ceb-6ffcc3535c84",
"metadata": {},
"outputs": [],
"source": [
"final_report = pd.concat(dfs)\n",
"final_report[final_report.MANAGERCODE == '30322345']"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e1fe5656-1166-4934-9fc5-6e16efcdf531",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"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.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
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