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
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
/******************************************************************************
   Author: Joaquín Béjar García
   Email: jb@taunais.com
   Date: 19/10/25
******************************************************************************/
use crate::constants::{DEFAULT_ORDER_BUY_LEVEL, DEFAULT_ORDER_SELL_LEVEL};
use crate::prelude::{Deserialize, Serialize, WorkingOrder};
use crate::presentation::order::{Direction, OrderType, TimeInForce};
use chrono::{Duration, Utc};
use pretty_simple_display::{DebugPretty, DisplaySimple};
use std::fmt;
use std::fmt::{Debug, Display};

/// Parameters for getting recent prices (API v3)
#[derive(Clone, Default, Deserialize, Serialize)]
pub struct RecentPricesRequest<'a> {
    /// Instrument epic
    pub epic: &'a str,
    /// Optional price resolution (default: MINUTE)
    pub resolution: Option<&'a str>,
    /// Optional start date time (yyyy-MM-dd'T'HH:mm:ss)
    pub from: Option<&'a str>,
    /// Optional end date time (yyyy-MM-dd'T'HH:mm:ss)
    pub to: Option<&'a str>,
    /// Optional max number of price points (default: 10)
    pub max_points: Option<i32>,
    /// Optional page size (default: 20, disable paging = 0)
    pub page_size: Option<i32>,
    /// Optional page number (default: 1)
    pub page_number: Option<i32>,
}

impl<'a> RecentPricesRequest<'a> {
    /// Create new parameters with just the epic (required field)
    #[must_use]
    pub fn new(epic: &'a str) -> Self {
        Self {
            epic,
            ..Default::default()
        }
    }

    /// Set the resolution
    #[must_use]
    pub fn with_resolution(mut self, resolution: &'a str) -> Self {
        self.resolution = Some(resolution);
        self
    }

    /// Set the from date
    #[must_use]
    pub fn with_from(mut self, from: &'a str) -> Self {
        self.from = Some(from);
        self
    }

    /// Set the to date
    #[must_use]
    pub fn with_to(mut self, to: &'a str) -> Self {
        self.to = Some(to);
        self
    }

    /// Set the max points
    #[must_use]
    pub fn with_max_points(mut self, max_points: i32) -> Self {
        self.max_points = Some(max_points);
        self
    }

    /// Set the page size
    #[must_use]
    pub fn with_page_size(mut self, page_size: i32) -> Self {
        self.page_size = Some(page_size);
        self
    }

    /// Set the page number
    #[must_use]
    pub fn with_page_number(mut self, page_number: i32) -> Self {
        self.page_number = Some(page_number);
        self
    }
}

impl Display for RecentPricesRequest<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let json = serde_json::to_string(self).unwrap_or_else(|_| "Invalid JSON".to_string());
        write!(f, "{}", json)
    }
}

impl Debug for RecentPricesRequest<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let json =
            serde_json::to_string_pretty(self).unwrap_or_else(|_| "Invalid JSON".to_string());
        write!(f, "{}", json)
    }
}

/// Model for creating a new order
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct CreateOrderRequest {
    /// Instrument EPIC identifier
    pub epic: String,
    /// Order direction (buy or sell)
    pub direction: Direction,
    /// Order size/quantity
    pub size: f64,
    /// Type of order (market, limit, etc.)
    #[serde(rename = "orderType")]
    pub order_type: OrderType,
    /// Order duration (how long the order remains valid)
    #[serde(rename = "timeInForce")]
    pub time_in_force: TimeInForce,
    /// Price level for limit orders
    #[serde(skip_serializing_if = "Option::is_none")]
    pub level: Option<f64>,
    /// Whether to use a guaranteed stop
    #[serde(rename = "guaranteedStop")]
    pub guaranteed_stop: bool,
    /// Price level for stop loss
    #[serde(rename = "stopLevel", skip_serializing_if = "Option::is_none")]
    pub stop_level: Option<f64>,
    /// Stop loss distance
    #[serde(rename = "stopDistance", skip_serializing_if = "Option::is_none")]
    pub stop_distance: Option<f64>,
    /// Price level for take profit
    #[serde(rename = "limitLevel", skip_serializing_if = "Option::is_none")]
    pub limit_level: Option<f64>,
    /// Take profit distance
    #[serde(rename = "limitDistance", skip_serializing_if = "Option::is_none")]
    pub limit_distance: Option<f64>,
    /// Expiry date for the order
    #[serde(skip_serializing_if = "Option::is_none")]
    pub expiry: Option<String>,
    /// Client-generated reference for the deal
    #[serde(rename = "dealReference", skip_serializing_if = "Option::is_none")]
    pub deal_reference: Option<String>,
    /// Whether to force open a new position
    #[serde(rename = "forceOpen")]
    pub force_open: bool,
    /// Currency code for the order (e.g., "USD", "EUR")
    #[serde(rename = "currencyCode")]
    pub currency_code: String,
    /// Quote identifier for the order
    #[serde(rename = "quoteId", skip_serializing_if = "Option::is_none")]
    pub quote_id: Option<String>,
    /// Trailing stop enabled
    #[serde(rename = "trailingStop", skip_serializing_if = "Option::is_none")]
    pub trailing_stop: Option<bool>,
    /// Trailing stop increment (only if trailingStop is true)
    #[serde(
        rename = "trailingStopIncrement",
        skip_serializing_if = "Option::is_none"
    )]
    pub trailing_stop_increment: Option<f64>,
}

