ccxt-exchanges 0.1.5

Exchange implementations for CCXT Rust
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
//! Binance spot trading operations.
//!
//! This module contains all spot trading methods including order creation,
//! cancellation, and order management.

use super::super::{
    Binance, BinanceEndpointRouter, constants::endpoints, parser, signed_request::HttpMethod,
};
use ccxt_core::{
    Error, ParseError, Result,
    types::{Amount, EndpointType, Order, OrderRequest, OrderSide, OrderType, Price, TimeInForce},
};
use rust_decimal::Decimal;
use std::collections::{BTreeMap, HashMap};
use tracing::warn;

impl Binance {
    /// Create a new order using the builder pattern.
    ///
    /// This is the preferred method for creating orders. It accepts an [`OrderRequest`]
    /// built using the builder pattern, which provides compile-time validation of
    /// required fields and a more ergonomic API.
    ///
    /// # Arguments
    ///
    /// * `request` - Order request built via [`OrderRequest::builder()`]
    ///
    /// # Returns
    ///
    /// Returns the created [`Order`] structure with order details.
    ///
    /// # Errors
    ///
    /// Returns an error if authentication fails, market is not found, or the API request fails.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use ccxt_exchanges::binance::Binance;
    /// use ccxt_core::{ExchangeConfig, types::{OrderRequest, OrderSide, OrderType, Amount, Price}};
    /// use rust_decimal_macros::dec;
    ///
    /// # async fn example() -> ccxt_core::Result<()> {
    /// let binance = Binance::new(ExchangeConfig::default())?;
    ///
    /// // Create a market order using the builder
    /// let request = OrderRequest::builder()
    ///     .symbol("BTC/USDT")
    ///     .side(OrderSide::Buy)
    ///     .order_type(OrderType::Market)
    ///     .amount(Amount::new(dec!(0.001)))
    ///     .build();
    ///
    /// let order = binance.create_order_v2(request).await?;
    /// println!("Order created: {:?}", order);
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// _Requirements: 2.2, 2.6_
    pub async fn create_order_v2(&self, request: OrderRequest) -> Result<Order> {
        let market = self.base().market(&request.symbol).await?;
        let mut request_params = BTreeMap::new();

        request_params.insert("symbol".to_string(), market.id.clone());
        request_params.insert(
            "side".to_string(),
            match request.side {
                OrderSide::Buy => "BUY".to_string(),
                OrderSide::Sell => "SELL".to_string(),
            },
        );
        request_params.insert(
            "type".to_string(),
            match request.order_type {
                OrderType::Market => "MARKET".to_string(),
                OrderType::Limit => "LIMIT".to_string(),
                OrderType::StopLoss => "STOP_LOSS".to_string(),
                OrderType::StopLossLimit => "STOP_LOSS_LIMIT".to_string(),
                OrderType::TakeProfit => "TAKE_PROFIT".to_string(),
                OrderType::TakeProfitLimit => "TAKE_PROFIT_LIMIT".to_string(),
                OrderType::LimitMaker => "LIMIT_MAKER".to_string(),
                OrderType::StopMarket => "STOP_MARKET".to_string(),
                OrderType::StopLimit => "STOP_LIMIT".to_string(),
                OrderType::TrailingStop => "TRAILING_STOP_MARKET".to_string(),
            },
        );
        request_params.insert("quantity".to_string(), request.amount.to_string());

        // Handle price
        if let Some(p) = request.price {
            request_params.insert("price".to_string(), p.to_string());
        }

        // Handle stop price
        if let Some(sp) = request.stop_price {
            request_params.insert("stopPrice".to_string(), sp.to_string());
        }

        // Handle time in force
        if let Some(tif) = request.time_in_force {
            let tif_str = match tif {
                TimeInForce::GTC => "GTC",
                TimeInForce::IOC => "IOC",
                TimeInForce::FOK => "FOK",
                TimeInForce::PO => "GTX", // Post-only maps to GTX on Binance
            };
            request_params.insert("timeInForce".to_string(), tif_str.to_string());
        } else if request.order_type == OrderType::Limit
            || request.order_type == OrderType::StopLossLimit
            || request.order_type == OrderType::TakeProfitLimit
        {
            // Limit orders require timeInForce parameter
            request_params.insert("timeInForce".to_string(), "GTC".to_string());
        }

        // Handle client order ID
        if let Some(client_id) = request.client_order_id {
            request_params.insert("newClientOrderId".to_string(), client_id);
        }

        // Handle reduce only (futures)
        if let Some(reduce_only) = request.reduce_only {
            request_params.insert("reduceOnly".to_string(), reduce_only.to_string());
        }

        // Handle post only
        if request.post_only == Some(true) {
            // For spot, post-only is handled via timeInForce=GTX
            if !request_params.contains_key("timeInForce") {
                request_params.insert("timeInForce".to_string(), "GTX".to_string());
            }
        }

        // Handle trigger price (for conditional orders)
        if let Some(trigger) = request.trigger_price {
            if !request_params.contains_key("stopPrice") {
                request_params.insert("stopPrice".to_string(), trigger.to_string());
            }
        }

        // Handle take profit price (only for non-spot markets; spot doesn't support takeProfitPrice)
        if let Some(tp) = request.take_profit_price {
            if !market.is_spot() {
                request_params.insert("takeProfitPrice".to_string(), tp.to_string());
            }
            if !request_params.contains_key("stopPrice") {
                request_params.insert("stopPrice".to_string(), tp.to_string());
            }
        }

        // Handle stop loss price (only for non-spot markets; spot doesn't support stopLossPrice)
        if let Some(sl) = request.stop_loss_price {
            if !market.is_spot() {
                request_params.insert("stopLossPrice".to_string(), sl.to_string());
            }
            if !request_params.contains_key("stopPrice") {
                request_params.insert("stopPrice".to_string(), sl.to_string());
            }
        }

        // Handle trailing delta
        if let Some(delta) = request.trailing_delta {
            request_params.insert("trailingDelta".to_string(), delta.to_string());
        }

        // Handle trailing percent (convert to basis points for spot)
        if let Some(percent) = request.trailing_percent {
            if market.is_spot() {
                // Convert percentage to basis points (e.g., 2.0% -> 200)
                use rust_decimal::prelude::ToPrimitive;
                let basis_points = (percent * Decimal::from(100)).round();
                let delta_int = basis_points.to_i64().ok_or_else(|| {
                    ccxt_core::Error::invalid_request(format!(
                        "Cannot convert trailing percent {} to basis points integer",
                        percent
                    ))
                })?;
                request_params.insert("trailingDelta".to_string(), delta_int.to_string());
            } else {
                request_params.insert("trailingPercent".to_string(), percent.to_string());
            }
        }

        // Handle activation price (for trailing stop orders)
        if let Some(activation) = request.activation_price {
            request_params.insert("activationPrice".to_string(), activation.to_string());
        }

        // Handle callback rate (for futures trailing stop orders)
        if let Some(rate) = request.callback_rate {
            request_params.insert("callbackRate".to_string(), rate.to_string());
        }

        // Handle working type (for futures)
        if let Some(working_type) = request.working_type {
            request_params.insert("workingType".to_string(), working_type);
        }

        // Handle position side (for hedge mode futures)
        if let Some(position_side) = request.position_side {
            request_params.insert("positionSide".to_string(), position_side);
        }

        // Use market-type-aware endpoint routing for correct API base URL
        let base_url = self.rest_endpoint(&market, EndpointType::Private);
        let url = format!("{}{}", base_url, endpoints::ORDER);
        let data = self
            .signed_request(url)
            .method(HttpMethod::Post)
            .params(request_params)
            .execute()
            .await?;

        parser::parse_order(&data, Some(&market))
    }

