digdigdig3 0.1.21

Unified async Rust API for 44 exchange connectors — crypto, stocks, forex. REST + WebSocket.
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
//! Zerodha Kite Connect Connector
//!
//! Implements all core traits for Zerodha broker API.

use std::collections::HashMap;
use async_trait::async_trait;
use serde_json::{json, Value};

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

use super::endpoints::{ZerodhaEndpoints, ZerodhaEndpoint, format_symbol};
use super::auth::ZerodhaAuth;
use super::parser::ZerodhaParser;

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

/// Zerodha Kite Connect connector
pub struct ZerodhaConnector {
    /// HTTP client
    http: HttpClient,
    /// Authentication
    auth: ZerodhaAuth,
    /// Base URLs
    endpoints: ZerodhaEndpoints,
    /// Per-symbol tick/step precision cache — populated after get_exchange_info()
    precision: PrecisionCache,
}

impl ZerodhaConnector {
    /// Create new connector with explicit auth
    pub fn new(auth: ZerodhaAuth) -> ExchangeResult<Self> {
        let http = HttpClient::new(30_000)?; // 30 sec timeout
        let endpoints = ZerodhaEndpoints::default();

        Ok(Self {
            http,
            auth,
            endpoints,
            precision: PrecisionCache::new(),
        })
    }

    /// Create connector from environment variables
    pub fn from_env() -> Self {
        let auth = ZerodhaAuth::from_env();
        Self::new(auth.clone()).unwrap_or_else(|_| {
            // Fallback if HTTP client creation fails
            Self {
                http: HttpClient::new(30_000).expect("HTTP client creation should not fail with valid timeout"),
                auth,
                endpoints: ZerodhaEndpoints::default(),
                precision: PrecisionCache::new(),
            }
        })
    }

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

