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

fix utility

parent 9ba39f80
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 9,
"id": "938912de-30f4-443b-8c1d-d9168c4a1d06", "id": "938912de-30f4-443b-8c1d-d9168c4a1d06",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -12,6 +12,8 @@ ...@@ -12,6 +12,8 @@
" read_sql,\n", " read_sql,\n",
" fd_basicinfo,\n", " fd_basicinfo,\n",
" fd_typeclass,\n", " fd_typeclass,\n",
" fd_hshkiport,\n",
" fd_assetportfolio,\n",
" nearest_report_date\n", " nearest_report_date\n",
")\n", ")\n",
"import pandas as pd" "import pandas as pd"
...@@ -19,7 +21,7 @@ ...@@ -19,7 +21,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 10,
"id": "ce3b53e6-60f3-48e5-9cdd-8a92cc70c6d7", "id": "ce3b53e6-60f3-48e5-9cdd-8a92cc70c6d7",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -34,7 +36,7 @@ ...@@ -34,7 +36,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 11,
"id": "07275364-e308-433e-9c3a-fe78ad8e9daa", "id": "07275364-e308-433e-9c3a-fe78ad8e9daa",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -48,43 +50,32 @@ ...@@ -48,43 +50,32 @@
" L1CODE = 3 AND\n", " L1CODE = 3 AND\n",
" (ENDDATE>='{report_dates_begin}' or ENDDATE = '19000101') \n", " (ENDDATE>='{report_dates_begin}' or ENDDATE = '19000101') \n",
"ORDER BY SECURITYID;\n", "ORDER BY SECURITYID;\n",
"\"\"\")[\"SECURITYID\"].unique().tolist()\n", "\"\"\")[\"SECURITYID\"].unique().tolist()"
"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", "cell_type": "code",
"execution_count": 5, "execution_count": 13,
"id": "5049fc82-e1ee-476a-9fb6-79adedb6028e", "id": "5049fc82-e1ee-476a-9fb6-79adedb6028e",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"# 获取相关组合情况\n", "# 获取相关组合情况\n",
"\n", "\n",
"query = f\"\"\"\n", "asset_port = fd_assetportfolio(security_ids, report_dates_begin).fillna(0)\n",
"SELECT SECURITYID, REPORTDATE, BDRTO, CONVBDRTO, EQUITYINVERTO from TQ_FD_ASSETPORTFOLIO\n", "asset_port[\"CONVBDRTO_TO_BDRTO\"] = asset_port[\"CONVBDRTO\"] / asset_port[\"BDRTO\"] * 100"
"WHERE\n",
" REPORTDATE >= '{report_dates_begin}' AND\n",
" ISVALID = 1 AND\n",
" SECURITYID in ({sec_id_strs}) ORDER BY SECURITYID, REPORTDATE\n",
"\"\"\"\n",
"portfolios = read_sql(query).fillna(0.0)\n",
"portfolios[\"CONVBDRTO_TO_BDRTO\"] = portfolios[\"CONVBDRTO\"] / portfolios[\"BDRTO\"] * 100"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 6, "execution_count": 14,
"id": "0c2416b1-4edb-45d9-a31a-11fb70344539", "id": "0c2416b1-4edb-45d9-a31a-11fb70344539",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"last_portfolio = portfolios.groupby(\"SECURITYID\").last()[[\"BDRTO\"]]\n", "last_portfolio = asset_port.groupby(\"SECURITYID\").last()[[\"BDRTO\"]]\n",
"last_4_cov_portfolio = portfolios.groupby(\"SECURITYID\").rolling(window=4).mean().groupby(level=0).last()[[\"CONVBDRTO_TO_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 = portfolios.groupby(\"SECURITYID\").rolling(window=4).mean().groupby(level=0).last()[[\"EQUITYINVERTO\"]]" "last_4_stk_portfolio = asset_port.groupby(\"SECURITYID\").rolling(window=4).mean().groupby(level=0).last()[[\"EQUITYINVERTO\"]]"
] ]
}, },
{ {
...@@ -102,7 +93,7 @@ ...@@ -102,7 +93,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 17, "execution_count": 16,
"id": "858a98ea-e17f-4b4b-be64-becb4ce0a9b9", "id": "858a98ea-e17f-4b4b-be64-becb4ce0a9b9",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
...@@ -120,7 +111,7 @@ ...@@ -120,7 +111,7 @@
" '灵活配置型基金(股票上限95%)(A类)', '灵活配置型基金(股票上限95%)(B/C类)'], dtype=object)" " '灵活配置型基金(股票上限95%)(A类)', '灵活配置型基金(股票上限95%)(B/C类)'], dtype=object)"
] ]
}, },
"execution_count": 17, "execution_count": 16,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
...@@ -131,7 +122,7 @@ ...@@ -131,7 +122,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 24, "execution_count": 17,
"id": "894e01ee-8f44-472f-b37a-1d23fc8c57c1", "id": "894e01ee-8f44-472f-b37a-1d23fc8c57c1",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
...@@ -147,7 +138,7 @@ ...@@ -147,7 +138,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 26, "execution_count": 18,
"id": "0fddaf03-8e90-474d-9584-7a5b60c3a15d", "id": "0fddaf03-8e90-474d-9584-7a5b60c3a15d",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
...@@ -210,93 +201,93 @@ ...@@ -210,93 +201,93 @@
" <tbody>\n", " <tbody>\n",
" <tr>\n", " <tr>\n",
" <th>中长期纯债型基金</th>\n", " <th>中长期纯债型基金</th>\n",
" <td>663</td>\n", " <td>665</td>\n",
" <td>663</td>\n", " <td>665</td>\n",
" <td>648</td>\n", " <td>652</td>\n",
" <td>657</td>\n", " <td>659</td>\n",
" <td>663</td>\n", " <td>665</td>\n",
" <td>663</td>\n", " <td>665</td>\n",
" <td>663</td>\n", " <td>665</td>\n",
" <td>663</td>\n", " <td>665</td>\n",
" <td>663</td>\n", " <td>665</td>\n",
" <td>663</td>\n", " <td>665</td>\n",
" <td>663</td>\n", " <td>665</td>\n",
" <td>466</td>\n", " <td>473</td>\n",
" <td>663</td>\n", " <td>665</td>\n",
" <td>663</td>\n", " <td>665</td>\n",
" <td>663</td>\n", " <td>665</td>\n",
" </tr>\n", " </tr>\n",
" <tr>\n", " <tr>\n",
" <th>可投股票型债券基金</th>\n", " <th>可投股票型债券基金</th>\n",
" <td>403</td>\n", " <td>408</td>\n",
" <td>403</td>\n", " <td>408</td>\n",
" <td>403</td>\n", " <td>408</td>\n",
" <td>403</td>\n", " <td>408</td>\n",
" <td>403</td>\n", " <td>408</td>\n",
" <td>403</td>\n", " <td>408</td>\n",
" <td>403</td>\n", " <td>408</td>\n",
" <td>403</td>\n", " <td>408</td>\n",
" <td>403</td>\n", " <td>408</td>\n",
" <td>403</td>\n", " <td>408</td>\n",
" <td>403</td>\n", " <td>408</td>\n",
" <td>349</td>\n", " <td>348</td>\n",
" <td>403</td>\n", " <td>408</td>\n",
" <td>403</td>\n", " <td>408</td>\n",
" <td>403</td>\n", " <td>408</td>\n",
" </tr>\n", " </tr>\n",
" <tr>\n", " <tr>\n",
" <th>可投转债型债券基金</th>\n", " <th>可投转债型债券基金</th>\n",
" <td>89</td>\n", " <td>93</td>\n",
" <td>89</td>\n", " <td>93</td>\n",
" <td>89</td>\n", " <td>93</td>\n",
" <td>89</td>\n", " <td>93</td>\n",
" <td>89</td>\n", " <td>93</td>\n",
" <td>89</td>\n", " <td>93</td>\n",
" <td>89</td>\n", " <td>93</td>\n",
" <td>89</td>\n", " <td>93</td>\n",
" <td>89</td>\n", " <td>93</td>\n",
" <td>89</td>\n", " <td>93</td>\n",
" <td>89</td>\n", " <td>93</td>\n",
" <td>82</td>\n", " <td>82</td>\n",
" <td>89</td>\n", " <td>93</td>\n",
" <td>89</td>\n", " <td>93</td>\n",
" <td>89</td>\n", " <td>93</td>\n",
" </tr>\n", " </tr>\n",
" <tr>\n", " <tr>\n",
" <th>可转债基金</th>\n", " <th>可转债基金</th>\n",
" <td>52</td>\n", " <td>45</td>\n",
" <td>52</td>\n", " <td>45</td>\n",
" <td>52</td>\n", " <td>45</td>\n",
" <td>52</td>\n", " <td>45</td>\n",
" <td>52</td>\n", " <td>45</td>\n",
" <td>52</td>\n", " <td>45</td>\n",
" <td>52</td>\n", " <td>45</td>\n",
" <td>52</td>\n", " <td>45</td>\n",
" <td>52</td>\n", " <td>45</td>\n",
" <td>52</td>\n", " <td>45</td>\n",
" <td>52</td>\n", " <td>45</td>\n",
" <td>41</td>\n", " <td>39</td>\n",
" <td>52</td>\n", " <td>45</td>\n",
" <td>52</td>\n", " <td>45</td>\n",
" <td>52</td>\n", " <td>45</td>\n",
" </tr>\n", " </tr>\n",
" <tr>\n", " <tr>\n",
" <th>短期纯债型基金</th>\n", " <th>短期纯债型基金</th>\n",
" <td>65</td>\n", " <td>61</td>\n",
" <td>65</td>\n", " <td>61</td>\n",
" <td>65</td>\n", " <td>59</td>\n",
" <td>65</td>\n", " <td>61</td>\n",
" <td>65</td>\n", " <td>61</td>\n",
" <td>65</td>\n", " <td>61</td>\n",
" <td>65</td>\n", " <td>61</td>\n",
" <td>65</td>\n", " <td>61</td>\n",
" <td>65</td>\n", " <td>61</td>\n",
" <td>65</td>\n", " <td>61</td>\n",
" <td>65</td>\n", " <td>61</td>\n",
" <td>64</td>\n", " <td>60</td>\n",
" <td>65</td>\n", " <td>61</td>\n",
" <td>65</td>\n", " <td>61</td>\n",
" <td>65</td>\n", " <td>61</td>\n",
" </tr>\n", " </tr>\n",
" <tr>\n", " <tr>\n",
" <th>被动指数型债券基金</th>\n", " <th>被动指数型债券基金</th>\n",
...@@ -323,33 +314,33 @@ ...@@ -323,33 +314,33 @@
"text/plain": [ "text/plain": [
" SECURITYID BDRTO CONVBDRTO_TO_BDRTO EQUITYINVERTO L1CODE \\\n", " SECURITYID BDRTO CONVBDRTO_TO_BDRTO EQUITYINVERTO L1CODE \\\n",
"债券型(子类) \n", "债券型(子类) \n",
"中长期纯债型基金 663 663 648 657 663 \n", "中长期纯债型基金 665 665 652 659 665 \n",
"可投股票型债券基金 403 403 403 403 403 \n", "可投股票型债券基金 408 408 408 408 408 \n",
"可投转债型债券基金 89 89 89 89 89 \n", "可投转债型债券基金 93 93 93 93 93 \n",
"可转债基金 52 52 52 52 52 \n", "可转债基金 45 45 45 45 45 \n",
"短期纯债型基金 65 65 65 65 65 \n", "短期纯债型基金 61 61 59 61 61 \n",
"被动指数型债券基金 17 17 17 17 17 \n", "被动指数型债券基金 17 17 17 17 17 \n",
"\n", "\n",
" L1NAME L2CODE L2NAME L3CODE L3NAME FDNAME SNAMECOMP FSYMBOL \\\n", " L1NAME L2CODE L2NAME L3CODE L3NAME FDNAME SNAMECOMP FSYMBOL \\\n",
"债券型(子类) \n", "债券型(子类) \n",
"中长期纯债型基金 663 663 663 663 663 663 466 663 \n", "中长期纯债型基金 665 665 665 665 665 665 473 665 \n",
"可投股票型债券基金 403 403 403 403 403 403 349 403 \n", "可投股票型债券基金 408 408 408 408 408 408 348 408 \n",
"可投转债型债券基金 89 89 89 89 89 89 82 89 \n", "可投转债型债券基金 93 93 93 93 93 93 82 93 \n",
"可转债基金 52 52 52 52 52 52 41 52 \n", "可转债基金 45 45 45 45 45 45 39 45 \n",
"短期纯债型基金 65 65 65 65 65 65 64 65 \n", "短期纯债型基金 61 61 61 61 61 61 60 61 \n",
"被动指数型债券基金 17 17 17 17 17 17 11 17 \n", "被动指数型债券基金 17 17 17 17 17 17 11 17 \n",
"\n", "\n",
" FDNATURE INVESTSTYLE \n", " FDNATURE INVESTSTYLE \n",
"债券型(子类) \n", "债券型(子类) \n",
"中长期纯债型基金 663 663 \n", "中长期纯债型基金 665 665 \n",
"可投股票型债券基金 403 403 \n", "可投股票型债券基金 408 408 \n",
"可投转债型债券基金 89 89 \n", "可投转债型债券基金 93 93 \n",
"可转债基金 52 52 \n", "可转债基金 45 45 \n",
"短期纯债型基金 65 65 \n", "短期纯债型基金 61 61 \n",
"被动指数型债券基金 17 17 " "被动指数型债券基金 17 17 "
] ]
}, },
"execution_count": 26, "execution_count": 18,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
......
...@@ -101,7 +101,7 @@ def fd_assetportfolio(security_ids, report_dates_begin): ...@@ -101,7 +101,7 @@ def fd_assetportfolio(security_ids, report_dates_begin):
sec_id_strs = ",".join(["'" + s + "'" for s in security_ids]) sec_id_strs = ",".join(["'" + s + "'" for s in security_ids])
query = f""" query = f"""
SELECT SECURITYID, REPORTDATE, EQUITYINVERTO from TQ_FD_ASSETPORTFOLIO SELECT SECURITYID, REPORTDATE, BDRTO, CONVBDRTO, EQUITYINVERTO from TQ_FD_ASSETPORTFOLIO
WHERE WHERE
REPORTDATE >= '{report_dates_begin}' AND REPORTDATE >= '{report_dates_begin}' AND
ISVALID = 1 AND ISVALID = 1 AND
......
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