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
use crate::models::screeners::condition::{
    LogicalOperator, QueryCondition, QueryGroup, QueryOperand, ScreenerField, ScreenerFieldExt,
};
use crate::models::screeners::fields::{EquityField, FundField};
use serde::{Deserialize, Serialize};

// ============================================================================
// QuoteType
// ============================================================================

/// Quote type for custom screener queries.
///
/// Yahoo Finance only supports `EQUITY` and `MUTUALFUND` for custom screener queries.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum QuoteType {
    /// Equity (stocks) — use [`EquityScreenerQuery`] with [`EquityField`] conditions.
    #[default]
    #[serde(rename = "EQUITY")]
    Equity,
    /// Mutual funds — use [`FundScreenerQuery`] with [`FundField`] conditions.
    #[serde(rename = "MUTUALFUND")]
    MutualFund,
}

impl std::str::FromStr for QuoteType {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().replace(['-', '_'], "").as_str() {
            "equity" | "stock" | "stocks" => Ok(QuoteType::Equity),
            "mutualfund" | "fund" | "funds" => Ok(QuoteType::MutualFund),
            _ => Err(()),
        }
    }
}

// ============================================================================
// SortType
// ============================================================================

/// Sort direction for screener results.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum SortType {
    /// Sort ascending (smallest first) — `"ASC"`
    #[serde(rename = "ASC")]
    Asc,
    /// Sort descending (largest first) — `"DESC"`
    #[default]
    #[serde(rename = "DESC")]
    Desc,
}

impl std::str::FromStr for SortType {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "asc" | "ascending" => Ok(SortType::Asc),
            "desc" | "descending" => Ok(SortType::Desc),
            _ => Err(()),
        }
    }
}

// ============================================================================
// ScreenerQuery<F>
// ============================================================================

/// A typed custom screener query for Yahoo Finance.
///
/// The type parameter `F` determines which field set is valid for this query.
/// Use the type aliases for the common cases:
/// - [`EquityScreenerQuery`] — for stock screeners
/// - [`FundScreenerQuery`] — for mutual fund screeners
///
/// # Example
///
/// ```
/// use finance_query::{EquityField, EquityScreenerQuery, ScreenerFieldExt};
///
/// // Find US large-cap value stocks
/// let query = EquityScreenerQuery::new()
///     .size(25)
///     .sort_by(EquityField::IntradayMarketCap, false)
///     .add_condition(EquityField::Region.eq_str("us"))
///     .add_condition(EquityField::AvgDailyVol3M.gt(200_000.0))
///     .add_condition(EquityField::PeRatio.between(10.0, 25.0))
///     .add_condition(EquityField::IntradayMarketCap.gt(10_000_000_000.0))
///     .include_fields(vec![
///         EquityField::Ticker,
///         EquityField::CompanyShortName,
///         EquityField::IntradayPrice,
///         EquityField::PeRatio,
///         EquityField::IntradayMarketCap,
///     ]);
/// ```
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ScreenerQuery<F: ScreenerField = EquityField> {
    /// Number of results to return (default: 25, max: 250).
    pub size: u32,

    /// Starting offset for pagination (default: 0).
    pub offset: u32,

    /// Sort direction.
    pub sort_type: SortType,

    /// Field to sort by.
    pub sort_field: F,

    /// Fields to include in the response.
    pub include_fields: Vec<F>,

    /// Top-level logical operator combining all conditions.
    pub top_operator: LogicalOperator,

    /// The nested condition tree.
    pub query: QueryGroup<F>,

    /// Quote type — determines which Yahoo Finance screener endpoint is used.
    pub quote_type: QuoteType,
}

/// Type alias for equity (stock) screener queries.
///
/// Use [`EquityField`] variants to build conditions.
pub type EquityScreenerQuery = ScreenerQuery<EquityField>;

/// Type alias for mutual fund screener queries.
///
/// Use [`FundField`] variants to build conditions.
pub type FundScreenerQuery = ScreenerQuery<FundField>;

// ============================================================================
// Default impls
// ============================================================================

impl Default for ScreenerQuery<EquityField> {
    fn default() -> Self {
        Self {
            size: 25,
            offset: 0,
            sort_type: SortType::Desc,
            sort_field: EquityField::IntradayMarketCap,
            include_fields: vec![
                EquityField::Ticker,
                EquityField::CompanyShortName,
                EquityField::IntradayPrice,
                EquityField::IntradayPriceChange,
                EquityField::PercentChange,
                EquityField::IntradayMarketCap,
                EquityField::DayVolume,
                EquityField::AvgDailyVol3M,
                EquityField::PeRatio,
                EquityField::FiftyTwoWkPctChange,
            ],
            top_operator: LogicalOperator::And,
            query: QueryGroup::new(LogicalOperator::And),
            quote_type: QuoteType::Equity,
        }
    }
}

