jquants-api-client 0.1.0

A Rust client for the J-Quants API, providing seamless access to financial data.
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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
//! Options OHLC (/derivatives/options) API

use serde::{Deserialize, Serialize};

use super::{
    shared::{
        deserialize_utils::{deserialize_f64_or_none, empty_string_or_null_as_none},
        traits::{
            builder::JQuantsBuilder,
            pagination::{HasPaginationKey, MergePage, Paginatable},
        },
        types::{
            central_contract_month_flag::CentralContractMonthFlag,
            emergency_margin_trigger_division::EmergencyMarginTriggerDivision,
            options_code::OptionsCode, put_call_division::PutCallDivision,
            underlying_sso::UnderlyingSSO,
        },
    },
    JQuantsApiClient, JQuantsPlanClient,
};

/// Builder for Options (OHLC) Data API.
#[derive(Clone, Serialize)]
pub struct OptionsPricesBuilder {
    #[serde(skip)]
    client: JQuantsApiClient,

    /// Category of data
    #[serde(skip_serializing_if = "Option::is_none")]
    category: Option<String>,

    /// Security options code (optional)
    #[serde(skip_serializing_if = "Option::is_none")]
    code: Option<OptionsCode>,

    /// Date of data (e.g., "20210901" or "2021-09-01")
    date: String,

    /// Central contract month flag (optional)
    #[serde(skip_serializing_if = "Option::is_none")]
    contract_flag: Option<String>,

    /// Pagination key.
    #[serde(skip_serializing_if = "Option::is_none")]
    pagination_key: Option<String>,
}

impl JQuantsBuilder<OptionsPricesResponse> for OptionsPricesBuilder {
    async fn send(self) -> Result<OptionsPricesResponse, crate::JQuantsError> {
        self.send_ref().await
    }

    async fn send_ref(&self) -> Result<OptionsPricesResponse, crate::JQuantsError> {
        self.client.inner.get("derivatives/options", self).await
    }
}

impl Paginatable<OptionsPricesResponse> for OptionsPricesBuilder {
    fn pagination_key(mut self, pagination_key: impl Into<String>) -> Self {
        self.pagination_key = Some(pagination_key.into());
        self
    }
}

impl OptionsPricesBuilder {
    /// Create a new builder.
    pub(crate) fn new(client: JQuantsApiClient, date: String) -> Self {
        Self {
            client,
            category: None,
            code: None,
            date,
            contract_flag: None,
            pagination_key: None,
        }
    }

    /// Set the category of data.
    pub fn category(mut self, category: impl Into<String>) -> Self {
        self.category = Some(category.into());
        self
    }

    /// Set the security options code.
    pub fn code(mut self, code: impl Into<OptionsCode>) -> Self {
        self.code = Some(code.into());
        self
    }

    /// Set the date of data (e.g., "20210901" or "2021-09-01")
    pub fn date(mut self, date: impl Into<String>) -> Self {
        self.date = date.into();
        self
    }

    /// Set the central contract month flag.
    pub fn contract_flag(mut self, flag: impl Into<String>) -> Self {
        self.contract_flag = Some(flag.into());
        self
    }

    /// Set pagination key for fetching the next set of data.
    pub fn pagination_key(mut self, pagination_key: impl Into<String>) -> Self {
        self.pagination_key = Some(pagination_key.into());
        self
    }
}

/// Trait for Options (OHLC) Data API.
pub trait OptionsPricesApi: JQuantsPlanClient {
    /// Get API builder for Options (OHLC) Data.
    ///
    /// Use [Options (OHLC) (/derivatives/options) API](https://jpx.gitbook.io/j-quants-en/api-reference/options)
    fn get_options_prices(&self, date: impl Into<String>) -> OptionsPricesBuilder {
        OptionsPricesBuilder::new(self.get_api_client().clone(), date.into())
    }
}

/// Options (OHLC) Data API response.
///
/// See: [API Reference](https://jpx.gitbook.io/j-quants-en/api-reference/options)
#[derive(Debug, Clone, PartialEq, Deserialize)]
pub struct OptionsPricesResponse {
    /// List of Options prices
    pub options: Vec<OptionsPricesItem>,
    /// Pagination key for fetching next set of data
    pub pagination_key: Option<String>,
}

