finance-query 2.5.1

A Rust library for querying financial data
Documentation
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
//! Compile and runtime tests for docs/library/tickers.md
//!
//! Pure tests verify builder patterns, struct field access, and API shape.
//! Network tests are marked `#[ignore = "requires network access"]`.
//!
//! Run with: `cargo test --test doc_tickers`
//! Run network tests: `cargo test --test doc_tickers -- --ignored`

// ---------------------------------------------------------------------------
// Network tests — mirrors tickers.md code blocks
// ---------------------------------------------------------------------------

#[tokio::test]
#[ignore = "requires network access"]
async fn test_tickers_simple_construction() {
    use finance_query::Tickers;

    // From tickers.md "Simple Construction" section
    let tickers = Tickers::new(vec!["AAPL", "MSFT", "GOOGL"]).await.unwrap();
    let response = tickers.quotes().await.unwrap();
    assert!(response.success_count() > 0);
}

#[tokio::test]
#[ignore = "requires network access"]
async fn test_tickers_builder_pattern() {
    use finance_query::{Region, Tickers};
    use std::time::Duration;

    // From tickers.md "Builder Pattern" section
    let tickers = Tickers::builder(vec!["AAPL", "MSFT"])
        .region(Region::UnitedStates)
        .timeout(Duration::from_secs(30))
        .build()
        .await
        .unwrap();

    let response = tickers.quotes().await.unwrap();
    assert!(response.success_count() > 0);
}

#[tokio::test]
#[ignore = "requires network access"]
async fn test_tickers_max_concurrency() {
    use finance_query::Tickers;

    // From tickers.md "max_concurrency" section
    let tickers = Tickers::builder(vec!["AAPL", "MSFT", "GOOGL", "TSLA"])
        .max_concurrency(3)
        .build()
        .await
        .unwrap();

    let response = tickers.quotes().await.unwrap();
    assert!(response.success_count() > 0);
}

#[tokio::test]
#[ignore = "requires network access"]
async fn test_tickers_shared_session() {
    use finance_query::{Ticker, Tickers};

    // From tickers.md "Sharing a Session" section
    let aapl = Ticker::new("AAPL").await.unwrap();
    let handle = aapl.client_handle();

    // Reuses AAPL's authenticated session — no extra auth round-trip
    let tickers = Tickers::builder(["MSFT", "GOOGL"])
        .client(handle)
        .build()
        .await
        .unwrap();

    let response = tickers.quotes().await.unwrap();
    assert!(response.success_count() > 0);
}

#[tokio::test]
#[ignore = "requires network access"]
async fn test_batch_quotes_with_logo() {
    use finance_query::Tickers;

    // From tickers.md "Batch Quotes" section
    let tickers = Tickers::builder(vec!["AAPL", "MSFT"])
        .logo()
        .build()
        .await
        .unwrap();
    let response = tickers.quotes().await.unwrap();

    // Process successful quotes
    for (symbol, quote) in &response.quotes {
        let price = quote
            .regular_market_price
            .as_ref()
            .and_then(|v| v.raw)
            .unwrap_or(0.0);
        println!("{} Price: ${:.2}", symbol, price);
        if let Some(logo) = &quote.logo_url {
            println!("  Logo: {}", logo);
        }
    }

    // Handle errors (prints any but doesn't fail)
    for (symbol, error) in &response.errors {
        eprintln!("Failed to fetch {}: {}", symbol, error);
    }

    assert!(response.success_count() > 0);
}

#[tokio::test]
#[ignore = "requires network access"]
async fn test_batch_quotes_utility_methods() {
    use finance_query::Tickers;

    // From tickers.md "Batch Response Utility Methods" section
    let tickers = Tickers::builder(vec!["AAPL", "GOOGL"])
        .logo()
        .build()
        .await
        .unwrap();
    let response = tickers.quotes().await.unwrap();

    println!("Successful: {}", response.success_count());
    println!("Failed:     {}", response.error_count());

    if !response.all_successful() {
        for (symbol, error) in &response.errors {
            eprintln!("Failed to fetch {}: {}", symbol, error);
        }
    }

    assert!(response.success_count() > 0);
}

