deribit-base 0.3.1

Base library with common structs, traits, and logic for Deribit API clients
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
/******************************************************************************
   Author: Joaquín Béjar García
   Email: jb@taunais.com
   Date: 21/7/25
******************************************************************************/
use crate::model::order::{OrderSide, OrderType, TimeInForce};
use pretty_simple_display::{DebugPretty, DisplaySimple};

use serde::{Deserialize, Serialize};

/// FIX protocol compatible structures
pub mod fix {
    use super::*;

    /// New order request structure for FIX protocol
    #[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
    pub struct NewOrderRequest {
        /// Instrument symbol (e.g., "BTC-PERPETUAL")
        pub symbol: String,
        /// Order side (buy/sell)
        pub side: OrderSide,
        /// Order type
        pub order_type: OrderType,
        /// Order quantity
        pub quantity: f64,
        /// Order price (required for limit orders)
        pub price: Option<f64>,
        /// Time in force
        pub time_in_force: TimeInForce,
        /// Client order ID
        pub client_order_id: Option<String>,
    }

    impl NewOrderRequest {
        /// Create a new market buy order
        pub fn market_buy(symbol: String, quantity: f64) -> Self {
            Self {
                symbol,
                side: OrderSide::Buy,
                order_type: OrderType::Market,
                quantity,
                price: None,
                time_in_force: TimeInForce::ImmediateOrCancel,
                client_order_id: None,
            }
        }

        /// Create a new market sell order
        pub fn market_sell(symbol: String, quantity: f64) -> Self {
            Self {
                symbol,
                side: OrderSide::Sell,
                order_type: OrderType::Market,
                quantity,
                price: None,
                time_in_force: TimeInForce::ImmediateOrCancel,
                client_order_id: None,
            }
        }

        /// Create a new limit buy order
        pub fn limit_buy(symbol: String, quantity: f64, price: f64) -> Self {
            Self {
                symbol,
                side: OrderSide::Buy,
                order_type: OrderType::Limit,
                quantity,
                price: Some(price),
                time_in_force: TimeInForce::GoodTilCancelled,
                client_order_id: None,
            }
        }

        /// Create a new limit sell order
        pub fn limit_sell(symbol: String, quantity: f64, price: f64) -> Self {
            Self {
                symbol,
                side: OrderSide::Sell,
                order_type: OrderType::Limit,
                quantity,
                price: Some(price),
                time_in_force: TimeInForce::GoodTilCancelled,
                client_order_id: None,
            }
        }

        /// Set client order ID
        pub fn with_client_order_id(mut self, client_order_id: String) -> Self {
            self.client_order_id = Some(client_order_id);
            self
        }

        /// Set time in force
        pub fn with_time_in_force(mut self, tif: TimeInForce) -> Self {
            self.time_in_force = tif;
            self
        }
    }

    /// Convert from REST/WebSocket NewOrderRequest to FIX NewOrderRequest
    impl From<super::NewOrderRequest> for NewOrderRequest {
        fn from(rest_order: super::NewOrderRequest) -> Self {
            Self {
                symbol: rest_order.instrument_name,
                side: rest_order.side,
                order_type: rest_order.order_type,
                quantity: rest_order.amount,
                price: rest_order.price,
                time_in_force: rest_order.time_in_force,
                client_order_id: rest_order.client_order_id,
            }
        }
    }

    /// Convert from FIX NewOrderRequest to REST/WebSocket NewOrderRequest
    impl From<NewOrderRequest> for super::NewOrderRequest {
        fn from(fix_order: NewOrderRequest) -> Self {
            Self {
                instrument_name: fix_order.symbol,
                amount: fix_order.quantity,
                order_type: fix_order.order_type,
                side: fix_order.side,
                price: fix_order.price,
                time_in_force: fix_order.time_in_force,
                post_only: None,
                reduce_only: None,
                label: None,
                stop_price: None,
                trigger: None,
                advanced: None,
                max_show: None,
                reject_post_only: None,
                valid_until: None,
                client_order_id: fix_order.client_order_id,
            }
        }
    }
}

