ig-client 0.11.3

This crate provides a client for the IG Markets API
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
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
use crate::presentation::instrument::InstrumentType;
use crate::presentation::serialization::{string_as_bool_opt, string_as_float_opt};
use lightstreamer_rs::subscription::ItemUpdate;
use pretty_simple_display::{DebugPretty, DisplaySimple};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Model for a market instrument with enhanced deserialization
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize, PartialEq)]
pub struct Instrument {
    /// Unique identifier for the instrument
    pub epic: String,
    /// Human-readable name of the instrument
    pub name: String,
    /// Expiry date of the instrument
    pub expiry: String,
    /// Size of one contract
    #[serde(rename = "contractSize")]
    pub contract_size: String,
    /// Size of one lot
    #[serde(rename = "lotSize")]
    pub lot_size: Option<f64>,
    /// Upper price limit for the instrument
    #[serde(rename = "highLimitPrice")]
    pub high_limit_price: Option<f64>,
    /// Lower price limit for the instrument
    #[serde(rename = "lowLimitPrice")]
    pub low_limit_price: Option<f64>,
    /// Margin factor for the instrument
    #[serde(rename = "marginFactor")]
    pub margin_factor: Option<f64>,
    /// Unit for the margin factor
    #[serde(rename = "marginFactorUnit")]
    pub margin_factor_unit: Option<String>,
    /// Available currencies for trading this instrument
    pub currencies: Option<Vec<Currency>>,
    #[serde(rename = "valueOfOnePip")]
    /// Value of one pip for this instrument
    pub value_of_one_pip: String,
    /// Type of the instrument
    #[serde(rename = "instrumentType")]
    pub instrument_type: Option<InstrumentType>,
    /// Expiry details including last dealing date
    #[serde(rename = "expiryDetails")]
    pub expiry_details: Option<ExpiryDetails>,
    #[serde(rename = "slippageFactor")]
    /// Slippage factor for the instrument
    pub slippage_factor: Option<StepDistance>,
    #[serde(rename = "limitedRiskPremium")]
    /// Premium for limited risk trades
    pub limited_risk_premium: Option<StepDistance>,
    #[serde(rename = "newsCode")]
    /// Code used for news related to this instrument
    pub news_code: Option<String>,
    #[serde(rename = "chartCode")]
    /// Code used for charting this instrument
    pub chart_code: Option<String>,
}

/// Model for an instrument's currency
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize, PartialEq)]
pub struct Currency {
    /// Currency code (e.g., "USD", "EUR")
    pub code: String,
    /// Currency symbol (e.g., "$", "€")
    pub symbol: Option<String>,
    /// Base exchange rate for the currency
    #[serde(rename = "baseExchangeRate")]
    pub base_exchange_rate: Option<f64>,
    /// Current exchange rate
    #[serde(rename = "exchangeRate")]
    pub exchange_rate: Option<f64>,
    /// Whether this is the default currency for the instrument
    #[serde(rename = "isDefault")]
    pub is_default: Option<bool>,
}

/// Model for market data with enhanced deserialization
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct MarketDetails {
    /// Detailed information about the instrument
    pub instrument: Instrument,
    /// Current market snapshot with prices
    pub snapshot: MarketSnapshot,
    /// Trading rules for the market
    #[serde(rename = "dealingRules")]
    pub dealing_rules: DealingRules,
}

/// Trading rules for a market with enhanced deserialization
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct DealingRules {
    /// Minimum step distance
    #[serde(rename = "minStepDistance")]
    pub min_step_distance: Option<StepDistance>,

    /// Minimum deal size allowed
    #[serde(rename = "minDealSize")]
    pub min_deal_size: Option<StepDistance>,

    /// Minimum distance for controlled risk stop
    #[serde(rename = "minControlledRiskStopDistance")]
    pub min_controlled_risk_stop_distance: Option<StepDistance>,

    /// Minimum distance for normal stop or limit orders
    #[serde(rename = "minNormalStopOrLimitDistance")]
    pub min_normal_stop_or_limit_distance: Option<StepDistance>,

    /// Maximum distance for stop or limit orders
    #[serde(rename = "maxStopOrLimitDistance")]
    pub max_stop_or_limit_distance: Option<StepDistance>,

    /// Controlled risk spacing
    #[serde(rename = "controlledRiskSpacing")]
    pub controlled_risk_spacing: Option<StepDistance>,

    /// Market order preference setting
    #[serde(rename = "marketOrderPreference")]
    pub market_order_preference: String,

    /// Trailing stops preference setting
    #[serde(rename = "trailingStopsPreference")]
    pub trailing_stops_preference: String,

    #[serde(rename = "maxDealSize")]
    /// Maximum deal size allowed
    pub max_deal_size: Option<f64>,
}