impl Default for ScreenerQuery<FundField> {
    fn default() -> Self {
        Self {
            size: 25,
            offset: 0,
            sort_type: SortType::Desc,
            sort_field: FundField::IntradayPrice,
            include_fields: vec![
                FundField::Ticker,
                FundField::CompanyShortName,
                FundField::IntradayPrice,
                FundField::IntradayPriceChange,
                FundField::CategoryName,
                FundField::PerformanceRating,
                FundField::RiskRating,
            ],
            top_operator: LogicalOperator::And,
            query: QueryGroup::new(LogicalOperator::And),
            quote_type: QuoteType::MutualFund,
        }
    }
}

// ============================================================================
// Shared builder methods
// ============================================================================

impl<F: ScreenerField> ScreenerQuery<F> {
    /// Create a new screener query with default settings.
    pub fn new() -> Self
    where
        Self: Default,
    {
        Self::default()
    }

    /// Set the number of results to return (capped at 250).
    pub fn size(mut self, size: u32) -> Self {
        self.size = size.min(250);
        self
    }

    /// Set the pagination offset.
    pub fn offset(mut self, offset: u32) -> Self {
        self.offset = offset;
        self
    }

    /// Set the field to sort by and the sort direction.
    ///
    /// # Example
    ///
    /// ```
    /// use finance_query::{EquityField, EquityScreenerQuery};
    ///
    /// let query = EquityScreenerQuery::new()
    ///     .sort_by(EquityField::PeRatio, true);  // ascending P/E
    /// ```
    pub fn sort_by(mut self, field: F, ascending: bool) -> Self {
        self.sort_field = field;
        self.sort_type = if ascending {
            SortType::Asc
        } else {
            SortType::Desc
        };
        self
    }

    /// Set the top-level logical operator (AND or OR).
    pub fn top_operator(mut self, op: LogicalOperator) -> Self {
        self.top_operator = op;
        self
    }

    /// Set which fields to include in the response.
    pub fn include_fields(mut self, fields: Vec<F>) -> Self {
        self.include_fields = fields;
        self
    }

    /// Add a field to include in the response.
    pub fn add_include_field(mut self, field: F) -> Self {
        self.include_fields.push(field);
        self
    }

    /// Add a typed filter condition to this query (ANDed with all others).
    ///
    /// Conditions are added directly as operands of the top-level AND group,
    /// matching the format Yahoo Finance's screener API expects. Use
    /// [`add_or_conditions`](Self::add_or_conditions) when you need to match
    /// any of several values for the same field.
    ///
    /// # Example
    ///
    /// ```
    /// use finance_query::{EquityField, EquityScreenerQuery, ScreenerFieldExt};
    ///
    /// let query = EquityScreenerQuery::new()
    ///     .add_condition(EquityField::Region.eq_str("us"))
    ///     .add_condition(EquityField::PeRatio.between(10.0, 25.0))
    ///     .add_condition(EquityField::AvgDailyVol3M.gt(200_000.0));
    /// ```
    pub fn add_condition(mut self, condition: QueryCondition<F>) -> Self {
        self.query.add_operand(QueryOperand::Condition(condition));
        self
    }

    /// Add multiple conditions that are OR'd together.
    ///
    /// # Example
    ///
    /// ```
    /// use finance_query::{EquityField, EquityScreenerQuery, ScreenerFieldExt};
    ///
    /// // Accept US or GB region
    /// let query = EquityScreenerQuery::new()
    ///     .add_or_conditions(vec![
    ///         EquityField::Region.eq_str("us"),
    ///         EquityField::Region.eq_str("gb"),
    ///     ]);
    /// ```
    pub fn add_or_conditions(mut self, conditions: Vec<QueryCondition<F>>) -> Self {
        let mut or_group = QueryGroup::new(LogicalOperator::Or);
        for condition in conditions {
            or_group.add_operand(QueryOperand::Condition(condition));
        }
        self.query.add_operand(QueryOperand::Group(or_group));
        self
    }
}

// ============================================================================
// Equity preset constructors
// ============================================================================

impl ScreenerQuery<EquityField> {
    /// Preset: US stocks sorted by short interest percentage of float.
    ///
    /// Filters: US region, average daily volume > 200K.
    ///
    /// ```no_run
    /// use finance_query::{EquityScreenerQuery, finance};
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let results = finance::custom_screener(EquityScreenerQuery::most_shorted()).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn most_shorted() -> Self {
        Self::new()
            .sort_by(EquityField::ShortPctFloat, false)
            .add_condition(EquityField::Region.eq_str("us"))
            .add_condition(EquityField::AvgDailyVol3M.gt(200_000.0))
    }