impl HasPaginationKey for OptionsPricesResponse {
    fn get_pagination_key(&self) -> Option<&str> {
        self.pagination_key.as_deref()
    }
}

impl MergePage for OptionsPricesResponse {
    fn merge_page(
        page: Result<Vec<Self>, crate::JQuantsError>,
    ) -> Result<Self, crate::JQuantsError> {
        let mut page = page?;
        let mut merged = page.pop().unwrap();
        for p in page {
            merged.options.extend(p.options);
        }
        merged.pagination_key = None;

        Ok(merged)
    }
}

/// Represents a single Options price record.
#[derive(Debug, Clone, PartialEq, Deserialize)]
pub struct OptionsPricesItem {
    /// Issue code
    #[serde(rename = "Code")]
    pub code: String,

    /// Derivative Product Category
    #[serde(rename = "DerivativesProductCategory")]
    pub derivatives_product_category: String,

    /// Underlying SSO
    #[serde(rename = "UnderlyingSSO")]
    pub underlying_sso: UnderlyingSSO,

    /// Trading day (YYYY-MM-DD)
    #[serde(rename = "Date")]
    pub date: String,

    /// Whole day open price
    #[serde(rename = "WholeDayOpen")]
    pub whole_day_open: f64,

    /// Whole day high price
    #[serde(rename = "WholeDayHigh")]
    pub whole_day_high: f64,

    /// Whole day low price
    #[serde(rename = "WholeDayLow")]
    pub whole_day_low: f64,

    /// Whole day close price
    #[serde(rename = "WholeDayClose")]
    pub whole_day_close: f64,

