eeyf 0.1.0

Eric Evans' Yahoo Finance API - A rate-limited, reliable Rust adapter for Yahoo Finance API
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
//! Query DSL for building custom screener queries
//!
//! This module provides a type-safe DSL for building complex screener queries
//! that are translated to Yahoo Finance's JSON query format.
//!
//! # Examples
//!
//! ```
//! use eeyf::screener::query::{Query, Field};
//!
//! // Find large tech stocks with strong gains
//! let query = Query::and(vec![
//!     Query::eq(Field::Region, "us"),
//!     Query::eq(Field::Sector, "Technology"),
//!     Query::gte(Field::IntradayMarketCap, 10_000_000_000.0),
//!     Query::gt(Field::PercentChange, 3.0),
//! ]);
//! ```

use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue;

/// Fields that can be used in screener queries
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Field {
    // Price fields
    /// Current intraday price
    IntradayPrice,
    /// Percentage change
    PercentChange,
    /// Price change amount
    PriceChange,
    
    // Volume fields
    /// Current day volume
    DayVolume,
    /// Average daily volume (3 months)
    AvgDailyVolume3Month,
    /// Average daily volume (10 days)
    AvgDailyVolume10Day,
    
    // Market cap
    /// Current intraday market cap
    IntradayMarketCap,
    
    // Valuation ratios
    /// Trailing P/E ratio (last 12 months)
    PERatioTTM,
    /// Forward P/E ratio
    PERatioForward,
    /// PEG ratio (5 year expected)
    PEGRatio5Y,
    /// Price to book ratio
    PriceToBook,
    /// Price to sales ratio
    PriceToSales,
    
    // Growth metrics
    /// EPS growth (last 12 months)
    EPSGrowthTTM,
    /// EPS growth (quarterly YoY)
    EPSGrowthQuarterlyYoY,
    /// Revenue growth (last 12 months)
    RevenueGrowthTTM,
    
    // Profitability
    /// Profit margin
    ProfitMargin,
    /// Operating margin
    OperatingMargin,
    /// Return on equity
    ReturnOnEquity,
    /// Return on assets
    ReturnOnAssets,
    
    // Dividends
    /// Dividend yield
    DividendYield,
    /// Trailing annual dividend rate
    TrailingAnnualDividendRate,
    /// Trailing annual dividend yield
    TrailingAnnualDividendYield,
    
    // 52-week metrics
    /// 52-week high price
    FiftyTwoWeekHigh,
    /// 52-week low price
    FiftyTwoWeekLow,
    /// Percentage from 52-week high
    PercentFromFiftyTwoWeekHigh,
    /// Percentage from 52-week low
    PercentFromFiftyTwoWeekLow,
    
    // Beta
    /// Beta (volatility measure)
    Beta,
    
    // Categorical fields
    /// Geographic region
    Region,
    /// Market sector
    Sector,
    /// Industry
    Industry,
    /// Exchange
    Exchange,
    /// Quote type (EQUITY, ETF, etc.)
    QuoteType,
}

