yfinance-rs 0.9.1

Ergonomic Rust client for Yahoo Finance, supporting historical prices, real-time streaming, options, fundamentals, and more.
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
use std::marker::PhantomData;

use serde_json::{Map, Value};
use url::Url;

use super::fields::{equity_fields, etf_fields, fund_fields};
use super::predefined::PredefinedScreener;
use super::query::{
    Equity, EquityQuery, Etf, EtfQuery, Fund, FundQuery, Predefined, ResultOffset, ScreenerCount,
    ScreenerQuery, SortDirection, SortField, YahooQuoteType,
};
use super::response::{ScreenerResponse, parse_screener_body_with_diagnostics};
use crate::YfResponse;
use crate::core::CallOptions;
use crate::core::client::{CacheEndpoint, CacheMode};
use crate::{YfClient, YfError};

const DEFAULT_SCREENER_BASE: &str = "https://query1.finance.yahoo.com/v1/finance/screener";
const DEFAULT_PREDEFINED_SCREENER_BASE: &str =
    "https://query1.finance.yahoo.com/v1/finance/screener/predefined/saved";

#[derive(Debug, Clone)]
enum RequestKind {
    Predefined(PredefinedScreener),
    Custom {
        query: Value,
        quote_type: YahooQuoteType,
        sort_field: &'static str,
        sort_direction: SortDirection,
    },
}

#[derive(Debug, Clone)]
pub(super) struct CustomParts {
    query: Value,
    quote_type: YahooQuoteType,
    sort_field: &'static str,
    sort_direction: SortDirection,
}

impl CustomParts {
    pub(super) fn equity(
        sort_field: SortField<Equity>,
        sort_direction: SortDirection,
        query: EquityQuery,
    ) -> Self {
        Self {
            query: query.into_wire_value(),
            quote_type: YahooQuoteType::Equity,
            sort_field: sort_field.key(),
            sort_direction,
        }
    }

    pub(super) fn fund(
        sort_field: SortField<Fund>,
        sort_direction: SortDirection,
        query: FundQuery,
    ) -> Self {
        Self {
            query: query.into_wire_value(),
            quote_type: YahooQuoteType::MutualFund,
            sort_field: sort_field.key(),
            sort_direction,
        }
    }

    pub(super) fn etf(
        sort_field: SortField<Etf>,
        sort_direction: SortDirection,
        query: EtfQuery,
    ) -> Self {
        Self {
            query: query.into_wire_value(),
            quote_type: YahooQuoteType::Etf,
            sort_field: sort_field.key(),
            sort_direction,
        }
    }
}

/// Builder for Yahoo Finance screener requests.
#[derive(Debug, Clone)]
pub struct ScreenerBuilder<U = Predefined> {
    client: YfClient,
    screener_base: Url,
    predefined_base: Url,
    kind: RequestKind,
    count: Option<ScreenerCount>,
    offset: Option<ResultOffset>,
    options: CallOptions,
    marker: PhantomData<U>,
}

impl ScreenerBuilder<Predefined> {
    /// Creates a builder for a predefined Yahoo screener.
    ///
    /// # Panics
    ///
    /// Panics if hardcoded Yahoo screener URLs are invalid.
    #[must_use]
    pub fn predefined(client: &YfClient, screener: PredefinedScreener) -> Self {
        Self {
            client: client.clone(),
            screener_base: Url::parse(DEFAULT_SCREENER_BASE).expect("valid screener URL"),
            predefined_base: Url::parse(DEFAULT_PREDEFINED_SCREENER_BASE)
                .expect("valid predefined screener URL"),
            kind: RequestKind::Predefined(screener),
            count: None,
            offset: None,
            options: CallOptions::default(),
            marker: PhantomData,
        }
    }

    /// Alias for [`ScreenerBuilder::predefined`].
    #[must_use]
    pub fn new(client: &YfClient, screener: PredefinedScreener) -> Self {
        Self::predefined(client, screener)
    }
}

impl ScreenerBuilder<Equity> {
    /// Creates a builder for a custom equity screener query.
    ///
    /// # Panics
    ///
    /// Panics if hardcoded Yahoo screener URLs are invalid.
    #[must_use]
    pub fn equity(client: &YfClient, query: EquityQuery) -> Self {
        Self::custom(
            client,
            query,
            YahooQuoteType::Equity,
            equity_fields::TICKER.key(),
        )
    }

    /// Sets the custom equity sort field and direction.
    #[must_use]
    pub const fn sort_by(mut self, field: SortField<Equity>, direction: SortDirection) -> Self {
        self.set_sort(field.key(), direction);
        self
    }
}

impl ScreenerBuilder<Fund> {
    /// Creates a builder for a custom mutual fund screener query.
    ///
    /// # Panics
    ///
    /// Panics if hardcoded Yahoo screener URLs are invalid.
    #[must_use]
    pub fn fund(client: &YfClient, query: FundQuery) -> Self {
        Self::custom(
            client,
            query,
            YahooQuoteType::MutualFund,
            fund_fields::TICKER.key(),
        )
    }

