digdigdig3 0.1.9

Multi-exchange connector library — unified async Rust API for 42 connectors: 19 CEX, 3 DEX, 5 forex/brokers, 14 stock providers, and 2 data feeds
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
//! # Dhan Connector
//!
//! Implementation of all core traits for Dhan.
//!
//! ## Core Traits
//! - `ExchangeIdentity` - exchange identification
//! - `MarketData` - market data
//! - `Trading` - trading operations
//! - `Account` - account information
//! - `Positions` - position management (for F&O)
//!
//! ## Extended Methods
//! Additional Dhan-specific methods as struct methods.

use std::collections::HashMap;
use std::sync::{Arc, Mutex as StdMutex};
use std::time::Duration;
use tokio::sync::Mutex;

use async_trait::async_trait;
use serde_json::{json, Value};
use reqwest;

use crate::core::{
    HttpClient, Credentials,
    ExchangeId, AccountType, Symbol,
    ExchangeError, ExchangeResult,
    Price, Kline, Ticker, OrderBook,
    Order, OrderSide, OrderType, OrderStatus, Balance, AccountInfo,
    Position, FundingRate,
    OrderRequest, CancelRequest, CancelScope,
    AmendRequest,
    BalanceQuery, PositionQuery, PositionModification,
    OrderHistoryFilter, PlaceOrderResponse, FeeInfo,
};
use crate::core::traits::{
    ExchangeIdentity, MarketData, Trading, Account, Positions, AmendOrder,
};
use crate::core::types::SymbolInfo;
use crate::core::utils::SimpleRateLimiter;

use super::endpoints::{DhanUrls, DhanEndpoint, DhanExchangeSegment, map_interval, map_product_type};
use super::auth::DhanAuth;
use super::parser::DhanParser;

// ═══════════════════════════════════════════════════════════════════════════════
// CONNECTOR
// ═══════════════════════════════════════════════════════════════════════════════

/// Dhan connector
pub struct DhanConnector {
    /// HTTP client
    http: HttpClient,
    /// Authentication
    auth: Arc<Mutex<DhanAuth>>,
    /// URLs (mainnet/testnet)
    urls: DhanUrls,
    /// Testnet mode
    testnet: bool,
    /// Rate limiter for order APIs (25/sec)
    order_limiter: Arc<StdMutex<SimpleRateLimiter>>,
    /// Rate limiter for data APIs (5/sec)
    data_limiter: Arc<StdMutex<SimpleRateLimiter>>,
    /// Rate limiter for quote APIs (1/sec)
    quote_limiter: Arc<StdMutex<SimpleRateLimiter>>,
    /// Rate limiter for non-trading APIs (20/sec)
    general_limiter: Arc<StdMutex<SimpleRateLimiter>>,
}

