digdigdig3 0.1.29

Unified async Rust API for 44 exchange connectors — crypto, stocks, forex. REST + WebSocket.
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
//! Yahoo Finance response parsers
//!
//! Parse JSON responses to domain types based on Yahoo Finance API formats

use serde_json::Value;
use crate::core::types::*;

pub struct YahooFinanceParser;

impl YahooFinanceParser {
    // ═══════════════════════════════════════════════════════════════════════
    // STANDARD MARKET DATA
    // ═══════════════════════════════════════════════════════════════════════

    /// Parse price from /v8/finance/chart/{symbol} response
    ///
    /// Response format:
    /// ```json
    /// {
    ///   "chart": {
    ///     "result": [{
    ///       "meta": {"regularMarketPrice": 150.25, ...}
    ///     }]
    ///   }
    /// }
    /// ```
    ///
    /// Note: Changed from quote endpoint to chart endpoint (quote returns 401 as of Jan 2026)
    pub fn parse_price(response: &Value) -> ExchangeResult<f64> {
        let result = Self::get_chart_result(response)?;
        let first = result
            .first()
            .ok_or_else(|| ExchangeError::Parse("Empty result array".to_string()))?;

        let meta = first
            .get("meta")
            .ok_or_else(|| ExchangeError::Parse("Missing meta field in chart response".to_string()))?;

        Self::require_f64(meta, "regularMarketPrice")
    }

    /// Parse ticker from /v8/finance/chart/{symbol} response
    ///
    /// Note: Changed from quote endpoint to chart endpoint (quote returns 401 as of Jan 2026)
    pub fn parse_ticker(response: &Value, symbol: &str) -> ExchangeResult<Ticker> {
        let result = Self::get_chart_result(response)?;
        let first = result
            .first()
            .ok_or_else(|| ExchangeError::Parse("Empty result array".to_string()))?;

        let meta = first
            .get("meta")
            .ok_or_else(|| ExchangeError::Parse("Missing meta field in chart response".to_string()))?;

        Ok(Ticker {
            symbol: symbol.to_string(),
            last_price: Self::require_f64(meta, "regularMarketPrice")?,
            bid_price: None, // Chart endpoint doesn't provide bid/ask
            ask_price: None, // Chart endpoint doesn't provide bid/ask
            high_24h: Self::get_f64(meta, "regularMarketDayHigh"),
            low_24h: Self::get_f64(meta, "regularMarketDayLow"),
            volume_24h: Self::get_f64(meta, "regularMarketVolume"),
            quote_volume_24h: None, // Yahoo doesn't provide quote volume
            price_change_24h: Self::get_f64(meta, "regularMarketChange"),
            price_change_percent_24h: Self::get_f64(meta, "regularMarketChangePercent")
                .map(|p| p * 100.0), // Convert to percentage
            timestamp: Self::get_i64(meta, "regularMarketTime")
                .unwrap_or_else(|| chrono::Utc::now().timestamp()),
        })
    }

    /// Parse klines from /v8/finance/chart/{symbol} response
    ///
    /// Response format:
    /// ```json
    /// {
    ///   "chart": {
    ///     "result": [{
    ///       "timestamp": [1640563200, 1640649600, ...],
    ///       "indicators": {
    ///         "quote": [{
    ///           "open": [148.50, ...],
    ///           "high": [149.50, ...],
    ///           "low": [147.00, ...],
    ///           "close": [148.00, ...],
    ///           "volume": [75000000, ...]
    ///         }]
    ///       }
    ///     }]
    ///   }
    /// }
    /// ```
    pub fn parse_klines(response: &Value) -> ExchangeResult<Vec<Kline>> {
        let result = Self::get_chart_result(response)?;
        let first = result
            .first()
            .ok_or_else(|| ExchangeError::Parse("Empty result array".to_string()))?;

        // Get timestamp array
        let timestamps = first
            .get("timestamp")
            .and_then(|v| v.as_array())
            .ok_or_else(|| ExchangeError::Parse("Missing timestamp array".to_string()))?;

        // Get indicators.quote[0]
        let quote = first
            .get("indicators")
            .and_then(|i| i.get("quote"))
            .and_then(|q| q.as_array())
            .and_then(|a| a.first())
            .ok_or_else(|| ExchangeError::Parse("Missing indicators.quote".to_string()))?;

        // Get OHLCV arrays
        let opens = Self::get_array(quote, "open")?;
        let highs = Self::get_array(quote, "high")?;
        let lows = Self::get_array(quote, "low")?;
        let closes = Self::get_array(quote, "close")?;
        let volumes = Self::get_array(quote, "volume")?;

        // Build klines
        let mut klines = Vec::new();
        let len = timestamps.len();

        for i in 0..len {
            let timestamp = timestamps
                .get(i)
                .and_then(|v| v.as_i64())
                .ok_or_else(|| ExchangeError::Parse(format!("Invalid timestamp at {}", i)))?;

            klines.push(Kline {
                open_time: timestamp,
                open: Self::extract_f64(opens, i)?,
                high: Self::extract_f64(highs, i)?,
                low: Self::extract_f64(lows, i)?,
                close: Self::extract_f64(closes, i)?,
                volume: Self::extract_f64(volumes, i)?,
                quote_volume: None,
                close_time: None,
                trades: None,
            });
        }

        Ok(klines)
    }

