Commit 98ba1063 authored by Dr.李's avatar Dr.李

added first

parents
{
"cells": [
{
"cell_type": "code",
"execution_count": 7,
"id": "25a5db3d-4c9d-46e8-bd5a-88cca84280ba",
"metadata": {},
"outputs": [],
"source": [
"import datetime as dt\n",
"from utility import (\n",
" read_sql,\n",
" fd_basicinfo,\n",
" nearest_report_date\n",
")\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "329e6a8c-4e60-43d7-8314-63cee4f9196a",
"metadata": {},
"outputs": [],
"source": [
"# 获取回溯的报告日\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\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "74246462-2676-4817-8c89-3a024ed8e412",
"metadata": {},
"outputs": [],
"source": [
"# 获取所有目标混合型基金(清算结束日期晚于当前日)\n",
"\n",
"security_ids = read_sql(f\"\"\"\n",
"select SECURITYID from TQ_FD_TYPECLASS \n",
"WHERE\n",
" ISVALID = 1 AND\n",
" L1CODE = 2 AND\n",
" (ENDDATE>='{report_dates_begin}' or ENDDATE = '19000101') \n",
"ORDER BY SECURITYID;\n",
"\"\"\")[\"SECURITYID\"].unique().tolist()\n",
"sec_id_strs = \",\".join([\"'\" + s + \"'\" for s in security_ids])\n",
"\n",
"not_liq_sec_ids = fd_basicinfo(security_ids, current_date)[\"SECURITYID\"].unique().tolist()\n",
"sec_id_strs = \",\".join([\"'\" + s + \"'\" for s in not_liq_sec_ids])"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "61270089-05d5-4396-829e-651118231b60",
"metadata": {},
"outputs": [],
"source": [
"# 获取相关组合情况\n",
"\n",
"query = f\"\"\"\n",
"SELECT SECURITYID, REPORTDATE, EQUITYINVERTO from TQ_FD_ASSETPORTFOLIO\n",
"WHERE\n",
" REPORTDATE >= '{report_dates_begin}' AND\n",
" ISVALID = 1 AND\n",
" SECURITYID in ({sec_id_strs})\n",
"\"\"\"\n",
"portfolios = read_sql(query).sort_values(\"SECURITYID\")\n",
"agg_port = portfolios.groupby(\"SECURITYID\", as_index=False).mean()\n",
"security_ids_has_port = agg_port.SECURITYID.tolist()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "73250ba3-83f4-41fe-8e61-2869f7db8662",
"metadata": {},
"outputs": [],
"source": [
"# 分类\n",
"agg_port.loc[agg_port[\"EQUITYINVERTO\"] >= 70, \"混合型(子类)\"] = \"高权益仓位混合型基金\"\n",
"agg_port.loc[((agg_port[\"EQUITYINVERTO\"] >= 50) & (agg_port[\"EQUITYINVERTO\"] < 70)), \"混合型(子类)\"] = \"中高权益仓位混合型基金\"\n",
"agg_port.loc[((agg_port[\"EQUITYINVERTO\"] >= 30) & (agg_port[\"EQUITYINVERTO\"] < 50)), \"混合型(子类)\"] = \"中权益仓位混合型基金\"\n",
"agg_port.loc[((agg_port[\"EQUITYINVERTO\"] >= 0) & (agg_port[\"EQUITYINVERTO\"] < 30)), \"混合型(子类)\"] = \"低权益仓位混合型基金\""
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "87566cc8-5684-4c03-b247-3fb9abf72cf6",
"metadata": {},
"outputs": [],
"source": [
"agg_port = pd.merge(agg_port, fd_basicinfo(agg_port.SECURITYID, current_date))"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "89e23e81-3398-40ab-a410-584b659f5db1",
"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>EQUITYINVERTO</th>\n",
" <th>混合型(子类)</th>\n",
" <th>FDNAME</th>\n",
" <th>SNAMECOMP</th>\n",
" <th>FSYMBOL</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>40</th>\n",
" <td>1030000079</td>\n",
" <td>17.740000</td>\n",
" <td>低权益仓位混合型基金</td>\n",
" <td>大成财富管理2020生命周期证券投资基金</td>\n",
" <td>None</td>\n",
" <td>090006</td>\n",
" </tr>\n",
" <tr>\n",
" <th>54</th>\n",
" <td>1030000112</td>\n",
" <td>24.380000</td>\n",
" <td>低权益仓位混合型基金</td>\n",
" <td>国投瑞银融华债券型证券投资基金</td>\n",
" <td>None</td>\n",
" <td>121001</td>\n",
" </tr>\n",
" <tr>\n",
" <th>62</th>\n",
" <td>1030000124</td>\n",
" <td>19.025833</td>\n",
" <td>低权益仓位混合型基金</td>\n",
" <td>银河收益证券投资基金</td>\n",
" <td>None</td>\n",
" <td>151002</td>\n",
" </tr>\n",
" <tr>\n",
" <th>94</th>\n",
" <td>1030000180</td>\n",
" <td>26.726667</td>\n",
" <td>低权益仓位混合型基金</td>\n",
" <td>南方宝元债券型基金</td>\n",
" <td>南方宝元债券</td>\n",
" <td>202101</td>\n",
" </tr>\n",
" <tr>\n",
" <th>95</th>\n",
" <td>1030000185</td>\n",
" <td>1.710000</td>\n",
" <td>低权益仓位混合型基金</td>\n",
" <td>鹏华弘泰灵活配置混合型证券投资基金</td>\n",
" <td>鹏华弘泰灵活配置混合</td>\n",
" <td>206001</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",
" </tr>\n",
" <tr>\n",
" <th>2067</th>\n",
" <td>1030009757</td>\n",
" <td>23.222500</td>\n",
" <td>低权益仓位混合型基金</td>\n",
" <td>东方价值挖掘灵活配置混合型证券投资基金</td>\n",
" <td>东方价值挖掘灵活配置混合</td>\n",
" <td>004166</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2070</th>\n",
" <td>1030009810</td>\n",
" <td>22.155833</td>\n",
" <td>低权益仓位混合型基金</td>\n",
" <td>易方达瑞富灵活配置混合型证券投资基金</td>\n",
" <td>易方达瑞富混合</td>\n",
" <td>001745</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2071</th>\n",
" <td>1030009811</td>\n",
" <td>22.155833</td>\n",
" <td>低权益仓位混合型基金</td>\n",
" <td>易方达瑞富灵活配置混合型证券投资基金</td>\n",
" <td>易方达瑞富混合</td>\n",
" <td>001746</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2072</th>\n",
" <td>1030009813</td>\n",
" <td>21.400833</td>\n",
" <td>低权益仓位混合型基金</td>\n",
" <td>南方安康混合型证券投资基金</td>\n",
" <td>None</td>\n",
" <td>004517</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2081</th>\n",
" <td>1030013275</td>\n",
" <td>7.011667</td>\n",
" <td>低权益仓位混合型基金</td>\n",
" <td>易方达裕惠回报定期开放式混合型发起式证券投资基金</td>\n",
" <td>None</td>\n",
" <td>000436</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>508 rows × 6 columns</p>\n",
"</div>"
],
"text/plain": [
" SECURITYID EQUITYINVERTO 混合型(子类) FDNAME \\\n",
"40 1030000079 17.740000 低权益仓位混合型基金 大成财富管理2020生命周期证券投资基金 \n",
"54 1030000112 24.380000 低权益仓位混合型基金 国投瑞银融华债券型证券投资基金 \n",
"62 1030000124 19.025833 低权益仓位混合型基金 银河收益证券投资基金 \n",
"94 1030000180 26.726667 低权益仓位混合型基金 南方宝元债券型基金 \n",
"95 1030000185 1.710000 低权益仓位混合型基金 鹏华弘泰灵活配置混合型证券投资基金 \n",
"... ... ... ... ... \n",
"2067 1030009757 23.222500 低权益仓位混合型基金 东方价值挖掘灵活配置混合型证券投资基金 \n",
"2070 1030009810 22.155833 低权益仓位混合型基金 易方达瑞富灵活配置混合型证券投资基金 \n",
"2071 1030009811 22.155833 低权益仓位混合型基金 易方达瑞富灵活配置混合型证券投资基金 \n",
"2072 1030009813 21.400833 低权益仓位混合型基金 南方安康混合型证券投资基金 \n",
"2081 1030013275 7.011667 低权益仓位混合型基金 易方达裕惠回报定期开放式混合型发起式证券投资基金 \n",
"\n",
" SNAMECOMP FSYMBOL \n",
"40 None 090006 \n",
"54 None 121001 \n",
"62 None 151002 \n",
"94 南方宝元债券 202101 \n",
"95 鹏华弘泰灵活配置混合 206001 \n",
"... ... ... \n",
"2067 东方价值挖掘灵活配置混合 004166 \n",
"2070 易方达瑞富混合 001745 \n",
"2071 易方达瑞富混合 001746 \n",
"2072 None 004517 \n",
"2081 None 000436 \n",
"\n",
"[508 rows x 6 columns]"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"agg_port.loc[agg_port[\"混合型(子类)\"] == \"低权益仓位混合型基金\", :]"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "a27e2473-ddf5-4883-bb50-6fe46914d3da",
"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>FSYMBOL</th>\n",
" </tr>\n",
" <tr>\n",
" <th>混合型(子类)</th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>中权益仓位混合型基金</th>\n",
" <td>144</td>\n",
" </tr>\n",
" <tr>\n",
" <th>中高权益仓位混合型基金</th>\n",
" <td>197</td>\n",
" </tr>\n",
" <tr>\n",
" <th>低权益仓位混合型基金</th>\n",
" <td>508</td>\n",
" </tr>\n",
" <tr>\n",
" <th>高权益仓位混合型基金</th>\n",
" <td>1205</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" FSYMBOL\n",
"混合型(子类) \n",
"中权益仓位混合型基金 144\n",
"中高权益仓位混合型基金 197\n",
"低权益仓位混合型基金 508\n",
"高权益仓位混合型基金 1205"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"agg_port.groupby(\"混合型(子类)\")[[\"FSYMBOL\"]].count()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6ee9ba23-6c27-4fbf-bd56-ed95404c4bee",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "653f4b5e-1063-4b2a-b03d-b35146a82cfc",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:root] *",
"language": "python",
"name": "conda-root-py"
},
"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.8.12"
}
},
"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