    /// Create a new order (deprecated).
    ///
    /// # Deprecated
    ///
    /// This method is deprecated. Use [`create_order_v2`](Self::create_order_v2) with
    /// [`OrderRequest::builder()`] instead for a more ergonomic API.
    ///
    /// # Arguments
    ///
    /// * `symbol` - Trading pair symbol.
    /// * `order_type` - Order type (Market, Limit, StopLoss, etc.).
    /// * `side` - Order side (Buy or Sell).
    /// * `amount` - Order quantity as [`Amount`] type.
    /// * `price` - Optional price as [`Price`] type (required for limit orders).
    /// * `params` - Additional parameters.
    ///
    /// # Returns
    ///
    /// Returns the created [`Order`] structure with order details.
    ///
    /// # Errors
    ///
    /// Returns an error if authentication fails, market is not found, or the API request fails.
    #[deprecated(
        since = "0.2.0",
        note = "Use create_order_v2 with OrderRequest::builder() instead"
    )]
    pub async fn create_order(
        &self,
        symbol: &str,
        order_type: OrderType,
        side: OrderSide,
        amount: Amount,
        price: Option<Price>,
        params: Option<HashMap<String, String>>,
    ) -> Result<Order> {
        let market = self.base().market(symbol).await?;
        let mut request_params = BTreeMap::new();

        request_params.insert("symbol".to_string(), market.id.clone());
        request_params.insert(
            "side".to_string(),
            match side {
                OrderSide::Buy => "BUY".to_string(),
                OrderSide::Sell => "SELL".to_string(),
            },
        );
        request_params.insert(
            "type".to_string(),
            match order_type {
                OrderType::Market => "MARKET".to_string(),
                OrderType::Limit => "LIMIT".to_string(),
                OrderType::StopLoss => "STOP_LOSS".to_string(),
                OrderType::StopLossLimit => "STOP_LOSS_LIMIT".to_string(),
                OrderType::TakeProfit => "TAKE_PROFIT".to_string(),
                OrderType::TakeProfitLimit => "TAKE_PROFIT_LIMIT".to_string(),
                OrderType::LimitMaker => "LIMIT_MAKER".to_string(),
                OrderType::StopMarket => "STOP_MARKET".to_string(),
                OrderType::StopLimit => "STOP_LIMIT".to_string(),
                OrderType::TrailingStop => "TRAILING_STOP_MARKET".to_string(),
            },
        );
        request_params.insert("quantity".to_string(), amount.to_string());

        if let Some(p) = price {
            request_params.insert("price".to_string(), p.to_string());
        }

        // Limit orders require timeInForce parameter
        if order_type == OrderType::Limit
            || order_type == OrderType::StopLossLimit
            || order_type == OrderType::TakeProfitLimit
        {
            if !request_params.contains_key("timeInForce") {
                request_params.insert("timeInForce".to_string(), "GTC".to_string());
            }
        }

        if let Some(extra) = params {
            for (k, v) in extra {
                request_params.insert(k, v);
            }
        }

        // Handle cost parameter for market buy orders (quoteOrderQty)
        if order_type == OrderType::Market && side == OrderSide::Buy {
            if let Some(cost_str) = request_params.get("cost") {
                request_params.insert("quoteOrderQty".to_string(), cost_str.clone());
                request_params.remove("quantity");
                request_params.remove("cost");
            }
        }

        // Handle conditional order parameters
        if matches!(
            order_type,
            OrderType::StopLoss
                | OrderType::StopLossLimit
                | OrderType::TakeProfit
                | OrderType::TakeProfitLimit
                | OrderType::StopMarket
        ) {
            if !request_params.contains_key("stopPrice") {
                if let Some(stop_loss) = request_params.get("stopLossPrice") {
                    request_params.insert("stopPrice".to_string(), stop_loss.clone());
                } else if let Some(take_profit) = request_params.get("takeProfitPrice") {
                    request_params.insert("stopPrice".to_string(), take_profit.clone());
                }
            }
        }

        // Trailing stop handling for spot market
        if order_type == OrderType::TrailingStop {
            if market.is_spot() {
                if !request_params.contains_key("trailingDelta") {
                    if let Some(percent_str) = request_params.get("trailingPercent") {
                        if let Ok(percent) = percent_str.parse::<Decimal>() {
                            // Convert percentage to basis points (e.g., 2.0% -> 200)
                            let delta = (percent * Decimal::from(100)).to_string();
                            // Parse as integer for the API
                            if let Ok(delta_int) = delta.parse::<i64>() {
                                request_params
                                    .insert("trailingDelta".to_string(), delta_int.to_string());
                                request_params.remove("trailingPercent");
                            }
                        }
                    }
                }
            }
        }

        // Use market-type-aware endpoint routing for correct API base URL
        let base_url = self.rest_endpoint(&market, EndpointType::Private);
        let url = format!("{}{}", base_url, endpoints::ORDER);
        let data = self
            .signed_request(url)
            .method(HttpMethod::Post)
            .params(request_params)
            .execute()
            .await?;

        parser::parse_order(&data, Some(&market))
    }

    /// Cancel an order.
    ///
    /// # Arguments
    ///
    /// * `id` - Order ID.
    /// * `symbol` - Trading pair symbol.
    ///
    /// # Returns
    ///
    /// Returns the cancelled [`Order`] information.
    ///
    /// # Errors
    ///
    /// Returns an error if authentication fails, market is not found, or the API request fails.
    pub async fn cancel_order(&self, id: &str, symbol: &str) -> Result<Order> {
        let market = self.base().market(symbol).await?;
        // Use market-type-aware endpoint routing
        let base_url = self.rest_endpoint(&market, EndpointType::Private);
        let url = format!("{}{}", base_url, endpoints::ORDER);

        let data = self
            .signed_request(url)
            .method(HttpMethod::Delete)
            .param("symbol", &market.id)
            .param("orderId", id)
            .execute()
            .await?;

        parser::parse_order(&data, Some(&market))
    }

    /// Fetch order details.
    ///
    /// # Arguments
    ///
    /// * `id` - Order ID.
    /// * `symbol` - Trading pair symbol.
    ///
    /// # Returns
    ///
    /// Returns the [`Order`] information.
    ///
    /// # Errors
    ///
    /// Returns an error if authentication fails, market is not found, or the API request fails.
    pub async fn fetch_order(&self, id: &str, symbol: &str) -> Result<Order> {
        let market = self.base().market(symbol).await?;
        // Use market-type-aware endpoint routing
        let base_url = self.rest_endpoint(&market, EndpointType::Private);
        let url = format!("{}{}", base_url, endpoints::ORDER);

        let data = self
            .signed_request(url)
            .param("symbol", &market.id)
            .param("orderId", id)
            .execute()
            .await?;

        parser::parse_order(&data, Some(&market))
    }

    /// Fetch open (unfilled) orders.
    ///
    /// # Arguments
    ///
    /// * `symbol` - Optional trading pair symbol. If `None`, fetches all open orders.
    ///
    /// # Returns
    ///
    /// Returns a vector of open [`Order`] structures.
    ///
    /// # Errors
    ///
    /// Returns an error if authentication fails or the API request fails.
    pub async fn fetch_open_orders(&self, symbol: Option<&str>) -> Result<Vec<Order>> {
        let market = if let Some(sym) = symbol {
            Some(self.base().market(sym).await?)
        } else {
            None
        };

        // Use market-type-aware endpoint routing if market is available,
        // otherwise fall back to spot endpoint
        let base_url = match &market {
            Some(m) => self.rest_endpoint(m, EndpointType::Private),
            None => self.urls().private.clone(),
        };
        let url = format!("{}{}", base_url, endpoints::OPEN_ORDERS);

        let data = self
            .signed_request(url)
            .optional_param("symbol", market.as_ref().map(|m| &m.id))
            .execute()
            .await?;

        let orders_array = data.as_array().ok_or_else(|| {
            Error::from(ParseError::invalid_format(
                "data",
                "Expected array of orders",
            ))
        })?;

        let mut orders = Vec::new();
        for order_data in orders_array {
            match parser::parse_order(order_data, market.as_deref()) {
                Ok(order) => orders.push(order),
                Err(e) => {
                    warn!(error = %e, "Failed to parse order");
                }
            }
        }

        Ok(orders)
    }

    /// Fetch closed (completed) orders.
    ///
    /// # Arguments
    ///
    /// * `symbol` - Optional trading pair symbol.
    /// * `since` - Optional start timestamp (milliseconds).
    /// * `limit` - Optional limit on number of orders (default 500, max 1000).
    ///
    /// # Returns
    ///
    /// Returns a vector of closed [`Order`] structures.
    ///
    /// # Errors
    ///
    /// Returns an error if authentication fails or the API request fails.
    pub async fn fetch_closed_orders(
        &self,
        symbol: Option<&str>,
        since: Option<i64>,
        limit: Option<u32>,
    ) -> Result<Vec<Order>> {
        let all_orders = self.fetch_orders(symbol, since, None).await?;

        let mut closed_orders: Vec<Order> = all_orders
            .into_iter()
            .filter(|order| order.status == ccxt_core::types::OrderStatus::Closed)
            .collect();

        if let Some(l) = limit {
            closed_orders.truncate(l as usize);
        }

        Ok(closed_orders)
    }

    /// Cancel all open orders.
    ///
    /// # Arguments
    ///
    /// * `symbol` - Trading pair symbol.
    ///
    /// # Returns
    ///
    /// Returns a vector of cancelled [`Order`] structures.
    ///
    /// # Errors
    ///
    /// Returns an error if authentication fails, market is not found, or the API request fails.
    pub async fn cancel_all_orders(&self, symbol: &str) -> Result<Vec<Order>> {
        let market = self.base().market(symbol).await?;
        // Use market-type-aware endpoint routing
        let base_url = self.rest_endpoint(&market, EndpointType::Private);
        let url = format!("{}{}", base_url, endpoints::OPEN_ORDERS);

        let data = self
            .signed_request(url)
            .method(HttpMethod::Delete)
            .param("symbol", &market.id)
            .execute()
            .await?;

        let orders_array = data.as_array().ok_or_else(|| {
            Error::from(ParseError::invalid_format(
                "data",
                "Expected array of orders",
            ))
        })?;

        let mut orders = Vec::new();
        for order_data in orders_array {
            match parser::parse_order(order_data, Some(&market)) {
                Ok(order) => orders.push(order),
                Err(e) => {
                    warn!(error = %e, "Failed to parse order");
                }
            }
        }

        Ok(orders)
    }

    /// Cancel multiple orders.
    ///
    /// # Arguments
    ///
    /// * `ids` - Vector of order IDs to cancel.
    /// * `symbol` - Trading pair symbol.
    ///
    /// # Returns
    ///
    /// Returns a vector of cancelled [`Order`] structures.
    ///
    /// # Errors
    ///
    /// Returns an error if authentication fails, market is not found, or the API request fails.
    pub async fn cancel_orders(&self, ids: Vec<String>, symbol: &str) -> Result<Vec<Order>> {
        let market = self.base().market(symbol).await?;

        let order_ids_json = serde_json::to_string(&ids).map_err(|e| {
            Error::from(ParseError::invalid_format(
                "data",
                format!("Failed to serialize order IDs: {}", e),
            ))
        })?;

        // Use market-type-aware endpoint routing
        let base_url = self.rest_endpoint(&market, EndpointType::Private);
        let url = format!("{}{}", base_url, endpoints::OPEN_ORDERS);

        let data = self
            .signed_request(url)
            .method(HttpMethod::Delete)
            .param("symbol", &market.id)
            .param("orderIdList", order_ids_json)
            .execute()
            .await?;

        let orders_array = data.as_array().ok_or_else(|| {
            Error::from(ParseError::invalid_format(
                "data",
                "Expected array of orders",
            ))
        })?;

        let mut orders = Vec::new();
        for order_data in orders_array {
            match parser::parse_order(order_data, Some(&market)) {
                Ok(order) => orders.push(order),
                Err(e) => {
                    warn!(error = %e, "Failed to parse order");
                }
            }
        }

        Ok(orders)
    }

    /// Fetch all orders (historical and current).
    ///
    /// # Arguments
    ///
    /// * `symbol` - Optional trading pair symbol.
    /// * `since` - Optional start timestamp (milliseconds).
    /// * `limit` - Optional limit on number of orders (default 500, max 1000).
    ///
    /// # Returns
    ///
    /// Returns a vector of [`Order`] structures.
    ///
    /// # Errors
    ///
    /// Returns an error if authentication fails or the API request fails.
    pub async fn fetch_orders(
        &self,
        symbol: Option<&str>,
        since: Option<i64>,
        limit: Option<u32>,
    ) -> Result<Vec<Order>> {
        let market = if let Some(sym) = symbol {
            Some(self.base().market(sym).await?)
        } else {
            None
        };

        // Use market-type-aware endpoint routing if market is available,
        // otherwise fall back to spot endpoint
        let base_url = match &market {
            Some(m) => self.rest_endpoint(m, EndpointType::Private),
            None => self.urls().private.clone(),
        };
        let url = format!("{}{}", base_url, endpoints::ALL_ORDERS);

        let data = self
            .signed_request(url)
            .optional_param("symbol", market.as_ref().map(|m| &m.id))
            .optional_param("startTime", since)
            .optional_param("limit", limit)
            .execute()
            .await?;

        let orders_array = data.as_array().ok_or_else(|| {
            Error::from(ParseError::invalid_format(
                "data",
                "Expected array of orders",
            ))
        })?;

        let mut orders = Vec::new();
        for order_data in orders_array {
            match parser::parse_order(order_data, market.as_deref()) {
                Ok(order) => orders.push(order),
                Err(e) => {
                    warn!(error = %e, "Failed to parse order");
                }
            }
        }

        Ok(orders)
    }

    /// Create a stop-loss order.
    ///
    /// # Arguments
    ///
    /// * `symbol` - Trading pair symbol.
    /// * `side` - Order side (Buy/Sell).
    /// * `amount` - Order quantity as [`Amount`] type.
    /// * `stop_price` - Stop-loss trigger price as [`Price`] type.
    /// * `price` - Optional limit price as [`Price`] type (if `None`, creates market stop-loss order).
    /// * `params` - Optional additional parameters.
    ///
    /// # Returns
    ///
    /// Returns the created stop-loss [`Order`].
    ///
    /// # Errors
    ///
    /// Returns an error if authentication fails or the API request fails.
    #[allow(deprecated)]
    pub async fn create_stop_loss_order(
        &self,
        symbol: &str,
        side: OrderSide,
        amount: Amount,
        stop_price: Price,
        price: Option<Price>,
        params: Option<HashMap<String, String>>,
    ) -> Result<Order> {
        let mut request_params = params.unwrap_or_default();

        request_params.insert("stopPrice".to_string(), stop_price.to_string());

        let order_type = if price.is_some() {
            OrderType::StopLossLimit
        } else {
            OrderType::StopLoss
        };

        self.create_order(
            symbol,
            order_type,
            side,
            amount,
            price,
            Some(request_params),
        )
        .await
    }

    /// Create a take-profit order.
    ///
    /// # Arguments
    ///
    /// * `symbol` - Trading pair symbol.
    /// * `side` - Order side (Buy/Sell).
    /// * `amount` - Order quantity as [`Amount`] type.
    /// * `take_profit_price` - Take-profit trigger price as [`Price`] type.
    /// * `price` - Optional limit price as [`Price`] type (if `None`, creates market take-profit order).
    /// * `params` - Optional additional parameters.
    ///
    /// # Returns
    ///
    /// Returns the created take-profit [`Order`].
    ///
    /// # Errors
    ///
    /// Returns an error if authentication fails or the API request fails.
    #[allow(deprecated)]
    pub async fn create_take_profit_order(
        &self,
        symbol: &str,
        side: OrderSide,
        amount: Amount,
        take_profit_price: Price,
        price: Option<Price>,
        params: Option<HashMap<String, String>>,
    ) -> Result<Order> {
        let mut request_params = params.unwrap_or_default();

        request_params.insert("stopPrice".to_string(), take_profit_price.to_string());

        let order_type = if price.is_some() {
            OrderType::TakeProfitLimit
        } else {
            OrderType::TakeProfit
        };

        self.create_order(
            symbol,
            order_type,
            side,
            amount,
            price,
            Some(request_params),
        )
        .await
    }

    /// Create a trailing stop order.
    ///
    /// # Arguments
    ///
    /// * `symbol` - Trading pair symbol.
    /// * `side` - Order side (Buy/Sell).
    /// * `amount` - Order quantity as [`Amount`] type.
    /// * `trailing_percent` - Trailing percentage as [`Decimal`] (e.g., 2.0 for 2%).
    /// * `activation_price` - Optional activation price as [`Price`] type (not supported for spot markets).
    /// * `params` - Optional additional parameters.
    ///
    /// # Returns
    ///
    /// Returns the created trailing stop [`Order`].
    ///
    /// # Errors
    ///
    /// Returns an error if authentication fails or the API request fails.
    #[allow(deprecated)]
    pub async fn create_trailing_stop_order(
        &self,
        symbol: &str,
        side: OrderSide,
        amount: Amount,
        trailing_percent: Decimal,
        activation_price: Option<Price>,
        params: Option<HashMap<String, String>>,
    ) -> Result<Order> {
        let mut request_params = params.unwrap_or_default();

        request_params.insert("trailingPercent".to_string(), trailing_percent.to_string());

        if let Some(activation) = activation_price {
            request_params.insert("activationPrice".to_string(), activation.to_string());
        }

        self.create_order(
            symbol,
            OrderType::TrailingStop,
            side,
            amount,
            None,
            Some(request_params),
        )
        .await
    }
}