/// Market snapshot with enhanced deserialization
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct MarketSnapshot {
    /// Current status of the market (e.g., "OPEN", "CLOSED")
    #[serde(rename = "marketStatus")]
    pub market_status: String,

    /// Net change in price since previous close
    #[serde(rename = "netChange")]
    pub net_change: Option<f64>,

    /// Percentage change in price since previous close
    #[serde(rename = "percentageChange")]
    pub percentage_change: Option<f64>,

    /// Time of the last price update
    #[serde(rename = "updateTime")]
    pub update_time: Option<String>,

    /// Delay time in milliseconds for market data
    #[serde(rename = "delayTime")]
    pub delay_time: Option<i64>,

    /// Current bid price
    pub bid: Option<f64>,

    /// Current offer/ask price
    pub offer: Option<f64>,

    /// Highest price of the current trading session
    pub high: Option<f64>,

    /// Lowest price of the current trading session
    pub low: Option<f64>,

    /// Odds for binary markets
    #[serde(rename = "binaryOdds")]
    pub binary_odds: Option<f64>,

    /// Factor for decimal places in price display
    #[serde(rename = "decimalPlacesFactor")]
    pub decimal_places_factor: Option<i64>,

    /// Factor for scaling prices
    #[serde(rename = "scalingFactor")]
    pub scaling_factor: Option<i64>,

    /// Extra spread for controlled risk trades
    #[serde(rename = "controlledRiskExtraSpread")]
    pub controlled_risk_extra_spread: Option<f64>,
}

/// Basic market data
#[derive(DebugPretty, DisplaySimple, Clone, Deserialize, Serialize)]
pub struct MarketData {
    /// Unique identifier for the market
    pub epic: String,
    /// Human-readable name of the instrument
    #[serde(rename = "instrumentName")]
    pub instrument_name: String,
    /// Type of the instrument
    #[serde(rename = "instrumentType")]
    pub instrument_type: InstrumentType,
    /// Expiry date of the instrument
    pub expiry: String,
    /// Upper price limit for the market
    #[serde(rename = "highLimitPrice")]
    pub high_limit_price: Option<f64>,
    /// Lower price limit for the market
    #[serde(rename = "lowLimitPrice")]
    pub low_limit_price: Option<f64>,
    /// Current status of the market
    #[serde(rename = "marketStatus")]
    pub market_status: String,
    /// Net change in price since previous close
    #[serde(rename = "netChange")]
    pub net_change: Option<f64>,
    /// Percentage change in price since previous close
    #[serde(rename = "percentageChange")]
    pub percentage_change: Option<f64>,
    /// Time of the last price update
    #[serde(rename = "updateTime")]
    pub update_time: Option<String>,
    /// Time of the last price update in UTC
    #[serde(rename = "updateTimeUTC")]
    pub update_time_utc: Option<String>,
    /// Current bid price
    pub bid: Option<f64>,
    /// Current offer/ask price
    pub offer: Option<f64>,
}

impl MarketData {
    /// Checks if the current financial instrument is a call option.
    ///
    /// A call option is a financial derivative that gives the holder the right (but not the obligation)
    /// to buy an underlying asset at a specified price within a specified time period. This method checks
    /// whether the instrument represented by this instance is a call option by inspecting the `instrument_name`
    /// field.
    ///
    /// # Returns
    ///
    /// * `true` if the instrument's name contains the substring `"CALL"`, indicating it is a call option.
    /// * `false` otherwise.
    ///
    pub fn is_call(&self) -> bool {
        self.instrument_name.contains("CALL")
    }