impl CreateOrderRequest {
    /// Creates a new market order, typically used for CFD (Contract for Difference) accounts
    #[must_use]
    pub fn market(
        epic: String,
        direction: Direction,
        size: f64,
        currency_code: Option<String>,
        deal_reference: Option<String>,
    ) -> Self {
        let rounded_size = (size * 100.0).floor() / 100.0;

        let currency_code = currency_code.unwrap_or_else(|| "EUR".to_string());

        Self {
            epic,
            direction,
            size: rounded_size,
            order_type: OrderType::Market,
            time_in_force: TimeInForce::FillOrKill,
            level: None,
            guaranteed_stop: false,
            stop_level: None,
            stop_distance: None,
            limit_level: None,
            limit_distance: None,
            expiry: Some("-".to_string()),
            deal_reference,
            force_open: true,
            currency_code,
            quote_id: None,
            trailing_stop: Some(false),
            trailing_stop_increment: None,
        }
    }

    /// Creates a new limit order, typically used for CFD (Contract for Difference) accounts
    #[must_use]
    pub fn limit(
        epic: String,
        direction: Direction,
        size: f64,
        level: f64,
        currency_code: Option<String>,
        deal_reference: Option<String>,
    ) -> Self {
        let rounded_size = (size * 100.0).floor() / 100.0;

        let currency_code = currency_code.unwrap_or_else(|| "EUR".to_string());

        Self {
            epic,
            direction,
            size: rounded_size,
            order_type: OrderType::Limit,
            time_in_force: TimeInForce::GoodTillCancelled,
            level: Some(level),
            guaranteed_stop: false,
            stop_level: None,
            stop_distance: None,
            limit_level: None,
            limit_distance: None,
            expiry: None,
            deal_reference,
            force_open: true,
            currency_code,
            quote_id: None,
            trailing_stop: Some(false),
            trailing_stop_increment: None,
        }
    }

    /// Creates a new instance of a market sell option with predefined parameters.
    ///
    /// This function sets up a sell option to the market for a given asset (`epic`)
    /// with the specified size. It configures the order with default values
    /// for attributes such as direction, order type, and time-in-force.
    ///
    /// # Parameters
    /// - `epic`: A `String` that represents the epic (unique identifier or code) of the instrument
    ///   being traded.
    /// - `size`: A `f64` value representing the size or quantity of the order.
    ///
    /// # Returns
    /// An instance of `Self` (the type implementing this function), containing the specified
    /// `epic` and `size`, along with default values for other parameters:
    ///
    /// - `direction`: Set to `Direction::Sell`.
    /// - `order_type`: Set to `OrderType::Limit`.
    /// - `time_in_force`: Set to `TimeInForce::FillOrKill`.
    /// - `level`: Set to `Some(DEFAULT_ORDER_SELL_SIZE)`.
    /// - `guaranteed_stop`: Set to `false`.
    /// - `stop_level`: Set to `None`.
    /// - `stop_distance`: Set to `None`.
    /// - `limit_level`: Set to `None`.
    /// - `limit_distance`: Set to `None`.
    /// - `expiry`: Set based on input or `None`.
    /// - `deal_reference`: Auto-generated if not provided.
    /// - `force_open`: Set to `true`.
    /// - `currency_code`: Defaults to `"EUR"` if not provided.
    ///
    /// Note that this function allows for minimal input (the instrument and size),
    /// while other fields are provided default values. If further customization is required,
    /// you can modify the returned instance as needed.
    #[must_use]
    pub fn sell_option_to_market(
        epic: String,
        size: f64,
        expiry: Option<String>,
        deal_reference: Option<String>,
        currency_code: Option<String>,
    ) -> Self {
        let rounded_size = (size * 100.0).floor() / 100.0;

        let currency_code = currency_code.unwrap_or_else(|| "EUR".to_string());

        let deal_reference =
            deal_reference.or_else(|| Some(nanoid::nanoid!(30, &nanoid::alphabet::SAFE)));

        Self {
            epic,
            direction: Direction::Sell,
            size: rounded_size,
            order_type: OrderType::Limit,
            time_in_force: TimeInForce::FillOrKill,
            level: Some(DEFAULT_ORDER_SELL_LEVEL),
            guaranteed_stop: false,
            stop_level: None,
            stop_distance: None,
            limit_level: None,
            limit_distance: None,
            expiry: expiry.clone(),
            deal_reference: deal_reference.clone(),
            force_open: true,
            currency_code,
            quote_id: None,
            trailing_stop: Some(false),
            trailing_stop_increment: None,
        }
    }