/// Generic request for creating new orders
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct NewOrderRequest {
    /// Instrument name
    pub instrument_name: String,
    /// Order amount
    pub amount: f64,
    /// Order type
    #[serde(rename = "type")]
    pub order_type: OrderType,
    /// Order side (buy/sell)
    pub side: OrderSide,
    /// Order price (required for limit orders)
    pub price: Option<f64>,
    /// Time in force
    pub time_in_force: TimeInForce,
    /// Post-only flag
    pub post_only: Option<bool>,
    /// Reduce-only flag
    pub reduce_only: Option<bool>,
    /// Order label
    pub label: Option<String>,
    /// Stop price for stop orders
    pub stop_price: Option<f64>,
    /// Trigger type for stop orders
    pub trigger: Option<TriggerType>,
    /// Advanced order type
    pub advanced: Option<AdvancedOrderType>,
    /// Maximum show amount (iceberg orders)
    pub max_show: Option<f64>,
    /// Reject post-only flag
    pub reject_post_only: Option<bool>,
    /// Valid until timestamp
    pub valid_until: Option<i64>,
    /// Client order ID for tracking
    pub client_order_id: Option<String>,
}

impl NewOrderRequest {
    /// Create a new market buy order
    pub fn market_buy(instrument_name: String, amount: f64) -> Self {
        Self {
            instrument_name,
            amount,
            order_type: OrderType::Market,
            side: OrderSide::Buy,
            price: None,
            time_in_force: TimeInForce::ImmediateOrCancel,
            post_only: None,
            reduce_only: None,
            label: None,
            stop_price: None,
            trigger: None,
            advanced: None,
            max_show: None,
            reject_post_only: None,
            valid_until: None,
            client_order_id: None,
        }
    }

    /// Create a new market sell order
    pub fn market_sell(instrument_name: String, amount: f64) -> Self {
        Self {
            instrument_name,
            amount,
            order_type: OrderType::Market,
            side: OrderSide::Sell,
            price: None,
            time_in_force: TimeInForce::ImmediateOrCancel,
            post_only: None,
            reduce_only: None,
            label: None,
            stop_price: None,
            trigger: None,
            advanced: None,
            max_show: None,
            reject_post_only: None,
            valid_until: None,
            client_order_id: None,
        }
    }

    /// Create a new limit buy order
    pub fn limit_buy(instrument_name: String, amount: f64, price: f64) -> Self {
        Self {
            instrument_name,
            amount,
            order_type: OrderType::Limit,
            side: OrderSide::Buy,
            price: Some(price),
            time_in_force: TimeInForce::GoodTilCancelled,
            post_only: None,
            reduce_only: None,
            label: None,
            stop_price: None,
            trigger: None,
            advanced: None,
            max_show: None,
            reject_post_only: None,
            valid_until: None,
            client_order_id: None,
        }
    }

    /// Create a new limit sell order
    pub fn limit_sell(instrument_name: String, amount: f64, price: f64) -> Self {
        Self {
            instrument_name,
            amount,
            order_type: OrderType::Limit,
            side: OrderSide::Sell,
            price: Some(price),
            time_in_force: TimeInForce::GoodTilCancelled,
            post_only: None,
            reduce_only: None,
            label: None,
            stop_price: None,
            trigger: None,
            advanced: None,
            max_show: None,
            reject_post_only: None,
            valid_until: None,
            client_order_id: None,
        }
    }

    /// Set the order as post-only
    pub fn with_post_only(mut self, post_only: bool) -> Self {
        self.post_only = Some(post_only);
        self
    }

    /// Set the order as reduce-only
    pub fn with_reduce_only(mut self, reduce_only: bool) -> Self {
        self.reduce_only = Some(reduce_only);
        self
    }