    /// Checks if the financial instrument is a "PUT" option.
    ///
    /// This method examines the `instrument_name` field of the struct to determine
    /// if it contains the substring "PUT". If the substring is found, the method
    /// returns `true`, indicating that the instrument is categorized as a "PUT" option.
    /// Otherwise, it returns `false`.
    ///
    /// # Returns
    /// * `true` - If `instrument_name` contains the substring "PUT".
    /// * `false` - If `instrument_name` does not contain the substring "PUT".
    ///
    pub fn is_put(&self) -> bool {
        self.instrument_name.contains("PUT")
    }
}

/// Historical price data point
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct HistoricalPrice {
    /// Timestamp of the price data point
    #[serde(rename = "snapshotTime")]
    pub snapshot_time: String,
    /// Opening price for the period
    #[serde(rename = "openPrice")]
    pub open_price: PricePoint,
    /// Highest price for the period
    #[serde(rename = "highPrice")]
    pub high_price: PricePoint,
    /// Lowest price for the period
    #[serde(rename = "lowPrice")]
    pub low_price: PricePoint,
    /// Closing price for the period
    #[serde(rename = "closePrice")]
    pub close_price: PricePoint,
    /// Volume traded during the period
    #[serde(rename = "lastTradedVolume")]
    pub last_traded_volume: Option<i64>,
}

/// Price point with bid, ask and last traded prices
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct PricePoint {
    /// Bid price at this point
    pub bid: Option<f64>,
    /// Ask/offer price at this point
    pub ask: Option<f64>,
    /// Last traded price at this point
    #[serde(rename = "lastTraded")]
    pub last_traded: Option<f64>,
}

/// Information about API usage allowance for price data
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct PriceAllowance {
    /// Remaining API calls allowed in the current period
    #[serde(rename = "remainingAllowance")]
    pub remaining_allowance: i64,
    /// Total API calls allowed per period
    #[serde(rename = "totalAllowance")]
    pub total_allowance: i64,
    /// Time until the allowance resets
    #[serde(rename = "allowanceExpiry")]
    pub allowance_expiry: i64,
}

/// Details about instrument expiry
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize, PartialEq)]
pub struct ExpiryDetails {
    /// The last dealing date and time for the instrument
    #[serde(rename = "lastDealingDate")]
    pub last_dealing_date: String,

    /// Information about settlement
    #[serde(rename = "settlementInfo")]
    pub settlement_info: Option<String>,
}

#[repr(u8)]
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
/// Unit for step distances in trading rules
pub enum StepUnit {
    #[serde(rename = "POINTS")]
    /// Points (price movement units)
    Points,
    #[serde(rename = "PERCENTAGE")]
    /// Percentage value
    Percentage,
    #[serde(rename = "pct")]
    /// Alternative representation for percentage
    Pct,
}

/// A struct to handle the minStepDistance value which can be a complex object
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize, PartialEq)]
pub struct StepDistance {
    /// Unit type for the distance
    pub unit: Option<StepUnit>,
    /// Numeric value of the distance
    pub value: Option<f64>,
}

/// Node in the market navigation hierarchy
#[derive(DebugPretty, DisplaySimple, Clone, Deserialize, Serialize)]
pub struct MarketNavigationNode {
    /// Unique identifier for the node
    pub id: String,
    /// Display name of the node
    pub name: String,
}

/// Structure representing a node in the market hierarchy
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct MarketNode {
    /// Node ID
    pub id: String,
    /// Node name
    pub name: String,
    /// Child nodes
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub children: Vec<MarketNode>,
    /// Markets in this node
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub markets: Vec<MarketData>,
}

/// Represents the current state of a market
#[repr(u8)]
#[derive(
    DebugPretty, DisplaySimple, Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Hash, Default,
)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum MarketState {
    /// Market is closed for trading
    Closed,
    /// Market is offline and not available
    #[default]
    Offline,
    /// Market is open and available for trading
    Tradeable,
    /// Market is in edit mode
    Edit,
    /// Market is in edit mode only (no new positions, only edits allowed)
    EditsOnly,
    /// Market is in auction phase
    Auction,
    /// Market is in auction phase but editing is not allowed
    AuctionNoEdit,
    /// Market is temporarily suspended
    Suspended,
    /// Market is in auction phase
    OnAuction,
    /// Market is in auction phase but editing is not allowed
    OnAuctionNoEdits,
}