    /// Constructs and returns a new instance of the `Self` struct representing a sell option
    /// to the market with specific parameters for execution.
    ///
    /// # Parameters
    /// - `epic`: A `String` that specifies the EPIC
    ///   (Exchanged Product Information Code) of the instrument for which the sell order is created.
    /// - `size`: A `f64` that represents the size of the sell
    ///   order. The size is rounded to two decimal places.
    /// - `expiry`: An optional `String` that indicates the expiry date or period for
    ///   the sell order. If `None`, no expiry date will be set for the order.
    /// - `deal_reference`: An optional `String` that contains a reference or identifier
    ///   for the deal. Can be used for tracking purposes.
    /// - `currency_code`: An optional `String` representing the currency code. Defaults
    ///   to `"EUR"` if not provided.
    /// - `force_open`: A `bool` that specifies whether to force open the
    ///   position. When `true`, a new position is opened even if an existing position for the
    ///   same instrument and direction is available.
    ///
    /// # Returns
    /// - `Self`: A new instance populated with the provided parameters, including the following default
    ///   properties:
    ///   - `direction`: Set to `Direction::Sell` to designate the sell operation.
    ///   - `order_type`: Set to `OrderType::Limit` to signify the type of the order.
    ///   - `time_in_force`: Set to `TimeInForce::FillOrKill` indicating the order should be fully
    ///     executed or canceled.
    ///   - `level`: Set to a constant value `DEFAULT_ORDER_SELL_SIZE`.
    ///   - `guaranteed_stop`: Set to `false`, indicating no guaranteed stop.
    ///   - Other optional levels/distance fields (`stop_level`, `stop_distance`, `limit_level`,
    ///     `limit_distance`): Set to `None` by default.
    ///
    /// # Notes
    /// - The input `size` is automatically rounded down to two decimal places before being stored.
    #[must_use]
    pub fn sell_option_to_market_w_force(
        epic: String,
        size: f64,
        expiry: Option<String>,
        deal_reference: Option<String>,
        currency_code: Option<String>,
        force_open: bool, // Compensate position if it is already open
    ) -> Self {
        let rounded_size = (size * 100.0).floor() / 100.0;

        let currency_code = currency_code.unwrap_or_else(|| "EUR".to_string());

        let deal_reference =
            deal_reference.or_else(|| Some(nanoid::nanoid!(30, &nanoid::alphabet::SAFE)));

        Self {
            epic,
            direction: Direction::Sell,
            size: rounded_size,
            order_type: OrderType::Limit,
            time_in_force: TimeInForce::FillOrKill,
            level: Some(DEFAULT_ORDER_SELL_LEVEL),
            guaranteed_stop: false,
            stop_level: None,
            stop_distance: None,
            limit_level: None,
            limit_distance: None,
            expiry: expiry.clone(),
            deal_reference: deal_reference.clone(),
            force_open,
            currency_code,
            quote_id: None,
            trailing_stop: Some(false),
            trailing_stop_increment: None,
        }
    }

    /// Creates a new instance of an order to buy an option in the market with specified parameters.
    ///
    /// This method initializes an order with the following default values:
    /// - `direction` is set to `Buy`.
    /// - `order_type` is set to `Limit`.
    /// - `time_in_force` is set to `FillOrKill`.
    /// - `level` is set to `Some(DEFAULT_ORDER_BUY_SIZE)`.
    /// - `force_open` is set to `true`.
    ///   Other optional parameters, such as stop levels, distances, expiry, and currency code, are left as `None`.
    ///
    /// # Parameters
    /// - `epic` (`String`): The identifier for the market or instrument to trade.
    /// - `size` (`f64`): The size or quantity of the order to be executed.
    ///
    /// # Returns
    /// A new instance of `Self` that represents the configured buy option for the given market.
    ///
    /// # Note
    /// Ensure the `epic` and `size` values provided are valid and match required market conditions.
    #[must_use]
    pub fn buy_option_to_market(
        epic: String,
        size: f64,
        expiry: Option<String>,
        deal_reference: Option<String>,
        currency_code: Option<String>,
    ) -> Self {
        let rounded_size = (size * 100.0).floor() / 100.0;

        let currency_code = currency_code.unwrap_or_else(|| "EUR".to_string());

        let deal_reference =
            deal_reference.or_else(|| Some(nanoid::nanoid!(30, &nanoid::alphabet::SAFE)));

        Self {
            epic,
            direction: Direction::Buy,
            size: rounded_size,
            order_type: OrderType::Limit,
            time_in_force: TimeInForce::FillOrKill,
            level: Some(DEFAULT_ORDER_BUY_LEVEL),
            guaranteed_stop: false,
            stop_level: None,
            stop_distance: None,
            limit_level: None,
            limit_distance: None,
            expiry: expiry.clone(),
            deal_reference: deal_reference.clone(),
            force_open: true,
            currency_code: currency_code.clone(),
            quote_id: None,
            trailing_stop: Some(false),
            trailing_stop_increment: None,
        }
    }