impl DhanConnector {
    /// Create new connector
    pub async fn new(credentials: Credentials, testnet: bool) -> ExchangeResult<Self> {
        let urls = if testnet {
            DhanUrls::TESTNET
        } else {
            DhanUrls::MAINNET
        };

        let http = HttpClient::new(30_000)?; // 30 sec timeout
        let auth = Arc::new(Mutex::new(DhanAuth::new(&credentials)?));

        // Initialize rate limiters per Dhan's documented limits
        let order_limiter = Arc::new(StdMutex::new(
            SimpleRateLimiter::new(25, Duration::from_secs(1)) // 25 orders/sec
        ));
        let data_limiter = Arc::new(StdMutex::new(
            SimpleRateLimiter::new(5, Duration::from_secs(1)) // 5 data requests/sec
        ));
        let quote_limiter = Arc::new(StdMutex::new(
            SimpleRateLimiter::new(1, Duration::from_secs(1)) // 1 quote request/sec
        ));
        let general_limiter = Arc::new(StdMutex::new(
            SimpleRateLimiter::new(20, Duration::from_secs(1)) // 20 general requests/sec
        ));

        Ok(Self {
            http,
            auth,
            urls,
            testnet,
            order_limiter,
            data_limiter,
            quote_limiter,
            general_limiter,
        })
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // HTTP HELPERS
    // ═══════════════════════════════════════════════════════════════════════════

    /// Wait for rate limit based on endpoint type
    async fn rate_limit_wait(&self, endpoint: DhanEndpoint) {
        let limiter = match endpoint {
            // Order APIs
            DhanEndpoint::PlaceOrder
            | DhanEndpoint::ModifyOrder
            | DhanEndpoint::CancelOrder
            | DhanEndpoint::PlaceSlicedOrder
            | DhanEndpoint::PlaceSuperOrder
            | DhanEndpoint::ModifySuperOrder
            | DhanEndpoint::CancelSuperOrder
            | DhanEndpoint::PlaceForeverOrder
            | DhanEndpoint::ModifyForeverOrder
            | DhanEndpoint::CancelForeverOrder => &self.order_limiter,

            // Data APIs
            DhanEndpoint::HistoricalDaily | DhanEndpoint::HistoricalIntraday => &self.data_limiter,

            // Quote APIs
            DhanEndpoint::LTP | DhanEndpoint::OHLC | DhanEndpoint::Quote | DhanEndpoint::OptionChain => &self.quote_limiter,

            // Everything else
            _ => &self.general_limiter,
        };

        loop {
            let wait_time = {
                let mut rate_limiter = limiter.lock().expect("Mutex poisoned");
                if rate_limiter.try_acquire() {
                    return;
                }
                rate_limiter.time_until_ready()
            };

            if wait_time > Duration::ZERO {
                tokio::time::sleep(wait_time).await;
            }
        }
    }

    /// GET request
    async fn get(&self, endpoint: DhanEndpoint, params: HashMap<String, String>) -> ExchangeResult<Value> {
        self.rate_limit_wait(endpoint).await;

        let base_url = self.urls.rest_url();
        let path = endpoint.path();

        // Build query string
        let query = if params.is_empty() {
            String::new()
        } else {
            let qs: Vec<String> = params.iter()
                .map(|(k, v)| format!("{}={}", k, v))
                .collect();
            format!("?{}", qs.join("&"))
        };

        let url = format!("{}{}{}", base_url, path, query);

        // Add auth headers if needed
        let headers = if endpoint.requires_auth() {
            let base_url_owned = base_url.to_string();
            let mut auth = self.auth.lock().await;
            auth.build_headers(&base_url_owned, &self.http).await?
        } else {
            let auth = self.auth.lock().await;
            auth.build_public_headers()
        };

        let response = self.http.get_with_headers(&url, &HashMap::new(), &headers).await?;
        Ok(response)
    }

    /// POST request
    async fn post(&self, endpoint: DhanEndpoint, body: Value) -> ExchangeResult<Value> {
        self.rate_limit_wait(endpoint).await;

        let base_url = self.urls.rest_url();
        let path = endpoint.path();
        let url = format!("{}{}", base_url, path);

        let headers = if endpoint.requires_auth() {
            let base_url_owned = base_url.to_string();
            let mut auth = self.auth.lock().await;
            auth.build_headers(&base_url_owned, &self.http).await?
        } else {
            let auth = self.auth.lock().await;
            auth.build_public_headers()
        };

        let response = self.http.post(&url, &body, &headers).await?;
        Ok(response)
    }

    /// PUT request
    async fn put(&self, endpoint: DhanEndpoint, path_params: &[(&str, &str)], body: Value) -> ExchangeResult<Value> {
        self.rate_limit_wait(endpoint).await;

        let base_url = self.urls.rest_url();
        let mut path = endpoint.path().to_string();

        // Replace path parameters
        for (key, value) in path_params {
            path = path.replace(&format!("{{{}}}", key), value);
        }

        let url = format!("{}{}", base_url, path);

        let base_url_owned = base_url.to_string();
        let mut auth = self.auth.lock().await;
        let headers = auth.build_headers(&base_url_owned, &self.http).await?;
        drop(auth); // Explicitly drop to release lock

        let response = self.http.put(&url, &body, &headers).await?;
        Ok(response)
    }

    /// DELETE request
    async fn delete(&self, endpoint: DhanEndpoint, path_params: &[(&str, &str)]) -> ExchangeResult<Value> {
        self.rate_limit_wait(endpoint).await;

        let base_url = self.urls.rest_url();
        let mut path = endpoint.path().to_string();

        // Replace path parameters
        for (key, value) in path_params {
            path = path.replace(&format!("{{{}}}", key), value);
        }

        let url = format!("{}{}", base_url, path);

        let base_url_owned = base_url.to_string();
        let mut auth = self.auth.lock().await;
        let headers = auth.build_headers(&base_url_owned, &self.http).await?;
        drop(auth); // Explicitly drop to release lock

        let response = self.http.delete(&url, &HashMap::new(), &headers).await?;
        Ok(response)
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // HELPER METHODS
    // ═══════════════════════════════════════════════════════════════════════════

    /// Get security ID from symbol (assumes symbol is already the security ID)
    fn get_security_id(&self, symbol: &Symbol) -> String {
        // In production, you'd look this up from instrument CSV
        // For now, assume symbol contains the security ID
        symbol.base.clone()
    }

    /// Get exchange segment from account type
    fn get_exchange_segment(&self, _account_type: AccountType) -> DhanExchangeSegment {
        // Default to NSE Equity
        // In production, this should be configurable or derived from symbol
        DhanExchangeSegment::NseEq
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // EXTENDED METHODS — Options Chain
    // ═══════════════════════════════════════════════════════════════════════════

    /// Get options chain — `GET /v2/optionchain`
    ///
    /// `underlying_scrip_id` — security ID of the underlying instrument.
    /// `expiry_date` — option expiry date in `YYYY-MM-DD` format.
    pub async fn get_options_chain(
        &self,
        underlying_scrip_id: &str,
        expiry_date: &str,
    ) -> ExchangeResult<Value> {
        let mut params = HashMap::new();
        params.insert("UnderlyingScripId".to_string(), underlying_scrip_id.to_string());
        params.insert("ExpiryDate".to_string(), expiry_date.to_string());
        self.get(DhanEndpoint::OptionChain, params).await
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // EXTENDED METHODS — Kill Switch
    // ═══════════════════════════════════════════════════════════════════════════

    /// Activate or deactivate kill switch — `POST /v2/killswitch`
    ///
    /// `kill_switch_status` — `"ACTIVATE"` to halt all trading, `"DEACTIVATE"` to resume.
    pub async fn kill_switch(&self, kill_switch_status: &str) -> ExchangeResult<Value> {
        let body = json!({
            "killSwitchStatus": kill_switch_status,
        });
        self.post(DhanEndpoint::KillSwitch, body).await
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// TRAIT IMPLEMENTATIONS
// ═══════════════════════════════════════════════════════════════════════════════

#[async_trait]
impl ExchangeIdentity for DhanConnector {
    fn exchange_id(&self) -> ExchangeId {
        ExchangeId::Dhan
    }

    fn is_testnet(&self) -> bool {
        self.testnet
    }

    fn supported_account_types(&self) -> Vec<AccountType> {
        vec![AccountType::Spot]
    }
}

#[async_trait]
impl MarketData for DhanConnector {
    async fn get_price(&self, symbol: Symbol, _account_type: AccountType) -> ExchangeResult<Price> {
        let security_id = self.get_security_id(&symbol);
        let segment = self.get_exchange_segment(_account_type);

        let body = json!({
            segment.as_str(): [security_id.clone()]
        });

        let response = self.post(DhanEndpoint::LTP, body).await?;
        DhanParser::parse_ltp(&response, &security_id)
    }

    async fn get_orderbook(
        &self,
        symbol: Symbol,
        _depth: Option<u16>,
        _account_type: AccountType,
    ) -> ExchangeResult<OrderBook> {
        let security_id = self.get_security_id(&symbol);
        let segment = self.get_exchange_segment(_account_type);

        let body = json!({
            segment.as_str(): [security_id.clone()]
        });

        let response = self.post(DhanEndpoint::Quote, body).await?;
        DhanParser::parse_quote(&response, &security_id)
    }

    async fn get_klines(
        &self,
        symbol: Symbol,
        interval: &str,
        limit: Option<u16>,
        _account_type: AccountType,
        _end_time: Option<i64>,
    ) -> ExchangeResult<Vec<Kline>> {
        let security_id = self.get_security_id(&symbol);
        let segment = self.get_exchange_segment(_account_type);

        // Calculate date range (default to last 90 days for intraday)
        let to_date = chrono::Utc::now();
        let from_date = to_date - chrono::Duration::days(90);

        let body = json!({
            "securityId": security_id,
            "exchangeSegment": segment.as_str(),
            "instrument": "EQUITY",
            "interval": map_interval(interval),
            "fromDate": from_date.format("%Y-%m-%d").to_string(),
            "toDate": to_date.format("%Y-%m-%d").to_string(),
        });

        let response = self.post(DhanEndpoint::HistoricalIntraday, body).await?;
        let mut klines = DhanParser::parse_historical_intraday(&response)?;

        // Apply limit if specified
        if let Some(limit) = limit {
            klines.truncate(limit as usize);
        }

        Ok(klines)
    }

    async fn get_ticker(&self, symbol: Symbol, _account_type: AccountType) -> ExchangeResult<Ticker> {
        let security_id = self.get_security_id(&symbol);
        let segment = self.get_exchange_segment(_account_type);

        let body = json!({
            segment.as_str(): [security_id.clone()]
        });

        let response = self.post(DhanEndpoint::OHLC, body).await?;
        DhanParser::parse_ticker(&response, &security_id)
    }

    async fn ping(&self) -> ExchangeResult<()> {
        // Dhan doesn't have a dedicated ping endpoint
        // Use token generation as health check
        let base_url = self.urls.rest_url().to_string();
        let mut auth = self.auth.lock().await;
        let result = auth.get_token(&base_url, &self.http).await?;
        drop(auth);
        drop(result);
        Ok(())
    }

    /// Get exchange info — returns NSE equity instruments from Dhan
    async fn get_exchange_info(&self, account_type: AccountType) -> ExchangeResult<Vec<SymbolInfo>> {
        // Dhan's InstrumentList endpoint: /v2/instrument/{exchangeSegment}
        // Returns CSV with columns: SEM_EXM_EXCH_ID, SEM_SEGMENT, SEM_SMST_SECURITY_ID, SEM_INSTRUMENT_NAME, SEM_CUSTOM_SYMBOL, ...
        let base_url = self.urls.rest_url();
        let url = format!("{}/v2/instrument/NSE_EQ", base_url);

        let base_url_owned = base_url.to_string();
        let mut auth = self.auth.lock().await;
        let headers = auth.build_headers(&base_url_owned, &self.http).await?;
        drop(auth);

        // Use reqwest directly for text response with auth headers
        let client = reqwest::Client::new();
        let mut req = client.get(&url);
        for (k, v) in &headers {
            req = req.header(k.as_str(), v.as_str());
        }
        let response = req.send().await
            .map_err(|e| ExchangeError::Network(format!("Request failed: {}", e)))?;

        let csv_text = response.text().await
            .map_err(|e| ExchangeError::Network(format!("Failed to read text: {}", e)))?;

        let mut infos = Vec::new();
        for (i, line) in csv_text.lines().enumerate() {
            if i == 0 {
                continue; // skip header
            }
            let cols: Vec<&str> = line.split(',').collect();
            if cols.len() < 5 {
                continue;
            }

            let symbol = cols[4].trim().trim_matches('"').to_string();
            if symbol.is_empty() {
                continue;
            }

            infos.push(SymbolInfo {
                symbol: symbol.clone(),
                base_asset: symbol,
                quote_asset: "INR".to_string(),
                status: "TRADING".to_string(),
                price_precision: 2,
                quantity_precision: 0,
                min_quantity: Some(1.0),
                max_quantity: None,
                tick_size: None,
                step_size: Some(1.0),
                min_notional: None,
                account_type,
            });
        }

        Ok(infos)
    }
}

#[async_trait]
impl Trading for DhanConnector {
    async fn place_order(&self, req: OrderRequest) -> ExchangeResult<PlaceOrderResponse> {
        let symbol = req.symbol.clone();
        let side = req.side;
        let quantity = req.quantity;
        let account_type = req.account_type;

        let transaction_type = match side {
            OrderSide::Buy => "BUY",
            OrderSide::Sell => "SELL",
        };

        let security_id = self.get_security_id(&symbol);
        let segment = self.get_exchange_segment(account_type);
        let product_type = map_product_type(account_type);
        let client_id = {
            let auth = self.auth.lock().await;
            auth.client_id().to_string()
        };

        // Determine validity from time_in_force
        let validity = match req.time_in_force {
            crate::core::TimeInForce::Ioc => "IOC",
            _ => "DAY",
        };

        match req.order_type {
            OrderType::Market => {
                let body = json!({
                    "dhanClientId": client_id,
                    "transactionType": transaction_type,
                    "exchangeSegment": segment.as_str(),
                    "productType": product_type,
                    "orderType": "MARKET",
                    "validity": validity,
                    "securityId": security_id,
                    "quantity": quantity as i64,
                    "price": 0,
                    "disclosedQuantity": 0,
                    "afterMarketOrder": false,
                });
                let response = self.post(DhanEndpoint::PlaceOrder, body).await?;
                DhanParser::parse_order_placement(&response).map(PlaceOrderResponse::Simple)
            }

            OrderType::Limit { price } => {
                let body = json!({
                    "dhanClientId": client_id,
                    "transactionType": transaction_type,
                    "exchangeSegment": segment.as_str(),
                    "productType": product_type,
                    "orderType": "LIMIT",
                    "validity": validity,
                    "securityId": security_id,
                    "quantity": quantity as i64,
                    "price": price,
                    "disclosedQuantity": 0,
                    "afterMarketOrder": false,
                });
                let response = self.post(DhanEndpoint::PlaceOrder, body).await?;
                DhanParser::parse_order_placement(&response).map(PlaceOrderResponse::Simple)
            }

            OrderType::StopMarket { stop_price } => {
                // Dhan STOP_LOSS_MARKET: triggerPrice required, price = 0
                let body = json!({
                    "dhanClientId": client_id,
                    "transactionType": transaction_type,
                    "exchangeSegment": segment.as_str(),
                    "productType": product_type,
                    "orderType": "STOP_LOSS_MARKET",
                    "validity": validity,
                    "securityId": security_id,
                    "quantity": quantity as i64,
                    "price": 0,
                    "triggerPrice": stop_price,
                    "disclosedQuantity": 0,
                    "afterMarketOrder": false,
                });
                let response = self.post(DhanEndpoint::PlaceOrder, body).await?;
                DhanParser::parse_order_placement(&response).map(PlaceOrderResponse::Simple)
            }

            OrderType::StopLimit { stop_price, limit_price } => {
                // Dhan STOP_LOSS: both price and triggerPrice required
                let body = json!({
                    "dhanClientId": client_id,
                    "transactionType": transaction_type,
                    "exchangeSegment": segment.as_str(),
                    "productType": product_type,
                    "orderType": "STOP_LOSS",
                    "validity": validity,
                    "securityId": security_id,
                    "quantity": quantity as i64,
                    "price": limit_price,
                    "triggerPrice": stop_price,
                    "disclosedQuantity": 0,
                    "afterMarketOrder": false,
                });
                let response = self.post(DhanEndpoint::PlaceOrder, body).await?;
                DhanParser::parse_order_placement(&response).map(PlaceOrderResponse::Simple)
            }

            OrderType::Bracket { price, take_profit, stop_loss } => {
                // Dhan Super Order (Bracket) — POST /v2/orders/super
                // Native bracket: legName, price, targetPrice, stopLossPrice, trailingJump
                let entry_price = price.unwrap_or(0.0);
                let order_type_str = if entry_price > 0.0 { "LIMIT" } else { "MARKET" };

                let body = json!({
                    "dhanClientId": client_id,
                    "transactionType": transaction_type,
                    "exchangeSegment": segment.as_str(),
                    "productType": "BO",
                    "orderType": order_type_str,
                    "validity": "DAY",
                    "securityId": security_id,
                    "quantity": quantity as i64,
                    "price": entry_price,
                    "disclosedQuantity": 0,
                    "afterMarketOrder": false,
                    "boProfitValue": take_profit,
                    "boStopLossValue": stop_loss,
                });
                let response = self.post(DhanEndpoint::PlaceSuperOrder, body).await?;
                DhanParser::parse_order_placement(&response).map(PlaceOrderResponse::Simple)
            }

            OrderType::Ioc { price } => {
                // IOC: validity = IOC, LIMIT if price given, MARKET otherwise
                let (order_type_str, price_val) = match price {
                    Some(p) => ("LIMIT", p),
                    None => ("MARKET", 0.0),
                };
                let body = json!({
                    "dhanClientId": client_id,
                    "transactionType": transaction_type,
                    "exchangeSegment": segment.as_str(),
                    "productType": product_type,
                    "orderType": order_type_str,
                    "validity": "IOC",
                    "securityId": security_id,
                    "quantity": quantity as i64,
                    "price": price_val,
                    "disclosedQuantity": 0,
                    "afterMarketOrder": false,
                });
                let response = self.post(DhanEndpoint::PlaceOrder, body).await?;
                DhanParser::parse_order_placement(&response).map(PlaceOrderResponse::Simple)
            }

            _ => Err(ExchangeError::UnsupportedOperation(
                format!("{:?} order type not supported on Dhan", req.order_type)
            )),
        }
    }

    async fn get_order_history(
        &self,
        _filter: OrderHistoryFilter,
        _account_type: AccountType,
    ) -> ExchangeResult<Vec<Order>> {
        // Dhan /v2/orders returns all orders (open + closed)
        let response = self.get(DhanEndpoint::GetOrderBook, HashMap::new()).await?;
        let all_orders = DhanParser::parse_orders(&response)?;

        // Filter for closed/filled/cancelled orders (history)
        Ok(all_orders
            .into_iter()
            .filter(|o| matches!(
                o.status,
                OrderStatus::Filled | OrderStatus::Canceled | OrderStatus::Rejected | OrderStatus::Expired
            ))
            .collect())
    }
async fn cancel_order(&self, req: CancelRequest) -> ExchangeResult<Order> {
        match req.scope {
            CancelScope::Single { ref order_id } => {
                let _symbol = req.symbol.as_ref()
                    .ok_or_else(|| ExchangeError::InvalidRequest("Symbol required for cancel".into()))?
                    .clone();
                let _account_type = req.account_type;

            let response = self.delete(DhanEndpoint::CancelOrder, &[("orderId", order_id)]).await?;
            DhanParser::parse_order_placement(&response)
    
            }
            _ => Err(ExchangeError::UnsupportedOperation(
                format!("{:?} cancel scope not supported on {:?}", req.scope, self.exchange_id())
            )),
        }
    }

    async fn get_order(
        &self,
        _symbol: &str,
        order_id: &str,
        _account_type: AccountType,
    ) -> ExchangeResult<Order> {
        // Parse symbol string into Symbol struct
        let _symbol_parts: Vec<&str> = _symbol.split('/').collect();
        let _symbol = if _symbol_parts.len() == 2 {
            crate::core::Symbol::new(_symbol_parts[0], _symbol_parts[1])
        } else {
            crate::core::Symbol { base: _symbol.to_string(), quote: String::new(), raw: Some(_symbol.to_string()) }
        };

        let mut path = DhanEndpoint::GetOrder.path().to_string();
        path = path.replace("{orderId}", order_id);

        let base_url = self.urls.rest_url();
        let url = format!("{}{}", base_url, path);

        let base_url_owned = base_url.to_string();
        let mut auth = self.auth.lock().await;
        let headers = auth.build_headers(&base_url_owned, &self.http).await?;
        drop(auth); // Explicitly drop to release lock

        let response = self.http.get_with_headers(&url, &HashMap::new(), &headers).await?;
        DhanParser::parse_order(&response)
    
    }

    async fn get_open_orders(
        &self,
        _symbol: Option<&str>,
        _account_type: AccountType,
    ) -> ExchangeResult<Vec<Order>> {
        // Convert Option<&str> to Option<Symbol>
        let _symbol_str = _symbol;
        let _symbol: Option<crate::core::Symbol> = _symbol_str.map(|s| {
            let parts: Vec<&str> = s.split('/').collect();
            if parts.len() == 2 {
                crate::core::Symbol::new(parts[0], parts[1])
            } else {
                crate::core::Symbol { base: s.to_string(), quote: String::new(), raw: Some(s.to_string()) }
            }
        });

        let response = self.get(DhanEndpoint::GetOrderBook, HashMap::new()).await?;
        let all_orders = DhanParser::parse_orders(&response)?;

        // Filter for open orders only
        Ok(all_orders
            .into_iter()
            .filter(|o| matches!(o.status, OrderStatus::New | OrderStatus::Open | OrderStatus::PartiallyFilled))
            .collect())
    
    }
}

#[async_trait]
impl Account for DhanConnector {
    async fn get_balance(&self, query: BalanceQuery) -> ExchangeResult<Vec<Balance>> {
        let _asset = query.asset;
        let _account_type = query.account_type;
        // /v2/fundlimit returns available cash/margin balance
        let response = self.get(DhanEndpoint::GetFunds, HashMap::new()).await?;
        DhanParser::parse_balance(&response)
    }

    async fn get_account_info(&self, _account_type: AccountType) -> ExchangeResult<AccountInfo> {
        // Dhan has no profile endpoint; return info derived from fund limit
        let response = self.get(DhanEndpoint::GetFunds, HashMap::new()).await?;
        DhanParser::parse_funds(&response)
    }

    async fn get_fees(&self, _symbol: Option<&str>) -> ExchangeResult<FeeInfo> {
        // Dhan does not expose a fee schedule API
        Err(ExchangeError::UnsupportedOperation(
            "Fee schedule endpoint not available on Dhan".to_string()
        ))
    }
}

#[async_trait]
impl Positions for DhanConnector {
    async fn get_positions(&self, query: PositionQuery) -> ExchangeResult<Vec<Position>> {
        let _symbol = query.symbol.clone();
        let _account_type = query.account_type;

        let response = self.get(DhanEndpoint::GetPositions, HashMap::new()).await?;
        DhanParser::parse_positions(&response)
    
    }

    async fn get_funding_rate(
        &self,
        _symbol: &str,
        _account_type: AccountType,
    ) -> ExchangeResult<FundingRate> {
        // Parse symbol string into Symbol struct
        let _symbol_str = _symbol;
        let _symbol = {
            let parts: Vec<&str> = _symbol_str.split('/').collect();
            if parts.len() == 2 {
                crate::core::Symbol::new(parts[0], parts[1])
            } else {
                crate::core::Symbol { base: _symbol_str.to_string(), quote: String::new(), raw: Some(_symbol_str.to_string()) }
            }
        };

        // Dhan doesn't have funding rates (equity derivatives don't have funding)
        Err(ExchangeError::UnsupportedOperation(
            "Funding rates not available for equity derivatives".to_string(),
        ))
    
    }

    async fn modify_position(&self, req: PositionModification) -> ExchangeResult<()> {
        match req {
            PositionModification::SetLeverage { symbol: ref _symbol, leverage: _leverage, account_type: _account_type } => {
                let _symbol = _symbol.clone();

                // Dhan uses fixed margin requirements, leverage not directly settable
                Err(ExchangeError::UnsupportedOperation(
                "Leverage setting not supported (uses fixed margin requirements)".to_string(),
                ))
    
            }
            _ => Err(ExchangeError::UnsupportedOperation(
                format!("{:?} not supported on {:?}", req, self.exchange_id())
            )),
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// AMEND ORDER (optional trait — Dhan supports PUT /v2/orders/{orderId})
// ═══════════════════════════════════════════════════════════════════════════════

#[async_trait]
impl AmendOrder for DhanConnector {
    async fn amend_order(&self, req: AmendRequest) -> ExchangeResult<Order> {
        let order_id = req.order_id.clone();

        // At least one field must be Some
        if req.fields.price.is_none()
            && req.fields.quantity.is_none()
            && req.fields.trigger_price.is_none()
        {
            return Err(ExchangeError::InvalidRequest(
                "At least one field (price, quantity, trigger_price) must be provided".to_string(),
            ));
        }

        // Fetch current order to fill in unchanged fields (Dhan PUT requires all fields)
        let current = self.get_order("", &order_id, req.account_type).await?;

        let new_price = req.fields.price.or(current.price).unwrap_or(0.0);
        let new_quantity = req.fields.quantity.unwrap_or(current.quantity);
        let new_trigger = req.fields.trigger_price.or(current.stop_price).unwrap_or(0.0);

        // Determine orderType string from current order
        let order_type_str = match &current.order_type {
            OrderType::Market => "MARKET",
            OrderType::Limit { .. } => "LIMIT",
            OrderType::StopMarket { .. } => "STOP_LOSS_MARKET",
            OrderType::StopLimit { .. } => "STOP_LOSS",
            _ => "LIMIT",
        };

        let validity = match current.time_in_force {
            crate::core::TimeInForce::Ioc => "IOC",
            _ => "DAY",
        };

        let client_id = {
            let auth = self.auth.lock().await;
            auth.client_id().to_string()
        };

        let body = json!({
            "dhanClientId": client_id,
            "orderId": order_id,
            "orderType": order_type_str,
            "validity": validity,
            "quantity": new_quantity as i64,
            "price": new_price,
            "disclosedQuantity": 0,
            "triggerPrice": new_trigger,
        });

        let response = self.put(DhanEndpoint::ModifyOrder, &[("orderId", &order_id)], body).await?;
        DhanParser::parse_order_placement(&response)
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// EXTENDED METHODS — Trade history addition
// ═══════════════════════════════════════════════════════════════════════════════

impl DhanConnector {
    /// Recent trade history — `GET /v2/trades` (signed)
    ///
    /// Returns the latest trades executed on the account without requiring a date
    /// range. Use `get_trade_history_paginated` for date-filtered paginated results.
    pub async fn get_trade_history(&self) -> ExchangeResult<Value> {
        self.get(DhanEndpoint::GetRecentTrades, HashMap::new()).await
    }

    /// Paginated trade history — `GET /v2/trades/{fromDate}/{toDate}/{page}` (signed)
    ///
    /// Parameters:
    /// - `from_date`: start date in `YYYY-MM-DD` format
    /// - `to_date`: end date in `YYYY-MM-DD` format
    /// - `page`: page number (0-indexed)
    pub async fn get_trade_history_paginated(
        &self,
        from_date: &str,
        to_date: &str,
        page: u32,
    ) -> ExchangeResult<Value> {
        let base_url = self.urls.rest_url();
        let path = format!("/v2/trades/{}/{}/{}", from_date, to_date, page);
        let url = format!("{}{}", base_url, path);

        let base_url_owned = base_url.to_string();
        let mut auth = self.auth.lock().await;
        let headers = auth.build_headers(&base_url_owned, &self.http).await?;
        drop(auth);

        let response = self.http.get_with_headers(&url, &HashMap::new(), &headers).await?;
        Ok(response)
    }
}