/// Representation of market data received from the IG Markets streaming API
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize, Default)]
pub struct PresentationMarketData {
    /// Name of the item this data belongs to
    pub item_name: String,
    /// Position of the item in the subscription
    pub item_pos: i32,
    /// All market fields
    pub fields: MarketFields,
    /// Fields that have changed in this update
    pub changed_fields: MarketFields,
    /// Whether this is a snapshot or an update
    pub is_snapshot: bool,
}

impl PresentationMarketData {
    /// Converts an ItemUpdate from the Lightstreamer API to a MarketData object
    ///
    /// # Arguments
    /// * `item_update` - The ItemUpdate received from the Lightstreamer API
    ///
    /// # Returns
    /// * `Result<Self, String>` - The converted MarketData or an error message
    pub fn from_item_update(item_update: &ItemUpdate) -> Result<Self, String> {
        // Extract the item_name, defaulting to an empty string if None
        let item_name = item_update.item_name.clone().unwrap_or_default();

        // Convert item_pos from usize to i32
        let item_pos = item_update.item_pos as i32;

        // Extract is_snapshot
        let is_snapshot = item_update.is_snapshot;

        // Convert fields
        let fields = Self::create_market_fields(&item_update.fields)?;

        // Convert changed_fields by first creating a HashMap<String, Option<String>>
        let mut changed_fields_map: HashMap<String, Option<String>> = HashMap::new();
        for (key, value) in &item_update.changed_fields {
            changed_fields_map.insert(key.clone(), Some(value.clone()));
        }
        let changed_fields = Self::create_market_fields(&changed_fields_map)?;

        Ok(PresentationMarketData {
            item_name,
            item_pos,
            fields,
            changed_fields,
            is_snapshot,
        })
    }

    /// Helper method to create MarketFields from a HashMap of field values
    ///
    /// # Arguments
    /// * `fields_map` - HashMap containing field names and their string values
    ///
    /// # Returns
    /// * `Result<MarketFields, String>` - The parsed MarketFields or an error message
    fn create_market_fields(
        fields_map: &HashMap<String, Option<String>>,
    ) -> Result<MarketFields, String> {
        // Helper function to safely get a field value
        let get_field = |key: &str| -> Option<String> { fields_map.get(key).cloned().flatten() };

        // Parse market state
        let market_state = match get_field("MARKET_STATE").as_deref() {
            Some("closed") => Some(MarketState::Closed),
            Some("offline") => Some(MarketState::Offline),
            Some("tradeable") => Some(MarketState::Tradeable),
            Some("edit") => Some(MarketState::Edit),
            Some("auction") => Some(MarketState::Auction),
            Some("auction_no_edit") => Some(MarketState::AuctionNoEdit),
            Some("suspended") => Some(MarketState::Suspended),
            Some("on_auction") => Some(MarketState::OnAuction),
            Some("on_auction_no_edit") => Some(MarketState::OnAuctionNoEdits),
            Some(unknown) => return Err(format!("Unknown market state: {unknown}")),
            None => None,
        };

        // Parse boolean field
        let market_delay = match get_field("MARKET_DELAY").as_deref() {
            Some("0") => Some(false),
            Some("1") => Some(true),
            Some(val) => return Err(format!("Invalid MARKET_DELAY value: {val}")),
            None => None,
        };

        // Helper function to parse float values
        let parse_float = |key: &str| -> Result<Option<f64>, String> {
            match get_field(key) {
                Some(val) if !val.is_empty() => val
                    .parse::<f64>()
                    .map(Some)
                    .map_err(|_| format!("Failed to parse {key} as float: {val}")),
                _ => Ok(None),
            }
        };

        Ok(MarketFields {
            mid_open: parse_float("MID_OPEN")?,
            high: parse_float("HIGH")?,
            offer: parse_float("OFFER")?,
            change: parse_float("CHANGE")?,
            market_delay,
            low: parse_float("LOW")?,
            bid: parse_float("BID")?,
            change_pct: parse_float("CHANGE_PCT")?,
            market_state,
            update_time: get_field("UPDATE_TIME"),
        })
    }
}