impl Field {
    /// Get the Yahoo Finance field name for this field
    pub fn yahoo_name(&self) -> &'static str {
        match self {
            // Price
            Field::IntradayPrice => "intradayprice",
            Field::PercentChange => "percentchange",
            Field::PriceChange => "pricechange",
            
            // Volume
            Field::DayVolume => "dayvolume",
            Field::AvgDailyVolume3Month => "avgdailyvol3m",
            Field::AvgDailyVolume10Day => "avgdailyvol10d",
            
            // Market cap
            Field::IntradayMarketCap => "intradaymarketcap",
            
            // Valuation
            Field::PERatioTTM => "peratio.lasttwelvemonths",
            Field::PERatioForward => "peratio.forward",
            Field::PEGRatio5Y => "pegratio_5y",
            Field::PriceToBook => "pricetobook",
            Field::PriceToSales => "pricetosales",
            
            // Growth
            Field::EPSGrowthTTM => "epsgrowth.lasttwelvemonths",
            Field::EPSGrowthQuarterlyYoY => "epsgrowth.quarterly.yoy",
            Field::RevenueGrowthTTM => "revenuegrowth.lasttwelvemonths",
            
            // Profitability
            Field::ProfitMargin => "profitmargin",
            Field::OperatingMargin => "operatingmargin",
            Field::ReturnOnEquity => "returnonequity",
            Field::ReturnOnAssets => "returnonassets",
            
            // Dividends
            Field::DividendYield => "dividendyield",
            Field::TrailingAnnualDividendRate => "trailingannualdividendrate",
            Field::TrailingAnnualDividendYield => "trailingannualdividendyield",
            
            // 52-week
            Field::FiftyTwoWeekHigh => "fiftytwoweek.high",
            Field::FiftyTwoWeekLow => "fiftytwoweek.low",
            Field::PercentFromFiftyTwoWeekHigh => "percentfromfiftytwoweek.high",
            Field::PercentFromFiftyTwoWeekLow => "percentfromfiftytwoweek.low",
            
            // Beta
            Field::Beta => "beta",
            
            // Categorical
            Field::Region => "region",
            Field::Sector => "sector",
            Field::Industry => "industry",
            Field::Exchange => "exchange",
            Field::QuoteType => "quotetype",
        }
    }

    /// Check if this field is numeric (vs categorical)
    pub fn is_numeric(&self) -> bool {
        !matches!(
            self,
            Field::Region | Field::Sector | Field::Industry | Field::Exchange | Field::QuoteType
        )
    }
}

/// Operators for building queries
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Operator {
    /// Logical AND
    And,
    /// Logical OR
    Or,
    /// Greater than
    GreaterThan,
    /// Less than
    LessThan,
    /// Greater than or equal
    GreaterThanOrEqual,
    /// Less than or equal
    LessThanOrEqual,
    /// Equal to
    Equal,
    /// Between two values
    Between,
    /// In a list of values
    In,
}

impl Operator {
    /// Get the Yahoo Finance operator name
    pub fn yahoo_name(&self) -> &'static str {
        match self {
            Operator::And => "and",
            Operator::Or => "or",
            Operator::GreaterThan => "gt",
            Operator::LessThan => "lt",
            Operator::GreaterThanOrEqual => "gte",
            Operator::LessThanOrEqual => "lte",
            Operator::Equal => "eq",
            Operator::Between => "btwn",
            Operator::In => "in",
        }
    }
}

/// Value type for query operands
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum QueryValue {
    /// String value
    String(String),
    /// Numeric value
    Number(f64),
    /// Integer value
    Integer(i64),
    /// Array of values
    Array(Vec<QueryValue>),
}

impl From<&str> for QueryValue {
    fn from(s: &str) -> Self {
        QueryValue::String(s.to_string())
    }
}

impl From<String> for QueryValue {
    fn from(s: String) -> Self {
        QueryValue::String(s)
    }
}

impl From<f64> for QueryValue {
    fn from(n: f64) -> Self {
        QueryValue::Number(n)
    }
}

impl From<i64> for QueryValue {
    fn from(n: i64) -> Self {
        QueryValue::Integer(n)
    }
}

impl From<Vec<QueryValue>> for QueryValue {
    fn from(arr: Vec<QueryValue>) -> Self {
        QueryValue::Array(arr)
    }
}

/// A screener query that can be built using the DSL
#[derive(Debug, Clone)]
pub struct Query {
    operator: Operator,
    operands: Vec<QueryOperand>,
}

/// Operand for a query (can be a field or nested query)
#[derive(Debug, Clone)]
enum QueryOperand {
    Field(Field),
    Value(QueryValue),
    NestedQuery(Box<Query>),
}

