Skip to main content

financial_api/fund/
portfolio.rs

1use crate::endpoints;
2use crate::{
3    Client, Error, FundAssetAllocationData, FundHoldingsData, FundIndustryAllocationData,
4    FundPortfolioHistoryData, FundReportDatesData, FundType, NaturalDate, ReportType, Response,
5};
6
7use super::fund_target_query;
8
9impl Client {
10    /// 获取定期披露的基金持仓及汇总比例。
11    ///
12    /// # 示例
13    #[doc = concat!(
14        "```no_run\n",
15        include_str!("../../examples/fund_portfolio_holdings.rs"),
16        "\n```"
17    )]
18    pub async fn fund_portfolio_holdings(
19        &self,
20        fund_type: FundType,
21        thscode: impl AsRef<str> + Send,
22    ) -> Result<Response<FundHoldingsData>, Error> {
23        self.fund_detail(endpoints::FUND_HOLDINGS, fund_type, thscode)
24            .await
25    }
26
27    /// 获取历史披露的股票持仓。
28    ///
29    /// # 示例
30    #[doc = concat!(
31        "```no_run\n",
32        include_str!("../../examples/fund_portfolio_stock_history.rs"),
33        "\n```"
34    )]
35    pub async fn fund_portfolio_stock_history(
36        &self,
37        fund_type: FundType,
38        thscode: impl AsRef<str> + Send,
39        report_type: impl AsRef<str> + Send,
40        end_date: &str,
41    ) -> Result<Response<FundPortfolioHistoryData>, Error> {
42        self.fund_portfolio_history(
43            endpoints::FUND_STOCK_HISTORY,
44            fund_type,
45            thscode,
46            report_type,
47            end_date,
48        )
49        .await
50    }
51
52    /// 获取历史披露的债券持仓。
53    ///
54    /// # 示例
55    #[doc = concat!(
56        "```no_run\n",
57        include_str!("../../examples/fund_portfolio_bond_history.rs"),
58        "\n```"
59    )]
60    pub async fn fund_portfolio_bond_history(
61        &self,
62        fund_type: FundType,
63        thscode: impl AsRef<str> + Send,
64        report_type: impl AsRef<str> + Send,
65        end_date: &str,
66    ) -> Result<Response<FundPortfolioHistoryData>, Error> {
67        self.fund_portfolio_history(
68            endpoints::FUND_BOND_HISTORY,
69            fund_type,
70            thscode,
71            report_type,
72            end_date,
73        )
74        .await
75    }
76
77    async fn fund_portfolio_history(
78        &self,
79        path: &str,
80        fund_type: FundType,
81        thscode: impl AsRef<str> + Send,
82        report_type: impl AsRef<str> + Send,
83        end_date: &str,
84    ) -> Result<Response<FundPortfolioHistoryData>, Error> {
85        let mut query = fund_target_query(fund_type, thscode)?;
86        let report_type = ReportType::new(report_type.as_ref())?;
87        let end_date = NaturalDate::parse(end_date)?;
88        query.push(("report_type", report_type.into_string()));
89        query.push(("end_date", end_date.to_string()));
90        self.get(path, &query).await
91    }
92
93    /// 获取可用的股票持仓报告期。
94    ///
95    /// # 示例
96    #[doc = concat!(
97        "```no_run\n",
98        include_str!("../../examples/fund_portfolio_stock_report_dates.rs"),
99        "\n```"
100    )]
101    pub async fn fund_portfolio_stock_report_dates(
102        &self,
103        fund_type: FundType,
104        thscode: impl AsRef<str> + Send,
105        report_type: Option<&str>,
106    ) -> Result<Response<FundReportDatesData>, Error> {
107        self.fund_report_dates(
108            endpoints::FUND_STOCK_REPORT_DATES,
109            fund_type,
110            thscode,
111            report_type,
112        )
113        .await
114    }
115
116    /// 获取可用的债券持仓报告期。
117    ///
118    /// # 示例
119    #[doc = concat!(
120        "```no_run\n",
121        include_str!("../../examples/fund_portfolio_bond_report_dates.rs"),
122        "\n```"
123    )]
124    pub async fn fund_portfolio_bond_report_dates(
125        &self,
126        fund_type: FundType,
127        thscode: impl AsRef<str> + Send,
128        report_type: Option<&str>,
129    ) -> Result<Response<FundReportDatesData>, Error> {
130        self.fund_report_dates(
131            endpoints::FUND_BOND_REPORT_DATES,
132            fund_type,
133            thscode,
134            report_type,
135        )
136        .await
137    }
138
139    async fn fund_report_dates(
140        &self,
141        path: &str,
142        fund_type: FundType,
143        thscode: impl AsRef<str> + Send,
144        report_type: Option<&str>,
145    ) -> Result<Response<FundReportDatesData>, Error> {
146        let mut query = fund_target_query(fund_type, thscode)?;
147        if let Some(report_type) = report_type {
148            let report_type = ReportType::new(report_type)?;
149            query.push(("report_type", report_type.into_string()));
150        }
151        self.get(path, &query).await
152    }
153
154    /// 获取基金资产配置。
155    ///
156    /// # 示例
157    #[doc = concat!(
158        "```no_run\n",
159        include_str!("../../examples/fund_portfolio_asset_allocation.rs"),
160        "\n```"
161    )]
162    pub async fn fund_portfolio_asset_allocation(
163        &self,
164        fund_type: FundType,
165        thscode: impl AsRef<str> + Send,
166    ) -> Result<Response<FundAssetAllocationData>, Error> {
167        self.fund_detail(endpoints::FUND_ASSET_ALLOCATION, fund_type, thscode)
168            .await
169    }
170
171    /// 获取基金行业配置。
172    ///
173    /// # 示例
174    #[doc = concat!(
175        "```no_run\n",
176        include_str!("../../examples/fund_portfolio_industry_allocation.rs"),
177        "\n```"
178    )]
179    pub async fn fund_portfolio_industry_allocation(
180        &self,
181        fund_type: FundType,
182        thscode: impl AsRef<str> + Send,
183    ) -> Result<Response<FundIndustryAllocationData>, Error> {
184        self.fund_detail(endpoints::FUND_INDUSTRY_ALLOCATION, fund_type, thscode)
185            .await
186    }
187}