    /// Constructs a new instance of an order to buy an option in the market with optional force_open behavior.
    ///
    /// # Parameters
    ///
    /// * `epic` - A `String` representing the unique identifier of the instrument to be traded.
    /// * `size` - A `f64` value that represents the size of the order.
    /// * `expiry` - An optional `String` representing the expiry date of the option.
    /// * `deal_reference` - An optional `String` for the deal reference identifier.
    /// * `currency_code` - An optional `String` representing the currency in which the order is denominated.
    ///   Defaults to "EUR" if not provided.
    /// * `force_open` - A `bool` indicating whether to force open a new position regardless of existing positions.
    ///
    /// # Returns
    ///
    /// Returns a new instance of `Self`, representing the constructed order with the provided parameters.
    ///
    /// # Behavior
    ///
    /// * The size of the order will be rounded down to two decimal places for precision.
    /// * If a `currency_code` is not provided, the default currency code "EUR" is used.
    /// * Other parameters are directly mapped into the returned instance.
    ///
    /// # Notes
    ///
    /// * This function assumes that other order-related fields such as `level`, `stop_level`, `stop_distance`,
    ///   etc., are set to their defaults or require specific business logic, such as
    ///   `DEFAULT_ORDER_BUY_SIZE` for the initial buy size.
    #[must_use]
    pub fn buy_option_to_market_w_force(
        epic: String,
        size: f64,
        expiry: Option<String>,
        deal_reference: Option<String>,
        currency_code: Option<String>,
        force_open: bool,
    ) -> Self {
        let rounded_size = (size * 100.0).floor() / 100.0;

        let currency_code = currency_code.unwrap_or_else(|| "EUR".to_string());

        let deal_reference =
            deal_reference.or_else(|| Some(nanoid::nanoid!(30, &nanoid::alphabet::SAFE)));

        Self {
            epic,
            direction: Direction::Buy,
            size: rounded_size,
            order_type: OrderType::Limit,
            time_in_force: TimeInForce::FillOrKill,
            level: Some(DEFAULT_ORDER_BUY_LEVEL),
            guaranteed_stop: false,
            stop_level: None,
            stop_distance: None,
            limit_level: None,
            limit_distance: None,
            expiry: expiry.clone(),
            deal_reference: deal_reference.clone(),
            force_open,
            currency_code: currency_code.clone(),
            quote_id: None,
            trailing_stop: Some(false),
            trailing_stop_increment: None,
        }
    }

    /// Adds a stop loss to the order
    #[must_use]
    pub fn with_stop_loss(mut self, stop_level: f64) -> Self {
        self.stop_level = Some(stop_level);
        self
    }

    /// Adds a take profit to the order
    #[must_use]
    pub fn with_take_profit(mut self, limit_level: f64) -> Self {
        self.limit_level = Some(limit_level);
        self
    }

    /// Adds a trailing stop loss to the order
    #[must_use]
    pub fn with_trailing_stop_loss(mut self, trailing_stop_increment: f64) -> Self {
        self.trailing_stop = Some(true);
        self.trailing_stop_increment = Some(trailing_stop_increment);
        self
    }

    /// Adds a reference to the order
    #[must_use]
    pub fn with_reference(mut self, reference: String) -> Self {
        self.deal_reference = Some(reference);
        self
    }

    /// Adds a stop distance to the order
    #[must_use]
    pub fn with_stop_distance(mut self, stop_distance: f64) -> Self {
        self.stop_distance = Some(stop_distance);
        self
    }

    /// Adds a limit distance to the order
    #[must_use]
    pub fn with_limit_distance(mut self, limit_distance: f64) -> Self {
        self.limit_distance = Some(limit_distance);
        self
    }

    /// Adds a guaranteed stop to the order
    #[must_use]
    pub fn with_guaranteed_stop(mut self, guaranteed: bool) -> Self {
        self.guaranteed_stop = guaranteed;
        self
    }
}

/// Model for updating an existing position (PUT /positions/otc/{dealId})
///
/// # Constraints
/// - If `guaranteed_stop` is `true`, then `stop_level` must be set
/// - If `guaranteed_stop` is `true`, then `trailing_stop` must be `false`
/// - If `trailing_stop` is `false`, then DO NOT set `trailing_stop_distance` or `trailing_stop_increment`
/// - If `trailing_stop` is `true`, then `guaranteed_stop` must be `false`
/// - If `trailing_stop` is `true`, then `trailing_stop_distance`, `trailing_stop_increment`, and `stop_level` must be set
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize, Default)]
pub struct UpdatePositionRequest {
    /// True if a guaranteed stop is required
    #[serde(rename = "guaranteedStop", skip_serializing_if = "Option::is_none")]
    pub guaranteed_stop: Option<bool>,
    /// New price level for take profit
    #[serde(rename = "limitLevel", skip_serializing_if = "Option::is_none")]
    pub limit_level: Option<f64>,
    /// New price level for stop loss
    #[serde(rename = "stopLevel", skip_serializing_if = "Option::is_none")]
    pub stop_level: Option<f64>,
    /// True if trailing stop is required
    #[serde(rename = "trailingStop", skip_serializing_if = "Option::is_none")]
    pub trailing_stop: Option<bool>,
    /// Distance for trailing stop in points
    #[serde(
        rename = "trailingStopDistance",
        skip_serializing_if = "Option::is_none"
    )]
    pub trailing_stop_distance: Option<f64>,
    /// Trailing stop step increment in points
    #[serde(
        rename = "trailingStopIncrement",
        skip_serializing_if = "Option::is_none"
    )]
    pub trailing_stop_increment: Option<f64>,
}