    /// Set order label
    pub fn with_label(mut self, label: String) -> Self {
        self.label = Some(label);
        self
    }

    /// Set time in force
    pub fn with_time_in_force(mut self, tif: TimeInForce) -> Self {
        self.time_in_force = tif;
        self
    }
}

/// Trigger type for stop orders
#[derive(DebugPretty, DisplaySimple, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TriggerType {
    /// Index price trigger
    IndexPrice,
    /// Mark price trigger
    MarkPrice,
    /// Last price trigger
    LastPrice,
}

/// Linked order type for conditional order execution
#[derive(DebugPretty, DisplaySimple, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum LinkedOrderType {
    /// One order triggers another (OTO)
    OneTriggersOther,
    /// One order cancels another (OCO)
    OneCancelsOther,
    /// One order triggers one and cancels another (OTOCO)
    OneTriggersOneCancelsOther,
}

/// Trigger fill condition for linked orders
#[derive(DebugPretty, DisplaySimple, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TriggerFillCondition {
    /// Trigger on first partial fill
    FirstHit,
    /// Trigger only when completely filled
    CompleteFill,
    /// Trigger incrementally as fills occur
    Incremental,
}

/// Advanced order type
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum AdvancedOrderType {
    /// USD denomination
    Usd,
    /// Implied volatility
    Implv,
}

/// Order modification request
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct ModifyOrderRequest {
    /// Order ID to modify
    pub order_id: String,
    /// New amount
    pub amount: Option<f64>,
    /// New price
    pub price: Option<f64>,
    /// New stop price
    pub stop_price: Option<f64>,
    /// New post-only flag
    pub post_only: Option<bool>,
    /// New reduce-only flag
    pub reduce_only: Option<bool>,
    /// New reject post-only flag
    pub reject_post_only: Option<bool>,
    /// New advanced order type
    pub advanced: Option<AdvancedOrderType>,
}

/// Order cancellation request
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct CancelOrderRequest {
    /// Order ID to cancel
    pub order_id: String,
}

/// Cancel all orders request
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct CancelAllOrdersRequest {
    /// Currency filter
    pub currency: Option<String>,
    /// Instrument kind filter
    pub kind: Option<String>,
    /// Instrument type filter
    #[serde(rename = "type")]
    pub instrument_type: Option<String>,
}

/// Position close request
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct ClosePositionRequest {
    /// Instrument name
    pub instrument_name: String,
    /// Order type for closing
    #[serde(rename = "type")]
    pub order_type: OrderType,
    /// Price for limit orders
    pub price: Option<f64>,
}

/// Authentication request
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct AuthRequest {
    /// Grant type
    pub grant_type: String,
    /// Client ID
    pub client_id: String,
    /// Client secret
    pub client_secret: String,
    /// Refresh token (for refresh grant)
    pub refresh_token: Option<String>,
    /// Scope
    pub scope: Option<String>,
}

impl AuthRequest {
    /// Create a client credentials authentication request
    pub fn client_credentials(client_id: String, client_secret: String) -> Self {
        Self {
            grant_type: "client_credentials".to_string(),
            client_id,
            client_secret,
            refresh_token: None,
            scope: None,
        }
    }