#[tokio::test]
#[ignore = "requires network access"]
async fn test_batch_charts() {
    use finance_query::{Interval, Tickers, TimeRange};

    // From tickers.md "Batch Charts" section
    let tickers = Tickers::new(vec!["AAPL", "MSFT"]).await.unwrap();

    // Fetch charts concurrently
    let response = tickers
        .charts(Interval::OneDay, TimeRange::OneMonth)
        .await
        .unwrap();

    // Process successful charts
    for (symbol, chart) in &response.charts {
        println!("{}: {} candles", symbol, chart.candles.len());
        if let Some(last) = chart.candles.last() {
            println!("  Last Close: ${:.2}", last.close);
        }
    }

    for (symbol, error) in &response.errors {
        eprintln!("Failed to fetch chart for {}: {}", symbol, error);
    }

    assert!(response.success_count() > 0);
}

#[tokio::test]
#[ignore = "requires network access"]
async fn test_batch_spark() {
    use finance_query::{Interval, Tickers, TimeRange};

    // From tickers.md "Spark Data" section
    let tickers = Tickers::new(vec!["AAPL", "MSFT"]).await.unwrap();

    // Fetch spark data for all symbols
    let response = tickers
        .spark(Interval::OneDay, TimeRange::FiveDays)
        .await
        .unwrap();

    // Process successful sparks
    for (symbol, spark) in &response.sparks {
        println!("{}: {} data points", symbol, spark.len());

        if let Some(change) = spark.percent_change() {
            println!("  Change: {:+.2}%", change);
        }

        if let Some(min) = spark.min_close() {
            println!("  Low: ${:.2}", min);
        }

        if let Some(max) = spark.max_close() {
            println!("  High: ${:.2}", max);
        }
    }

    for (symbol, error) in &response.errors {
        eprintln!("Failed to fetch spark for {}: {}", symbol, error);
    }

    assert!(response.success_count() > 0);
}

#[tokio::test]
#[ignore = "requires network access"]
async fn test_batch_dividends() {
    use finance_query::{Tickers, TimeRange};

    // From tickers.md "Batch Dividends" section
    let tickers = Tickers::new(vec!["AAPL", "MSFT"]).await.unwrap();

    // Fetch dividends for all symbols
    let response = tickers.dividends(TimeRange::OneYear).await.unwrap();

    // Process successful dividends
    for (symbol, dividends) in &response.dividends {
        println!("{}: {} dividends", symbol, dividends.len());
        for div in dividends {
            println!("  Timestamp: {}, Amount: ${:.2}", div.timestamp, div.amount);
        }
    }

    for (symbol, error) in &response.errors {
        eprintln!("Failed to fetch dividends for {}: {}", symbol, error);
    }

    // AAPL and MSFT both pay dividends
    assert!(response.success_count() > 0);
}

#[tokio::test]
#[ignore = "requires network access"]
async fn test_batch_splits() {
    use finance_query::{Tickers, TimeRange};

    // From tickers.md "Batch Splits" section
    let tickers = Tickers::new(vec!["NVDA", "TSLA", "AAPL"]).await.unwrap();
    let response = tickers.splits(TimeRange::FiveYears).await.unwrap();

    // Process splits
    for (symbol, splits) in &response.splits {
        if !splits.is_empty() {
            println!("{}: {} splits", symbol, splits.len());
            for split in splits {
                println!("  Timestamp: {}, Ratio: {}", split.timestamp, split.ratio);
            }
        }
    }

    assert!(response.success_count() > 0);
}

#[tokio::test]
#[ignore = "requires network access"]
async fn test_batch_capital_gains() {
    use finance_query::{Tickers, TimeRange};

    // From tickers.md "Batch Capital Gains" section — ETFs
    let etfs = Tickers::new(vec!["SPY", "VOO", "VTI"]).await.unwrap();
    let response = etfs.capital_gains(TimeRange::TwoYears).await.unwrap();

    // Process capital gains
    for (symbol, gains) in &response.capital_gains {
        if !gains.is_empty() {
            println!("{}: {} capital gains distributions", symbol, gains.len());
            for gain in gains {
                println!(
                    "  Timestamp: {}, Amount: ${:.2}",
                    gain.timestamp, gain.amount
                );
            }
        }
    }

    assert!(response.success_count() > 0);
}