impl From<&ItemUpdate> for PresentationMarketData {
    fn from(item_update: &ItemUpdate) -> Self {
        Self::from_item_update(item_update).unwrap_or_else(|_| PresentationMarketData {
            item_name: String::new(),
            item_pos: 0,
            fields: MarketFields::default(),
            changed_fields: MarketFields::default(),
            is_snapshot: false,
        })
    }
}

/// Represents a category of instruments in the IG Markets API
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize, Default, PartialEq)]
pub struct Category {
    /// Category code identifier
    pub code: String,
    /// True if the category is non-tradeable
    #[serde(rename = "nonTradeable")]
    pub non_tradeable: bool,
}

/// Market status for category instruments
#[repr(u8)]
#[derive(
    DebugPretty, DisplaySimple, Clone, Copy, Serialize, Deserialize, Default, PartialEq, Eq, Hash,
)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum CategoryMarketStatus {
    /// Market is offline
    #[default]
    Offline,
    /// Market is closed
    Closed,
    /// Market is suspended
    Suspended,
    /// Market is in auction mode
    OnAuction,
    /// Market is in no-edits mode
    OnAuctionNoEdits,
    /// Market is open for edits only
    EditsOnly,
    /// Market allows closings only
    ClosingsOnly,
    /// Market allows deals but not edits
    DealNoEdit,
    /// Market is open for trades
    Tradeable,
}

/// Represents an instrument within a category
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize, Default, PartialEq)]
pub struct CategoryInstrument {
    /// Unique instrument identifier (EPIC)
    pub epic: String,
    /// Name of the instrument
    #[serde(rename = "instrumentName")]
    pub instrument_name: String,
    /// Expiry date of the instrument
    pub expiry: String,
    /// Type of the instrument
    #[serde(rename = "instrumentType")]
    pub instrument_type: InstrumentType,
    /// Size of an instrument lot
    #[serde(rename = "lotSize")]
    pub lot_size: Option<f64>,
    /// True if the instrument can be traded OTC
    #[serde(rename = "otcTradeable")]
    pub otc_tradeable: bool,
    /// Current status of the market
    #[serde(rename = "marketStatus")]
    pub market_status: CategoryMarketStatus,
    /// Price delay time for market data in minutes
    #[serde(rename = "delayTime")]
    pub delay_time: Option<i64>,
    /// Current bid price
    pub bid: Option<f64>,
    /// Current offer price
    pub offer: Option<f64>,
    /// Highest price for the current session
    pub high: Option<f64>,
    /// Lowest price for the current session
    pub low: Option<f64>,
    /// Net change in price
    #[serde(rename = "netChange")]
    pub net_change: Option<f64>,
    /// Percentage change in price
    #[serde(rename = "percentageChange")]
    pub percentage_change: Option<f64>,
    /// Time of last price update
    #[serde(rename = "updateTime")]
    pub update_time: Option<String>,
    /// Multiplying factor to determine actual pip value
    #[serde(rename = "scalingFactor")]
    pub scaling_factor: Option<i64>,
}

/// Paging metadata for category instruments response
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize, Default, PartialEq)]
pub struct CategoryInstrumentsMetadata {
    /// Current page number
    #[serde(rename = "pageNumber")]
    pub page_number: i32,
    /// Number of items per page
    #[serde(rename = "pageSize")]
    pub page_size: i32,
}

/// Fields containing market price and status information
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize, Default, PartialEq)]
pub struct MarketFields {
    /// The mid-open price of the market
    #[serde(rename = "MID_OPEN")]
    #[serde(with = "string_as_float_opt")]
    #[serde(default)]
    pub mid_open: Option<f64>,

    /// The highest price reached by the market in the current trading session
    #[serde(rename = "HIGH")]
    #[serde(with = "string_as_float_opt")]
    #[serde(default)]
    pub high: Option<f64>,