impl Query {
    /// Create a logical AND query combining multiple queries
    ///
    /// # Example
    ///
    /// ```
    /// use eeyf::screener::query::{Query, Field};
    ///
    /// let query = Query::and(vec![
    ///     Query::eq(Field::Region, "us"),
    ///     Query::gt(Field::PercentChange, 5.0),
    /// ]);
    /// ```
    pub fn and(queries: Vec<Query>) -> Self {
        Self {
            operator: Operator::And,
            operands: queries.into_iter().map(|q| QueryOperand::NestedQuery(Box::new(q))).collect(),
        }
    }

    /// Create a logical OR query combining multiple queries
    pub fn or(queries: Vec<Query>) -> Self {
        Self {
            operator: Operator::Or,
            operands: queries.into_iter().map(|q| QueryOperand::NestedQuery(Box::new(q))).collect(),
        }
    }

    /// Create an equality query (field == value)
    ///
    /// # Example
    ///
    /// ```
    /// use eeyf::screener::query::{Query, Field};
    ///
    /// let query = Query::eq(Field::Region, "us");
    /// ```
    pub fn eq<V: Into<QueryValue>>(field: Field, value: V) -> Self {
        Self {
            operator: Operator::Equal,
            operands: vec![QueryOperand::Field(field), QueryOperand::Value(value.into())],
        }
    }

    /// Create a greater-than query (field > value)
    pub fn gt<V: Into<QueryValue>>(field: Field, value: V) -> Self {
        Self {
            operator: Operator::GreaterThan,
            operands: vec![QueryOperand::Field(field), QueryOperand::Value(value.into())],
        }
    }

    /// Create a less-than query (field < value)
    pub fn lt<V: Into<QueryValue>>(field: Field, value: V) -> Self {
        Self {
            operator: Operator::LessThan,
            operands: vec![QueryOperand::Field(field), QueryOperand::Value(value.into())],
        }
    }

    /// Create a greater-than-or-equal query (field >= value)
    pub fn gte<V: Into<QueryValue>>(field: Field, value: V) -> Self {
        Self {
            operator: Operator::GreaterThanOrEqual,
            operands: vec![QueryOperand::Field(field), QueryOperand::Value(value.into())],
        }
    }

    /// Create a less-than-or-equal query (field <= value)
    pub fn lte<V: Into<QueryValue>>(field: Field, value: V) -> Self {
        Self {
            operator: Operator::LessThanOrEqual,
            operands: vec![QueryOperand::Field(field), QueryOperand::Value(value.into())],
        }
    }

    /// Create a between query (min <= field <= max)
    ///
    /// # Example
    ///
    /// ```
    /// use eeyf::screener::query::{Query, Field};
    ///
    /// // Find stocks priced between $10 and $50
    /// let query = Query::between(Field::IntradayPrice, 10.0, 50.0);
    /// ```
    pub fn between<V1: Into<QueryValue>, V2: Into<QueryValue>>(
        field: Field,
        min: V1,
        max: V2,
    ) -> Self {
        Self {
            operator: Operator::Between,
            operands: vec![
                QueryOperand::Field(field),
                QueryOperand::Value(QueryValue::Array(vec![min.into(), max.into()])),
            ],
        }
    }

    /// Create an IN query (field IN [values...])
    ///
    /// # Example
    ///
    /// ```
    /// use eeyf::screener::query::{Query, Field, QueryValue};
    ///
    /// // Find stocks in specific sectors
    /// let query = Query::in_list(
    ///     Field::Sector,
    ///     vec![
    ///         QueryValue::from("Technology"),
    ///         QueryValue::from("Healthcare"),
    ///     ],
    /// );
    /// ```
    pub fn in_list(field: Field, values: Vec<QueryValue>) -> Self {
        Self {
            operator: Operator::In,
            operands: vec![
                QueryOperand::Field(field),
                QueryOperand::Value(QueryValue::Array(values)),
            ],
        }
    }