/// Model for closing an existing position
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct ClosePositionRequest {
    /// Unique identifier for the position to close
    #[serde(rename = "dealId", skip_serializing_if = "Option::is_none")]
    pub deal_id: Option<String>,
    /// Direction of the closing order (opposite to the position)
    pub direction: Direction,
    /// Instrument EPIC identifier
    #[serde(skip_serializing_if = "Option::is_none")]
    pub epic: Option<String>,
    /// Expiry date for the order
    #[serde(rename = "expiry", skip_serializing_if = "Option::is_none")]
    pub expiry: Option<String>,
    /// Price level for limit close orders
    #[serde(rename = "level", skip_serializing_if = "Option::is_none")]
    pub level: Option<f64>,
    /// Type of order to use for closing
    #[serde(rename = "orderType")]
    pub order_type: OrderType,
    /// Quote identifier for the order, used for certain order types that require a specific quote
    #[serde(rename = "quoteId", skip_serializing_if = "Option::is_none")]
    pub quote_id: Option<String>,
    /// Size/quantity to close
    pub size: f64,
    /// Order duration for the closing order
    #[serde(rename = "timeInForce")]
    pub time_in_force: TimeInForce,
}

impl ClosePositionRequest {
    /// Creates a request to close a position at market price
    #[must_use]
    pub fn market(deal_id: String, direction: Direction, size: f64) -> Self {
        Self {
            deal_id: Some(deal_id),
            direction,
            size,
            order_type: OrderType::Market,
            time_in_force: TimeInForce::FillOrKill,
            level: None,
            expiry: None,
            epic: None,
            quote_id: None,
        }
    }

    /// Creates a request to close a position at a specific price level
    ///
    /// This is useful for instruments that don't support market orders
    #[must_use]
    pub fn limit(deal_id: String, direction: Direction, size: f64, level: f64) -> Self {
        Self {
            deal_id: Some(deal_id),
            direction,
            size,
            order_type: OrderType::Limit,
            time_in_force: TimeInForce::FillOrKill,
            level: Some(level),
            expiry: None,
            epic: None,
            quote_id: None,
        }
    }

    /// Creates a request to close an option position by deal ID using a limit order with predefined price levels
    ///
    /// This is specifically designed for options trading where market orders are not supported
    /// and a limit order with a predefined price level is required based on the direction.
    ///
    /// # Arguments
    /// * `deal_id` - The ID of the deal to close
    /// * `direction` - The direction of the closing order (opposite of the position direction)
    /// * `size` - The size of the position to close
    #[must_use]
    pub fn close_option_to_market_by_id(deal_id: String, direction: Direction, size: f64) -> Self {
        // For options, we need to use limit orders with appropriate levels
        // Use reasonable levels based on direction to ensure fill while being accepted
        let level = match direction {
            Direction::Buy => Some(DEFAULT_ORDER_BUY_LEVEL),
            Direction::Sell => Some(DEFAULT_ORDER_SELL_LEVEL),
        };

        Self {
            deal_id: Some(deal_id),
            direction,
            size,
            order_type: OrderType::Limit,
            time_in_force: TimeInForce::FillOrKill,
            level,
            expiry: None,
            epic: None,
            quote_id: None,
        }
    }

    /// Creates a request to close an option position by epic identifier using a limit order with predefined price levels
    ///
    /// This is specifically designed for options trading where market orders are not supported
    /// and a limit order with a predefined price level is required based on the direction.
    /// This method is used when the deal ID is not available but the epic and expiry are known.
    ///
    /// # Arguments
    /// * `epic` - The epic identifier of the instrument
    /// * `expiry` - The expiry date of the option
    /// * `direction` - The direction of the closing order (opposite of the position direction)
    /// * `size` - The size of the position to close
    #[must_use]
    pub fn close_option_to_market_by_epic(
        epic: String,
        expiry: String,
        direction: Direction,
        size: f64,
    ) -> Self {
        // For options, we need to use limit orders with appropriate levels
        // Use reasonable levels based on direction to ensure fill while being accepted
        let level = match direction {
            Direction::Buy => Some(DEFAULT_ORDER_BUY_LEVEL),
            Direction::Sell => Some(DEFAULT_ORDER_SELL_LEVEL),
        };

        Self {
            deal_id: None,
            direction,
            size,
            order_type: OrderType::Limit,
            time_in_force: TimeInForce::FillOrKill,
            level,
            expiry: Some(expiry),
            epic: Some(epic),
            quote_id: None,
        }
    }
}