    /// GET request
    async fn get(
        &self,
        endpoint: ZerodhaEndpoint,
        params: HashMap<String, String>,
    ) -> ExchangeResult<Value> {
        let base_url = self.endpoints.rest_base;
        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
        let mut headers = HashMap::new();
        self.auth.sign_headers(&mut headers);

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

    /// POST request
    async fn post(
        &self,
        endpoint: ZerodhaEndpoint,
        body: HashMap<String, String>,
    ) -> ExchangeResult<Value> {
        let base_url = self.endpoints.rest_base;
        let path = endpoint.path();
        let url = format!("{}{}", base_url, path);

        // Add auth headers
        let mut headers = HashMap::new();
        self.auth.sign_headers(&mut headers);
        headers.insert("Content-Type".to_string(), "application/x-www-form-urlencoded".to_string());

        // Convert HashMap to JSON Value (the post method will handle it)
        let json_body = json!(body);
        let response = self.http.post(&url, &json_body, &headers).await?;
        Ok(response)
    }

    /// PUT request
    async fn put(
        &self,
        endpoint: ZerodhaEndpoint,
        body: HashMap<String, String>,
    ) -> ExchangeResult<Value> {
        let base_url = self.endpoints.rest_base;
        let path = endpoint.path();
        let url = format!("{}{}", base_url, path);

        // Add auth headers
        let mut headers = HashMap::new();
        self.auth.sign_headers(&mut headers);
        headers.insert("Content-Type".to_string(), "application/x-www-form-urlencoded".to_string());

        // Convert HashMap to JSON Value (the put method will handle it)
        let json_body = json!(body);
        let response = self.http.put(&url, &json_body, &headers).await?;
        Ok(response)
    }

    /// DELETE request
    async fn delete(
        &self,
        endpoint: ZerodhaEndpoint,
    ) -> ExchangeResult<Value> {
        let base_url = self.endpoints.rest_base;
        let path = endpoint.path();
        let url = format!("{}{}", base_url, path);

        // Add auth headers
        let mut headers = HashMap::new();
        self.auth.sign_headers(&mut headers);

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

    // ═══════════════════════════════════════════════════════════════════════════
    // EXTENDED METHODS — GTT (Good Till Triggered)
    // ═══════════════════════════════════════════════════════════════════════════

    /// List all GTT triggers — `GET /gtt/triggers`
    pub async fn gtt_list(&self) -> ExchangeResult<Value> {
        self.get(ZerodhaEndpoint::GetGtts, HashMap::new()).await
    }

    /// Delete a GTT trigger — `DELETE /gtt/triggers/{trigger_id}`
    pub async fn gtt_delete(&self, trigger_id: u64) -> ExchangeResult<Value> {
        self.delete(ZerodhaEndpoint::DeleteGtt(trigger_id)).await
    }

    /// Modify a GTT trigger — `PUT /gtt/triggers/{trigger_id}`
    ///
    /// `body` — form parameters for the updated GTT trigger.
    pub async fn gtt_modify(
        &self,
        trigger_id: u64,
        body: HashMap<String, String>,
    ) -> ExchangeResult<Value> {
        self.put(ZerodhaEndpoint::ModifyGtt(trigger_id), body).await
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // EXTENDED METHODS — Instruments Master
    // ═══════════════════════════════════════════════════════════════════════════

    /// Download instruments master CSV — `GET /instruments`
    ///
    /// Returns all tradeable instruments as a raw CSV string.
    /// Optionally filter by exchange (e.g. `"NSE"`, `"BSE"`, `"NFO"`).
    pub async fn get_instruments_master(&self, exchange: Option<&str>) -> ExchangeResult<Value> {
        let endpoint = match exchange {
            Some(ex) => ZerodhaEndpoint::InstrumentsExchange(ex.to_uppercase()),
            None => ZerodhaEndpoint::Instruments,
        };
        self.get(endpoint, HashMap::new()).await
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // EXTENDED METHODS — Basket Orders
    // ═══════════════════════════════════════════════════════════════════════════

    /// Place basket orders — `POST /orders/baskets`
    ///
    /// `orders` — list of order parameter maps (same fields as individual order placement).
    /// All orders in the basket are validated together; none execute if any fail validation.
    pub async fn place_basket_orders(
        &self,
        orders: Vec<HashMap<String, String>>,
    ) -> ExchangeResult<Value> {
        // Kite Connect basket API accepts JSON array
        let base_url = self.endpoints.rest_base;
        let path = ZerodhaEndpoint::BasketOrders.path();
        let url = format!("{}{}", base_url, path);

        let mut headers = HashMap::new();
        self.auth.sign_headers(&mut headers);
        headers.insert("Content-Type".to_string(), "application/json".to_string());

        let body = serde_json::Value::Array(
            orders.into_iter().map(|o| json!(o)).collect()
        );
        let response = self.http.post(&url, &body, &headers).await?;
        Ok(response)
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// CORE TRAITS
// ═══════════════════════════════════════════════════════════════════════════════

impl ExchangeIdentity for ZerodhaConnector {
    fn exchange_id(&self) -> ExchangeId {
        ExchangeId::Zerodha
    }

    fn is_testnet(&self) -> bool {
        false // Zerodha has no testnet
    }

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

    fn exchange_type(&self) -> ExchangeType {
        ExchangeType::Cex
    }
}

#[async_trait]
impl MarketData for ZerodhaConnector {
    async fn get_price(&self, symbol: Symbol, _account_type: AccountType) -> ExchangeResult<Price> {
        let symbol_key = format_symbol(&symbol);

        let mut params = HashMap::new();
        params.insert("i".to_string(), symbol_key.clone());

        let response = self.get(ZerodhaEndpoint::QuoteLtp, params).await?;
        ZerodhaParser::parse_ltp(&response, &symbol_key)
    }

    async fn get_ticker(&self, symbol: Symbol, _account_type: AccountType) -> ExchangeResult<Ticker> {
        let symbol_key = format_symbol(&symbol);

        let mut params = HashMap::new();
        params.insert("i".to_string(), symbol_key.clone());

        let response = self.get(ZerodhaEndpoint::Quote, params).await?;
        ZerodhaParser::parse_quote(&response, &symbol_key)
    }

    async fn get_orderbook(
        &self,
        symbol: Symbol,
        _depth: Option<u16>,
        _account_type: AccountType,
    ) -> ExchangeResult<OrderBook> {
        let symbol_key = format_symbol(&symbol);

        let mut params = HashMap::new();
        params.insert("i".to_string(), symbol_key.clone());

        let response = self.get(ZerodhaEndpoint::Quote, params).await?;
        ZerodhaParser::parse_orderbook(&response, &symbol_key)
    }

    async fn get_klines(
        &self,
        symbol: Symbol,
        interval: &str,
        _limit: Option<u16>,
        _account_type: AccountType,
        _end_time: Option<i64>,
    ) -> ExchangeResult<Vec<Kline>> {
        // Note: Zerodha historical endpoint requires instrument_token
        // For now, return error indicating this needs instrument token lookup
        let _ = (symbol, interval);
        Err(ExchangeError::UnsupportedOperation(
            "Historical data requires instrument_token. Use get_instruments() first to map symbols to tokens.".to_string()
        ))
    }

    async fn ping(&self) -> ExchangeResult<()> {
        // Zerodha doesn't have a dedicated ping endpoint, so we use the user profile endpoint
        let _response = self.get(ZerodhaEndpoint::UserProfile, HashMap::new()).await?;
        Ok(())
    }

    /// Get exchange info — returns NSE equity instruments from Zerodha
    ///
    /// The /instruments endpoint returns CSV data. We parse it here.
    async fn get_exchange_info(&self, account_type: AccountType) -> ExchangeResult<Vec<SymbolInfo>> {
        let url = format!("{}{}", self.endpoints.rest_base, ZerodhaEndpoint::InstrumentsExchange("NSE".to_string()).path());

        // Add auth headers
        let mut headers = HashMap::new();
        self.auth.sign_headers(&mut headers);

        let bytes = self.http.get_bytes(&url).await?;
        let csv_text = String::from_utf8(bytes)
            .map_err(|e| ExchangeError::Parse(format!("Invalid UTF-8 in instruments CSV: {}", e)))?;

        // CSV format: instrument_token,exchange_token,tradingsymbol,name,last_price,expiry,strike,tick_size,lot_size,instrument_type,segment,exchange
        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() < 12 {
                continue;
            }
            let symbol = cols[2].trim().to_string();
            let instrument_type = cols[9].trim();
            let exchange = cols[11].trim();

            // Only EQ (equity) instruments from NSE
            if instrument_type != "EQ" || exchange != "NSE" {
                continue;
            }

            // CSV column 7 is tick_size (minimum price movement)
            let tick_size = cols.get(7)
                .and_then(|s| s.trim().parse::<f64>().ok())
                .filter(|&v| v > 0.0);

            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,
                step_size: Some(1.0),
                min_notional: None,
                account_type,
            });
        }

        // Populate precision cache so place_order/amend_order can format prices correctly
        self.precision.load_from_symbols(&infos);

        Ok(infos)
    }
}

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

        let symbol_key = format_symbol(&symbol);
        let parts: Vec<&str> = symbol_key.split(':').collect();
        if parts.len() != 2 {
            return Err(ExchangeError::Parse(format!("Invalid symbol format: {}", symbol_key)));
        }
        let exchange = parts[0];
        let tradingsymbol = parts[1];

        // IOC validity string
        let validity = match time_in_force {
            TimeInForce::Ioc => "IOC",
            _ => "DAY",
        };

        match req.order_type {
            OrderType::Fok { .. } => {
                return Err(ExchangeError::UnsupportedOperation(
                    "FOK orders not supported by Zerodha Kite Connect".to_string(),
                ));
            }

            OrderType::Market => {
                let mut body = HashMap::new();
                body.insert("exchange".to_string(), exchange.to_string());
                body.insert("tradingsymbol".to_string(), tradingsymbol.to_string());
                body.insert("transaction_type".to_string(), match side {
                    OrderSide::Buy => "BUY",
                    OrderSide::Sell => "SELL",
                }.to_string());
                body.insert("quantity".to_string(), self.precision.qty(tradingsymbol, quantity));
                body.insert("order_type".to_string(), "MARKET".to_string());
                body.insert("product".to_string(), "CNC".to_string());
                body.insert("validity".to_string(), validity.to_string());

                let response = self.post(ZerodhaEndpoint::PlaceOrder("regular".to_string()), body).await?;
                ZerodhaParser::parse_order(&response).map(PlaceOrderResponse::Simple)
            }

            OrderType::Limit { price } => {
                let mut body = HashMap::new();
                body.insert("exchange".to_string(), exchange.to_string());
                body.insert("tradingsymbol".to_string(), tradingsymbol.to_string());
                body.insert("transaction_type".to_string(), match side {
                    OrderSide::Buy => "BUY",
                    OrderSide::Sell => "SELL",
                }.to_string());
                body.insert("quantity".to_string(), self.precision.qty(tradingsymbol, quantity));
                body.insert("order_type".to_string(), "LIMIT".to_string());
                body.insert("price".to_string(), self.precision.price(tradingsymbol, price));
                body.insert("product".to_string(), "CNC".to_string());
                body.insert("validity".to_string(), validity.to_string());

                let response = self.post(ZerodhaEndpoint::PlaceOrder("regular".to_string()), body).await?;
                ZerodhaParser::parse_order(&response).map(PlaceOrderResponse::Simple)
            }

            OrderType::Ioc { price } => {
                // IOC: variety=regular, validity=IOC, order_type=MARKET or LIMIT
                let mut body = HashMap::new();
                body.insert("exchange".to_string(), exchange.to_string());
                body.insert("tradingsymbol".to_string(), tradingsymbol.to_string());
                body.insert("transaction_type".to_string(), match side {
                    OrderSide::Buy => "BUY",
                    OrderSide::Sell => "SELL",
                }.to_string());
                body.insert("quantity".to_string(), self.precision.qty(tradingsymbol, quantity));
                body.insert("product".to_string(), "CNC".to_string());
                body.insert("validity".to_string(), "IOC".to_string());

                if let Some(p) = price {
                    body.insert("order_type".to_string(), "LIMIT".to_string());
                    body.insert("price".to_string(), self.precision.price(tradingsymbol, p));
                } else {
                    body.insert("order_type".to_string(), "MARKET".to_string());
                }

                let response = self.post(ZerodhaEndpoint::PlaceOrder("regular".to_string()), body).await?;
                ZerodhaParser::parse_order(&response).map(PlaceOrderResponse::Simple)
            }

            OrderType::StopMarket { stop_price } => {
                // SL-M order: variety=regular, order_type=SL-M, trigger_price
                let mut body = HashMap::new();
                body.insert("exchange".to_string(), exchange.to_string());
                body.insert("tradingsymbol".to_string(), tradingsymbol.to_string());
                body.insert("transaction_type".to_string(), match side {
                    OrderSide::Buy => "BUY",
                    OrderSide::Sell => "SELL",
                }.to_string());
                body.insert("quantity".to_string(), self.precision.qty(tradingsymbol, quantity));
                body.insert("order_type".to_string(), "SL-M".to_string());
                body.insert("trigger_price".to_string(), self.precision.price(tradingsymbol, stop_price));
                body.insert("product".to_string(), "CNC".to_string());
                body.insert("validity".to_string(), validity.to_string());

                let response = self.post(ZerodhaEndpoint::PlaceOrder("regular".to_string()), body).await?;
                ZerodhaParser::parse_order(&response).map(PlaceOrderResponse::Simple)
            }

            OrderType::StopLimit { stop_price, limit_price } => {
                // SL order: variety=regular, order_type=SL, price + trigger_price
                let mut body = HashMap::new();
                body.insert("exchange".to_string(), exchange.to_string());
                body.insert("tradingsymbol".to_string(), tradingsymbol.to_string());
                body.insert("transaction_type".to_string(), match side {
                    OrderSide::Buy => "BUY",
                    OrderSide::Sell => "SELL",
                }.to_string());
                body.insert("quantity".to_string(), self.precision.qty(tradingsymbol, quantity));
                body.insert("order_type".to_string(), "SL".to_string());
                body.insert("price".to_string(), self.precision.price(tradingsymbol, limit_price));
                body.insert("trigger_price".to_string(), self.precision.price(tradingsymbol, stop_price));
                body.insert("product".to_string(), "CNC".to_string());
                body.insert("validity".to_string(), validity.to_string());

                let response = self.post(ZerodhaEndpoint::PlaceOrder("regular".to_string()), body).await?;
                ZerodhaParser::parse_order(&response).map(PlaceOrderResponse::Simple)
            }

            OrderType::Bracket { price, stop_loss, .. } => {
                // Cover Order (CO): variety=co, entry price + SL trigger_price
                // Note: Kite's CO is entry + SL only — no TP leg. take_profit is ignored.
                let entry_price = price.ok_or_else(|| {
                    ExchangeError::InvalidRequest(
                        "Bracket orders on Zerodha (Cover Order) require an entry price".to_string(),
                    )
                })?;

                let mut body = HashMap::new();
                body.insert("exchange".to_string(), exchange.to_string());
                body.insert("tradingsymbol".to_string(), tradingsymbol.to_string());
                body.insert("transaction_type".to_string(), match side {
                    OrderSide::Buy => "BUY",
                    OrderSide::Sell => "SELL",
                }.to_string());
                body.insert("quantity".to_string(), self.precision.qty(tradingsymbol, quantity));
                body.insert("order_type".to_string(), "LIMIT".to_string());
                body.insert("price".to_string(), self.precision.price(tradingsymbol, entry_price));
                body.insert("trigger_price".to_string(), self.precision.price(tradingsymbol, stop_loss));
                body.insert("product".to_string(), "MIS".to_string()); // CO requires MIS
                body.insert("validity".to_string(), "DAY".to_string());

                let response = self.post(ZerodhaEndpoint::PlaceOrder("co".to_string()), body).await?;
                ZerodhaParser::parse_order(&response).map(PlaceOrderResponse::Simple)
            }

            OrderType::Iceberg { price, display_quantity } => {
                // Iceberg order: variety=iceberg, iceberg_legs + iceberg_quantity
                let total_qty = quantity as u32;
                let display_qty = display_quantity as u32;
                // Zerodha requires iceberg_legs = ceil(total / display)
                let legs = total_qty.div_ceil(display_qty);
                let legs = legs.clamp(2, 10); // Zerodha: 2–10 legs

                let mut body = HashMap::new();
                body.insert("exchange".to_string(), exchange.to_string());
                body.insert("tradingsymbol".to_string(), tradingsymbol.to_string());
                body.insert("transaction_type".to_string(), match side {
                    OrderSide::Buy => "BUY",
                    OrderSide::Sell => "SELL",
                }.to_string());
                body.insert("quantity".to_string(), self.precision.qty(tradingsymbol, quantity));
                body.insert("order_type".to_string(), "LIMIT".to_string());
                body.insert("price".to_string(), self.precision.price(tradingsymbol, price));
                body.insert("product".to_string(), "CNC".to_string());
                body.insert("validity".to_string(), "DAY".to_string());
                body.insert("iceberg_legs".to_string(), legs.to_string());
                body.insert("iceberg_quantity".to_string(), self.precision.qty(tradingsymbol, display_quantity));

                let response = self.post(ZerodhaEndpoint::PlaceOrder("iceberg".to_string()), body).await?;
                ZerodhaParser::parse_order(&response).map(PlaceOrderResponse::Simple)
            }

            other => Err(ExchangeError::UnsupportedOperation(
                format!("{:?} order type not supported on Zerodha Kite Connect", other)
            )),
        }
    }

    async fn get_order_history(
        &self,
        filter: OrderHistoryFilter,
        _account_type: AccountType,
    ) -> ExchangeResult<Vec<Order>> {
        // Zerodha returns today's full order log via GET /orders
        let response = self.get(ZerodhaEndpoint::GetOrders, HashMap::new()).await?;
        let all_orders = ZerodhaParser::parse_orders(&response)?;

        // Apply client-side filtering (Zerodha returns all of today's orders)
        let filtered: Vec<Order> = all_orders
            .into_iter()
            .filter(|o| {
                // Exclude open orders from history
                !matches!(o.status, OrderStatus::Open | OrderStatus::New | OrderStatus::PartiallyFilled)
            })
            .filter(|o| {
                // Apply status filter if provided
                if let Some(status) = &filter.status {
                    &o.status == status
                } else {
                    true
                }
            })
            .filter(|o| {
                // Apply symbol filter if provided
                if let Some(sym) = &filter.symbol {
                    let sym_str = format_symbol(sym);
                    let parts: Vec<&str> = sym_str.split(':').collect();
                    let trading_sym = if parts.len() == 2 { parts[1] } else { sym_str.as_str() };
                    o.symbol == trading_sym || o.symbol == sym_str
                } else {
                    true
                }
            })
            .take(filter.limit.unwrap_or(500) as usize)
            .collect();

        Ok(filtered)
    }
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(
                ZerodhaEndpoint::CancelOrder("regular".to_string(), order_id.to_string())
            ).await?;

            // Return a basic success result
            Ok(Order {
                id: order_id.to_string(),
                client_order_id: None,
                symbol: String::new(),
                side: OrderSide::Buy,
                order_type: OrderType::Market,
                status: OrderStatus::Canceled,
                price: None,
                stop_price: None,
                quantity: 0.0,
                filled_quantity: 0.0,
                average_price: None,
                commission: None,
                commission_asset: None,
                created_at: 0,
                updated_at: Some(crate::core::timestamp_millis() as i64),
                time_in_force: TimeInForce::Gtc,
            })
    
            }
            _ => 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 response = self.get(
            ZerodhaEndpoint::GetOrder(order_id.to_string()),
            HashMap::new()
        ).await?;

