Commit ee1c5ca5 authored by Dr.李's avatar Dr.李

update

parent 05b0d095
......@@ -12,6 +12,7 @@
" read_sql,\n",
" fd_basicinfo,\n",
" fd_typeclass,\n",
" fd_hshkiport,\n",
" nearest_report_date\n",
")\n",
"import pandas as pd"
......@@ -26,7 +27,12 @@
"source": [
"# 获取回溯的报告日\n",
"\n",
"today = dt.datetime.today().strftime(\"%Y%m%d\")"
"# 获取回溯的报告日\n",
"\n",
"today = dt.datetime.today()\n",
"date_3yrs_ago = today.replace(year=today.year - 3)\n",
"report_dates_begin = nearest_report_date(date_3yrs_ago.strftime(\"%Y%m%d\"))\n",
"current_date = today.strftime(\"%Y%m%d\")"
]
},
{
......@@ -50,18 +56,232 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 7,
"id": "79115d35-0712-4432-a73c-29bd5262dda7",
"metadata": {},
"outputs": [],
"source": [
"basic_info = fd_basicinfo(security_ids)\n",
"type_info = fd_typeclass(security_ids)"
"basic_info = fd_basicinfo(security_ids, current_date)\n",
"type_info = fd_typeclass(security_ids, current_date)\n",
"hkport_info = fd_hshkiport(security_ids, current_date)\n",
"hkport_info = hkport_info[hkport_info.INDUSTRYNAME == \"合计\"]"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "964ecb93-784d-42b0-95b6-66e08b1c51b9",
"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>REPORTDATE</th>\n",
" <th>MVALUE</th>\n",
" <th>ACCNETMKTCAP</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1030004030</td>\n",
" <td>20200930</td>\n",
" <td>5.729386e+07</td>\n",
" <td>9.13</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1030004030</td>\n",
" <td>20201231</td>\n",
" <td>9.348727e+07</td>\n",
" <td>12.81</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1030004030</td>\n",
" <td>20210331</td>\n",
" <td>1.735264e+08</td>\n",
" <td>23.05</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1030004030</td>\n",
" <td>20210630</td>\n",
" <td>1.639447e+07</td>\n",
" <td>15.62</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>1030004030</td>\n",
" <td>20210930</td>\n",
" <td>3.454067e+06</td>\n",
" <td>32.85</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>359</th>\n",
" <td>1030009731</td>\n",
" <td>20200930</td>\n",
" <td>1.399008e+07</td>\n",
" <td>95.53</td>\n",
" </tr>\n",
" <tr>\n",
" <th>360</th>\n",
" <td>1030009731</td>\n",
" <td>20201231</td>\n",
" <td>9.791957e+06</td>\n",
" <td>95.17</td>\n",
" </tr>\n",
" <tr>\n",
" <th>361</th>\n",
" <td>1030009731</td>\n",
" <td>20210331</td>\n",
" <td>1.490032e+07</td>\n",
" <td>93.93</td>\n",
" </tr>\n",
" <tr>\n",
" <th>362</th>\n",
" <td>1030009731</td>\n",
" <td>20210630</td>\n",
" <td>2.953495e+07</td>\n",
" <td>92.27</td>\n",
" </tr>\n",
" <tr>\n",
" <th>363</th>\n",
" <td>1030009731</td>\n",
" <td>20210930</td>\n",
" <td>2.886370e+07</td>\n",
" <td>91.64</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>364 rows × 4 columns</p>\n",
"</div>"
],
"text/plain": [
" SECURITYID REPORTDATE MVALUE ACCNETMKTCAP\n",
"0 1030004030 20200930 5.729386e+07 9.13\n",
"1 1030004030 20201231 9.348727e+07 12.81\n",
"2 1030004030 20210331 1.735264e+08 23.05\n",
"3 1030004030 20210630 1.639447e+07 15.62\n",
"4 1030004030 20210930 3.454067e+06 32.85\n",
".. ... ... ... ...\n",
"359 1030009731 20200930 1.399008e+07 95.53\n",
"360 1030009731 20201231 9.791957e+06 95.17\n",
"361 1030009731 20210331 1.490032e+07 93.93\n",
"362 1030009731 20210630 2.953495e+07 92.27\n",
"363 1030009731 20210930 2.886370e+07 91.64\n",
"\n",
"[364 rows x 4 columns]"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"hkport_info.groupby([\"SECURITYID\", \"REPORTDATE\"], as_index=False).sum()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "c3ffab81-d57d-4d91-9815-fdf50cd2fa76",
"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>INDCLASSCODE</th>\n",
" <th>INDUSTRYCODE</th>\n",
" <th>INDUSTRYNAME</th>\n",
" <th>REPORTDATE</th>\n",
" <th>MVALUE</th>\n",
" <th>ACCNETMKTCAP</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>4306</th>\n",
" <td>1030009731</td>\n",
" <td>2102</td>\n",
" <td>ZZZ</td>\n",
" <td>合计</td>\n",
" <td>20210930</td>\n",
" <td>28863697.51</td>\n",
" <td>91.64</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" SECURITYID INDCLASSCODE INDUSTRYCODE INDUSTRYNAME REPORTDATE \\\n",
"4306 1030009731 2102 ZZZ 合计 20210930 \n",
"\n",
" MVALUE ACCNETMKTCAP \n",
"4306 28863697.51 91.64 "
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sss = hkport_info[hkport_info.SECURITYID == '1030009731']\n",
"\n",
"sss[sss.REPORTDATE == '20210930']"
]
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 28,
"id": "094c2bee-696b-4ebd-92ec-38d116e0acdb",
"metadata": {},
"outputs": [],
......@@ -71,7 +291,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 29,
"id": "0c30cc50-a1c3-4774-84a8-5ab5faca8b67",
"metadata": {},
"outputs": [
......@@ -83,7 +303,7 @@
" '股票型分级子基金(优先份额)', '股票型分级子基金(进取份额)', '封闭式标准股票型基金'], dtype=object)"
]
},
"execution_count": 10,
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
......@@ -94,7 +314,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 30,
"id": "c99e0fe3-7193-449e-b5ee-6cbd0b003035",
"metadata": {},
"outputs": [],
......@@ -102,13 +322,12 @@
"# 分类\n",
"df.loc[(df[\"L3NAME\"].isin([\"其他行业股票型基金\", \"偏股型基金(股票上下限60%-95%)\", '标准股票型基金', '医药行业股票型基金', '封闭式标准股票型基金'])), \"股票型(子类)\"] = \"普通股票型基金\"\n",
"df.loc[(df[\"L3NAME\"].isin([\"标准指数股票型基金\"])), \"股票型(子类)\"] = \"被动指数型基金\"\n",
"df.loc[(df[\"L3NAME\"].isin([\"增强指数股票型基金\"])), \"股票型(子类)\"] = \"增强指数型基金\"\n",
"# df.loc[(df[\"FDNATURE\"] != \"ETF\"), \"货币型(子类)\"] = \"场外货币型基金\""
"df.loc[(df[\"L3NAME\"].isin([\"增强指数股票型基金\"])), \"股票型(子类)\"] = \"增强指数型基金\""
]
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 31,
"id": "a42b357e-6b87-424b-9ca9-4ce60904b66b",
"metadata": {},
"outputs": [
......@@ -226,7 +445,7 @@
"被动指数型基金 215 344 344 344 "
]
},
"execution_count": 20,
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
......@@ -237,7 +456,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 32,
"id": "3a20a243-ebe0-4410-a3e8-3363e1e793e2",
"metadata": {},
"outputs": [
......@@ -308,7 +527,7 @@
" <td>040002</td>\n",
" <td>证券投资基金</td>\n",
" <td>指数型</td>\n",
" <td>NaN</td>\n",
" <td>增强指数型基金</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
......@@ -356,7 +575,7 @@
" <td>110003</td>\n",
" <td>证券投资基金</td>\n",
" <td>指数型</td>\n",
" <td>NaN</td>\n",
" <td>增强指数型基金</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
......@@ -475,10 +694,10 @@
"\n",
" FDNAME SNAMECOMP FSYMBOL FDNATURE INVESTSTYLE 股票型(子类) \n",
"0 国泰沪深300指数证券投资基金 国泰沪深300指数 020011 证券投资基金 指数型 被动指数型基金 \n",
"1 华安MSCI中国A股指数增强型证券投资基金 None 040002 证券投资基金 指数型 NaN \n",
"1 华安MSCI中国A股指数增强型证券投资基金 None 040002 证券投资基金 指数型 增强指数型基金 \n",
"2 博时裕富沪深300指数证券投资基金 博时沪深300指数 050002 证券投资基金 指数型 被动指数型基金 \n",
"3 博时裕富沪深300指数证券投资基金 博时沪深300指数 050002 证券投资基金 指数型 被动指数型基金 \n",
"4 易方达上证50指数增强型证券投资基金 易方达上证50增强 110003 证券投资基金 指数型 NaN \n",
"4 易方达上证50指数增强型证券投资基金 易方达上证50增强 110003 证券投资基金 指数型 增强指数型基金 \n",
".. ... ... ... ... ... ... \n",
"933 富国中证煤炭指数分级证券投资基金 富国中证煤炭指数分级 161032 证券投资基金 指数型 被动指数型基金 \n",
"934 信诚中证信息安全指数分级证券投资基金 信诚中证信息安全指数分级 165523 证券投资基金 指数型 被动指数型基金 \n",
......@@ -489,7 +708,7 @@
"[938 rows x 13 columns]"
]
},
"execution_count": 16,
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
......@@ -505,6 +724,14 @@
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "c9c557cf-2b92-402a-8c57-9711f42de08b",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
......
......@@ -36,8 +36,7 @@ def fd_basicinfo(security_ids=None, trade_dt=None):
sec_id_strs = None
else:
sec_id_strs = ",".join(["'" + s + "'" for s in security_ids])
engine = sa.create_engine(CONN, connect_args={"charset": "GBK"})
if sec_id_strs:
query = f"""
select SECURITYID, FDNAME, SNAMECOMP, FSYMBOL, FDNATURE, INVESTSTYLE from TQ_FD_BASICINFO
......@@ -53,7 +52,7 @@ def fd_basicinfo(security_ids=None, trade_dt=None):
ISVALID = 1 AND
(LIQUENDDATE >= '{trade_dt}' or LIQUENDDATE = '19000101')
"""
return pd.read_sql(query, con=engine)
return read_sql(query)
def fd_typeclass(security_ids, trade_dt=None):
......@@ -67,11 +66,45 @@ def fd_typeclass(security_ids, trade_dt=None):
else:
sec_id_strs = ",".join(["'" + s + "'" for s in security_ids])
engine = sa.create_engine(CONN, connect_args={"charset": "GBK"})
query = f"""
select SECURITYID, L1CODE, L1NAME, L2CODE, L2NAME, L3CODE, L3NAME from TQ_FD_TYPECLASS
WHERE
ISVALID = 1 AND
SECURITYID in ({sec_id_strs})
"""
return pd.read_sql(query, con=engine)
return read_sql(query)
def fd_hshkiport(security_ids, report_dates_begin):
if isinstance(security_ids, str):
sec_id_strs = security_ids
else:
sec_id_strs = ",".join(["'" + s + "'" for s in security_ids])
query = f"""
select SECODE as SECURITYID, INDCLASSCODE, INDUSTRYCODE, INDUSTRYNAME, REPORTDATE, MVALUE, ACCNETMKTCAP from TQ_FD_HSHKIPORT
WHERE
REPORTDATE >= '{report_dates_begin}' AND
ISVALID = 1 AND
INDCLASSCODE = '2102' AND
SECODE in ({sec_id_strs})
"""
return read_sql(query).sort_values("SECURITYID")
def fd_assetportfolio(security_ids, report_dates_begin):
# 获取相关组合情况
f isinstance(security_ids, str):
sec_id_strs = security_ids
else:
sec_id_strs = ",".join(["'" + s + "'" for s in security_ids])
query = f"""
SELECT SECURITYID, REPORTDATE, EQUITYINVERTO from TQ_FD_ASSETPORTFOLIO
WHERE
REPORTDATE >= '{report_dates_begin}' AND
ISVALID = 1 AND
SECURITYID in ({sec_id_strs})
"""
return = read_sql(query).sort_values("SECURITYID")
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