/// Model for creating a new working order
#[derive(DebugPretty, DisplaySimple, Clone, Deserialize, Serialize, Default)]
pub struct CreateWorkingOrderRequest {
    /// Instrument EPIC identifier
    pub epic: String,
    /// Order direction (buy or sell)
    pub direction: Direction,
    /// Order size/quantity
    pub size: f64,
    /// Price level for the order
    pub level: f64,
    /// Type of working order (LIMIT or STOP)
    #[serde(rename = "type")]
    pub order_type: OrderType,
    /// Order duration (how long the order remains valid)
    #[serde(rename = "timeInForce")]
    pub time_in_force: TimeInForce,
    /// Whether to use a guaranteed stop
    #[serde(rename = "guaranteedStop", skip_serializing_if = "Option::is_none")]
    pub guaranteed_stop: Option<bool>,
    /// Price level for stop loss
    #[serde(rename = "stopLevel", skip_serializing_if = "Option::is_none")]
    pub stop_level: Option<f64>,
    /// Distance for stop loss
    #[serde(rename = "stopDistance", skip_serializing_if = "Option::is_none")]
    pub stop_distance: Option<f64>,
    /// Price level for take profit
    #[serde(rename = "limitLevel", skip_serializing_if = "Option::is_none")]
    pub limit_level: Option<f64>,
    /// Distance for take profit
    #[serde(rename = "limitDistance", skip_serializing_if = "Option::is_none")]
    pub limit_distance: Option<f64>,
    /// Expiry date for GTD orders
    #[serde(rename = "goodTillDate", skip_serializing_if = "Option::is_none")]
    pub good_till_date: Option<String>,
    /// Client-generated reference for the deal
    #[serde(rename = "dealReference", skip_serializing_if = "Option::is_none")]
    pub deal_reference: Option<String>,
    /// Currency code for the order (e.g., "USD", "EUR")
    #[serde(rename = "currencyCode")]
    pub currency_code: String,
    /// Expiry date for the order
    pub expiry: String,
}

impl From<WorkingOrder> for CreateWorkingOrderRequest {
    fn from(value: WorkingOrder) -> Self {
        let data = value.working_order_data;
        Self {
            epic: data.epic,
            direction: data.direction,
            size: data.order_size,
            level: data.order_level,
            order_type: data.order_type,
            time_in_force: data.time_in_force,
            guaranteed_stop: Some(data.guaranteed_stop),
            stop_level: data.stop_level,
            stop_distance: data.stop_distance,
            limit_level: data.limit_level,
            limit_distance: data.limit_distance,
            good_till_date: data.good_till_date,
            deal_reference: data.deal_reference,
            currency_code: data.currency_code,
            expiry: value.market_data.expiry,
        }
    }
}

impl CreateWorkingOrderRequest {
    /// Creates a new limit working order
    #[must_use]
    pub fn limit(
        epic: String,
        direction: Direction,
        size: f64,
        level: f64,
        currency_code: String,
        expiry: String,
    ) -> Self {
        Self {
            epic,
            direction,
            size,
            level,
            order_type: OrderType::Limit,
            time_in_force: TimeInForce::GoodTillCancelled,
            guaranteed_stop: Some(false),
            stop_level: None,
            stop_distance: None,
            limit_level: None,
            limit_distance: None,
            good_till_date: None,
            deal_reference: None,
            currency_code,
            expiry,
        }
    }

    /// Creates a new stop working order
    #[must_use]
    pub fn stop(
        epic: String,
        direction: Direction,
        size: f64,
        level: f64,
        currency_code: String,
        expiry: String,
    ) -> Self {
        Self {
            epic,
            direction,
            size,
            level,
            order_type: OrderType::Stop,
            time_in_force: TimeInForce::GoodTillCancelled,
            guaranteed_stop: Some(false),
            stop_level: None,
            stop_distance: None,
            limit_level: None,
            limit_distance: None,
            good_till_date: None,
            deal_reference: None,
            currency_code,
            expiry,
        }
    }

    /// Adds a stop loss to the working order
    #[must_use]
    pub fn with_stop_loss(mut self, stop_level: f64) -> Self {
        self.stop_level = Some(stop_level);
        self
    }

    /// Adds a take profit to the working order
    #[must_use]
    pub fn with_take_profit(mut self, limit_level: f64) -> Self {
        self.limit_level = Some(limit_level);
        self
    }

    /// Adds a reference to the working order
    #[must_use]
    pub fn with_reference(mut self, reference: String) -> Self {
        self.deal_reference = Some(reference);
        self
    }