    /// The current offer (ask) price of the market
    #[serde(rename = "OFFER")]
    #[serde(with = "string_as_float_opt")]
    #[serde(default)]
    pub offer: Option<f64>,

    /// The absolute price change since the previous close
    #[serde(rename = "CHANGE")]
    #[serde(with = "string_as_float_opt")]
    #[serde(default)]
    pub change: Option<f64>,

    /// Indicates if there is a delay in market data
    #[serde(rename = "MARKET_DELAY")]
    #[serde(with = "string_as_bool_opt")]
    #[serde(default)]
    pub market_delay: Option<bool>,

    /// The lowest price reached by the market in the current trading session
    #[serde(rename = "LOW")]
    #[serde(with = "string_as_float_opt")]
    #[serde(default)]
    pub low: Option<f64>,

    /// The current bid price of the market
    #[serde(rename = "BID")]
    #[serde(with = "string_as_float_opt")]
    #[serde(default)]
    pub bid: Option<f64>,

    /// The percentage price change since the previous close
    #[serde(rename = "CHANGE_PCT")]
    #[serde(with = "string_as_float_opt")]
    #[serde(default)]
    pub change_pct: Option<f64>,

    /// The current state of the market (e.g., Tradeable, Closed, etc.)
    #[serde(rename = "MARKET_STATE")]
    #[serde(default)]
    pub market_state: Option<MarketState>,