    /// Sets the custom mutual fund sort field and direction.
    #[must_use]
    pub const fn sort_by(mut self, field: SortField<Fund>, direction: SortDirection) -> Self {
        self.set_sort(field.key(), direction);
        self
    }
}

impl ScreenerBuilder<Etf> {
    /// Creates a builder for a custom ETF screener query.
    ///
    /// # Panics
    ///
    /// Panics if hardcoded Yahoo screener URLs are invalid.
    #[must_use]
    pub fn etf(client: &YfClient, query: EtfQuery) -> Self {
        Self::custom(client, query, YahooQuoteType::Etf, etf_fields::TICKER.key())
    }

    /// Sets the custom ETF sort field and direction.
    #[must_use]
    pub const fn sort_by(mut self, field: SortField<Etf>, direction: SortDirection) -> Self {
        self.set_sort(field.key(), direction);
        self
    }
}

impl<U: Send + Sync> ScreenerBuilder<U> {
    fn custom(
        client: &YfClient,
        query: ScreenerQuery<U>,
        quote_type: YahooQuoteType,
        default_sort_field: &'static str,
    ) -> Self {
        Self {
            client: client.clone(),
            screener_base: Url::parse(DEFAULT_SCREENER_BASE).expect("valid screener URL"),
            predefined_base: Url::parse(DEFAULT_PREDEFINED_SCREENER_BASE)
                .expect("valid predefined screener URL"),
            kind: RequestKind::Custom {
                query: query.into_wire_value(),
                quote_type,
                sort_field: default_sort_field,
                sort_direction: SortDirection::Desc,
            },
            count: Some(ScreenerCount::DEFAULT),
            offset: Some(ResultOffset::ZERO),
            options: CallOptions::default(),
            marker: PhantomData,
        }
    }

    /// Overrides the base URL for custom screener POST requests.
    #[must_use]
    pub fn screener_base(mut self, base: Url) -> Self {
        self.screener_base = base;
        self
    }

    /// Overrides the base URL for predefined screener GET requests.
    #[must_use]
    pub fn predefined_screener_base(mut self, base: Url) -> Self {
        self.predefined_base = base;
        self
    }

    crate::core::impl_call_option_setters!(
        strict_doc = "Fails when Yahoo screener data cannot be projected losslessly."
    );

    /// Sets the requested row count.
    #[must_use]
    pub const fn count(mut self, count: ScreenerCount) -> Self {
        self.count = Some(count);
        self
    }

    /// Sets the result offset.
    #[must_use]
    pub const fn offset(mut self, offset: ResultOffset) -> Self {
        self.offset = Some(offset);
        self
    }

    const fn set_sort(&mut self, field: &'static str, direction: SortDirection) {
        if let RequestKind::Custom {
            sort_field,
            sort_direction,
            ..
        } = &mut self.kind
        {
            *sort_field = field;
            *sort_direction = direction;
        }
    }

    /// Executes the screener request.
    ///
    /// # Errors
    ///
    /// Returns `YfError` if the network request fails, Yahoo returns an error
    /// status, or the response cannot be parsed.
    pub async fn fetch(&self) -> Result<ScreenerResponse, YfError> {
        Ok(self.fetch_with_diagnostics().await?.into_data())
    }

    /// Executes the screener request with projection diagnostics.
    ///
    /// # Errors
    ///
    /// Returns `YfError` if the network request fails, Yahoo returns an error
    /// status, the response cannot be parsed, or strict data-quality mode rejects a projection issue.
    pub async fn fetch_with_diagnostics(&self) -> Result<YfResponse<ScreenerResponse>, YfError> {
        match self.kind.clone() {
            RequestKind::Predefined(screener) if self.offset.is_none() => {
                self.fetch_predefined_get(screener).await
            }
            RequestKind::Predefined(screener) => {
                let parts = screener.custom_parts()?;
                self.fetch_custom_post(parts).await
            }
            RequestKind::Custom {
                query,
                quote_type,
                sort_field,
                sort_direction,
            } => {
                self.fetch_custom_post(CustomParts {
                    query,
                    quote_type,
                    sort_field,
                    sort_direction,
                })
                .await
            }
        }
    }

    async fn fetch_predefined_get(
        &self,
        screener: PredefinedScreener,
    ) -> Result<YfResponse<ScreenerResponse>, YfError> {
        let mut url = self.predefined_base.clone();
        append_common_params(&mut url);
        {
            let mut qp = url.query_pairs_mut();
            qp.append_pair("scrIds", screener.id());
            if let Some(count) = self.count {
                qp.append_pair("count", &count.get().to_string());
            }
        }

        if self.options.cache_mode().reads(CacheEndpoint::Screener)
            && let Some(body) = self.client.cache_get(&url)
        {
            return parse_screener_body_with_diagnostics(&body, self.options.data_quality());
        }

        let cache_url = url.clone();
        let (body, _) = self.get_with_auth_retry(url, screener.id()).await?;
        let response = parse_screener_body_with_diagnostics(&body, self.options.data_quality())?;
        if self.options.cache_mode().writes(CacheEndpoint::Screener) {
            self.client
                .cache_put(CacheEndpoint::Screener, &cache_url, &body, None);
        }
        Ok(response)
    }

