Skip to main content

financial_api/fund/
performance.rs

1use crate::endpoints;
2use crate::{
3    Client, Error, FundDrawdownsData, FundIndicatorHistoryData, FundNavData, FundNavType,
4    FundRange, FundReturnsData, FundType, Response, Thscode, UnixMillis,
5};
6
7use super::{FundTarget, validate_history_range};
8
9impl Client {
10    /// Fetch fund NAV series.
11    pub async fn fund_performance_nav(
12        &self,
13        fund_type: FundType,
14        thscode: &Thscode,
15        range: Option<FundRange>,
16        nav_type: FundNavType,
17    ) -> Result<Response<FundNavData>, Error> {
18        let mut query = FundTarget::new(fund_type, thscode)?.query();
19        if let Some(range) = range {
20            query.push(("range", range.to_string()));
21        }
22        query.push(("nav_type", nav_type.to_string()));
23        self.get(endpoints::FUND_NAV, &query).await
24    }
25
26    /// Fetch multi-range fund returns and peer rankings.
27    pub async fn fund_performance_returns(
28        &self,
29        fund_type: FundType,
30        thscode: &Thscode,
31    ) -> Result<Response<FundReturnsData>, Error> {
32        self.fund_detail(endpoints::FUND_RETURNS, fund_type, thscode)
33            .await
34    }
35
36    /// Fetch historical fund performance indicators.
37    pub async fn fund_performance_indicators_historical(
38        &self,
39        fund_type: FundType,
40        thscode: &Thscode,
41        start: UnixMillis,
42        end: UnixMillis,
43    ) -> Result<Response<FundIndicatorHistoryData>, Error> {
44        let target = FundTarget::new(fund_type, thscode)?;
45        validate_history_range(start, end)?;
46        let mut query = target.query();
47        query.push(("start", start.to_string()));
48        query.push(("end", end.to_string()));
49        self.get(endpoints::FUND_INDICATORS_HISTORICAL, &query)
50            .await
51    }
52
53    /// Fetch standard-period maximum drawdowns.
54    pub async fn fund_performance_drawdowns(
55        &self,
56        fund_type: FundType,
57        thscode: &Thscode,
58    ) -> Result<Response<FundDrawdownsData>, Error> {
59        self.fund_detail(endpoints::FUND_DRAWDOWNS, fund_type, thscode)
60            .await
61    }
62}
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67    use crate::ApiKey;
68
69    #[tokio::test]
70    async fn historical_indicators_keeps_range_validation_on_its_call_edge() {
71        let client = Client::builder(ApiKey::new("test-api-key").unwrap())
72            .base_url("http://127.0.0.1:9")
73            .build()
74            .unwrap();
75        let fund = Thscode::new("025480.OF").unwrap();
76
77        let error = client
78            .fund_performance_indicators_historical(
79                FundType::Otc,
80                &fund,
81                UnixMillis::new(1_577_836_800_000).unwrap(),
82                UnixMillis::new(1_735_776_000_000).unwrap(),
83            )
84            .await
85            .unwrap_err();
86
87        assert!(matches!(error, Error::InvalidInput(_)));
88    }
89}