1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
//! Mutual Funds endpoints
use crate::{client::FmpClient, error::Result, models::mutual_funds::*};
use serde::Serialize;
/// Mutual Funds API endpoints
pub struct MutualFunds {
client: FmpClient,
}
impl MutualFunds {
pub(crate) fn new(client: FmpClient) -> Self {
Self { client }
}
/// Search for mutual funds
///
/// Returns a list of mutual funds matching search criteria or all available funds.
/// Useful for discovering funds by name, symbol, or category.
///
/// # Arguments
/// * `query` - Optional search term to filter funds
///
/// # Example
/// ```no_run
/// # use fmp_rs::FmpClient;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = FmpClient::new()?;
/// let funds = client.mutual_funds().search_funds(Some("Vanguard")).await?;
/// for fund in funds.iter().take(5) {
/// println!("{}: {} - AUM: ${:.1}B",
/// fund.symbol.as_deref().unwrap_or("N/A"),
/// fund.name.as_deref().unwrap_or("Unknown"),
/// fund.total_assets.unwrap_or(0.0) / 1_000_000_000.0);
/// }
/// # Ok(())
/// # }
/// ```
pub async fn search_funds(&self, query: Option<&str>) -> Result<Vec<MutualFund>> {
#[derive(Serialize)]
struct Query<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
query: Option<&'a str>,
apikey: &'a str,
}
let url = self.client.build_url("/mutual_fund");
self.client
.get_with_query(
&url,
&Query {
query,
apikey: self.client.api_key(),
},
)
.await
}
/// Get mutual fund details by symbol
///
/// Returns detailed information about a specific mutual fund including
/// NAV, expense ratio, total assets, and fund characteristics.
///
/// # Arguments
/// * `symbol` - Fund symbol (e.g., "VTSAX", "FXNAX")
///
/// # Example
/// ```no_run
/// # use fmp_rs::FmpClient;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = FmpClient::new()?;
/// let fund_details = client.mutual_funds().get_fund_details("VTSAX").await?;
/// if let Some(fund) = fund_details.first() {
/// println!("{}: NAV = ${:.2}, Expense Ratio = {:.2}%",
/// fund.name.as_deref().unwrap_or("Unknown"),
/// fund.nav.unwrap_or(0.0),
/// fund.expense_ratio.unwrap_or(0.0) * 100.0);
/// }
/// # Ok(())
/// # }
/// ```
pub async fn get_fund_details(&self, symbol: &str) -> Result<Vec<MutualFund>> {
#[derive(Serialize)]
struct Query<'a> {
apikey: &'a str,
}
let url = self.client.build_url(&format!("/mutual_fund/{}", symbol));
self.client
.get_with_query(
&url,
&Query {
apikey: self.client.api_key(),
},
)
.await
}
/// Get mutual fund historical prices
///
/// Returns historical NAV (Net Asset Value) data for a mutual fund.
/// Useful for performance analysis and trend identification.
///
/// # Arguments
/// * `symbol` - Fund symbol (e.g., "VTSAX")
/// * `from_date` - Optional start date (YYYY-MM-DD format)
/// * `to_date` - Optional end date (YYYY-MM-DD format)
///
/// # Example
/// ```no_run
/// # use fmp_rs::FmpClient;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = FmpClient::new()?;
/// let history = client.mutual_funds()
/// .get_fund_historical("VTSAX", Some("2024-01-01"), Some("2024-03-31")).await?;
/// for price in history.iter().take(10) {
/// println!("{}: NAV = ${:.2} ({:+.2}%)",
/// price.date.as_deref().unwrap_or("N/A"),
/// price.nav.unwrap_or(0.0),
/// price.change_percent.unwrap_or(0.0));
/// }
/// # Ok(())
/// # }
/// ```
pub async fn get_fund_historical(
&self,
symbol: &str,
from_date: Option<&str>,
to_date: Option<&str>,
) -> Result<Vec<MutualFundHistorical>> {
#[derive(Serialize)]
struct Query<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
from: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
to: Option<&'a str>,
apikey: &'a str,
}
let url = self
.client
.build_url(&format!("/mutual_fund_historical/{}", symbol));
self.client
.get_with_query(
&url,
&Query {
from: from_date,
to: to_date,
apikey: self.client.api_key(),
},
)
.await
}
/// Get mutual fund holdings
///
/// Returns the top holdings of a mutual fund, showing what securities
/// the fund invests in and their allocation percentages.
///
/// # Arguments
/// * `symbol` - Fund symbol (e.g., "VTSAX")
///
/// # Example
/// ```no_run
/// # use fmp_rs::FmpClient;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = FmpClient::new()?;
/// let holdings = client.mutual_funds().get_fund_holdings("VTSAX").await?;
/// for holding in holdings.iter().take(10) {
/// println!("{}: {:.2}% - {}",
/// holding.symbol.as_deref().unwrap_or("N/A"),
/// holding.weight_percentage.unwrap_or(0.0),
/// holding.name.as_deref().unwrap_or("Unknown"));
/// }
/// # Ok(())
/// # }
/// ```
pub async fn get_fund_holdings(&self, symbol: &str) -> Result<Vec<MutualFundHolding>> {
#[derive(Serialize)]
struct Query<'a> {
apikey: &'a str,
}
let url = self
.client
.build_url(&format!("/mutual_fund_holdings/{}", symbol));
self.client
.get_with_query(
&url,
&Query {
apikey: self.client.api_key(),
},
)
.await
}
/// Get mutual fund performance metrics
///
/// Returns performance statistics including returns over various time periods,
/// risk metrics (alpha, beta, Sharpe ratio), and benchmark comparisons.
///
/// # Arguments
/// * `symbol` - Fund symbol (e.g., "VTSAX")
///
/// # Example
/// ```no_run
/// # use fmp_rs::FmpClient;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = FmpClient::new()?;
/// let performance = client.mutual_funds().get_fund_performance("VTSAX").await?;
/// if let Some(perf) = performance.first() {
/// println!("{}: 1Y = {:.1}%, 3Y = {:.1}%, Sharpe = {:.2}",
/// perf.name.as_deref().unwrap_or("Unknown"),
/// perf.one_year.unwrap_or(0.0),
/// perf.three_years.unwrap_or(0.0),
/// perf.sharpe_ratio.unwrap_or(0.0));
/// }
/// # Ok(())
/// # }
/// ```
pub async fn get_fund_performance(&self, symbol: &str) -> Result<Vec<MutualFundPerformance>> {
#[derive(Serialize)]
struct Query<'a> {
apikey: &'a str,
}
let url = self
.client
.build_url(&format!("/mutual_fund_performance/{}", symbol));
self.client
.get_with_query(
&url,
&Query {
apikey: self.client.api_key(),
},
)
.await
}
/// Get mutual fund sector allocation
///
/// Returns the sector breakdown of a mutual fund's holdings,
/// showing how the fund is allocated across different industries.
///
/// # Arguments
/// * `symbol` - Fund symbol (e.g., "VTSAX")
///
/// # Example
/// ```no_run
/// # use fmp_rs::FmpClient;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = FmpClient::new()?;
/// let sectors = client.mutual_funds().get_fund_sectors("VTSAX").await?;
/// for sector in sectors.iter().take(10) {
/// println!("{}: {:.1}%",
/// sector.sector.as_deref().unwrap_or("Unknown"),
/// sector.weight_percentage.unwrap_or(0.0));
/// }
/// # Ok(())
/// # }
/// ```
pub async fn get_fund_sectors(&self, symbol: &str) -> Result<Vec<MutualFundSector>> {
#[derive(Serialize)]
struct Query<'a> {
apikey: &'a str,
}
let url = self
.client
.build_url(&format!("/mutual_fund_sectors/{}", symbol));
self.client
.get_with_query(
&url,
&Query {
apikey: self.client.api_key(),
},
)
.await
}
}
#[cfg(test)]
mod tests {
use super::*;
fn create_test_client() -> FmpClient {
FmpClient::builder().api_key("test_key").build().unwrap()
}
#[test]
fn test_new() {
let client = create_test_client();
let mutual_funds = MutualFunds::new(client);
// Test passes if no panic occurs
}
#[tokio::test]
#[ignore] // Requires API key
async fn test_search_funds_no_query() {
let client = FmpClient::new().unwrap();
let result = client.mutual_funds().search_funds(None).await;
assert!(result.is_ok());
let funds = result.unwrap();
if !funds.is_empty() {
let first_fund = &funds[0];
// Should have at least symbol or name
assert!(first_fund.symbol.is_some() || first_fund.name.is_some());
println!("Found {} mutual funds", funds.len());
}
}
#[tokio::test]
#[ignore] // Requires API key
async fn test_search_funds_with_query() {
let client = FmpClient::new().unwrap();
let result = client.mutual_funds().search_funds(Some("Vanguard")).await;
assert!(result.is_ok());
let funds = result.unwrap();
println!("Found {} Vanguard funds", funds.len());
// Check that results contain Vanguard in name or symbol
if !funds.is_empty() {
let has_vanguard = funds.iter().any(|fund| {
fund.name
.as_deref()
.unwrap_or("")
.to_lowercase()
.contains("vanguard")
|| fund
.fund_family
.as_deref()
.unwrap_or("")
.to_lowercase()
.contains("vanguard")
});
println!("Search results contain Vanguard: {}", has_vanguard);
}
}
#[tokio::test]
#[ignore] // Requires API key
async fn test_get_fund_details() {
let client = FmpClient::new().unwrap();
let result = client.mutual_funds().get_fund_details("VTSAX").await;
assert!(result.is_ok());
let fund_details = result.unwrap();
if let Some(fund) = fund_details.first() {
assert_eq!(fund.symbol.as_deref(), Some("VTSAX"));
assert!(fund.name.is_some());
println!("VTSAX: {}", fund.name.as_deref().unwrap_or("Unknown"));
}
}
#[tokio::test]
#[ignore] // Requires API key
async fn test_get_fund_historical() {
let client = FmpClient::new().unwrap();
let result = client
.mutual_funds()
.get_fund_historical("VTSAX", Some("2024-01-01"), Some("2024-01-31"))
.await;
assert!(result.is_ok());
let history = result.unwrap();
if !history.is_empty() {
let first_entry = &history[0];
assert!(first_entry.date.is_some());
assert!(first_entry.nav.is_some());
println!("Historical data points: {}", history.len());
}
}
#[tokio::test]
#[ignore] // Requires API key
async fn test_get_fund_holdings() {
let client = FmpClient::new().unwrap();
let result = client.mutual_funds().get_fund_holdings("VTSAX").await;
assert!(result.is_ok());
let holdings = result.unwrap();
if !holdings.is_empty() {
let top_holding = &holdings[0];
assert!(top_holding.symbol.is_some() || top_holding.name.is_some());
assert!(top_holding.weight_percentage.is_some());
println!("Top holdings count: {}", holdings.len());
}
}
#[tokio::test]
#[ignore] // Requires API key
async fn test_get_fund_performance() {
let client = FmpClient::new().unwrap();
let result = client.mutual_funds().get_fund_performance("VTSAX").await;
assert!(result.is_ok());
let performance = result.unwrap();
if let Some(perf) = performance.first() {
assert!(perf.symbol.is_some());
// Should have at least some performance metrics
assert!(perf.one_year.is_some() || perf.ytd.is_some());
println!("Performance data available");
}
}
#[tokio::test]
#[ignore] // Requires API key
async fn test_get_fund_sectors() {
let client = FmpClient::new().unwrap();
let result = client.mutual_funds().get_fund_sectors("VTSAX").await;
assert!(result.is_ok());
let sectors = result.unwrap();
if !sectors.is_empty() {
let top_sector = §ors[0];
assert!(top_sector.sector.is_some());
assert!(top_sector.weight_percentage.is_some());
println!("Sector allocations: {}", sectors.len());
}
}
#[test]
fn test_mutual_fund_models_serialization() {
use serde_json;
// Test MutualFund model
let fund = MutualFund {
symbol: Some("VTSAX".to_string()),
name: Some("Vanguard Total Stock Market Index Fund".to_string()),
fund_family: Some("Vanguard".to_string()),
category: Some("Large Blend".to_string()),
nav: Some(120.50),
expense_ratio: Some(0.0003),
total_assets: Some(1_400_000_000_000.0), // $1.4T
yield_: Some(1.45),
fund_type: Some("Index Fund".to_string()),
investment_objective: Some("Track total stock market".to_string()),
previous_nav: Some(120.25),
change: Some(0.25),
change_percent: Some(0.21),
minimum_investment: Some(3000.0),
inception_date: Some("2000-11-13".to_string()),
manager_name: Some("Vanguard Equity Investment Group".to_string()),
currency: Some("USD".to_string()),
exchange: Some("NASDAQ".to_string()),
last_updated: Some("2024-01-15".to_string()),
};
let json = serde_json::to_string(&fund).unwrap();
let deserialized: MutualFund = serde_json::from_str(&json).unwrap();
assert_eq!(deserialized.symbol, Some("VTSAX".to_string()));
assert_eq!(deserialized.nav, Some(120.50));
// Test MutualFundPerformance model
let performance = MutualFundPerformance {
symbol: Some("VTSAX".to_string()),
name: Some("Vanguard Total Stock Market".to_string()),
one_day: Some(-0.15),
one_week: Some(1.25),
one_month: Some(2.80),
three_months: Some(8.45),
ytd: Some(12.30),
one_year: Some(15.75),
three_years: Some(10.20),
five_years: Some(11.85),
ten_years: Some(12.45),
since_inception: Some(9.95),
standard_deviation: Some(14.85),
sharpe_ratio: Some(0.95),
alpha: Some(0.15),
beta: Some(0.98),
r_squared: Some(0.995),
tracking_error: Some(0.05),
information_ratio: Some(0.25),
benchmark: Some("VTI".to_string()),
six_months: Some(6.75),
date: Some("2024-01-15".to_string()),
};
let json = serde_json::to_string(&performance).unwrap();
let deserialized: MutualFundPerformance = serde_json::from_str(&json).unwrap();
assert_eq!(deserialized.one_year, Some(15.75));
assert_eq!(deserialized.sharpe_ratio, Some(0.95));
}
#[test]
fn test_fund_holdings_model() {
let holding = MutualFundHolding {
symbol: Some("AAPL".to_string()),
name: Some("Apple Inc.".to_string()),
sector: Some("Technology".to_string()),
industry: Some("Consumer Electronics".to_string()),
country: Some("United States".to_string()),
weight_percentage: Some(7.25),
market_value: Some(125_000_000.0),
shares_held: Some(675_000),
asset_type: Some("Common Stock".to_string()),
cusip: Some("037833100".to_string()),
isin: Some("US0378331005".to_string()),
date: Some("2024-01-15".to_string()),
};
// Verify all fields are accessible and serializable
assert_eq!(holding.symbol, Some("AAPL".to_string()));
assert_eq!(holding.weight_percentage, Some(7.25));
assert_eq!(holding.sector, Some("Technology".to_string()));
let json = serde_json::to_string(&holding).unwrap();
let deserialized: MutualFundHolding = serde_json::from_str(&json).unwrap();
assert_eq!(deserialized.symbol, Some("AAPL".to_string()));
}
#[test]
fn test_date_range_validation() {
// Test that date parameters are properly formatted
let start_date = "2024-01-01";
let end_date = "2024-12-31";
// These should be valid ISO date formats
assert!(chrono::NaiveDate::parse_from_str(start_date, "%Y-%m-%d").is_ok());
assert!(chrono::NaiveDate::parse_from_str(end_date, "%Y-%m-%d").is_ok());
}
// Additional edge case tests
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_fund_details_invalid_symbol() {
let client = FmpClient::new().unwrap();
let result = client
.mutual_funds()
.get_fund_details("INVALID_FUND_XYZ")
.await;
match result {
Ok(funds) => assert!(funds.is_empty()),
Err(_) => {} // Error is acceptable for invalid symbols
}
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_fund_historical_with_dates() {
let client = FmpClient::new().unwrap();
let result = client
.mutual_funds()
.get_fund_historical("VTSAX", Some("2024-01-01"), Some("2024-01-31"))
.await;
assert!(result.is_ok());
let historical = result.unwrap();
// Should have at most 31 days of data
assert!(historical.len() <= 31);
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_fund_holdings_empty_result() {
let client = FmpClient::new().unwrap();
let result = client
.mutual_funds()
.get_fund_holdings("INVALID_FUND")
.await;
match result {
Ok(holdings) => assert!(holdings.is_empty()),
Err(_) => {} // Error is acceptable
}
}
}