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

update

parent 4a42a53c
......@@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 1,
"id": "938912de-30f4-443b-8c1d-d9168c4a1d06",
"metadata": {},
"outputs": [],
......@@ -10,18 +10,22 @@
"import datetime as dt\n",
"from utility import (\n",
" read_sql,\n",
" fd_alive_funds,\n",
" fd_basicinfo,\n",
" fd_typeclass,\n",
" fd_hshkiport,\n",
" fd_assetportfolio,\n",
" fd_derieden,\n",
" nearest_report_date\n",
")\n",
"import pandas as pd"
"import pandas as pd\n",
"from PyFin.api import makeSchedule\n",
"from PyFin.api import BizDayConventions"
]
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 2,
"id": "ce3b53e6-60f3-48e5-9cdd-8a92cc70c6d7",
"metadata": {},
"outputs": [],
......@@ -36,39 +40,32 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 3,
"id": "07275364-e308-433e-9c3a-fe78ad8e9daa",
"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 = 3 AND\n",
" (ENDDATE>='{report_dates_begin}' or ENDDATE = '19000101') \n",
"ORDER BY SECURITYID;\n",
"\"\"\")[\"SECURITYID\"].unique().tolist()"
"security_ids = fd_alive_funds(report_dates_begin, 3)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 4,
"id": "5049fc82-e1ee-476a-9fb6-79adedb6028e",
"metadata": {},
"outputs": [],
"source": [
"# 获取相关组合情况\n",
"\n",
"asset_port = fd_assetportfolio(security_ids, report_dates_begin).fillna(0)\n",
"asset_port = fd_assetportfolio(security_ids, report_dates_begin, current_date).fillna(0)\n",
"asset_port[\"CONVBDRTO_TO_BDRTO\"] = asset_port[\"CONVBDRTO\"] / asset_port[\"BDRTO\"] * 100"
]
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 5,
"id": "0c2416b1-4edb-45d9-a31a-11fb70344539",
"metadata": {},
"outputs": [],
......@@ -80,7 +77,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 6,
"id": "7810ff27-b4b0-4b30-a77a-c3e6d531e1ae",
"metadata": {},
"outputs": [],
......@@ -93,7 +90,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 7,
"id": "858a98ea-e17f-4b4b-be64-becb4ce0a9b9",
"metadata": {},
"outputs": [
......@@ -101,17 +98,14 @@
"data": {
"text/plain": [
"array(['普通债券型基金(一级A类)', '普通债券型基金(一级B/C类)', '普通债券型基金(二级A类)',\n",
" '普通债券型基金(二级B/C类)', '中短期标准债券型基金', '普通债券型基金(可投转债A类)', '保本型基金',\n",
" '普通债券型基金(可投转债B类)', '指数债券型基金(A类)', '封闭式普通债券型基金(一级)(A类)',\n",
" '长期标准债券型基金(A类)', '封闭式普通债券型基金(二级)(A类)', '可转换债券型基金(A类)',\n",
" '普通债券型基金(二级B/C类)', '中短期标准债券型基金', '普通债券型基金(可投转债A类)',\n",
" '普通债券型基金(可投转债B类)', '指数债券型基金(A类)', '长期标准债券型基金(A类)', '可转换债券型基金(A类)',\n",
" '可转换债券型基金(B/C类)', '债券型分级子基金(优先份额)', '债券型分级子基金(进取份额)',\n",
" '指数债券型基金(B/C类)', '封闭式债券型分级子基金(优先份额)', '封闭式长期标准债券型基金(A类)',\n",
" '长期标准债券型基金(B/C类)', '短期理财债券型基金(A类)', '短期理财债券型基金(B/C类)',\n",
" '封闭式普通债券型基金(可投转债)(A类)', '灵活策略基金(A类)', '封闭式长期标准债券型基金(B/C类)',\n",
" '灵活配置型基金(股票上限95%)(A类)', '灵活配置型基金(股票上限95%)(B/C类)'], dtype=object)"
" '指数债券型基金(B/C类)', '长期标准债券型基金(B/C类)', '短期理财债券型基金(A类)',\n",
" '短期理财债券型基金(B/C类)'], dtype=object)"
]
},
"execution_count": 16,
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
......@@ -122,7 +116,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 8,
"id": "894e01ee-8f44-472f-b37a-1d23fc8c57c1",
"metadata": {},
"outputs": [],
......@@ -138,7 +132,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 9,
"id": "0fddaf03-8e90-474d-9584-7a5b60c3a15d",
"metadata": {},
"outputs": [
......@@ -201,93 +195,93 @@
" <tbody>\n",
" <tr>\n",
" <th>中长期纯债型基金</th>\n",
" <td>665</td>\n",
" <td>665</td>\n",
" <td>652</td>\n",
" <td>659</td>\n",
" <td>665</td>\n",
" <td>665</td>\n",
" <td>665</td>\n",
" <td>665</td>\n",
" <td>665</td>\n",
" <td>665</td>\n",
" <td>665</td>\n",
" <td>473</td>\n",
" <td>665</td>\n",
" <td>665</td>\n",
" <td>665</td>\n",
" <td>638</td>\n",
" <td>638</td>\n",
" <td>625</td>\n",
" <td>632</td>\n",
" <td>638</td>\n",
" <td>638</td>\n",
" <td>638</td>\n",
" <td>638</td>\n",
" <td>638</td>\n",
" <td>638</td>\n",
" <td>638</td>\n",
" <td>450</td>\n",
" <td>638</td>\n",
" <td>638</td>\n",
" <td>638</td>\n",
" </tr>\n",
" <tr>\n",
" <th>可投股票型债券基金</th>\n",
" <td>408</td>\n",
" <td>408</td>\n",
" <td>408</td>\n",
" <td>408</td>\n",
" <td>408</td>\n",
" <td>408</td>\n",
" <td>408</td>\n",
" <td>408</td>\n",
" <td>408</td>\n",
" <td>408</td>\n",
" <td>408</td>\n",
" <td>348</td>\n",
" <td>408</td>\n",
" <td>408</td>\n",
" <td>408</td>\n",
" <td>370</td>\n",
" <td>370</td>\n",
" <td>370</td>\n",
" <td>370</td>\n",
" <td>370</td>\n",
" <td>370</td>\n",
" <td>370</td>\n",
" <td>370</td>\n",
" <td>370</td>\n",
" <td>370</td>\n",
" <td>370</td>\n",
" <td>321</td>\n",
" <td>370</td>\n",
" <td>370</td>\n",
" <td>370</td>\n",
" </tr>\n",
" <tr>\n",
" <th>可投转债型债券基金</th>\n",
" <td>93</td>\n",
" <td>93</td>\n",
" <td>93</td>\n",
" <td>93</td>\n",
" <td>93</td>\n",
" <td>93</td>\n",
" <td>93</td>\n",
" <td>93</td>\n",
" <td>93</td>\n",
" <td>93</td>\n",
" <td>93</td>\n",
" <td>82</td>\n",
" <td>93</td>\n",
" <td>93</td>\n",
" <td>93</td>\n",
" <td>87</td>\n",
" <td>87</td>\n",
" <td>87</td>\n",
" <td>87</td>\n",
" <td>87</td>\n",
" <td>87</td>\n",
" <td>87</td>\n",
" <td>87</td>\n",
" <td>87</td>\n",
" <td>87</td>\n",
" <td>87</td>\n",
" <td>77</td>\n",
" <td>87</td>\n",
" <td>87</td>\n",
" <td>87</td>\n",
" </tr>\n",
" <tr>\n",
" <th>可转债基金</th>\n",
" <td>45</td>\n",
" <td>45</td>\n",
" <td>45</td>\n",
" <td>45</td>\n",
" <td>45</td>\n",
" <td>45</td>\n",
" <td>45</td>\n",
" <td>45</td>\n",
" <td>45</td>\n",
" <td>45</td>\n",
" <td>45</td>\n",
" <td>39</td>\n",
" <td>45</td>\n",
" <td>45</td>\n",
" <td>45</td>\n",
" <td>41</td>\n",
" <td>41</td>\n",
" <td>41</td>\n",
" <td>41</td>\n",
" <td>41</td>\n",
" <td>41</td>\n",
" <td>41</td>\n",
" <td>41</td>\n",
" <td>41</td>\n",
" <td>41</td>\n",
" <td>41</td>\n",
" <td>36</td>\n",
" <td>41</td>\n",
" <td>41</td>\n",
" <td>41</td>\n",
" </tr>\n",
" <tr>\n",
" <th>短期纯债型基金</th>\n",
" <td>61</td>\n",
" <td>61</td>\n",
" <td>59</td>\n",
" <td>61</td>\n",
" <td>61</td>\n",
" <td>61</td>\n",
" <td>61</td>\n",
" <td>61</td>\n",
" <td>61</td>\n",
" <td>61</td>\n",
" <td>61</td>\n",
" <td>60</td>\n",
" <td>61</td>\n",
" <td>61</td>\n",
" <td>61</td>\n",
" <td>55</td>\n",
" <td>55</td>\n",
" <td>54</td>\n",
" <td>55</td>\n",
" <td>55</td>\n",
" <td>55</td>\n",
" <td>55</td>\n",
" <td>55</td>\n",
" <td>55</td>\n",
" <td>55</td>\n",
" <td>55</td>\n",
" <td>54</td>\n",
" <td>55</td>\n",
" <td>55</td>\n",
" <td>55</td>\n",
" </tr>\n",
" <tr>\n",
" <th>被动指数型债券基金</th>\n",
......@@ -314,33 +308,33 @@
"text/plain": [
" SECURITYID BDRTO CONVBDRTO_TO_BDRTO EQUITYINVERTO L1CODE \\\n",
"债券型(子类) \n",
"中长期纯债型基金 665 665 652 659 665 \n",
"可投股票型债券基金 408 408 408 408 408 \n",
"可投转债型债券基金 93 93 93 93 93 \n",
"可转债基金 45 45 45 45 45 \n",
"短期纯债型基金 61 61 59 61 61 \n",
"中长期纯债型基金 638 638 625 632 638 \n",
"可投股票型债券基金 370 370 370 370 370 \n",
"可投转债型债券基金 87 87 87 87 87 \n",
"可转债基金 41 41 41 41 41 \n",
"短期纯债型基金 55 55 54 55 55 \n",
"被动指数型债券基金 17 17 17 17 17 \n",
"\n",
" L1NAME L2CODE L2NAME L3CODE L3NAME FDNAME SNAMECOMP FSYMBOL \\\n",
"债券型(子类) \n",
"中长期纯债型基金 665 665 665 665 665 665 473 665 \n",
"可投股票型债券基金 408 408 408 408 408 408 348 408 \n",
"可投转债型债券基金 93 93 93 93 93 93 82 93 \n",
"可转债基金 45 45 45 45 45 45 39 45 \n",
"短期纯债型基金 61 61 61 61 61 61 60 61 \n",
"中长期纯债型基金 638 638 638 638 638 638 450 638 \n",
"可投股票型债券基金 370 370 370 370 370 370 321 370 \n",
"可投转债型债券基金 87 87 87 87 87 87 77 87 \n",
"可转债基金 41 41 41 41 41 41 36 41 \n",
"短期纯债型基金 55 55 55 55 55 55 54 55 \n",
"被动指数型债券基金 17 17 17 17 17 17 11 17 \n",
"\n",
" FDNATURE INVESTSTYLE \n",
"债券型(子类) \n",
"中长期纯债型基金 665 665 \n",
"可投股票型债券基金 408 408 \n",
"可投转债型债券基金 93 93 \n",
"可转债基金 45 45 \n",
"短期纯债型基金 61 61 \n",
"中长期纯债型基金 638 638 \n",
"可投股票型债券基金 370 370 \n",
"可投转债型债券基金 87 87 \n",
"可转债基金 41 41 \n",
"短期纯债型基金 55 55 \n",
"被动指数型债券基金 17 17 "
]
},
"execution_count": 18,
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
......@@ -349,13 +343,378 @@
"agg_port.groupby(\"债券型(子类)\").count()"
]
},
{
"cell_type": "markdown",
"id": "eb47d745-b1ae-46f4-bfe1-81243474c576",
"metadata": {},
"source": [
"## 1.1 Put it all totether"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "1985799f-9a72-4f53-a09b-e15c738f211f",
"metadata": {},
"outputs": [],
"source": [
"def create_bond_fund_info(trade_dt):\n",
" trade_dt = dt.datetime.strptime(trade_dt, \"%Y%m%d\")\n",
" date_3yrs_ago = trade_dt.replace(year=trade_dt.year - 3)\n",
" report_dates_begin = nearest_report_date(date_3yrs_ago.strftime(\"%Y%m%d\"))\n",
" current_date = trade_dt.strftime(\"%Y%m%d\")\n",
" \n",
" security_ids = fd_alive_funds(report_dates_begin, 3)\n",
" basic_info = fd_basicinfo(security_ids, current_date)\n",
" type_info = fd_typeclass(security_ids, current_date)\n",
" asset_port = fd_assetportfolio(security_ids, report_dates_begin, current_date).fillna(0)\n",
" asset_port[\"CONVBDRTO_TO_BDRTO\"] = asset_port[\"CONVBDRTO\"] / asset_port[\"BDRTO\"] * 100\n",
" last_portfolio = asset_port.groupby(\"SECURITYID\").last()[[\"BDRTO\"]]\n",
" last_4_cov_portfolio = asset_port.groupby(\"SECURITYID\").rolling(window=4).mean().groupby(level=0).last()[[\"CONVBDRTO_TO_BDRTO\"]]\n",
" last_4_stk_portfolio = asset_port.groupby(\"SECURITYID\").rolling(window=4).mean().groupby(level=0).last()[[\"EQUITYINVERTO\"]]\n",
"\n",
" df = pd.merge(basic_info, type_info, on=\"SECURITYID\")\n",
" df = pd.merge(df, last_portfolio, on=\"SECURITYID\", how=\"left\").drop_duplicates()\n",
" df = pd.merge(df, last_4_stk_portfolio, on=\"SECURITYID\", how=\"left\").drop_duplicates()\n",
" df = pd.merge(df, last_4_cov_portfolio, on=\"SECURITYID\", how=\"left\").drop_duplicates()\n",
" \n",
" df.loc[(df[\"BDRTO\"] >= 80) & (df.L2NAME == \"短期理财债券型基金\"), \"债券型(子类)\"] = \"短期纯债型基金\"\n",
" df.loc[(df[\"BDRTO\"] >= 80) & (df[\"CONVBDRTO_TO_BDRTO\"] >= 80), \"债券型(子类)\"] = \"可转债基金\"\n",
" df.loc[(df[\"BDRTO\"] >= 80) & (df[\"CONVBDRTO_TO_BDRTO\"] >= 5) & (df[\"CONVBDRTO_TO_BDRTO\"] < 80) & (df[\"EQUITYINVERTO\"] <= 0.1), \"债券型(子类)\"] = \"可投转债型债券基金\"\n",
" df.loc[(df[\"BDRTO\"] >= 80) & (df[\"EQUITYINVERTO\"] >= 0.1) & (~df[\"债券型(子类)\"].isin([\"可投转债型债券基金\", \"可转债基金\"])), \"债券型(子类)\"] = \"可投股票型债券基金\"\n",
" df.loc[(df[\"BDRTO\"] >= 80) & (df[\"L2NAME\"] == \"指数债券型基金\") & (~df[\"债券型(子类)\"].isin([\"可投转债型债券基金\", \"可转债基金\"])), \"债券型(子类)\"] = \"被动指数型债券基金\"\n",
" df.loc[pd.isnull(df[\"债券型(子类)\"]), \"债券型(子类)\"] = \"中长期纯债型基金\"\n",
" \n",
" return df"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "992ce6ba-b2eb-4e11-b025-53bd78366ff0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Wall time: 7.81 s\n"
]
}
],
"source": [
"%%time\n",
"\n",
"df = create_bond_fund_info(\"20211111\")#.groupby( \"债券型(子类)\")"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "514f4c0b-bd1e-430b-9c5a-721bec381dc0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Wall time: 6.12 s\n"
]
},
{
"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>FDNAME</th>\n",
" <th>SNAMECOMP</th>\n",
" <th>FSYMBOL</th>\n",
" <th>FDNATURE</th>\n",
" <th>INVESTSTYLE</th>\n",
" <th>L1CODE</th>\n",
" <th>L1NAME</th>\n",
" <th>L2CODE</th>\n",
" <th>L2NAME</th>\n",
" <th>L3CODE</th>\n",
" <th>L3NAME</th>\n",
" <th>BDRTO</th>\n",
" <th>EQUITYINVERTO</th>\n",
" <th>CONVBDRTO_TO_BDRTO</th>\n",
" </tr>\n",
" <tr>\n",
" <th>债券型(子类)</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>中长期纯债型基金</th>\n",
" <td>655</td>\n",
" <td>655</td>\n",
" <td>465</td>\n",
" <td>655</td>\n",
" <td>655</td>\n",
" <td>655</td>\n",
" <td>655</td>\n",
" <td>655</td>\n",
" <td>655</td>\n",
" <td>655</td>\n",
" <td>655</td>\n",
" <td>655</td>\n",
" <td>639</td>\n",
" <td>633</td>\n",
" <td>626</td>\n",
" </tr>\n",
" <tr>\n",
" <th>可投股票型债券基金</th>\n",
" <td>370</td>\n",
" <td>370</td>\n",
" <td>321</td>\n",
" <td>370</td>\n",
" <td>370</td>\n",
" <td>370</td>\n",
" <td>370</td>\n",
" <td>370</td>\n",
" <td>370</td>\n",
" <td>370</td>\n",
" <td>370</td>\n",
" <td>370</td>\n",
" <td>370</td>\n",
" <td>370</td>\n",
" <td>370</td>\n",
" </tr>\n",
" <tr>\n",
" <th>可投转债型债券基金</th>\n",
" <td>87</td>\n",
" <td>87</td>\n",
" <td>77</td>\n",
" <td>87</td>\n",
" <td>87</td>\n",
" <td>87</td>\n",
" <td>87</td>\n",
" <td>87</td>\n",
" <td>87</td>\n",
" <td>87</td>\n",
" <td>87</td>\n",
" <td>87</td>\n",
" <td>87</td>\n",
" <td>87</td>\n",
" <td>87</td>\n",
" </tr>\n",
" <tr>\n",
" <th>可转债基金</th>\n",
" <td>41</td>\n",
" <td>41</td>\n",
" <td>36</td>\n",
" <td>41</td>\n",
" <td>41</td>\n",
" <td>41</td>\n",
" <td>41</td>\n",
" <td>41</td>\n",
" <td>41</td>\n",
" <td>41</td>\n",
" <td>41</td>\n",
" <td>41</td>\n",
" <td>41</td>\n",
" <td>41</td>\n",
" <td>41</td>\n",
" </tr>\n",
" <tr>\n",
" <th>短期纯债型基金</th>\n",
" <td>55</td>\n",
" <td>55</td>\n",
" <td>54</td>\n",
" <td>55</td>\n",
" <td>55</td>\n",
" <td>55</td>\n",
" <td>55</td>\n",
" <td>55</td>\n",
" <td>55</td>\n",
" <td>55</td>\n",
" <td>55</td>\n",
" <td>55</td>\n",
" <td>55</td>\n",
" <td>55</td>\n",
" <td>54</td>\n",
" </tr>\n",
" <tr>\n",
" <th>被动指数型债券基金</th>\n",
" <td>17</td>\n",
" <td>17</td>\n",
" <td>11</td>\n",
" <td>17</td>\n",
" <td>17</td>\n",
" <td>17</td>\n",
" <td>17</td>\n",
" <td>17</td>\n",
" <td>17</td>\n",
" <td>17</td>\n",
" <td>17</td>\n",
" <td>17</td>\n",
" <td>17</td>\n",
" <td>17</td>\n",
" <td>17</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" SECURITYID FDNAME SNAMECOMP FSYMBOL FDNATURE INVESTSTYLE \\\n",
"债券型(子类) \n",
"中长期纯债型基金 655 655 465 655 655 655 \n",
"可投股票型债券基金 370 370 321 370 370 370 \n",
"可投转债型债券基金 87 87 77 87 87 87 \n",
"可转债基金 41 41 36 41 41 41 \n",
"短期纯债型基金 55 55 54 55 55 55 \n",
"被动指数型债券基金 17 17 11 17 17 17 \n",
"\n",
" L1CODE L1NAME L2CODE L2NAME L3CODE L3NAME BDRTO \\\n",
"债券型(子类) \n",
"中长期纯债型基金 655 655 655 655 655 655 639 \n",
"可投股票型债券基金 370 370 370 370 370 370 370 \n",
"可投转债型债券基金 87 87 87 87 87 87 87 \n",
"可转债基金 41 41 41 41 41 41 41 \n",
"短期纯债型基金 55 55 55 55 55 55 55 \n",
"被动指数型债券基金 17 17 17 17 17 17 17 \n",
"\n",
" EQUITYINVERTO CONVBDRTO_TO_BDRTO \n",
"债券型(子类) \n",
"中长期纯债型基金 633 626 \n",
"可投股票型债券基金 370 370 \n",
"可投转债型债券基金 87 87 \n",
"可转债基金 41 41 \n",
"短期纯债型基金 55 54 \n",
"被动指数型债券基金 17 17 "
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%time\n",
"\n",
"create_bond_fund_info(\"20211111\").groupby( \"债券型(子类)\").count()"
]
},
{
"cell_type": "markdown",
"id": "2128b616-dfce-497f-abec-6023d2e7ada9",
"metadata": {},
"source": [
"# 2. 基金指数\n",
"--------------------"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "bc46497a-3044-4455-b427-5e461acffc71",
"metadata": {},
"outputs": [],
"source": [
"# 我们只计算指数的每日收益\n",
"# 在确定指数的基准日之后,可以直接使用收益计算指数的值。\n",
"# 使用 TQ_FD_DERIVEDN 获取基金的净值情况\n",
"\n",
"start_dt = \"2015-01-31\"\n",
"final_trade_dt = \"2021-11-09\"\n",
"nav_type = \"REPAIRUNITNAV\"\n",
"\n",
"rebalance_dates = [d.strftime(\"%Y%m%d\") for d in makeSchedule(start_dt, final_trade_dt, tenor=\"3M\", calendar=\"china.sse\", dateRule=BizDayConventions.ModifiedFollowing)]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4555a339-5ad5-4158-81a0-4011ab35d755",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"20150130 20150430\n",
"20150430 20150731\n",
"20150731 20151030\n",
"20151030 20160129\n",
"20160129 20160429\n",
"20160429 20160729\n",
"20160729 20161031\n",
"20161031 20170126\n",
"20170126 20170428\n",
"20170428 20170731\n",
"20170731 20171031\n",
"20171031 20180131\n",
"20180131 20180427\n",
"20180427 20180731\n",
"20180731 20181031\n",
"20181031 20190131\n",
"20190131 20190430\n",
"20190430 20190731\n"
]
}
],
"source": [
"dfs = []\n",
"\n",
"for i, trade_dt in enumerate(rebalance_dates[1:]):\n",
" pre_trade_dt = rebalance_dates[i]\n",
" print(pre_trade_dt, trade_dt)\n",
" fund_info = create_bond_fund_info(trade_dt)\n",
" pre_nav_info = fd_derieden(fund_info.SECURITYID.tolist(), pre_trade_dt).rename(columns={nav_type: \"PRE\" + nav_type})\n",
" nav_info = fd_derieden(fund_info.SECURITYID.tolist(), trade_dt)\n",
"\n",
" total_df = pd.merge(fund_info, nav_info, on=[\"SECURITYID\"])\n",
" total_df = pd.merge(total_df, pre_nav_info, on=[\"SECURITYID\"])\n",
" total_df[\"chg.\"] = total_df[nav_type] / total_df[\"PRE\" + nav_type] - 1.0\n",
" res = total_df.groupby(\"债券型(子类)\")[[\"chg.\"]].mean()\n",
" dfs.append(res)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bb0108f9-c5be-459e-8ae0-d6bda4ecb5ee",
"id": "f1f60b62-c7c8-4295-9b3c-4fbbc7bc26bd",
"metadata": {},
"outputs": [],
"source": []
"source": [
"final_report = pd.concat(dfs, keys=rebalance_dates[1:]).reset_index()\n",
"final_report.pivot_table(index=\"level_0\", columns=\"债券型(子类)\", values=\"chg.\").to_excel(\"030_债券型基金_bak.xlsx\")"
]
}
],
"metadata": {
......
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