    /// Parse symbols from /v7/finance/quote response (multiple symbols)
    pub fn parse_symbols(response: &Value) -> ExchangeResult<Vec<String>> {
        let result = Self::get_quote_response_result(response)?;

        Ok(result
            .iter()
            .filter_map(|quote| {
                quote
                    .get("symbol")
                    .and_then(|s| s.as_str())
                    .map(str::to_string)
            })
            .collect())
    }

    // ═══════════════════════════════════════════════════════════════════════
    // ORDERBOOK (NOT AVAILABLE - Yahoo doesn't provide orderbook)
    // ═══════════════════════════════════════════════════════════════════════

    /// Parse orderbook - NOT SUPPORTED by Yahoo Finance
    pub fn parse_orderbook(_response: &Value) -> ExchangeResult<OrderBook> {
        Err(ExchangeError::UnsupportedOperation(
            "Yahoo Finance does not provide orderbook data".to_string(),
        ))
    }

    // ═══════════════════════════════════════════════════════════════════════
    // EXTENDED DATA TYPES (Yahoo-specific)
    // ═══════════════════════════════════════════════════════════════════════

    /// Parse crumb from /v1/test/getcrumb response
    ///
    /// Response is plain text: "AbCdEfGhIjK"
    pub fn parse_crumb(response_text: &str) -> ExchangeResult<String> {
        let crumb = response_text.trim();
        if crumb.is_empty() {
            return Err(ExchangeError::Parse("Empty crumb response".to_string()));
        }
        Ok(crumb.to_string())
    }

    // ═══════════════════════════════════════════════════════════════════════
    // HELPER METHODS
    // ═══════════════════════════════════════════════════════════════════════

    /// Get quoteResponse.result array
    fn get_quote_response_result(response: &Value) -> ExchangeResult<&Vec<Value>> {
        response
            .get("quoteResponse")
            .and_then(|qr| qr.get("result"))
            .and_then(|r| r.as_array())
            .ok_or_else(|| ExchangeError::Parse("Missing quoteResponse.result".to_string()))
    }

    /// Get chart.result array
    fn get_chart_result(response: &Value) -> ExchangeResult<&Vec<Value>> {
        response
            .get("chart")
            .and_then(|c| c.get("result"))
            .and_then(|r| r.as_array())
            .ok_or_else(|| ExchangeError::Parse("Missing chart.result".to_string()))
    }