    /// Preset: US stocks with forward dividend yield > 3%, sorted by yield descending.
    ///
    /// Filters: US region, forward dividend yield > 3%, average daily volume > 100K.
    ///
    /// ```no_run
    /// use finance_query::{EquityScreenerQuery, finance};
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let results = finance::custom_screener(EquityScreenerQuery::high_dividend()).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn high_dividend() -> Self {
        Self::new()
            .sort_by(EquityField::ForwardDivYield, false)
            .add_condition(EquityField::Region.eq_str("us"))
            .add_condition(EquityField::ForwardDivYield.gt(3.0))
            .add_condition(EquityField::AvgDailyVol3M.gt(100_000.0))
    }

    /// Preset: US large-cap stocks with positive EPS growth, sorted by market cap.
    ///
    /// Filters: US region, market cap > $10B, positive EPS growth.
    ///
    /// ```no_run
    /// use finance_query::{EquityScreenerQuery, finance};
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let results = finance::custom_screener(EquityScreenerQuery::large_cap_growth()).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn large_cap_growth() -> Self {
        Self::new()
            .sort_by(EquityField::IntradayMarketCap, false)
            .add_condition(EquityField::Region.eq_str("us"))
            .add_condition(EquityField::IntradayMarketCap.gt(10_000_000_000.0))
            .add_condition(EquityField::EpsGrowth.gt(0.0))
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::models::screeners::condition::ScreenerFieldExt;

    #[test]
    fn test_default_equity_query() {
        let query = EquityScreenerQuery::new();
        assert_eq!(query.size, 25);
        assert_eq!(query.offset, 0);
        assert_eq!(query.quote_type, QuoteType::Equity);
        assert_eq!(query.sort_field, EquityField::IntradayMarketCap);
    }

    #[test]
    fn test_default_fund_query() {
        let query = FundScreenerQuery::new();
        assert_eq!(query.size, 25);
        assert_eq!(query.quote_type, QuoteType::MutualFund);
        assert_eq!(query.sort_field, FundField::IntradayPrice);
    }

    #[test]
    fn test_most_shorted_preset() {
        let query = EquityScreenerQuery::most_shorted();
        assert_eq!(query.sort_field, EquityField::ShortPctFloat);
        assert_eq!(query.sort_type, SortType::Desc);
    }

    #[test]
    fn test_high_dividend_preset() {
        let query = EquityScreenerQuery::high_dividend();
        assert_eq!(query.sort_field, EquityField::ForwardDivYield);
    }

    #[test]
    fn test_large_cap_growth_preset() {
        let query = EquityScreenerQuery::large_cap_growth();
        assert_eq!(query.sort_field, EquityField::IntradayMarketCap);
    }

    #[test]
    fn test_sort_by_typed_field() {
        let query = EquityScreenerQuery::new().sort_by(EquityField::PeRatio, true);
        assert_eq!(query.sort_field, EquityField::PeRatio);
        assert_eq!(query.sort_type, SortType::Asc);
    }

    #[test]
    fn test_size_capped_at_250() {
        let query = EquityScreenerQuery::new().size(9999);
        assert_eq!(query.size, 250);
    }

    #[test]
    fn test_query_serializes_sort_field_as_string() {
        let query = EquityScreenerQuery::new().sort_by(EquityField::PeRatio, false);
        let json = serde_json::to_value(&query).unwrap();
        assert_eq!(json["sortField"], "peratio.lasttwelvemonths");
        assert_eq!(json["sortType"], "DESC");
    }

    #[test]
    fn test_query_serializes_include_fields_as_strings() {
        let query = EquityScreenerQuery::new()
            .include_fields(vec![EquityField::Ticker, EquityField::PeRatio]);
        let json = serde_json::to_value(&query).unwrap();
        let fields = json["includeFields"].as_array().unwrap();
        assert_eq!(fields[0], "ticker");
        assert_eq!(fields[1], "peratio.lasttwelvemonths");
    }

    #[test]
    fn test_add_condition_adds_directly_to_and_group() {
        let query = EquityScreenerQuery::new().add_condition(EquityField::Region.eq_str("us"));
        let json = serde_json::to_value(&query).unwrap();
        // condition is a direct operand of the AND group (no OR wrapper)
        let outer_operands = json["query"]["operands"].as_array().unwrap();
        assert_eq!(outer_operands.len(), 1);
        assert_eq!(outer_operands[0]["operator"], "eq");
        assert_eq!(outer_operands[0]["operands"][0], "region");
    }

    #[test]
    fn test_full_query_serialization() {
        let query = EquityScreenerQuery::new()
            .size(10)
            .add_condition(EquityField::Region.eq_str("us"))
            .add_condition(EquityField::AvgDailyVol3M.gt(200_000.0));

        let json = serde_json::to_string(&query).unwrap();
        assert!(json.contains("\"size\":10"));
        assert!(json.contains("\"region\""));
        assert!(json.contains("\"avgdailyvol3m\""));
        assert!(json.contains("\"EQUITY\""));
    }
}