akshare 0.1.3

100% pure Rust implementation of akshare — unified access to Chinese and global financial market data APIs
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
//! Option data from Eastmoney.

use serde::Deserialize;

use crate::client::AkShareClient;
use crate::error::{Error, Result};
use crate::types::OptionSnapshot;
use crate::util::{parse_csv_line, parse_f64_safe, today_iso};

// ---------------------------------------------------------------------------
// Wire types
// ---------------------------------------------------------------------------

#[derive(Debug, Deserialize)]
struct DatacenterEnvelope {
    result: Option<DatacenterResult>,
}

#[derive(Debug, Deserialize)]
struct DatacenterResult {
    #[serde(default)]
    data: Vec<serde_json::Value>,
}

#[derive(Debug, Deserialize)]
struct KlineEnvelope {
    data: Option<KlineData>,
}

#[derive(Debug, Deserialize)]
struct KlineData {
    klines: Option<Vec<String>>,
}

// ---------------------------------------------------------------------------
// Implementation
// ---------------------------------------------------------------------------

impl AkShareClient {
    /// Fetch option chain snapshots from Eastmoney.
    ///
    /// `symbol` identifies the underlying and option type. Common values:
    /// - `"510050"` — 50ETF options
    /// - `"510300"` — 300ETF options
    ///
    /// Returns up to `limit` option contracts with latest pricing data.
    pub async fn option_chain(&self, symbol: &str, limit: usize) -> Result<Vec<OptionSnapshot>> {
        let trimmed = symbol.trim();
        if trimmed.is_empty() {
            return Err(Error::invalid_input("option symbol is empty"));
        }
        let page_size = limit.clamp(1, 200).to_string();
        let filter = format!("(UNDERLYING_SECURITY_CODE=\"{trimmed}\")");

        let response = self
            .get("https://datacenter-web.eastmoney.com/api/data/v1/get")
            .query(&[
                ("reportName", "RPT_OPTION_CURRENTDAY"),
                ("columns", "ALL"),
                ("filter", filter.as_str()),
                ("pageNumber", "1"),
                ("pageSize", page_size.as_str()),
                ("sortTypes", "-1"),
                ("sortColumns", "TRADE_DATE"),
                ("source", "WEB"),
                ("client", "WEB"),
            ])
            .send()
            .await
            .map_err(Error::from)?
            .error_for_status()
            .map_err(Error::from)?;

        let payload: DatacenterEnvelope = response.json().await.map_err(Error::from)?;
        let data = payload.result.map(|r| r.data).unwrap_or_default();
        let today = today_iso();

        let mut items = Vec::with_capacity(data.len());
        for v in &data {
            let option_code = v
                .get("SECURITY_CODE")
                .or_else(|| v.get("CONTRACT_CODE"))
                .and_then(|x| x.as_str())
                .unwrap_or("")
                .to_string();
            if option_code.is_empty() {
                continue;
            }

            let name = v
                .get("SECURITY_NAME_ABBR")
                .or_else(|| v.get("CONTRACT_NAME"))
                .and_then(|x| x.as_str())
                .unwrap_or("")
                .to_string();

            let date = v
                .get("TRADE_DATE")
                .and_then(|x| x.as_str())
                .map_or_else(|| today.clone(), |s| s.get(..10).unwrap_or(s).to_string());

            let close = v
                .get("CLOSE_PRICE")
                .or_else(|| v.get("LATEST_PRICE"))
                .and_then(serde_json::Value::as_f64)
                .unwrap_or(0.0);

            let change_pct = v
                .get("CHANGE_RATE")
                .or_else(|| v.get("CHANGE_PCT"))
                .and_then(serde_json::Value::as_f64)
                .unwrap_or(0.0);

            let volume = v
                .get("VOLUME")
                .or_else(|| v.get("TRADE_VOLUME"))
                .and_then(serde_json::Value::as_f64)
                .unwrap_or(0.0);

            let open_interest = v
                .get("OPEN_INTEREST")
                .or_else(|| v.get("HOLD_VOLUME"))
                .and_then(serde_json::Value::as_f64)
                .unwrap_or(0.0);

            let strike_price = v
                .get("STRIKE_PRICE")
                .or_else(|| v.get("EXERCISE_PRICE"))
                .and_then(serde_json::Value::as_f64);

            let expiry_date = v
                .get("EXPIRE_DATE")
                .or_else(|| v.get("EXPIRATION_DATE"))
                .and_then(|x| x.as_str())
                .map(|s| s.get(..10).unwrap_or(s).to_string());

            items.push(OptionSnapshot {
                symbol: option_code,
                name,
                date,
                close,
                change_pct,
                volume,
                open_interest,
                strike_price,
                expiry_date,
            });
        }

        if items.is_empty() {
            // Fallback: try the push2 kline API for a specific option contract
            return self.option_chain_from_kline(trimmed, limit).await;
        }

        items.truncate(limit);
        Ok(items)
    }

