finance-query 2.5.0

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
//! Compile and runtime tests for docs/library/finance.md
//!
//! Pure tests verify type/variant existence and field access patterns.
//! Network tests are marked `#[ignore = "requires network access"]`.
//!
//! Run with: `cargo test --test doc_finance`
//! Run network tests: `cargo test --test doc_finance -- --ignored`

use finance_query::{FearAndGreed, FearGreedLabel, Sector};

// ---------------------------------------------------------------------------
// FearAndGreed — compile-time field verification
// ---------------------------------------------------------------------------

/// Verifies FearAndGreed struct fields documented in finance.md.
#[allow(dead_code)]
fn _verify_fear_and_greed_fields(fg: FearAndGreed) {
    let _: u8 = fg.value;
    let _: FearGreedLabel = fg.classification;
    let _: i64 = fg.timestamp;
}

#[test]
fn test_fear_greed_label_variants_and_as_str() {
    // All variants documented in finance.md must exist and have non-empty as_str().
    let cases = [
        (FearGreedLabel::ExtremeFear, "Extreme Fear"),
        (FearGreedLabel::Fear, "Fear"),
        (FearGreedLabel::Neutral, "Neutral"),
        (FearGreedLabel::Greed, "Greed"),
        (FearGreedLabel::ExtremeGreed, "Extreme Greed"),
    ];
    for (variant, expected) in cases {
        assert_eq!(variant.as_str(), expected);
    }
}

// ---------------------------------------------------------------------------
// Sector enum — compile-time variant verification (all 11 in finance.md)
// ---------------------------------------------------------------------------

#[test]
fn test_sector_variants_compile() {
    // Mirrors the sector() example in finance.md.
    let _ = Sector::Technology;
    let _ = Sector::Healthcare;
    let _ = Sector::FinancialServices;
    let _ = Sector::BasicMaterials;
    let _ = Sector::CommunicationServices;
    let _ = Sector::ConsumerCyclical;
    let _ = Sector::ConsumerDefensive;
    let _ = Sector::Energy;
    let _ = Sector::Industrials;
    let _ = Sector::RealEstate;
    let _ = Sector::Utilities;
}

// ---------------------------------------------------------------------------
// Network tests
// ---------------------------------------------------------------------------

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

    let fg = finance::fear_and_greed().await.unwrap();
    println!(
        "Fear & Greed: {} / 100 ({})",
        fg.value,
        fg.classification.as_str()
    );
    assert!(fg.timestamp > 0);
}

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

    let tech = finance::sector(Sector::Technology).await.unwrap();
    println!("Sector: {}", tech.name);
    assert!(!tech.name.is_empty());
}

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

    let semi = finance::industry("semiconductors").await.unwrap();
    println!("Industry: {}", semi.name);
    assert!(!semi.name.is_empty());
}

#[tokio::test]
#[ignore = "requires network access"]
async fn test_search() {
    use finance_query::{SearchOptions, finance};

    let results = finance::search("Apple", &SearchOptions::default())
        .await
        .unwrap();
    println!("Found {} quotes", results.result_count());
    assert!(results.result_count() > 0);
}

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

    let summary = finance::market_summary(None).await.unwrap();
    assert!(!summary.is_empty());
    for quote in &summary {
        let price = quote
            .regular_market_price
            .as_ref()
            .and_then(|v| v.raw)
            .unwrap_or(0.0);
        println!("{}: {:.2}", quote.symbol, price);
    }
}

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

    let trending = finance::trending(None).await.unwrap();
    assert!(!trending.is_empty());
    for quote in &trending {
        println!("{}", quote.symbol);
    }
}

// ---------------------------------------------------------------------------
// Network tests — Search (advanced options, from finance.md "Search" section)
// ---------------------------------------------------------------------------

#[tokio::test]
#[ignore = "requires network access"]
async fn test_search_advanced_options() {
    use finance_query::{Region, SearchOptions, finance};

    // From finance.md "Search" section — advanced search with options
    let options = SearchOptions::new()
        .quotes_count(10)
        .news_count(5)
        .enable_research_reports(true)
        .enable_fuzzy_query(true)
        .region(Region::Canada);

    let results = finance::search("tesla", &options).await.unwrap();
    println!("Found {} results", results.result_count());

    for quote in &results.quotes {
        let exchange = quote.exchange.as_deref().unwrap_or("N/A");
        let name = quote.short_name.as_deref().unwrap_or("N/A");
        println!("{} ({}): {}", quote.symbol, exchange, name);
    }
}

// ---------------------------------------------------------------------------
// Network tests — Lookup (from finance.md "Lookup" section)
// ---------------------------------------------------------------------------

#[tokio::test]
#[ignore = "requires network access"]
async fn test_lookup() {
    use finance_query::{LookupOptions, LookupType, finance};

    // From finance.md "Lookup" section
    // Simple lookup
    let results = finance::lookup("NVDA", &LookupOptions::default())
        .await
        .unwrap();
    assert!(!results.quotes.is_empty());

    // Lookup equities with logos
    let options = LookupOptions::new()
        .lookup_type(LookupType::Equity)
        .count(10)
        .include_logo(true);

    let results = finance::lookup("tech", &options).await.unwrap();
    for quote in &results.quotes {
        let name = quote.short_name.as_deref().unwrap_or("N/A");
        println!("{}: {} - {:?}", quote.symbol, name, quote.logo_url);
    }
}