    /// The timestamp of the last market update
    #[serde(rename = "UPDATE_TIME")]
    #[serde(default)]
    pub update_time: Option<String>,
}

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

    #[test]
    fn test_market_data_is_call_returns_true_for_call_option() {
        let market = MarketData {
            epic: "test".to_string(),
            instrument_name: "DAX CALL 18000".to_string(),
            instrument_type: InstrumentType::default(),
            expiry: "-".to_string(),
            high_limit_price: None,
            low_limit_price: None,
            market_status: "TRADEABLE".to_string(),
            net_change: None,
            percentage_change: None,
            update_time: None,
            update_time_utc: None,
            bid: Some(100.0),
            offer: Some(101.0),
        };
        assert!(market.is_call());
        assert!(!market.is_put());
    }

    #[test]
    fn test_market_data_is_put_returns_true_for_put_option() {
        let market = MarketData {
            epic: "test".to_string(),
            instrument_name: "DAX PUT 17000".to_string(),
            instrument_type: InstrumentType::default(),
            expiry: "-".to_string(),
            high_limit_price: None,
            low_limit_price: None,
            market_status: "TRADEABLE".to_string(),
            net_change: None,
            percentage_change: None,
            update_time: None,
            update_time_utc: None,
            bid: Some(50.0),
            offer: Some(51.0),
        };
        assert!(market.is_put());
        assert!(!market.is_call());
    }

    #[test]
    fn test_market_data_neither_call_nor_put() {
        let market = MarketData {
            epic: "IX.D.DAX.DAILY.IP".to_string(),
            instrument_name: "Germany 40".to_string(),
            instrument_type: InstrumentType::default(),
            expiry: "-".to_string(),
            high_limit_price: None,
            low_limit_price: None,
            market_status: "TRADEABLE".to_string(),
            net_change: None,
            percentage_change: None,
            update_time: None,
            update_time_utc: None,
            bid: Some(18000.0),
            offer: Some(18001.0),
        };
        assert!(!market.is_call());
        assert!(!market.is_put());
    }

    #[test]
    fn test_market_state_default() {
        let state = MarketState::default();
        assert_eq!(state, MarketState::Offline);
    }

    #[test]
    fn test_category_market_status_default() {
        let status = CategoryMarketStatus::default();
        assert_eq!(status, CategoryMarketStatus::Offline);
    }

    #[test]
    fn test_step_unit_serialization() {
        let points = StepUnit::Points;
        let json = serde_json::to_string(&points).expect("serialize failed");
        assert_eq!(json, "\"POINTS\"");

        let pct = StepUnit::Percentage;
        let json = serde_json::to_string(&pct).expect("serialize failed");
        assert_eq!(json, "\"PERCENTAGE\"");
    }

    #[test]
    fn test_step_distance_creation() {
        let distance = StepDistance {
            unit: Some(StepUnit::Points),
            value: Some(1.5),
        };
        assert_eq!(distance.unit, Some(StepUnit::Points));
        assert_eq!(distance.value, Some(1.5));
    }

    #[test]
    fn test_market_fields_default() {
        let fields = MarketFields::default();
        assert!(fields.mid_open.is_none());
        assert!(fields.high.is_none());
        assert!(fields.offer.is_none());
        assert!(fields.change.is_none());
        assert!(fields.market_delay.is_none());
        assert!(fields.low.is_none());
        assert!(fields.bid.is_none());
        assert!(fields.change_pct.is_none());
        assert!(fields.market_state.is_none());
        assert!(fields.update_time.is_none());
    }

    #[test]
    fn test_presentation_market_data_default() {
        let data = PresentationMarketData::default();
        assert!(data.item_name.is_empty());
        assert_eq!(data.item_pos, 0);
        assert!(!data.is_snapshot);
    }

    #[test]
    fn test_category_default() {
        let cat = Category::default();
        assert!(cat.code.is_empty());
        assert!(!cat.non_tradeable);
    }

    #[test]
    fn test_category_instrument_default() {
        let inst = CategoryInstrument::default();
        assert!(inst.epic.is_empty());
        assert!(inst.instrument_name.is_empty());
        assert_eq!(inst.market_status, CategoryMarketStatus::Offline);
    }

    #[test]
    fn test_market_state_serialization() {
        let tradeable = MarketState::Tradeable;
        let json = serde_json::to_string(&tradeable).expect("serialize failed");
        assert_eq!(json, "\"TRADEABLE\"");

        let closed = MarketState::Closed;
        let json = serde_json::to_string(&closed).expect("serialize failed");
        assert_eq!(json, "\"CLOSED\"");
    }

    #[test]
    fn test_price_point_creation() {
        let point = PricePoint {
            bid: Some(100.5),
            ask: Some(101.0),
            last_traded: Some(100.75),
        };
        assert_eq!(point.bid, Some(100.5));
        assert_eq!(point.ask, Some(101.0));
        assert_eq!(point.last_traded, Some(100.75));
    }

    #[test]
    fn test_price_allowance_creation() {
        let allowance = PriceAllowance {
            remaining_allowance: 100,
            total_allowance: 1000,
            allowance_expiry: 3600,
        };
        assert_eq!(allowance.remaining_allowance, 100);
        assert_eq!(allowance.total_allowance, 1000);
        assert_eq!(allowance.allowance_expiry, 3600);
    }

    #[test]
    fn test_expiry_details_creation() {
        let expiry = ExpiryDetails {
            last_dealing_date: "2024-12-31".to_string(),
            settlement_info: Some("Cash settlement".to_string()),
        };
        assert_eq!(expiry.last_dealing_date, "2024-12-31");
        assert_eq!(expiry.settlement_info, Some("Cash settlement".to_string()));
    }

    #[test]
    fn test_market_navigation_node_creation() {
        let node = MarketNavigationNode {
            id: "12345".to_string(),
            name: "Indices".to_string(),
        };
        assert_eq!(node.id, "12345");
        assert_eq!(node.name, "Indices");
    }

    #[test]
    fn test_market_node_creation() {
        let node = MarketNode {
            id: "node1".to_string(),
            name: "Test Node".to_string(),
            children: Vec::new(),
            markets: Vec::new(),
        };
        assert_eq!(node.id, "node1");
        assert_eq!(node.name, "Test Node");
        assert!(node.children.is_empty());
        assert!(node.markets.is_empty());
    }

    #[test]
    fn test_currency_creation() {
        let currency = Currency {
            code: "USD".to_string(),
            symbol: Some("$".to_string()),
            base_exchange_rate: Some(1.0),
            exchange_rate: Some(1.0),
            is_default: Some(true),
        };
        assert_eq!(currency.code, "USD");
        assert_eq!(currency.symbol, Some("$".to_string()));
        assert_eq!(currency.is_default, Some(true));
    }

    #[test]
    fn test_create_market_fields_with_valid_data() {
        let mut fields_map: HashMap<String, Option<String>> = HashMap::new();
        fields_map.insert("BID".to_string(), Some("100.5".to_string()));
        fields_map.insert("OFFER".to_string(), Some("101.0".to_string()));
        fields_map.insert("HIGH".to_string(), Some("102.0".to_string()));
        fields_map.insert("LOW".to_string(), Some("99.0".to_string()));
        fields_map.insert("CHANGE".to_string(), Some("1.5".to_string()));
        fields_map.insert("CHANGE_PCT".to_string(), Some("1.5".to_string()));
        fields_map.insert("MARKET_STATE".to_string(), Some("tradeable".to_string()));
        fields_map.insert("MARKET_DELAY".to_string(), Some("0".to_string()));
        fields_map.insert("UPDATE_TIME".to_string(), Some("12:30:00".to_string()));

        let result = PresentationMarketData::create_market_fields(&fields_map);
        assert!(result.is_ok());

        let fields = result.expect("should parse");
        assert_eq!(fields.bid, Some(100.5));
        assert_eq!(fields.offer, Some(101.0));
        assert_eq!(fields.high, Some(102.0));
        assert_eq!(fields.low, Some(99.0));
        assert_eq!(fields.change, Some(1.5));
        assert_eq!(fields.market_state, Some(MarketState::Tradeable));
        assert_eq!(fields.market_delay, Some(false));
        assert_eq!(fields.update_time, Some("12:30:00".to_string()));
    }

    #[test]
    fn test_create_market_fields_with_empty_map() {
        let fields_map: HashMap<String, Option<String>> = HashMap::new();
        let result = PresentationMarketData::create_market_fields(&fields_map);
        assert!(result.is_ok());

        let fields = result.expect("should parse");
        assert!(fields.bid.is_none());
        assert!(fields.offer.is_none());
    }

    #[test]
    fn test_create_market_fields_invalid_market_state() {
        let mut fields_map: HashMap<String, Option<String>> = HashMap::new();
        fields_map.insert(
            "MARKET_STATE".to_string(),
            Some("invalid_state".to_string()),
        );

        let result = PresentationMarketData::create_market_fields(&fields_map);
        assert!(result.is_err());
    }

    #[test]
    fn test_create_market_fields_invalid_market_delay() {
        let mut fields_map: HashMap<String, Option<String>> = HashMap::new();
        fields_map.insert("MARKET_DELAY".to_string(), Some("invalid".to_string()));

        let result = PresentationMarketData::create_market_fields(&fields_map);
        assert!(result.is_err());
    }

    #[test]
    fn test_create_market_fields_all_market_states() {
        let states = vec![
            ("closed", MarketState::Closed),
            ("offline", MarketState::Offline),
            ("tradeable", MarketState::Tradeable),
            ("edit", MarketState::Edit),
            ("auction", MarketState::Auction),
            ("auction_no_edit", MarketState::AuctionNoEdit),
            ("suspended", MarketState::Suspended),
            ("on_auction", MarketState::OnAuction),
            ("on_auction_no_edit", MarketState::OnAuctionNoEdits),
        ];

        for (state_str, expected_state) in states {
            let mut fields_map: HashMap<String, Option<String>> = HashMap::new();
            fields_map.insert("MARKET_STATE".to_string(), Some(state_str.to_string()));

            let result = PresentationMarketData::create_market_fields(&fields_map);
            assert!(result.is_ok(), "Failed for state: {}", state_str);
            let fields = result.expect("should parse");
            assert_eq!(fields.market_state, Some(expected_state));
        }
    }

    #[test]
    fn test_market_delay_values() {
        let mut fields_map: HashMap<String, Option<String>> = HashMap::new();
        fields_map.insert("MARKET_DELAY".to_string(), Some("1".to_string()));

        let result = PresentationMarketData::create_market_fields(&fields_map);
        assert!(result.is_ok());
        let fields = result.expect("should parse");
        assert_eq!(fields.market_delay, Some(true));
    }
}