    /// Create a refresh token authentication request
    pub fn refresh_token(client_id: String, client_secret: String, refresh_token: String) -> Self {
        Self {
            grant_type: "refresh_token".to_string(),
            client_id,
            client_secret,
            refresh_token: Some(refresh_token),
            scope: None,
        }
    }
}

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

    #[test]
    fn test_fix_new_order_request_market_buy() {
        let order = fix::NewOrderRequest::market_buy("BTC-PERPETUAL".to_string(), 1.0);
        assert_eq!(order.symbol, "BTC-PERPETUAL");
        assert_eq!(order.side, OrderSide::Buy);
        assert_eq!(order.order_type, OrderType::Market);
        assert_eq!(order.quantity, 1.0);
        assert_eq!(order.price, None);
        assert_eq!(order.time_in_force, TimeInForce::ImmediateOrCancel);
        assert_eq!(order.client_order_id, None);
    }

    #[test]
    fn test_fix_new_order_request_market_sell() {
        let order = fix::NewOrderRequest::market_sell("ETH-PERPETUAL".to_string(), 2.0);
        assert_eq!(order.symbol, "ETH-PERPETUAL");
        assert_eq!(order.side, OrderSide::Sell);
        assert_eq!(order.order_type, OrderType::Market);
        assert_eq!(order.quantity, 2.0);
        assert_eq!(order.price, None);
        assert_eq!(order.time_in_force, TimeInForce::ImmediateOrCancel);
    }

    #[test]
    fn test_fix_new_order_request_limit_buy() {
        let order = fix::NewOrderRequest::limit_buy("BTC-PERPETUAL".to_string(), 1.0, 50000.0);
        assert_eq!(order.symbol, "BTC-PERPETUAL");
        assert_eq!(order.side, OrderSide::Buy);
        assert_eq!(order.order_type, OrderType::Limit);
        assert_eq!(order.quantity, 1.0);
        assert_eq!(order.price, Some(50000.0));
        assert_eq!(order.time_in_force, TimeInForce::GoodTilCancelled);
    }

    #[test]
    fn test_fix_new_order_request_limit_sell() {
        let order = fix::NewOrderRequest::limit_sell("ETH-PERPETUAL".to_string(), 2.0, 3500.0);
        assert_eq!(order.symbol, "ETH-PERPETUAL");
        assert_eq!(order.side, OrderSide::Sell);
        assert_eq!(order.order_type, OrderType::Limit);
        assert_eq!(order.quantity, 2.0);
        assert_eq!(order.price, Some(3500.0));
        assert_eq!(order.time_in_force, TimeInForce::GoodTilCancelled);
    }

    #[test]
    fn test_fix_new_order_request_with_client_order_id() {
        let order = fix::NewOrderRequest::market_buy("BTC-PERPETUAL".to_string(), 1.0)
            .with_client_order_id("CLIENT_ORDER_123".to_string());
        assert_eq!(order.client_order_id, Some("CLIENT_ORDER_123".to_string()));
    }

    #[test]
    fn test_fix_new_order_request_with_time_in_force() {
        let order = fix::NewOrderRequest::limit_buy("BTC-PERPETUAL".to_string(), 1.0, 50000.0)
            .with_time_in_force(TimeInForce::FillOrKill);
        assert_eq!(order.time_in_force, TimeInForce::FillOrKill);
    }

    #[test]
    fn test_new_order_request_market_buy() {
        let order = NewOrderRequest::market_buy("BTC-PERPETUAL".to_string(), 1.0);
        assert_eq!(order.instrument_name, "BTC-PERPETUAL");
        assert_eq!(order.amount, 1.0);
        assert_eq!(order.order_type, OrderType::Market);
        assert_eq!(order.side, OrderSide::Buy);
        assert_eq!(order.price, None);
        assert_eq!(order.time_in_force, TimeInForce::ImmediateOrCancel);
        assert_eq!(order.post_only, None);
        assert_eq!(order.reduce_only, None);
    }

    #[test]
    fn test_new_order_request_market_sell() {
        let order = NewOrderRequest::market_sell("ETH-PERPETUAL".to_string(), 2.0);
        assert_eq!(order.instrument_name, "ETH-PERPETUAL");
        assert_eq!(order.amount, 2.0);
        assert_eq!(order.order_type, OrderType::Market);
        assert_eq!(order.side, OrderSide::Sell);
        assert_eq!(order.price, None);
        assert_eq!(order.time_in_force, TimeInForce::ImmediateOrCancel);
    }

    #[test]
    fn test_new_order_request_limit_buy() {
        let order = NewOrderRequest::limit_buy("BTC-PERPETUAL".to_string(), 1.0, 50000.0);
        assert_eq!(order.instrument_name, "BTC-PERPETUAL");
        assert_eq!(order.amount, 1.0);
        assert_eq!(order.order_type, OrderType::Limit);
        assert_eq!(order.side, OrderSide::Buy);
        assert_eq!(order.price, Some(50000.0));
        assert_eq!(order.time_in_force, TimeInForce::GoodTilCancelled);
    }

    #[test]
    fn test_new_order_request_limit_sell() {
        let order = NewOrderRequest::limit_sell("ETH-PERPETUAL".to_string(), 2.0, 3500.0);
        assert_eq!(order.instrument_name, "ETH-PERPETUAL");
        assert_eq!(order.amount, 2.0);
        assert_eq!(order.order_type, OrderType::Limit);
        assert_eq!(order.side, OrderSide::Sell);
        assert_eq!(order.price, Some(3500.0));
        assert_eq!(order.time_in_force, TimeInForce::GoodTilCancelled);
    }

    #[test]
    fn test_new_order_request_with_post_only() {
        let order = NewOrderRequest::limit_buy("BTC-PERPETUAL".to_string(), 1.0, 50000.0)
            .with_post_only(true);
        assert_eq!(order.post_only, Some(true));
    }

    #[test]
    fn test_new_order_request_with_reduce_only() {
        let order = NewOrderRequest::limit_sell("BTC-PERPETUAL".to_string(), 1.0, 50000.0)
            .with_reduce_only(true);
        assert_eq!(order.reduce_only, Some(true));
    }

    #[test]
    fn test_new_order_request_with_label() {
        let order = NewOrderRequest::market_buy("BTC-PERPETUAL".to_string(), 1.0)
            .with_label("test_order".to_string());
        assert_eq!(order.label, Some("test_order".to_string()));
    }

    #[test]
    fn test_new_order_request_with_time_in_force() {
        let order = NewOrderRequest::limit_buy("BTC-PERPETUAL".to_string(), 1.0, 50000.0)
            .with_time_in_force(TimeInForce::FillOrKill);
        assert_eq!(order.time_in_force, TimeInForce::FillOrKill);
    }

    #[test]
    fn test_trigger_type_serialization() {
        let trigger_types = vec![
            TriggerType::IndexPrice,
            TriggerType::MarkPrice,
            TriggerType::LastPrice,
        ];

        for trigger_type in trigger_types {
            let json = serde_json::to_string(&trigger_type).unwrap();
            let deserialized: TriggerType = serde_json::from_str(&json).unwrap();
            assert_eq!(trigger_type, deserialized);
        }
    }

    #[test]
    fn test_advanced_order_type_serialization() {
        let advanced_types = vec![AdvancedOrderType::Usd, AdvancedOrderType::Implv];

        for advanced_type in advanced_types {
            let json = serde_json::to_string(&advanced_type).unwrap();
            let deserialized: AdvancedOrderType = serde_json::from_str(&json).unwrap();
            assert_eq!(advanced_type, deserialized);
        }
    }

    #[test]
    fn test_modify_order_request() {
        let modify_request = ModifyOrderRequest {
            order_id: "ORDER_123".to_string(),
            amount: Some(2.0),
            price: Some(51000.0),
            stop_price: None,
            post_only: Some(true),
            reduce_only: Some(false),
            reject_post_only: None,
            advanced: Some(AdvancedOrderType::Usd),
        };

        assert_eq!(modify_request.order_id, "ORDER_123");
        assert_eq!(modify_request.amount, Some(2.0));
        assert_eq!(modify_request.price, Some(51000.0));
        assert_eq!(modify_request.post_only, Some(true));
        assert_eq!(modify_request.advanced, Some(AdvancedOrderType::Usd));
    }

    #[test]
    fn test_cancel_order_request() {
        let cancel_request = CancelOrderRequest {
            order_id: "ORDER_123".to_string(),
        };
        assert_eq!(cancel_request.order_id, "ORDER_123");
    }

    #[test]
    fn test_cancel_all_orders_request() {
        let cancel_all_request = CancelAllOrdersRequest {
            currency: Some("BTC".to_string()),
            kind: Some("future".to_string()),
            instrument_type: Some("perpetual".to_string()),
        };

        assert_eq!(cancel_all_request.currency, Some("BTC".to_string()));
        assert_eq!(cancel_all_request.kind, Some("future".to_string()));
        assert_eq!(
            cancel_all_request.instrument_type,
            Some("perpetual".to_string())
        );
    }

    #[test]
    fn test_cancel_all_orders_request_empty() {
        let cancel_all_request = CancelAllOrdersRequest {
            currency: None,
            kind: None,
            instrument_type: None,
        };

        assert_eq!(cancel_all_request.currency, None);
        assert_eq!(cancel_all_request.kind, None);
        assert_eq!(cancel_all_request.instrument_type, None);
    }

    #[test]
    fn test_close_position_request() {
        let close_request = ClosePositionRequest {
            instrument_name: "BTC-PERPETUAL".to_string(),
            order_type: OrderType::Market,
            price: None,
        };

        assert_eq!(close_request.instrument_name, "BTC-PERPETUAL");
        assert_eq!(close_request.order_type, OrderType::Market);
        assert_eq!(close_request.price, None);

        let close_limit_request = ClosePositionRequest {
            instrument_name: "ETH-PERPETUAL".to_string(),
            order_type: OrderType::Limit,
            price: Some(3500.0),
        };

        assert_eq!(close_limit_request.price, Some(3500.0));
    }

    #[test]
    fn test_auth_request_client_credentials() {
        let auth_request = AuthRequest::client_credentials(
            "client_id_123".to_string(),
            "client_secret_456".to_string(),
        );

        assert_eq!(auth_request.grant_type, "client_credentials");
        assert_eq!(auth_request.client_id, "client_id_123");
        assert_eq!(auth_request.client_secret, "client_secret_456");
        assert_eq!(auth_request.refresh_token, None);
        assert_eq!(auth_request.scope, None);
    }

    #[test]
    fn test_auth_request_refresh_token() {
        let auth_request = AuthRequest::refresh_token(
            "client_id_123".to_string(),
            "client_secret_456".to_string(),
            "refresh_token_789".to_string(),
        );

        assert_eq!(auth_request.grant_type, "refresh_token");
        assert_eq!(auth_request.client_id, "client_id_123");
        assert_eq!(auth_request.client_secret, "client_secret_456");
        assert_eq!(
            auth_request.refresh_token,
            Some("refresh_token_789".to_string())
        );
        assert_eq!(auth_request.scope, None);
    }

    #[test]
    fn test_fix_to_rest_conversion() {
        let fix_order = fix::NewOrderRequest::limit_buy("BTC-PERPETUAL".to_string(), 1.0, 50000.0)
            .with_client_order_id("CLIENT_ORDER_123".to_string());

        let rest_order: NewOrderRequest = fix_order.into();

        assert_eq!(rest_order.instrument_name, "BTC-PERPETUAL");
        assert_eq!(rest_order.amount, 1.0);
        assert_eq!(rest_order.order_type, OrderType::Limit);
        assert_eq!(rest_order.side, OrderSide::Buy);
        assert_eq!(rest_order.price, Some(50000.0));
        assert_eq!(
            rest_order.client_order_id,
            Some("CLIENT_ORDER_123".to_string())
        );
        assert_eq!(rest_order.post_only, None);
        assert_eq!(rest_order.reduce_only, None);
    }

    #[test]
    fn test_rest_to_fix_conversion() {
        let rest_order = NewOrderRequest::limit_sell("ETH-PERPETUAL".to_string(), 2.0, 3500.0)
            .with_label("test_order".to_string())
            .with_post_only(true);

        let fix_order: fix::NewOrderRequest = rest_order.into();

        assert_eq!(fix_order.symbol, "ETH-PERPETUAL");
        assert_eq!(fix_order.quantity, 2.0);
        assert_eq!(fix_order.order_type, OrderType::Limit);
        assert_eq!(fix_order.side, OrderSide::Sell);
        assert_eq!(fix_order.price, Some(3500.0));
        assert_eq!(fix_order.time_in_force, TimeInForce::GoodTilCancelled);
    }

    #[test]
    fn test_serialization_roundtrip() {
        let order = NewOrderRequest::limit_buy("BTC-PERPETUAL".to_string(), 1.0, 50000.0)
            .with_post_only(true)
            .with_label("test_order".to_string());

        let json = serde_json::to_string(&order).unwrap();
        let deserialized: NewOrderRequest = serde_json::from_str(&json).unwrap();

        assert_eq!(order.instrument_name, deserialized.instrument_name);
        assert_eq!(order.amount, deserialized.amount);
        assert_eq!(order.order_type, deserialized.order_type);
        assert_eq!(order.side, deserialized.side);
        assert_eq!(order.price, deserialized.price);
        assert_eq!(order.post_only, deserialized.post_only);
        assert_eq!(order.label, deserialized.label);
    }

    #[test]
    fn test_debug_and_display_implementations() {
        let order = NewOrderRequest::market_buy("BTC-PERPETUAL".to_string(), 1.0);
        let debug_str = format!("{:?}", order);
        let display_str = format!("{}", order);

        assert!(debug_str.contains("BTC-PERPETUAL"));
        assert!(display_str.contains("BTC-PERPETUAL"));

        let auth_request =
            AuthRequest::client_credentials("client_id".to_string(), "client_secret".to_string());
        let auth_debug = format!("{:?}", auth_request);
        let auth_display = format!("{}", auth_request);

        assert!(auth_debug.contains("client_credentials"));
        assert!(auth_display.contains("client_credentials"));
    }

    #[test]
    fn test_enum_equality_and_cloning() {
        let trigger1 = TriggerType::IndexPrice;
        let trigger2 = trigger1.clone();
        assert_eq!(trigger1, trigger2);

        let advanced1 = AdvancedOrderType::Usd;
        let advanced2 = advanced1.clone();
        assert_eq!(advanced1, advanced2);
    }

    #[test]
    fn test_complex_order_with_all_fields() {
        let order = NewOrderRequest {
            instrument_name: "BTC-PERPETUAL".to_string(),
            amount: 1.5,
            order_type: OrderType::StopLimit,
            side: OrderSide::Buy,
            price: Some(50000.0),
            time_in_force: TimeInForce::GoodTilDay,
            post_only: Some(true),
            reduce_only: Some(false),
            label: Some("complex_order".to_string()),
            stop_price: Some(49000.0),
            trigger: Some(TriggerType::MarkPrice),
            advanced: Some(AdvancedOrderType::Implv),
            max_show: Some(0.5),
            reject_post_only: Some(false),
            valid_until: Some(1640995200000),
            client_order_id: Some("CLIENT_ORDER_COMPLEX".to_string()),
        };

        assert_eq!(order.instrument_name, "BTC-PERPETUAL");
        assert_eq!(order.amount, 1.5);
        assert_eq!(order.order_type, OrderType::StopLimit);
        assert_eq!(order.stop_price, Some(49000.0));
        assert_eq!(order.trigger, Some(TriggerType::MarkPrice));
        assert_eq!(order.advanced, Some(AdvancedOrderType::Implv));
        assert_eq!(order.max_show, Some(0.5));
        assert_eq!(order.valid_until, Some(1640995200000));
    }
}