    /// Get array field from object
    fn get_array<'a>(obj: &'a Value, field: &str) -> ExchangeResult<&'a Vec<Value>> {
        obj.get(field)
            .and_then(|v| v.as_array())
            .ok_or_else(|| ExchangeError::Parse(format!("Missing array field '{}'", field)))
    }

    /// Extract f64 from array at index (handles null values)
    fn extract_f64(array: &[Value], index: usize) -> ExchangeResult<f64> {
        array
            .get(index)
            .and_then(|v| {
                if v.is_null() {
                    // Yahoo sometimes has null values in data
                    // Use 0.0 as fallback or previous value
                    Some(0.0)
                } else {
                    v.as_f64()
                }
            })
            .ok_or_else(|| ExchangeError::Parse(format!("Invalid f64 at index {}", index)))
    }

    /// Require f64 field
    fn require_f64(obj: &Value, field: &str) -> ExchangeResult<f64> {
        obj.get(field)
            .and_then(|v| {
                v.as_f64()
                    .or_else(|| v.as_str().and_then(|s| s.parse().ok()))
            })
            .ok_or_else(|| ExchangeError::Parse(format!("Missing/invalid '{}'", field)))
    }

    /// Get optional f64 field
    fn get_f64(obj: &Value, field: &str) -> Option<f64> {
        obj.get(field).and_then(|v| {
            v.as_f64()
                .or_else(|| v.as_str().and_then(|s| s.parse().ok()))
        })
    }

    /// Get optional i64 field
    fn get_i64(obj: &Value, field: &str) -> Option<i64> {
        obj.get(field).and_then(|v| {
            v.as_i64()
                .or_else(|| v.as_str().and_then(|s| s.parse().ok()))
        })
    }

    /// Check for error in response
    pub fn check_error(response: &Value) -> ExchangeResult<()> {
        // Check quoteResponse.error
        if let Some(error) = response
            .get("quoteResponse")
            .and_then(|qr| qr.get("error"))
            .and_then(|e| e.as_object())
        {
            if !error.is_empty() {
                let code = error
                    .get("code")
                    .and_then(|c| c.as_str())
                    .unwrap_or("Unknown");
                let desc = error
                    .get("description")
                    .and_then(|d| d.as_str())
                    .unwrap_or("No description");
                return Err(ExchangeError::Api { code: 0, message: format!("{}: {}", code, desc) });
            }
        }

        // Check chart.error
        if let Some(error) = response
            .get("chart")
            .and_then(|c| c.get("error"))
            .and_then(|e| e.as_object())
        {
            if !error.is_empty() {
                let code = error
                    .get("code")
                    .and_then(|c| c.as_str())
                    .unwrap_or("Unknown");
                let desc = error
                    .get("description")
                    .and_then(|d| d.as_str())
                    .unwrap_or("No description");
                return Err(ExchangeError::Api { code: 0, message: format!("{}: {}", code, desc) });
            }
        }

        // Check finance.error (for other endpoints)
        if let Some(error) = response
            .get("finance")
            .and_then(|f| f.get("error"))
            .and_then(|e| e.as_object())
        {
            if !error.is_empty() {
                let code = error
                    .get("code")
                    .and_then(|c| c.as_str())
                    .unwrap_or("Unknown");
                let desc = error
                    .get("description")
                    .and_then(|d| d.as_str())
                    .unwrap_or("No description");
                return Err(ExchangeError::Api { code: 0, message: format!("{}: {}", code, desc) });
            }
        }

        Ok(())
    }
}

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

    #[test]
    fn test_parse_price() {
        let response = json!({
            "chart": {
                "result": [{
                    "meta": {
                        "symbol": "AAPL",
                        "regularMarketPrice": 150.25
                    }
                }],
                "error": null
            }
        });

        let price = YahooFinanceParser::parse_price(&response).unwrap();
        assert_eq!(price, 150.25);
    }

    #[test]
    fn test_parse_ticker() {
        let response = json!({
            "chart": {
                "result": [{
                    "meta": {
                        "symbol": "AAPL",
                        "regularMarketPrice": 150.25,
                        "regularMarketDayHigh": 151.50,
                        "regularMarketDayLow": 149.00,
                        "regularMarketVolume": 75234000,
                        "regularMarketChange": 1.25,
                        "regularMarketChangePercent": 0.835,
                        "regularMarketTime": 1640980800
                    }
                }],
                "error": null
            }
        });

        let ticker = YahooFinanceParser::parse_ticker(&response, "AAPL").unwrap();
        assert_eq!(ticker.symbol, "AAPL");
        assert_eq!(ticker.last_price, 150.25);
        assert_eq!(ticker.bid_price, None); // Chart endpoint doesn't provide bid/ask
        assert_eq!(ticker.ask_price, None); // Chart endpoint doesn't provide bid/ask
    }

    #[test]
    fn test_parse_klines() {
        let response = json!({
            "chart": {
                "result": [{
                    "timestamp": [1640563200, 1640649600],
                    "indicators": {
                        "quote": [{
                            "open": [148.50, 149.00],
                            "high": [149.50, 150.00],
                            "low": [147.00, 148.00],
                            "close": [148.00, 149.50],
                            "volume": [75000000, 80000000]
                        }]
                    }
                }],
                "error": null
            }
        });

        let klines = YahooFinanceParser::parse_klines(&response).unwrap();
        assert_eq!(klines.len(), 2);
        assert_eq!(klines[0].open, 148.50);
        assert_eq!(klines[0].high, 149.50);
        assert_eq!(klines[0].low, 147.00);
        assert_eq!(klines[0].close, 148.00);
        assert_eq!(klines[0].volume, 75000000.0);
    }

    #[test]
    fn test_check_error() {
        let error_response = json!({
            "chart": {
                "result": null,
                "error": {
                    "code": "Not Found",
                    "description": "No data found"
                }
            }
        });

        let result = YahooFinanceParser::check_error(&error_response);
        assert!(result.is_err());
    }
}