    /// Morning session open price
    #[serde(
        rename = "MorningSessionOpen",
        deserialize_with = "deserialize_f64_or_none"
    )]
    pub morning_session_open: Option<f64>,

    /// Morning session high price
    #[serde(
        rename = "MorningSessionHigh",
        deserialize_with = "deserialize_f64_or_none"
    )]
    pub morning_session_high: Option<f64>,

    /// Morning session low price
    #[serde(
        rename = "MorningSessionLow",
        deserialize_with = "deserialize_f64_or_none"
    )]
    pub morning_session_low: Option<f64>,

    /// Morning session close price
    #[serde(
        rename = "MorningSessionClose",
        deserialize_with = "deserialize_f64_or_none"
    )]
    pub morning_session_close: Option<f64>,

    /// Night session open price
    #[serde(
        rename = "NightSessionOpen",
        deserialize_with = "deserialize_f64_or_none"
    )]
    pub night_session_open: Option<f64>,

    /// Night session high price
    #[serde(
        rename = "NightSessionHigh",
        deserialize_with = "deserialize_f64_or_none"
    )]
    pub night_session_high: Option<f64>,

    /// Night session low price
    #[serde(
        rename = "NightSessionLow",
        deserialize_with = "deserialize_f64_or_none"
    )]
    pub night_session_low: Option<f64>,

    /// Night session close price
    #[serde(
        rename = "NightSessionClose",
        deserialize_with = "deserialize_f64_or_none"
    )]
    pub night_session_close: Option<f64>,

    /// Day session open price
    #[serde(
        rename = "DaySessionOpen",
        deserialize_with = "deserialize_f64_or_none"
    )]
    pub day_session_open: Option<f64>,

    /// Day session high price
    #[serde(rename = "DaySessionHigh")]
    pub day_session_high: f64,

    /// Day session low price
    #[serde(rename = "DaySessionLow")]
    pub day_session_low: f64,

    /// Day session close price
    #[serde(rename = "DaySessionClose")]
    pub day_session_close: f64,

    /// Volume
    #[serde(rename = "Volume")]
    pub volume: f64,

    /// Open interest
    #[serde(rename = "OpenInterest")]
    pub open_interest: f64,

    /// Turnover value
    #[serde(rename = "TurnoverValue")]
    pub turnover_value: f64,

    /// Contract month (YYYY-MM)
    #[serde(rename = "ContractMonth")]
    pub contract_month: String,

    /// Strike price
    #[serde(rename = "StrikePrice")]
    pub strike_price: f64,

    /// Volume only auction
    #[serde(
        rename = "Volume(OnlyAuction)",
        deserialize_with = "deserialize_f64_or_none"
    )]
    pub volume_only_auction: Option<f64>,

    /// Emergency margin trigger division
    #[serde(rename = "EmergencyMarginTriggerDivision")]
    pub emergency_margin_trigger_division: EmergencyMarginTriggerDivision,

    /// Put Call division (1: put, 2: call)
    #[serde(rename = "PutCallDivision")]
    pub put_call_division: PutCallDivision,

    /// Last trading day (YYYY-MM-DD)
    #[serde(
        rename = "LastTradingDay",
        deserialize_with = "empty_string_or_null_as_none"
    )]
    pub last_trading_day: Option<String>,

    /// Special quotation day (YYYY-MM-DD)
    #[serde(
        rename = "SpecialQuotationDay",
        deserialize_with = "empty_string_or_null_as_none"
    )]
    pub special_quotation_day: Option<String>,

    /// Settlement price
    #[serde(
        rename = "SettlementPrice",
        deserialize_with = "deserialize_f64_or_none"
    )]
    pub settlement_price: Option<f64>,

    /// Theoretical price
    #[serde(
        rename = "TheoreticalPrice",
        deserialize_with = "deserialize_f64_or_none"
    )]
    pub theoretical_price: Option<f64>,

    /// Base volatility
    #[serde(
        rename = "BaseVolatility",
        deserialize_with = "deserialize_f64_or_none"
    )]
    pub base_volatility: Option<f64>,

    /// Underlying asset price
    #[serde(
        rename = "UnderlyingPrice",
        deserialize_with = "deserialize_f64_or_none"
    )]
    pub underlying_price: Option<f64>,

    /// Implied volatility
    #[serde(
        rename = "ImpliedVolatility",
        deserialize_with = "deserialize_f64_or_none"
    )]
    pub implied_volatility: Option<f64>,

    /// Interest rate for theoretical price calculation
    #[serde(rename = "InterestRate", deserialize_with = "deserialize_f64_or_none")]
    pub interest_rate: Option<f64>,

    /// Flag of the central contract month
    #[serde(
        rename = "CentralContractMonthFlag",
        deserialize_with = "empty_string_or_null_as_none"
    )]
    pub central_contract_month_flag: Option<CentralContractMonthFlag>,
}

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

    #[test]
    fn test_deserialize_options_prices_response() {
        let json_data = r#"
        {
            "options": [
                {
                    "Code": "140014505", 
                    "DerivativesProductCategory": "TOPIXE", 
                    "UnderlyingSSO": "-", 
                    "Date": "2024-07-23", 
                    "WholeDayOpen": 0.0, 
                    "WholeDayHigh": 0.0, 
                    "WholeDayLow": 0.0, 
                    "WholeDayClose": 0.0, 
                    "MorningSessionOpen": "", 
                    "MorningSessionHigh": "", 
                    "MorningSessionLow": "", 
                    "MorningSessionClose": "", 
                    "NightSessionOpen": 0.0, 
                    "NightSessionHigh": 0.0, 
                    "NightSessionLow": 0.0, 
                    "NightSessionClose": 0.0, 
                    "DaySessionOpen": 0.0, 
                    "DaySessionHigh": 0.0, 
                    "DaySessionLow": 0.0, 
                    "DaySessionClose": 0.0, 
                    "Volume": 0.0, 
                    "OpenInterest": 0.0, 
                    "TurnoverValue": 0.0, 
                    "ContractMonth": "2025-01", 
                    "StrikePrice": 2450.0, 
                    "Volume(OnlyAuction)": 0.0, 
                    "EmergencyMarginTriggerDivision": "002", 
                    "PutCallDivision": "2", 
                    "LastTradingDay": "2025-01-09", 
                    "SpecialQuotationDay": "2025-01-10", 
                    "SettlementPrice": 377.0, 
                    "TheoreticalPrice": 380.3801, 
                    "BaseVolatility": 18.115, 
                    "UnderlyingPrice": 2833.39, 
                    "ImpliedVolatility": 17.2955, 
                    "InterestRate": 0.3527, 
                    "CentralContractMonthFlag": "0"
                }
            ],
            "pagination_key": "value1.value2."
        }
        "#;

        let response: OptionsPricesResponse = serde_json::from_str(json_data).unwrap();

        let expected_option = vec![OptionsPricesItem {
            code: "140014505".to_string(),
            derivatives_product_category: "TOPIXE".to_string(),
            underlying_sso: UnderlyingSSO::Other,
            date: "2024-07-23".to_string(),
            whole_day_open: 0.0,
            whole_day_high: 0.0,
            whole_day_low: 0.0,
            whole_day_close: 0.0,
            morning_session_open: None,
            morning_session_high: None,
            morning_session_low: None,
            morning_session_close: None,
            night_session_open: Some(0.0),
            night_session_high: Some(0.0),
            night_session_low: Some(0.0),
            night_session_close: Some(0.0),
            day_session_open: Some(0.0),
            day_session_high: 0.0,
            day_session_low: 0.0,
            day_session_close: 0.0,
            volume: 0.0,
            open_interest: 0.0,
            turnover_value: 0.0,
            contract_month: "2025-01".to_string(),
            strike_price: 2450.0,
            volume_only_auction: Some(0.0),
            emergency_margin_trigger_division: EmergencyMarginTriggerDivision::Calculated,
            put_call_division: PutCallDivision::Call,
            last_trading_day: Some("2025-01-09".to_string()),
            special_quotation_day: Some("2025-01-10".to_string()),
            settlement_price: Some(377.0),
            theoretical_price: Some(380.3801),
            base_volatility: Some(18.115),
            underlying_price: Some(2833.39),
            implied_volatility: Some(17.2955),
            interest_rate: Some(0.3527),
            central_contract_month_flag: Some(CentralContractMonthFlag::Others),
        }];

        let expected_response = OptionsPricesResponse {
            options: expected_option,
            pagination_key: Some("value1.value2.".to_string()),
        };

        pretty_assertions::assert_eq!(response, expected_response);
    }

    #[test]
    fn test_deserialize_options_prices_response_with_missing_optional_fields() {
        let json_data = r#"
        {
            "options": [
                {
                    "Code": "140014505",
                    "DerivativesProductCategory": "TOPIXE",
                    "UnderlyingSSO": "-",
                    "Date": "2024-07-23",
                    "WholeDayOpen": 0.0,
                    "WholeDayHigh": 0.0,
                    "WholeDayLow": 0.0,
                    "WholeDayClose": 0.0,
                    "MorningSessionOpen": "",
                    "MorningSessionHigh": "",
                    "MorningSessionLow": "",
                    "MorningSessionClose": "",
                    "NightSessionOpen": "",
                    "NightSessionHigh": "",
                    "NightSessionLow": "",
                    "NightSessionClose": "",
                    "DaySessionOpen": "",
                    "DaySessionHigh": 0.0,
                    "DaySessionLow": 0.0,
                    "DaySessionClose": 0.0,
                    "Volume": 0.0,
                    "OpenInterest": 0.0,
                    "TurnoverValue": 0.0,
                    "ContractMonth": "2025-01",
                    "StrikePrice": 2450.0,
                    "Volume(OnlyAuction)": "",
                    "EmergencyMarginTriggerDivision": "001",
                    "PutCallDivision": "2",
                    "LastTradingDay": "2025-01-09",
                    "SpecialQuotationDay": "2025-01-10",
                    "SettlementPrice": "",
                    "TheoreticalPrice": "",
                    "BaseVolatility": "",
                    "UnderlyingPrice": "",
                    "ImpliedVolatility": "",
                    "InterestRate": "",
                    "CentralContractMonthFlag": ""
                }
            ],
            "pagination_key": "value1.value2."
        }
        "#;

        let response: OptionsPricesResponse = serde_json::from_str(json_data).unwrap();

        let expected_option = vec![OptionsPricesItem {
            code: "140014505".to_string(),
            derivatives_product_category: "TOPIXE".to_string(),
            underlying_sso: UnderlyingSSO::Other,
            date: "2024-07-23".to_string(),
            whole_day_open: 0.0,
            whole_day_high: 0.0,
            whole_day_low: 0.0,
            whole_day_close: 0.0,
            morning_session_open: None,
            morning_session_high: None,
            morning_session_low: None,
            morning_session_close: None,
            night_session_open: None,
            night_session_high: None,
            night_session_low: None,
            night_session_close: None,
            day_session_open: None,
            day_session_high: 0.0,
            day_session_low: 0.0,
            day_session_close: 0.0,
            volume: 0.0,
            open_interest: 0.0,
            turnover_value: 0.0,
            contract_month: "2025-01".to_string(),
            strike_price: 2450.0,
            volume_only_auction: None,
            emergency_margin_trigger_division: EmergencyMarginTriggerDivision::Triggered,
            put_call_division: PutCallDivision::Call,
            last_trading_day: Some("2025-01-09".to_string()),
            special_quotation_day: Some("2025-01-10".to_string()),
            settlement_price: None,
            theoretical_price: None,
            base_volatility: None,
            underlying_price: None,
            implied_volatility: None,
            interest_rate: None,
            central_contract_month_flag: None,
        }];

        let expected_response = OptionsPricesResponse {
            options: expected_option,
            pagination_key: Some("value1.value2.".to_string()),
        };

        pretty_assertions::assert_eq!(response, expected_response);
    }

    #[test]
    fn test_deserialize_options_prices_response_multiple_items() {
        let json_data = r#"
        {
            "options": [
                {
                    "Code": "140014505",
                    "DerivativesProductCategory": "TOPIXE",
                    "UnderlyingSSO": "-",
                    "Date": "2024-07-23",
                    "WholeDayOpen": 1000.0,
                    "WholeDayHigh": 1050.0,
                    "WholeDayLow": 990.0,
                    "WholeDayClose": 1025.0,
                    "MorningSessionOpen": "1005.0",
                    "MorningSessionHigh": "1045.0",
                    "MorningSessionLow": "995.0",
                    "MorningSessionClose": "1020.0",
                    "NightSessionOpen": 1010.0,
                    "NightSessionHigh": 1040.0,
                    "NightSessionLow": 995.0,
                    "NightSessionClose": 1030.0,
                    "DaySessionOpen": 1025.0,
                    "DaySessionHigh": 1060.0,
                    "DaySessionLow": 1000.0,
                    "DaySessionClose": 1045.0,
                    "Volume": 1500.0,
                    "OpenInterest": 300.0,
                    "TurnoverValue": 1500000.0,
                    "ContractMonth": "2025-02",
                    "StrikePrice": 2500.0,
                    "Volume(OnlyAuction)": 500.0,
                    "EmergencyMarginTriggerDivision": "001",
                    "PutCallDivision": "1",
                    "LastTradingDay": "2025-02-09",
                    "SpecialQuotationDay": "2025-02-10",
                    "SettlementPrice": 1025.0,
                    "TheoreticalPrice": 1030.5001,
                    "BaseVolatility": 19.200,
                    "UnderlyingPrice": 2850.00,
                    "ImpliedVolatility": 18.5000,
                    "InterestRate": 0.3600,
                    "CentralContractMonthFlag": "1"
                },
                {
                    "Code": "140014506",
                    "DerivativesProductCategory": "TOPIXE",
                    "UnderlyingSSO": "-",
                    "Date": "2024-07-23",
                    "WholeDayOpen": 2000.0,
                    "WholeDayHigh": 2050.0,
                    "WholeDayLow": 1990.0,
                    "WholeDayClose": 2025.0,
                    "MorningSessionOpen": "2005.0",
                    "MorningSessionHigh": "2045.0",
                    "MorningSessionLow": "1995.0",
                    "MorningSessionClose": "2020.0",
                    "NightSessionOpen": 2010.0,
                    "NightSessionHigh": 2040.0,
                    "NightSessionLow": 1995.0,
                    "NightSessionClose": 2030.0,
                    "DaySessionOpen": 2025.0,
                    "DaySessionHigh": 2060.0,
                    "DaySessionLow": 2000.0,
                    "DaySessionClose": 2045.0,
                    "Volume": 2500.0,
                    "OpenInterest": 400.0,
                    "TurnoverValue": 2500000.0,
                    "ContractMonth": "2025-03",
                    "StrikePrice": 2550.0,
                    "Volume(OnlyAuction)": 600.0,
                    "EmergencyMarginTriggerDivision": "002",
                    "PutCallDivision": "2",
                    "LastTradingDay": "2025-03-09",
                    "SpecialQuotationDay": "2025-03-10",
                    "SettlementPrice": 2025.0,
                    "TheoreticalPrice": 2030.5001,
                    "BaseVolatility": 19.500,
                    "UnderlyingPrice": 2855.00,
                    "ImpliedVolatility": 18.7000,
                    "InterestRate": 0.3650,
                    "CentralContractMonthFlag": "1"
                }
            ],
            "pagination_key": "value3.value4."
        }
        "#;

        let response: OptionsPricesResponse = serde_json::from_str(json_data).unwrap();

        let expected_options = vec![
            OptionsPricesItem {
                code: "140014505".to_string(),
                derivatives_product_category: "TOPIXE".to_string(),
                underlying_sso: UnderlyingSSO::Other,
                date: "2024-07-23".to_string(),
                whole_day_open: 1000.0,
                whole_day_high: 1050.0,
                whole_day_low: 990.0,
                whole_day_close: 1025.0,
                morning_session_open: Some(1005.0),
                morning_session_high: Some(1045.0),
                morning_session_low: Some(995.0),
                morning_session_close: Some(1020.0),
                night_session_open: Some(1010.0),
                night_session_high: Some(1040.0),
                night_session_low: Some(995.0),
                night_session_close: Some(1030.0),
                day_session_open: Some(1025.0),
                day_session_high: 1060.0,
                day_session_low: 1000.0,
                day_session_close: 1045.0,
                volume: 1500.0,
                open_interest: 300.0,
                turnover_value: 1500000.0,
                contract_month: "2025-02".to_string(),
                strike_price: 2500.0,
                volume_only_auction: Some(500.0),
                emergency_margin_trigger_division: EmergencyMarginTriggerDivision::Triggered,
                put_call_division: PutCallDivision::Put,
                last_trading_day: Some("2025-02-09".to_string()),
                special_quotation_day: Some("2025-02-10".to_string()),
                settlement_price: Some(1025.0),
                theoretical_price: Some(1030.5001),
                base_volatility: Some(19.200),
                underlying_price: Some(2850.00),
                implied_volatility: Some(18.5000),
                interest_rate: Some(0.3600),
                central_contract_month_flag: Some(CentralContractMonthFlag::CentralContractMonth),
            },
            OptionsPricesItem {
                code: "140014506".to_string(),
                derivatives_product_category: "TOPIXE".to_string(),
                underlying_sso: UnderlyingSSO::Other,
                date: "2024-07-23".to_string(),
                whole_day_open: 2000.0,
                whole_day_high: 2050.0,
                whole_day_low: 1990.0,
                whole_day_close: 2025.0,
                morning_session_open: Some(2005.0),
                morning_session_high: Some(2045.0),
                morning_session_low: Some(1995.0),
                morning_session_close: Some(2020.0),
                night_session_open: Some(2010.0),
                night_session_high: Some(2040.0),
                night_session_low: Some(1995.0),
                night_session_close: Some(2030.0),
                day_session_open: Some(2025.0),
                day_session_high: 2060.0,
                day_session_low: 2000.0,
                day_session_close: 2045.0,
                volume: 2500.0,
                open_interest: 400.0,
                turnover_value: 2500000.0,
                contract_month: "2025-03".to_string(),
                strike_price: 2550.0,
                volume_only_auction: Some(600.0),
                emergency_margin_trigger_division: EmergencyMarginTriggerDivision::Calculated,
                put_call_division: PutCallDivision::Call,
                last_trading_day: Some("2025-03-09".to_string()),
                special_quotation_day: Some("2025-03-10".to_string()),
                settlement_price: Some(2025.0),
                theoretical_price: Some(2030.5001),
                base_volatility: Some(19.500),
                underlying_price: Some(2855.00),
                implied_volatility: Some(18.7000),
                interest_rate: Some(0.3650),
                central_contract_month_flag: Some(CentralContractMonthFlag::CentralContractMonth),
            },
        ];

        let expected_response = OptionsPricesResponse {
            options: expected_options,
            pagination_key: Some("value3.value4.".to_string()),
        };

        pretty_assertions::assert_eq!(response, expected_response);
    }

    #[test]
    fn test_deserialize_options_prices_response_no_pagination_key() {
        let json_data = r#"
        {
            "options": [
                {
                    "Code": "140014505",
                    "DerivativesProductCategory": "TOPIXE",
                    "UnderlyingSSO": "-",
                    "Date": "2024-07-23",
                    "WholeDayOpen": 0.0,
                    "WholeDayHigh": 0.0,
                    "WholeDayLow": 0.0,
                    "WholeDayClose": 0.0,
                    "MorningSessionOpen": "",
                    "MorningSessionHigh": "",
                    "MorningSessionLow": "",
                    "MorningSessionClose": "",
                    "NightSessionOpen": 0.0,
                    "NightSessionHigh": 0.0,
                    "NightSessionLow": 0.0,
                    "NightSessionClose": 0.0,
                    "DaySessionOpen": 0.0,
                    "DaySessionHigh": 0.0,
                    "DaySessionLow": 0.0,
                    "DaySessionClose": 0.0,
                    "Volume": 0.0,
                    "OpenInterest": 0.0,
                    "TurnoverValue": 0.0,
                    "ContractMonth": "2025-01",
                    "StrikePrice": 2450.0,
                    "Volume(OnlyAuction)": 0.0,
                    "EmergencyMarginTriggerDivision": "002",
                    "PutCallDivision": "2",
                    "LastTradingDay": "2025-01-09",
                    "SpecialQuotationDay": "2025-01-10",
                    "SettlementPrice": 377.0,
                    "TheoreticalPrice": 380.3801,
                    "BaseVolatility": 18.115,
                    "UnderlyingPrice": 2833.39,
                    "ImpliedVolatility": 17.2955,
                    "InterestRate": 0.3527,
                    "CentralContractMonthFlag": "0"
                }
            ]
        }
        "#;

        let response: OptionsPricesResponse = serde_json::from_str(json_data).unwrap();

        let expected_option = vec![OptionsPricesItem {
            code: "140014505".to_string(),
            derivatives_product_category: "TOPIXE".to_string(),
            underlying_sso: UnderlyingSSO::Other,
            date: "2024-07-23".to_string(),
            whole_day_open: 0.0,
            whole_day_high: 0.0,
            whole_day_low: 0.0,
            whole_day_close: 0.0,
            morning_session_open: None,
            morning_session_high: None,
            morning_session_low: None,
            morning_session_close: None,
            night_session_open: Some(0.0),
            night_session_high: Some(0.0),
            night_session_low: Some(0.0),
            night_session_close: Some(0.0),
            day_session_open: Some(0.0),
            day_session_high: 0.0,
            day_session_low: 0.0,
            day_session_close: 0.0,
            volume: 0.0,
            open_interest: 0.0,
            turnover_value: 0.0,
            contract_month: "2025-01".to_string(),
            strike_price: 2450.0,
            volume_only_auction: Some(0.0),
            emergency_margin_trigger_division: EmergencyMarginTriggerDivision::Calculated,
            put_call_division: PutCallDivision::Call,
            last_trading_day: Some("2025-01-09".to_string()),
            special_quotation_day: Some("2025-01-10".to_string()),
            settlement_price: Some(377.0),
            theoretical_price: Some(380.3801),
            base_volatility: Some(18.115),
            underlying_price: Some(2833.39),
            implied_volatility: Some(17.2955),
            interest_rate: Some(0.3527),
            central_contract_month_flag: Some(CentralContractMonthFlag::Others),
        }];

        let expected_response = OptionsPricesResponse {
            options: expected_option,
            pagination_key: None,
        };

        pretty_assertions::assert_eq!(response, expected_response);
    }

    #[test]
    fn test_deserialize_options_prices_response_no_data() {
        let json_data = r#"
        {
            "options": []
        }
        "#;

        let response: OptionsPricesResponse = serde_json::from_str(json_data).unwrap();
        let expected_response = OptionsPricesResponse {
            options: vec![],
            pagination_key: None,
        };

        pretty_assertions::assert_eq!(response, expected_response);
    }
}