        ZerodhaParser::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(ZerodhaEndpoint::GetOrders, HashMap::new()).await?;
        let all_orders = ZerodhaParser::parse_orders(&response)?;

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

#[async_trait]
impl Account for ZerodhaConnector {
    async fn get_balance(&self, query: BalanceQuery) -> ExchangeResult<Vec<Balance>> {
        let _asset = query.asset.clone();
        let _account_type = query.account_type;

        let response = self.get(ZerodhaEndpoint::GetMargins, HashMap::new()).await?;
        ZerodhaParser::parse_balance(&response)
    
    }

    async fn get_account_info(&self, _account_type: AccountType) -> ExchangeResult<AccountInfo> {
        let response = self.get(ZerodhaEndpoint::UserProfile, HashMap::new()).await?;
        ZerodhaParser::parse_account_info(&response)
    }

    async fn get_fees(&self, _symbol: Option<&str>) -> ExchangeResult<FeeInfo> {
        Err(ExchangeError::UnsupportedOperation(
            "get_fees not yet implemented".to_string()
        ))
    }
}

#[async_trait]
impl Positions for ZerodhaConnector {
    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(ZerodhaEndpoint::Positions, HashMap::new()).await?;
        ZerodhaParser::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()) }
            }
        };

        // Zerodha is a stock broker, not futures exchange
        Err(ExchangeError::UnsupportedOperation(
            "Funding rates not supported for stock broker".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();

                // Zerodha is a stock broker, leverage is product-specific (MIS/NRML)
                Err(ExchangeError::UnsupportedOperation(
                "Leverage setting not supported. Use product types (MIS/NRML) instead".to_string()
                ))
    
            }
            _ => Err(ExchangeError::UnsupportedOperation(
                format!("{:?} not supported on {:?}", req, self.exchange_id())
            )),
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// AMEND ORDER (Zerodha supports PUT /orders/{variety}/{order_id})
// ═══════════════════════════════════════════════════════════════════════════════

#[async_trait]
impl AmendOrder for ZerodhaConnector {
    /// Modify a live order via PUT /orders/{variety}/{order_id}.
    ///
    /// Zerodha's modify endpoint accepts variety (regular/amo/co/iceberg)
    /// and optional price, quantity, trigger_price fields.
    async fn amend_order(&self, req: AmendRequest) -> ExchangeResult<Order> {
        if req.fields.price.is_none()
            && req.fields.quantity.is_none()
            && req.fields.trigger_price.is_none()
        {
            return Err(ExchangeError::InvalidRequest(
                "At least one of price, quantity, or trigger_price must be provided".to_string(),
            ));
        }

        // Derive tradingsymbol from the request symbol for precision lookup
        let symbol_key = format_symbol(&req.symbol);
        let amend_tradingsymbol: &str = symbol_key.split(':').nth(1).unwrap_or(&symbol_key);

        let mut body = HashMap::new();

        if let Some(price) = req.fields.price {
            body.insert("price".to_string(), self.precision.price(amend_tradingsymbol, price));
        }
        if let Some(qty) = req.fields.quantity {
            body.insert("quantity".to_string(), self.precision.qty(amend_tradingsymbol, qty));
        }
        if let Some(trigger) = req.fields.trigger_price {
            body.insert("trigger_price".to_string(), self.precision.price(amend_tradingsymbol, trigger));
        }

        // Default variety to "regular"; callers can pass variety in order_id prefix
        // convention "regular:{order_id}" for CO/iceberg if needed.
        let (variety, order_id) = if let Some(sep) = req.order_id.find(':') {
            let v = req.order_id[..sep].to_string();
            let id = req.order_id[sep + 1..].to_string();
            (v, id)
        } else {
            ("regular".to_string(), req.order_id.clone())
        };

        let _response = self
            .put(ZerodhaEndpoint::ModifyOrder(variety, order_id.clone()), body)
            .await?;

        // Zerodha's modify response just returns the order_id confirmation
        // Fetch full order details to return a complete Order
        let updated = self
            .get(ZerodhaEndpoint::GetOrder(order_id), HashMap::new())
            .await?;
        ZerodhaParser::parse_order(&updated)
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// EXTENDED METHODS (Zerodha-specific)
// ═══════════════════════════════════════════════════════════════════════════════

impl ZerodhaConnector {
    /// Get holdings (long-term delivery portfolio)
    pub async fn get_holdings(&self) -> ExchangeResult<Value> {
        self.get(ZerodhaEndpoint::Holdings, HashMap::new()).await
    }

    /// Convert position between products (CNC/MIS/NRML)
    pub async fn convert_position(
        &self,
        exchange: &str,
        tradingsymbol: &str,
        transaction_type: &str,
        quantity: f64,
        old_product: &str,
        new_product: &str,
    ) -> ExchangeResult<Value> {
        let mut body = HashMap::new();
        body.insert("exchange".to_string(), exchange.to_string());
        body.insert("tradingsymbol".to_string(), tradingsymbol.to_string());
        body.insert("transaction_type".to_string(), transaction_type.to_string());
        body.insert("quantity".to_string(), quantity.to_string());
        body.insert("old_product".to_string(), old_product.to_string());
        body.insert("new_product".to_string(), new_product.to_string());

        self.put(ZerodhaEndpoint::ConvertPosition, body).await
    }
}