    /// Fallback: fetch option data via the kline API for a specific contract.
    ///
    /// `contract_code` is the option contract code (e.g. "10005765").
    async fn option_chain_from_kline(
        &self,
        contract_code: &str,
        limit: usize,
    ) -> Result<Vec<OptionSnapshot>> {
        // Try secid format for Eastmoney option contracts
        let secid = format!("1.{contract_code}");
        let lmt = limit.max(5).to_string();

        let response = self
            .get("https://push2his.eastmoney.com/api/qt/stock/kline/get")
            .query(&[
                ("secid", secid.as_str()),
                ("ut", "fa5fd1943c7b386f172d6893dbfba10b"),
                ("klt", "101"),
                ("fqt", "1"),
                ("lmt", lmt.as_str()),
                ("end", "20500000"),
                ("fields1", "f1,f2,f3,f4,f5,f6"),
                ("fields2", "f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61"),
            ])
            .send()
            .await
            .map_err(Error::from)?
            .error_for_status()
            .map_err(Error::from)?;

        let payload: KlineEnvelope = response.json().await.map_err(Error::from)?;
        let data = payload
            .data
            .ok_or_else(|| Error::upstream("eastmoney option kline response missing data"))?;
        let klines = data
            .klines
            .ok_or_else(|| Error::upstream("eastmoney option kline response missing klines"))?;

        let mut items = Vec::with_capacity(klines.len());
        for line in &klines {
            let f = parse_csv_line(line);
            if f.len() < 6 {
                continue;
            }
            let close = parse_f64_safe(f[2]);
            let volume = parse_f64_safe(f[5]);
            items.push(OptionSnapshot {
                symbol: contract_code.to_string(),
                name: String::new(),
                date: f[0].to_string(),
                close,
                change_pct: 0.0,
                volume,
                open_interest: 0.0,
                strike_price: None,
                expiry_date: None,
            });
        }

        if items.is_empty() {
            return Err(Error::not_found(format!(
                "no option data found for {contract_code}"
            )));
        }

        // Compute change_pct from consecutive close prices
        for i in 1..items.len() {
            let prev = items[i - 1].close;
            if prev > 0.0 {
                items[i].change_pct = ((items[i].close - prev) / prev) * 100.0;
            }
        }

        items.sort_by(|a, b| a.date.cmp(&b.date));
        if items.len() > limit {
            let start = items.len() - limit;
            items = items[start..].to_vec();
        }
        Ok(items)
    }
}

// ---------------------------------------------------------------------------
// Return types for option_current_em / option_current_cffex_em
// ---------------------------------------------------------------------------

/// Option current day data from Eastmoney (push2 clist API).
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct OptionCurrentEmRow {
    /// Sequential number.
    pub index: usize,
    /// Option code.
    pub code: String,
    /// Option name.
    pub name: String,
    /// Latest price.
    pub latest_price: f64,
    /// Change amount.
    pub change: f64,
    /// Change percent.
    pub change_pct: f64,
    /// Volume.
    pub volume: f64,
    /// Turnover amount.
    pub amount: f64,
    /// Open interest.
    pub open_interest: f64,
    /// Exercise price.
    pub exercise_price: f64,
    /// Remaining days.
    pub remaining_days: f64,
    /// Daily change in open interest.
    pub daily_change: f64,
    /// Previous settlement.
    pub prev_settlement: f64,
    /// Open price.
    pub open: f64,
    /// Market identifier.
    pub market_id: i64,
}