    async fn fetch_custom_post(
        &self,
        parts: CustomParts,
    ) -> Result<YfResponse<ScreenerResponse>, YfError> {
        let mut url = self.screener_base.clone();
        append_common_params(&mut url);

        let fixture_key = parts.quote_type.as_str().to_ascii_lowercase();
        let body = self.custom_body(parts);
        let body_json = serde_json::to_string(&body).map_err(YfError::json)?;
        let response = self
            .post_with_auth_retry(url, &body_json, &fixture_key)
            .await?;
        parse_screener_body_with_diagnostics(&response, self.options.data_quality())
    }

    fn custom_body(&self, parts: CustomParts) -> Value {
        let mut body = Map::new();

        if let Some(offset) = self.offset {
            body.insert("offset".into(), Value::from(offset.get()));
        }
        if let Some(count) = self.count {
            body.insert("count".into(), Value::from(count.get()));
        }

        body.insert(
            "sortField".into(),
            Value::String(parts.sort_field.to_string()),
        );
        body.insert(
            "sortType".into(),
            Value::String(parts.sort_direction.as_str().to_string()),
        );
        body.insert("userId".into(), Value::String(String::new()));
        body.insert("userIdType".into(), Value::String("guid".into()));
        body.insert(
            "quoteType".into(),
            Value::String(parts.quote_type.as_str().into()),
        );
        body.insert("query".into(), parts.query);

        Value::Object(body)
    }

    async fn get_with_auth_retry(
        &self,
        url: Url,
        fixture_key: &str,
    ) -> Result<(String, Url), YfError> {
        let options = self.options.clone().with_cache_mode(CacheMode::Bypass);
        crate::core::net::fetch_text_with_auth_retry(
            &self.client,
            url,
            crate::core::net::AuthFetchConfig {
                auth_mode: crate::core::net::AuthMode::OptionalCrumb,
                cache_endpoint: CacheEndpoint::Screener,
                options: &options,
                cache_body: None,
                endpoint: "screener_predefined",
                fixture_key,
                ext: "json",
                retry_on_invalid_crumb_body: true,
                cache_validator: Some(super::response::validate_screener_body),
            },
            |url| {
                self.client
                    .http()
                    .get(url)
                    .header("accept", "application/json")
            },
        )
        .await
    }

    async fn post_with_auth_retry(
        &self,
        url: Url,
        body_json: &str,
        fixture_key: &str,
    ) -> Result<String, YfError> {
        let (body, _) = crate::core::net::fetch_text_with_auth_retry(
            &self.client,
            url,
            crate::core::net::AuthFetchConfig {
                auth_mode: crate::core::net::AuthMode::OptionalCrumb,
                cache_endpoint: CacheEndpoint::Screener,
                options: &self.options,
                cache_body: Some(body_json),
                endpoint: "screener_custom",
                fixture_key,
                ext: "json",
                retry_on_invalid_crumb_body: true,
                cache_validator: Some(super::response::validate_screener_body),
            },
            |url| {
                self.client
                    .http()
                    .post(url)
                    .header("accept", "application/json")
                    .header("content-type", "application/json")
                    .body(body_json.to_owned())
            },
        )
        .await?;

        Ok(body)
    }
}

fn append_common_params(url: &mut Url) {
    let mut qp = url.query_pairs_mut();
    qp.append_pair("corsDomain", "finance.yahoo.com");
    qp.append_pair("formatted", "false");
    qp.append_pair("lang", "en-US");
    qp.append_pair("region", "US");
}

#[cfg(test)]
mod tests {
    use serde_json::json;

    use super::*;
    use crate::screener::{PercentPoints, Region, equity_fields};

    #[test]
    fn custom_body_matches_python_wire_shape() {
        let client = YfClient::default();
        let query = EquityQuery::and(vec![
            equity_fields::PERCENT_CHANGE.gt(PercentPoints::new(3.0).unwrap()),
            equity_fields::REGION.eq(Region::Us),
        ])
        .unwrap();
        let builder = ScreenerBuilder::equity(&client, query);
        let body = match &builder.kind {
            RequestKind::Custom {
                query,
                quote_type,
                sort_field,
                sort_direction,
            } => builder.custom_body(CustomParts {
                query: query.clone(),
                quote_type: *quote_type,
                sort_field,
                sort_direction: *sort_direction,
            }),
            RequestKind::Predefined(_) => unreachable!(),
        };

        assert_eq!(
            body,
            json!({
                "offset": 0,
                "count": 25,
                "sortField": "ticker",
                "sortType": "DESC",
                "userId": "",
                "userIdType": "guid",
                "quoteType": "EQUITY",
                "query": {
                    "operator": "AND",
                    "operands": [
                        {"operator": "GT", "operands": ["percentchange", 3.0]},
                        {"operator": "EQ", "operands": ["region", "us"]}
                    ]
                }
            })
        );
    }
}