    /// Sets the expiration date for an order and updates the time-in-force policy.
    ///
    /// This method updates the `time_in_force` property to `GoodTillDate` and assigns
    /// the provided expiration date to the `good_till_date` property. It allows chaining
    /// as it consumes the current instance and returns it after modification.
    ///
    /// # Arguments
    /// * `date` - A `String` representing the expiration date for the order.
    ///
    /// # Returns
    /// * `Self` - The updated instance of the type, allowing method chaining.
    ///
    /// In the example above, the expiration date for the order is set to
    /// "2023-12-31T23:59:59Z" and the `time_in_force` policy is set to `GoodTillDate`.
    #[must_use]
    pub fn expires_at(mut self, date: String) -> Self {
        self.time_in_force = TimeInForce::GoodTillDate;
        self.good_till_date = Some(date);
        self
    }

    ///
    /// Sets the order to expire by the end of the next day (tomorrow).
    ///
    /// This method modifies the `time_in_force` field to `GoodTillDate` and calculates
    /// the expiration date as tomorrow's date and time. The calculated date is then
    /// formatted as a string in the format `YYYY/MM/DD HH:MM:SS` and assigned to
    /// the `good_till_date` field.
    ///
    /// # Returns
    /// Returns the updated instance of the struct with the expiration date set to tomorrow.
    ///
    /// In this example, the `expires_tomorrow` method sets the order to expire at the
    /// same time on the next calendar day.
    ///
    /// Note: The function uses the UTC timezone for calculating the date and time.
    ///
    #[must_use]
    pub fn expires_tomorrow(mut self) -> Self {
        self.time_in_force = TimeInForce::GoodTillDate;
        let tomorrow = Utc::now() + Duration::days(1);
        self.good_till_date = Some(tomorrow.format("%Y/%m/%d %H:%M:%S").to_string());
        self
    }

    /// Sets the expiration time for an order and configures it to expire at a specific date and time.
    ///
    /// This method modifies the `time_in_force` for the order to `GoodTillDate`
    /// and calculates the expiration time by adding the provided `duration` to
    /// the current UTC time. The calculated expiration time is formatted as
    /// "YYYY/MM/DD HH:MM:SS" and stored in the `good_till_date` field.
    ///
    /// # Parameters
    /// - `duration`: A `Duration` instance that represents the amount of time
    ///   after the current UTC time when the order should expire.
    ///
    /// # Returns
    /// Returns `Self` with updated `time_in_force` and `good_till_date`.
    ///
    /// Note: This method assumes that the runtime uses the `chrono` crate for
    /// time handling and formatting.
    #[must_use]
    pub fn expires_in(mut self, duration: Duration) -> Self {
        self.time_in_force = TimeInForce::GoodTillDate;
        let tomorrow = Utc::now() + duration;
        self.good_till_date = Some(tomorrow.format("%Y/%m/%d %H:%M:%S").to_string());
        self
    }
}

// ============================================================================
// WATCHLIST REQUESTS
// ============================================================================

/// Request to create a new watchlist
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize, Default)]
pub struct CreateWatchlistRequest {
    /// Name for the new watchlist
    pub name: String,
    /// Optional list of EPICs to add to the watchlist
    #[serde(skip_serializing_if = "Option::is_none")]
    pub epics: Option<Vec<String>>,
}

impl CreateWatchlistRequest {
    /// Create a new watchlist request with just a name
    #[must_use]
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
            epics: None,
        }
    }

    /// Create a watchlist request with name and epics
    #[must_use]
    pub fn with_epics(name: &str, epics: Vec<String>) -> Self {
        Self {
            name: name.to_string(),
            epics: Some(epics),
        }
    }
}

/// Request to add an instrument to a watchlist
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize, Default)]
pub struct AddToWatchlistRequest {
    /// EPIC of the instrument to add
    pub epic: String,
}

// ============================================================================
// WORKING ORDER UPDATE REQUEST
// ============================================================================

/// Request to update an existing working order
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize, Default)]
pub struct UpdateWorkingOrderRequest {
    /// Good till date for the order (format: yyyy/MM/dd HH:mm:ss)
    #[serde(rename = "goodTillDate", skip_serializing_if = "Option::is_none")]
    pub good_till_date: Option<String>,
    /// Order level
    pub level: f64,
    /// Distance from current price to limit level
    #[serde(rename = "limitDistance", skip_serializing_if = "Option::is_none")]
    pub limit_distance: Option<f64>,
    /// Limit level
    #[serde(rename = "limitLevel", skip_serializing_if = "Option::is_none")]
    pub limit_level: Option<f64>,
    /// Distance from current price to stop level
    #[serde(rename = "stopDistance", skip_serializing_if = "Option::is_none")]
    pub stop_distance: Option<f64>,
    /// Stop level
    #[serde(rename = "stopLevel", skip_serializing_if = "Option::is_none")]
    pub stop_level: Option<f64>,
    /// Whether the stop is guaranteed
    #[serde(rename = "guaranteedStop")]
    pub guaranteed_stop: bool,
    /// Time in force
    #[serde(rename = "timeInForce")]
    pub time_in_force: TimeInForce,
    /// Order type (LIMIT or STOP)
    #[serde(rename = "type")]
    pub order_type: OrderType,
}