// ---------------------------------------------------------------------------
// Wire types for clist API
// ---------------------------------------------------------------------------

#[derive(Debug, Deserialize)]
struct ClistEnvelope {
    data: Option<ClistData>,
}

#[derive(Debug, Default, Deserialize)]
struct ClistData {
    total: Option<usize>,
    #[serde(default)]
    diff: Vec<serde_json::Value>,
}

#[derive(Debug, Deserialize)]
struct FutsseapiEnvelope {
    #[serde(default)]
    list: Vec<serde_json::Value>,
}

// ---------------------------------------------------------------------------
// Implementation — option_current_em / option_current_cffex_em / option_minute_em
// ---------------------------------------------------------------------------

impl AkShareClient {
    /// All current-day options from Eastmoney (push2 clist + CFFEX).
    ///
    /// Fetches SSE/SZSE options via push2 clist API and CFFEX options via futsseapi,
    /// then combines them into a single list.
    pub async fn option_current_em(&self) -> Result<Vec<OptionCurrentEmRow>> {
        let mut all_rows = Vec::new();

        // Part 1: SSE/SZSE options via push2 clist
        let sse_rows = self.fetch_em_option_clist_current().await?;
        all_rows.extend(sse_rows);

        // Part 2: CFFEX options via futsseapi
        let cffex_rows = self.option_current_cffex_em().await?;
        all_rows.extend(cffex_rows);

        // Re-number
        for (i, row) in all_rows.iter_mut().enumerate() {
            row.index = i + 1;
        }

        Ok(all_rows)
    }

    /// CFFEX options from Eastmoney (futsseapi).
    pub async fn option_current_cffex_em(&self) -> Result<Vec<OptionCurrentEmRow>> {
        let url = "https://futsseapi.eastmoney.com/list/option/221";

        let resp: FutsseapiEnvelope = self
            .get(url)
            .query(&[
                ("orderBy", "zdf"),
                ("sort", "desc"),
                ("pageSize", "20000"),
                ("pageIndex", "0"),
                ("token", "58b2fa8f54638b60b87d69b31969089c"),
                (
                    "field",
                    "dm,sc,name,p,zsjd,zde,zdf,f152,vol,cje,ccl,xqj,syr,rz,zjsj,o",
                ),
                ("blockName", "callback"),
            ])
            .send()
            .await
            .map_err(Error::from)?
            .json()
            .await
            .map_err(Error::from)?;

        let mut rows = Vec::with_capacity(resp.list.len());
        for (i, item) in resp.list.iter().enumerate() {
            rows.push(OptionCurrentEmRow {
                index: i + 1,
                code: json_str(item, "dm"),
                name: json_str(item, "name"),
                latest_price: json_f64(item, "p"),
                change: json_f64(item, "zde"),
                change_pct: json_f64(item, "zdf"),
                volume: json_f64(item, "vol"),
                amount: json_f64(item, "cje"),
                open_interest: json_f64(item, "ccl"),
                exercise_price: json_f64(item, "xqj"),
                remaining_days: json_f64(item, "syr"),
                daily_change: json_f64(item, "rz"),
                prev_settlement: json_f64(item, "zjsj"),
                open: json_f64(item, "o"),
                market_id: json_i64(item, "sc"),
            });
        }

        Ok(rows)
    }