    /// Convert this query to Yahoo Finance's JSON format
    pub(crate) fn to_json(&self) -> JsonValue {
        let operator_name = self.operator.yahoo_name();
        
        let operands: Vec<JsonValue> = self
            .operands
            .iter()
            .map(|operand| match operand {
                QueryOperand::Field(field) => JsonValue::String(field.yahoo_name().to_string()),
                QueryOperand::Value(value) => serde_json::to_value(value).unwrap(),
                QueryOperand::NestedQuery(query) => query.to_json(),
            })
            .collect();

        serde_json::json!({
            "operator": operator_name,
            "operands": operands,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_field_yahoo_names() {
        assert_eq!(Field::IntradayPrice.yahoo_name(), "intradayprice");
        assert_eq!(Field::PercentChange.yahoo_name(), "percentchange");
        assert_eq!(Field::IntradayMarketCap.yahoo_name(), "intradaymarketcap");
    }

    #[test]
    fn test_field_is_numeric() {
        assert!(Field::IntradayPrice.is_numeric());
        assert!(Field::PercentChange.is_numeric());
        assert!(!Field::Region.is_numeric());
        assert!(!Field::Sector.is_numeric());
    }

    #[test]
    fn test_operator_yahoo_names() {
        assert_eq!(Operator::And.yahoo_name(), "and");
        assert_eq!(Operator::GreaterThan.yahoo_name(), "gt");
        assert_eq!(Operator::Equal.yahoo_name(), "eq");
    }

    #[test]
    fn test_query_value_conversions() {
        let v1: QueryValue = "test".into();
        assert!(matches!(v1, QueryValue::String(_)));

        let v2: QueryValue = 42.5.into();
        assert!(matches!(v2, QueryValue::Number(_)));

        let v3: QueryValue = 100i64.into();
        assert!(matches!(v3, QueryValue::Integer(_)));
    }

    #[test]
    fn test_simple_query_to_json() {
        let query = Query::eq(Field::Region, "us");
        let json = query.to_json();

        assert_eq!(json["operator"], "eq");
        assert_eq!(json["operands"][0], "region");
        assert_eq!(json["operands"][1], "us");
    }

    #[test]
    fn test_comparison_query_to_json() {
        let query = Query::gt(Field::PercentChange, 5.0);
        let json = query.to_json();

        assert_eq!(json["operator"], "gt");
        assert_eq!(json["operands"][0], "percentchange");
        assert_eq!(json["operands"][1], 5.0);
    }

    #[test]
    fn test_between_query_to_json() {
        let query = Query::between(Field::IntradayPrice, 10.0, 50.0);
        let json = query.to_json();

        assert_eq!(json["operator"], "btwn");
        assert_eq!(json["operands"][0], "intradayprice");
        assert!(json["operands"][1].is_array());
    }

    #[test]
    fn test_and_query_to_json() {
        let query = Query::and(vec![
            Query::eq(Field::Region, "us"),
            Query::gt(Field::PercentChange, 3.0),
        ]);

        let json = query.to_json();
        assert_eq!(json["operator"], "and");
        assert!(json["operands"].is_array());
        assert_eq!(json["operands"].as_array().unwrap().len(), 2);
    }

    #[test]
    fn test_complex_nested_query() {
        let query = Query::and(vec![
            Query::eq(Field::Region, "us"),
            Query::or(vec![
                Query::eq(Field::Sector, "Technology"),
                Query::eq(Field::Sector, "Healthcare"),
            ]),
            Query::gt(Field::IntradayMarketCap, 1_000_000_000.0),
        ]);

        let json = query.to_json();
        assert_eq!(json["operator"], "and");
        assert_eq!(json["operands"].as_array().unwrap().len(), 3);
        
        // Check nested OR
        let nested_or = &json["operands"][1];
        assert_eq!(nested_or["operator"], "or");
    }
}