impl UpdateWorkingOrderRequest {
    /// Create a new update working order request
    #[must_use]
    pub fn new(level: f64, order_type: OrderType, time_in_force: TimeInForce) -> Self {
        Self {
            level,
            order_type,
            time_in_force,
            guaranteed_stop: false,
            good_till_date: None,
            limit_distance: None,
            limit_level: None,
            stop_distance: None,
            stop_level: None,
        }
    }

    /// Set the stop level
    #[must_use]
    pub fn with_stop_level(mut self, stop_level: f64) -> Self {
        self.stop_level = Some(stop_level);
        self
    }

    /// Set the limit level
    #[must_use]
    pub fn with_limit_level(mut self, limit_level: f64) -> Self {
        self.limit_level = Some(limit_level);
        self
    }

    /// Set guaranteed stop
    #[must_use]
    pub fn with_guaranteed_stop(mut self, guaranteed: bool) -> Self {
        self.guaranteed_stop = guaranteed;
        self
    }
}

// ============================================================================
// INDICATIVE COSTS REQUESTS
// ============================================================================

/// Request for indicative costs when opening a position
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize, Default)]
pub struct OpenCostsRequest {
    /// Instrument epic
    pub epic: String,
    /// Trade direction (BUY or SELL)
    pub direction: Direction,
    /// Trade size
    pub size: f64,
    /// Order type
    #[serde(rename = "orderType")]
    pub order_type: OrderType,
    /// Currency code
    #[serde(rename = "currencyCode")]
    pub currency_code: String,
    /// Whether the stop is guaranteed
    #[serde(rename = "guaranteedStop", skip_serializing_if = "Option::is_none")]
    pub guaranteed_stop: Option<bool>,
    /// Stop distance
    #[serde(rename = "stopDistance", skip_serializing_if = "Option::is_none")]
    pub stop_distance: Option<f64>,
    /// Limit distance
    #[serde(rename = "limitDistance", skip_serializing_if = "Option::is_none")]
    pub limit_distance: Option<f64>,
}

impl OpenCostsRequest {
    /// Create a new open costs request
    #[must_use]
    pub fn new(epic: &str, direction: Direction, size: f64, currency_code: &str) -> Self {
        Self {
            epic: epic.to_string(),
            direction,
            size,
            order_type: OrderType::Market,
            currency_code: currency_code.to_string(),
            guaranteed_stop: None,
            stop_distance: None,
            limit_distance: None,
        }
    }
}

/// Request for indicative costs when closing a position
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize, Default)]
pub struct CloseCostsRequest {
    /// Deal ID of the position to close
    #[serde(rename = "dealId")]
    pub deal_id: String,
    /// Trade direction (opposite of the position direction)
    pub direction: Direction,
    /// Size to close
    pub size: f64,
    /// Order type
    #[serde(rename = "orderType")]
    pub order_type: OrderType,
}

impl CloseCostsRequest {
    /// Create a new close costs request
    #[must_use]
    pub fn new(deal_id: &str, direction: Direction, size: f64) -> Self {
        Self {
            deal_id: deal_id.to_string(),
            direction,
            size,
            order_type: OrderType::Market,
        }
    }
}

/// Request for indicative costs when editing a position
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize, Default)]
pub struct EditCostsRequest {
    /// Deal ID of the position to edit
    #[serde(rename = "dealId")]
    pub deal_id: String,
    /// New stop level
    #[serde(rename = "stopLevel", skip_serializing_if = "Option::is_none")]
    pub stop_level: Option<f64>,
    /// New limit level
    #[serde(rename = "limitLevel", skip_serializing_if = "Option::is_none")]
    pub limit_level: Option<f64>,
    /// Whether the stop is guaranteed
    #[serde(rename = "guaranteedStop", skip_serializing_if = "Option::is_none")]
    pub guaranteed_stop: Option<bool>,
}

impl EditCostsRequest {
    /// Create a new edit costs request
    #[must_use]
    pub fn new(deal_id: &str) -> Self {
        Self {
            deal_id: deal_id.to_string(),
            stop_level: None,
            limit_level: None,
            guaranteed_stop: None,
        }
    }

    /// Set the new stop level
    #[must_use]
    pub fn with_stop_level(mut self, stop_level: f64) -> Self {
        self.stop_level = Some(stop_level);
        self
    }

    /// Set the new limit level
    #[must_use]
    pub fn with_limit_level(mut self, limit_level: f64) -> Self {
        self.limit_level = Some(limit_level);
        self
    }
}

// ============================================================================
// ACCOUNT PREFERENCES REQUEST
// ============================================================================

/// Request to update account preferences
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize, Default)]
pub struct UpdatePreferencesRequest {
    /// Whether trailing stops should be enabled
    #[serde(rename = "trailingStopsEnabled")]
    pub trailing_stops_enabled: bool,
}