    /// Option minute (intraday trend) data from Eastmoney.
    ///
    /// `symbol` is the option code, e.g. "MO2404-P-4450".
    pub async fn option_minute_em(&self, symbol: &str) -> Result<Vec<OptionMinuteRow>> {
        // First, look up the market id from the current options list
        let current = self.fetch_em_option_clist_current().await?;
        let option = current
            .iter()
            .find(|r| r.code == symbol)
            .ok_or_else(|| Error::not_found(format!("option not found: {symbol}")))?;
        let secid = format!("{}.{}", option.market_id, symbol);

        let url = "https://push2.eastmoney.com/api/qt/stock/trends2/get";

        let resp = self
            .get(url)
            .query(&[
                ("secid", secid.as_str()),
                (
                    "fields1",
                    "f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f17",
                ),
                ("fields2", "f51,f53,f54,f55,f56,f57,f58"),
                ("iscr", "0"),
                ("iscca", "0"),
                ("ut", "f057cbcbce2a86e2866ab8877db1d059"),
                ("ndays", "1"),
                ("cb", "quotepushdata1"),
            ])
            .send()
            .await
            .map_err(Error::from)?
            .text()
            .await
            .map_err(Error::from)?;

        // Response is JSONP: quotepushdata1({...})
        let json_start = resp.find('{').unwrap_or(0);
        let json_end = resp.rfind('}').map_or(resp.len(), |i| i + 1);
        let json_str = &resp[json_start..json_end];

        let data: serde_json::Value = serde_json::from_str(json_str)
            .map_err(|e| Error::decode(format!("option minute json: {e}")))?;

        let trends = data
            .get("data")
            .and_then(|d| d.get("trends"))
            .and_then(|t| t.as_array())
            .cloned()
            .unwrap_or_default();

        let mut rows = Vec::with_capacity(trends.len());
        for trend in &trends {
            if let Some(s) = trend.as_str() {
                let fields: Vec<&str> = s.split(',').collect();
                if fields.len() < 6 {
                    continue;
                }
                rows.push(OptionMinuteRow {
                    time: fields[0].to_string(),
                    close: fields[1].parse::<f64>().unwrap_or(0.0),
                    high: fields[2].parse::<f64>().unwrap_or(0.0),
                    low: fields[3].parse::<f64>().unwrap_or(0.0),
                    volume: fields[4].parse::<f64>().unwrap_or(0.0),
                    amount: fields[5].parse::<f64>().unwrap_or(0.0),
                });
            }
        }

        if rows.is_empty() {
            return Err(Error::not_found(format!("no minute data for {symbol}")));
        }

        Ok(rows)
    }

    // -- private helpers ----------------------------------------------------

    /// Fetch SSE/SZSE options via the push2 clist API with pagination.
    async fn fetch_em_option_clist_current(&self) -> Result<Vec<OptionCurrentEmRow>> {
        let mut all_rows = Vec::new();
        let mut page = 1_u32;
        let page_size = 100;

        loop {
            let pz = page_size.to_string();
            let pn = page.to_string();

            let resp: ClistEnvelope = self
                                .get("https://23.push2.eastmoney.com/api/qt/clist/get")
                .query(&[
                    ("pn", pn.as_str()),
                    ("pz", pz.as_str()),
                    ("po", "1"),
                    ("np", "1"),
                    ("ut", "bd1d9ddb04089700cf9c27f6f7426281"),
                    ("fltt", "2"),
                    ("invt", "2"),
                    ("fid", "f3"),
                    ("fs", "m:10,m:12,m:140,m:141,m:151,m:163,m:226"),
                    (
                        "fields",
                        "f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f22,f28,f11,f62,f128,f136,f115,f152,f133,f108,f163,f161,f162",
                    ),
                ])
                .send()
                .await
                .map_err(Error::from)?
                .json()
                .await
                .map_err(Error::from)?;

            let data = resp.data.unwrap_or_default();
            let total = data.total.unwrap_or(0);
            let diff = data.diff;

            if diff.is_empty() {
                break;
            }

            for item in &diff {
                // Items may be objects with numeric keys or direct arrays
                let values = if let Some(arr) = item.as_array() {
                    arr.clone()
                } else if let Some(obj) = item.as_object() {
                    let max_key = obj
                        .keys()
                        .filter_map(|k| k.parse::<usize>().ok())
                        .max()
                        .unwrap_or(0);
                    (0..=max_key)
                        .map(|i| {
                            obj.get(&i.to_string())
                                .cloned()
                                .unwrap_or(serde_json::Value::Null)
                        })
                        .collect::<Vec<_>>()
                } else {
                    continue;
                };

                if values.len() < 36 {
                    continue;
                }

                all_rows.push(OptionCurrentEmRow {
                    index: all_rows.len() + 1,
                    code: em_str(&values, 12),
                    name: em_str(&values, 14),
                    latest_price: em_f64(&values, 2),
                    change: em_f64(&values, 4),
                    change_pct: em_f64(&values, 3),
                    volume: em_f64(&values, 5),
                    amount: em_f64(&values, 6),
                    open_interest: em_f64(&values, 27),
                    exercise_price: em_f64(&values, 35),
                    remaining_days: em_f64(&values, 36),
                    daily_change: em_f64(&values, 37),
                    prev_settlement: em_f64(&values, 25),
                    open: em_f64(&values, 17),
                    market_id: em_i64(&values, 13),
                });
            }

            if all_data_len(&diff) >= total || page_size > diff.len() as u32 {
                break;
            }
            page += 1;
        }

        if all_rows.is_empty() {
            return Err(Error::not_found("no EM option current data"));
        }

        Ok(all_rows)
    }
}