#[tokio::test]
#[ignore = "requires network access"]
async fn test_batch_financials() {
    use finance_query::{Frequency, StatementType, Tickers};

    // From tickers.md "Batch Financials" section
    let tickers = Tickers::new(vec!["AAPL", "MSFT"]).await.unwrap();

    // Fetch quarterly income statements
    let response = tickers
        .financials(StatementType::Income, Frequency::Quarterly)
        .await
        .unwrap();

    // Process financial statements
    for (symbol, statement) in &response.financials {
        println!("{}: {} metrics", symbol, statement.statement.len());

        // Access specific metrics
        if let Some(revenue_data) = statement.statement.get("TotalRevenue") {
            println!("  Revenue data points: {}", revenue_data.len());

            // Get most recent revenue
            if let Some((date, value)) = revenue_data.iter().next() {
                println!("  Latest Revenue ({}): ${}", date, value);
            }
        }

        if let Some(income_data) = statement.statement.get("NetIncome")
            && let Some((date, value)) = income_data.iter().next()
        {
            println!("  Latest Net Income ({}): ${}", date, value);
        }
    }

    assert!(response.success_count() > 0);
}

#[tokio::test]
#[ignore = "requires network access"]
async fn test_batch_news() {
    use finance_query::Tickers;

    // From tickers.md "Batch News" section
    let tickers = Tickers::new(vec!["AAPL", "MSFT"]).await.unwrap();

    // Fetch news for all symbols
    let response = tickers.news().await.unwrap();

    // Process news
    for (symbol, articles) in &response.news {
        println!("{}: {} news articles", symbol, articles.len());
        for article in articles.iter().take(3) {
            println!("  Title: {}", article.title);
            println!("  Source: {}", article.source);
            println!("  Link: {}", article.link);
        }
    }

    assert!(response.success_count() > 0);
}

#[tokio::test]
#[ignore = "requires network access"]
async fn test_batch_recommendations() {
    use finance_query::Tickers;

    // From tickers.md "Batch Recommendations" section
    let tickers = Tickers::new(vec!["AAPL", "MSFT"]).await.unwrap();

    // Fetch recommendations with limit
    let response = tickers.recommendations(5).await.unwrap();

    // Process recommendations
    for (symbol, rec) in &response.recommendations {
        println!("{}: {} recommendations", symbol, rec.recommendations.len());
        for r in &rec.recommendations {
            println!("  {} ({})", r.symbol, r.score);
        }
    }

    assert!(response.success_count() > 0);
}

#[tokio::test]
#[ignore = "requires network access"]
async fn test_batch_options() {
    use finance_query::Tickers;

    // From tickers.md "Batch Options" section
    let tickers = Tickers::new(vec!["AAPL", "MSFT"]).await.unwrap();

    // Fetch options for all symbols (nearest expiration)
    let response = tickers.options(None).await.unwrap();

    // Process options
    for (symbol, options) in &response.options {
        let exp_dates = options.expiration_dates();
        println!("{}: {} expirations", symbol, exp_dates.len());

        // Show calls and puts count for nearest expiration
        let calls = options.calls();
        let puts = options.puts();
        println!("  Calls: {} contracts", calls.len());
        println!("  Puts: {} contracts", puts.len());
    }

    // Fetch for a specific expiration date — use a dynamic date from the first response
    // so the call is valid (the doc uses a hardcoded Unix timestamp as an example)
    let first_exp = response
        .options
        .values()
        .next()
        .and_then(|opts| opts.expiration_dates().into_iter().nth(1));
    if let Some(specific_date) = first_exp {
        let _dated = tickers.options(Some(specific_date)).await.unwrap();
    }

    assert!(response.success_count() > 0);
}

#[tokio::test]
#[ignore = "requires network access"]
async fn test_dynamic_symbol_management() {
    use finance_query::Tickers;

    // From tickers.md "Dynamic Symbol Management" section
    let mut tickers = Tickers::new(vec!["AAPL", "MSFT"]).await.unwrap();
    println!("Initial symbols: {:?}", tickers.symbols());

    // Add more symbols
    tickers.add_symbols(&["GOOGL", "TSLA", "NVDA"]);
    println!("After adding: {:?}", tickers.symbols());

    // Remove symbols (also clears their cached data)
    tickers.remove_symbols(&["MSFT", "TSLA"]).await;
    println!("After removing: {:?}", tickers.symbols());

    // Fetch quotes for current symbols
    let response = tickers.quotes().await.unwrap();
    // Response will only include AAPL, GOOGL, NVDA
    assert!(response.success_count() > 0);
}

