equity_scanner/utils/
structs.rs

1use serde_json::Value;
2/// NOTE: datapoints are from old to new
3#[derive(Debug, Clone, Default)]
4pub struct HistoricalData {
5    pub metadata: Value,
6    pub timestamp: Vec<u64>,
7    pub high: Vec<f64>,
8    pub low: Vec<f64>,
9    pub open: Vec<f64>,
10    pub close: Vec<f64>,
11    pub volume: Vec<u64>,
12}
13
14impl HistoricalData {
15    pub fn new(yahoo_raw: &Value) -> Self {
16        let data = yahoo_raw
17            .get("chart")
18            .and_then(|c| c.get("result"))
19            .and_then(|r| r.get(0));
20
21        let result = match data {
22            Some(result) => result,
23            None => return Self::default(),
24        };
25
26        let metadata = result.get("meta").cloned().unwrap_or_default();
27
28        let quotes = result
29            .get("indicators")
30            .and_then(|i| i.get("quote"))
31            .and_then(|q| q.get(0))
32            .unwrap();
33
34        let timestamp = result
35            .get("timestamp")
36            .and_then(|t| t.as_array())
37            .map(|arr| arr.iter().map(|v| v.as_u64().unwrap_or(0)).collect())
38            .unwrap_or_default();
39
40        let close = quotes
41            .get("close")
42            .and_then(|c| c.as_array())
43            .map(|arr| arr.iter().map(|v| v.as_f64().unwrap_or(0.0)).collect())
44            .unwrap_or_default();
45
46        let open = quotes
47            .get("open")
48            .and_then(|o| o.as_array())
49            .map(|arr| arr.iter().map(|v| v.as_f64().unwrap_or(0.0)).collect())
50            .unwrap_or_default();
51
52        let high = quotes
53            .get("high")
54            .and_then(|h| h.as_array())
55            .map(|arr| arr.iter().map(|v| v.as_f64().unwrap_or(0.0)).collect())
56            .unwrap_or_default();
57
58        let low = quotes
59            .get("low")
60            .and_then(|l| l.as_array())
61            .map(|arr| arr.iter().map(|v| v.as_f64().unwrap_or(0.0)).collect())
62            .unwrap_or_default();
63
64        let volume = quotes
65            .get("volume")
66            .and_then(|v| v.as_array())
67            .map(|arr| arr.iter().map(|v| v.as_u64().unwrap_or(0)).collect())
68            .unwrap_or_default();
69
70        Self {
71            metadata,
72            timestamp,
73            high,
74            low,
75            open,
76            close,
77            volume,
78        }
79    }
80}