/// Minute data row from Eastmoney option trend API.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct OptionMinuteRow {
    /// Time (HH:MM).
    pub time: String,
    /// Close price.
    pub close: f64,
    /// High price.
    pub high: f64,
    /// Low price.
    pub low: f64,
    /// Volume.
    pub volume: f64,
    /// Amount.
    pub amount: f64,
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn json_str(v: &serde_json::Value, key: &str) -> String {
    v.get(key)
        .and_then(|x| x.as_str())
        .unwrap_or("")
        .to_string()
}

fn json_f64(v: &serde_json::Value, key: &str) -> f64 {
    match v.get(key) {
        Some(serde_json::Value::Number(n)) => n.as_f64().unwrap_or(0.0),
        Some(serde_json::Value::String(s)) => s.trim().parse::<f64>().unwrap_or(0.0),
        _ => 0.0,
    }
}

fn json_i64(v: &serde_json::Value, key: &str) -> i64 {
    match v.get(key) {
        Some(serde_json::Value::Number(n)) => n.as_i64().unwrap_or(0),
        Some(serde_json::Value::String(s)) => s.trim().parse::<i64>().unwrap_or(0),
        _ => 0,
    }
}

fn em_str(arr: &[serde_json::Value], idx: usize) -> String {
    arr.get(idx)
        .and_then(|v| v.as_str())
        .unwrap_or("")
        .to_string()
}

fn em_f64(arr: &[serde_json::Value], idx: usize) -> f64 {
    match arr.get(idx) {
        Some(serde_json::Value::Number(n)) => n.as_f64().unwrap_or(0.0),
        Some(serde_json::Value::String(s)) => s.trim().parse::<f64>().unwrap_or(0.0),
        _ => 0.0,
    }
}

fn em_i64(arr: &[serde_json::Value], idx: usize) -> i64 {
    match arr.get(idx) {
        Some(serde_json::Value::Number(n)) => n.as_i64().unwrap_or(0),
        Some(serde_json::Value::String(s)) => s.trim().parse::<i64>().unwrap_or(0),
        _ => 0,
    }
}

const fn all_data_len(diff: &[serde_json::Value]) -> usize {
    diff.len()
}

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

    #[test]
    fn test_parse_option_kline_line() {
        let line = "2025-01-02,0.0500,0.0550,0.0600,0.0480,123456";
        let f = parse_csv_line(line);
        assert_eq!(f.len(), 6);
        assert_eq!(f[0], "2025-01-02");
        assert!((parse_f64_safe(f[2]) - 0.055).abs() < 0.001);
    }

    #[test]
    fn test_option_chain_empty_symbol_validation() {
        // Empty symbol should be rejected; test the validation path directly
        let trimmed = "";
        assert!(trimmed.is_empty());
    }
}