#[tokio::test]
#[ignore = "requires network access"]
async fn test_tickers_caching() {
    use finance_query::Tickers;
    use std::time::Duration;

    // From tickers.md "Caching" section
    let tickers = Tickers::builder(vec!["AAPL", "MSFT"])
        .cache(Duration::from_secs(30))
        .build()
        .await
        .unwrap();

    // First call: Network request, result cached for 30s
    let response1 = tickers.quotes().await.unwrap();

    // Second call within TTL: Returns cached data (no network request)
    let response2 = tickers.quotes().await.unwrap();

    // Clear all caches to force fresh data
    tickers.clear_cache().await;
    let response3 = tickers.quotes().await.unwrap(); // Network request

    // Or clear selectively:
    tickers.clear_quote_cache().await; // Quotes only
    tickers.clear_chart_cache().await; // Charts, sparks, and events

    assert_eq!(response1.success_count(), response2.success_count());
    assert_eq!(response2.success_count(), response3.success_count());
}

// ---------------------------------------------------------------------------
// Network tests — Individual Access from tickers.md
// ---------------------------------------------------------------------------

#[tokio::test]
#[ignore = "requires network access"]
async fn test_individual_access() {
    use finance_query::{Interval, Tickers, TimeRange};

    // From tickers.md "Individual Access" section
    let tickers = Tickers::new(vec!["AAPL", "MSFT"]).await.unwrap();

    // Get single quote (uses cache if available)
    let aapl = tickers.quote("AAPL").await.unwrap();
    println!(
        "AAPL price: {:?}",
        aapl.regular_market_price.as_ref().and_then(|v| v.raw)
    );

    // Get single chart (uses cache if available)
    let msft_chart = tickers
        .chart("MSFT", Interval::OneDay, TimeRange::OneMonth)
        .await
        .unwrap();
    println!("MSFT candles: {}", msft_chart.candles.len());

    assert!(!aapl.symbol.is_empty());
    assert!(!msft_chart.candles.is_empty());
}

// ---------------------------------------------------------------------------
// Network tests — Best Practices from tickers.md
// ---------------------------------------------------------------------------

#[tokio::test]
#[ignore = "requires network access"]
async fn test_tickers_best_practices() {
    use finance_query::{Interval, Tickers, TimeRange};

    // From tickers.md "Best Practices" admonition — reuse instance, handle partial failures
    let tickers = Tickers::builder(vec!["AAPL", "GOOGL", "INVALID", "MSFT"])
        .logo()
        .build()
        .await
        .unwrap();

    // First operation - fetches data
    let quotes_response = tickers.quotes().await.unwrap();

    // Handle partial failures - check which symbols failed
    for (symbol, error) in &quotes_response.errors {
        println!("Failed to fetch {}: {}", symbol, error);
    }

    // Process successful results
    for (symbol, quote) in &quotes_response.quotes {
        let price = quote
            .regular_market_price
            .as_ref()
            .and_then(|v| v.raw)
            .unwrap_or(0.0);
        println!("{}: ${:.2}", symbol, price);
    }

    // Second operation - uses cached data (no network request)
    let charts_response = tickers
        .charts(Interval::OneDay, TimeRange::OneMonth)
        .await
        .unwrap();

    assert!(quotes_response.success_count() > 0);
    let _ = charts_response;
}

#[tokio::test]
#[ignore = "requires network access"]
async fn test_batch_indicators() {
    #[cfg(feature = "indicators")]
    {
        use finance_query::{Interval, Tickers, TimeRange};

        // From tickers.md "Batch Indicators" section
        let tickers = Tickers::new(vec!["AAPL", "MSFT"]).await.unwrap();

        // Fetch indicators for all symbols
        let response = tickers
            .indicators(Interval::OneDay, TimeRange::OneMonth)
            .await
            .unwrap();

        // Process indicators
        for (symbol, indicators) in &response.indicators {
            println!("{} Indicators:", symbol);

            if let Some(rsi) = indicators.rsi_14 {
                println!("  RSI(14): {:.2}", rsi);
            }

            if let Some(sma) = indicators.sma_20 {
                println!("  SMA(20): {:.2}", sma);
            }

            if let Some(macd) = &indicators.macd
                && let Some(line) = macd.macd
            {
                println!("  MACD: {:.2}", line);
            }
        }

        assert!(response.success_count() > 0);
    }
}