// ---------------------------------------------------------------------------
// Network tests — Market Summary region (from finance.md "Market Summary" section)
// ---------------------------------------------------------------------------

#[tokio::test]
#[ignore = "requires network access"]
async fn test_market_summary_region() {
    use finance_query::{Region, finance};

    // From finance.md "Market Summary" section — specific region + full field access pattern
    let summary = finance::market_summary(Some(Region::Canada)).await.unwrap();
    for quote in &summary {
        let price = quote.regular_market_price.as_ref().and_then(|v| v.raw);
        let change_pct = quote
            .regular_market_change_percent
            .as_ref()
            .and_then(|v| v.raw)
            .unwrap_or(0.0);
        println!("{}: ${:?} ({:+.2}%)", quote.symbol, price, change_pct);
    }
}

// ---------------------------------------------------------------------------
// Network tests — Trending region (from finance.md "Trending" section)
// ---------------------------------------------------------------------------

#[tokio::test]
#[ignore = "requires network access"]
async fn test_trending_region() {
    use finance_query::{Region, finance};

    // From finance.md "Trending" section — specific region
    let trending = finance::trending(Some(Region::Singapore)).await.unwrap();
    for quote in &trending {
        println!("{}", quote.symbol);
    }
    assert!(!trending.is_empty());
}

// ---------------------------------------------------------------------------
// Network tests — Market Hours (from finance.md "Market Hours" section)
// ---------------------------------------------------------------------------

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

    // From finance.md "Market Hours" section
    // US market hours (default)
    let hours = finance::hours(None).await.unwrap();
    assert!(!hours.markets.is_empty());

    // Japan market hours
    let hours = finance::hours(Some("JP")).await.unwrap();
    for market in &hours.markets {
        println!("{}: {}", market.name, market.status);
        println!("  Open: {:?}", market.open);
        println!("  Close: {:?}", market.close);
    }
}

// ---------------------------------------------------------------------------
// Network tests — Indices (from finance.md "Indices" section)
// ---------------------------------------------------------------------------

#[tokio::test]
#[ignore = "requires network access"]
async fn test_indices() {
    use finance_query::{IndicesRegion, finance};

    // From finance.md "Indices" section

    // All world indices
    let all = finance::indices(None).await.unwrap();
    println!("Fetched {} indices", all.success_count());
    assert!(all.success_count() > 0);

    // Only Americas indices (^DJI, ^GSPC, ^IXIC, etc.)
    let americas = finance::indices(Some(IndicesRegion::Americas))
        .await
        .unwrap();
    for (symbol, quote) in &americas.quotes {
        if let (Some(price_fv), Some(change_pct_fv)) = (
            &quote.regular_market_price,
            &quote.regular_market_change_percent,
        ) && let (Some(price), Some(change_pct)) = (price_fv.raw, change_pct_fv.raw)
        {
            println!("{}: {:.2} ({:+.2}%)", symbol, price, change_pct);
        }
    }

    // Other regions
    let _europe = finance::indices(Some(IndicesRegion::Europe)).await.unwrap();
    let _asia = finance::indices(Some(IndicesRegion::AsiaPacific))
        .await
        .unwrap();
}

// ---------------------------------------------------------------------------
// Network tests — Predefined Screeners (from finance.md "Predefined Screeners" section)
// ---------------------------------------------------------------------------

#[tokio::test]
#[ignore = "requires network access"]
async fn test_screener_predefined() {
    use finance_query::{Screener, finance};

    // From finance.md "Predefined Screeners" section

    // Top gainers
    let gainers = finance::screener(Screener::DayGainers, 25).await.unwrap();

    // Most actives
    let actives = finance::screener(Screener::MostActives, 25).await.unwrap();

    // Day losers
    let losers = finance::screener(Screener::DayLosers, 25).await.unwrap();

    // Process results — regular_market_change_percent is FormattedValue<f64> (not Option)
    for quote in &gainers.quotes {
        let change_pct = quote.regular_market_change_percent.raw.unwrap_or(0.0);
        println!("{}: {:+.2}%", quote.symbol, change_pct);
    }

    assert!(!gainers.quotes.is_empty());
    let _ = actives;
    let _ = losers;
}

// ---------------------------------------------------------------------------
// Network tests — Custom Screener (from finance.md "Custom Screeners" section)
// ---------------------------------------------------------------------------

#[tokio::test]
#[ignore = "requires network access"]
async fn test_custom_screener() {
    use finance_query::{EquityField, EquityScreenerQuery, ScreenerFieldExt, finance};

    // From finance.md "Custom Screeners" section — find US large-cap tech stocks
    let query = EquityScreenerQuery::new()
        .size(50)
        .sort_by(EquityField::IntradayMarketCap, false)
        .add_condition(EquityField::Region.eq_str("us"))
        .add_condition(EquityField::Sector.eq_str("Technology"))
        .add_condition(EquityField::IntradayMarketCap.gt(10_000_000_000.0))
        .add_condition(EquityField::AvgDailyVol3M.gt(1_000_000.0));

    let results = finance::custom_screener(query).await.unwrap();
    println!("Found {} stocks", results.quotes.len());
    assert!(!results.quotes.is_empty());
}

// ---------------------------------------------------------------------------
// Network tests — Sector full fields (from finance.md "Sectors" section)
// ---------------------------------------------------------------------------

#[tokio::test]
#[ignore = "requires network access"]
async fn test_sector_full_fields() {
    use finance_query::{Sector, finance};

    // From finance.md "Sectors" section — full field access pattern
    let tech = finance::sector(Sector::Technology).await.unwrap();

    println!("Sector: {}", tech.name);
    if let Some(overview) = &tech.overview {
        if let Some(count) = overview.companies_count {
            println!("  Companies: {}", count);
        }
        if let Some(market_cap_fv) = &overview.market_cap
            && let Some(market_cap) = market_cap_fv.raw
        {
            println!("  Market Cap: ${:.2}B", market_cap / 1_000_000_000.0);
        }
    }

    // Top companies in the sector
    println!("Top companies:");
    for company in tech.top_companies.iter().take(10) {
        println!(
            "  {} - {}",
            company.symbol,
            company.name.as_deref().unwrap_or("N/A")
        );
    }

    // Sector ETFs
    println!("Sector ETFs: {}", tech.top_etfs.len());

    // Industries in this sector
    println!("Industries: {}", tech.industries.len());

    assert!(!tech.name.is_empty());
}

// ---------------------------------------------------------------------------
// Network tests — Industry full fields (from finance.md "Industries" section)
// ---------------------------------------------------------------------------

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

    // From finance.md "Industries" section — full field access pattern
    let semiconductors = finance::industry("semiconductors").await.unwrap();

    println!("Industry: {}", semiconductors.name);
    if let Some(overview) = &semiconductors.overview {
        println!("  Companies: {:?}", overview.companies_count);
        println!("  Market Cap: ${:?}B", overview.market_cap.map(|m| m / 1e9));
    }

    // Top companies
    for company in semiconductors.top_companies.iter().take(5) {
        println!(
            "  {} - {}",
            company.symbol,
            company.name.as_deref().unwrap_or("")
        );
    }

    assert!(!semiconductors.name.is_empty());
}

// ---------------------------------------------------------------------------
// Network tests — General News (from finance.md "News & Transcripts" section)
// ---------------------------------------------------------------------------

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

    // From finance.md "General News" section
    let news = finance::news().await.unwrap();

    for article in news.iter().take(10) {
        println!("{}", article.title);
        println!("  Source: {}", article.source);
        println!("  Time: {}", article.time);
        println!("  Link: {}", article.link);
    }

    assert!(!news.is_empty());
}

// ---------------------------------------------------------------------------
// Network tests — Earnings Transcripts (from finance.md "Earnings Transcripts" section)
// ---------------------------------------------------------------------------

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

    // From finance.md "Earnings Transcripts" section

    // Get the latest transcript
    let latest = finance::earnings_transcript("AAPL", None, None)
        .await
        .unwrap();
    println!("Quarter: {} {}", latest.quarter(), latest.year());
    println!(
        "Speakers: {}",
        latest.transcript_content.speaker_mapping.len()
    );

    // Get specific quarter
    let _q4_2024 = finance::earnings_transcript("AAPL", Some("Q4"), Some(2024))
        .await
        .unwrap();

    // Get all available transcripts
    let all = finance::earnings_transcripts("MSFT", None).await.unwrap();
    println!("Found {} transcripts", all.len());

    // Get only recent transcripts
    let recent = finance::earnings_transcripts("NVDA", Some(5))
        .await
        .unwrap();
    for t in &recent {
        println!(
            "{}: {} {}",
            t.title,
            t.transcript.quarter(),
            t.transcript.year()
        );
    }
}

// ---------------------------------------------------------------------------
// Network tests — Exchanges (from finance.md "Exchanges" section)
// ---------------------------------------------------------------------------

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

    // From finance.md "Exchanges" section
    let exchanges = finance::exchanges().await.unwrap();
    for exchange in &exchanges {
        println!(
            "{} - {} (suffix: {})",
            exchange.country, exchange.market, exchange.suffix
        );
    }
    assert!(!exchanges.is_empty());
}

// ---------------------------------------------------------------------------
// Network tests — Currencies (from finance.md "Currencies" section)
// ---------------------------------------------------------------------------

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

    // From finance.md "Currencies" section
    let currencies = finance::currencies().await.unwrap();
    for currency in &currencies {
        let symbol = currency.symbol.as_deref().unwrap_or("N/A");
        let name = currency.short_name.as_deref().unwrap_or("N/A");
        println!("{}: {}", symbol, name);
    }
    assert!(!currencies.is_empty());
}