kiteconnect 1.0.0

Kite Connect API client for Rust: REST calls with typed responses and the WebSocket ticker
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
use crate::{Call, Exchange, KiteConnect, OrderType, Product, Request, TransactionType, Variety};
use serde::{Deserialize, Serialize};

/// Order in the `POST /margins/orders` and `POST /margins/basket` body.
#[derive(Debug, Clone, Deserialize, Serialize)]
#[non_exhaustive]
#[must_use]
pub struct OrderMarginParam {
    /// Exchange of the order.
    pub exchange: Exchange,
    /// Trading symbol of the order.
    pub tradingsymbol: String,
    /// Transaction type (buy or sell).
    pub transaction_type: TransactionType,
    /// Order variety.
    pub variety: Variety,
    /// Product type.
    pub product: Product,
    /// Order type.
    pub order_type: OrderType,
    /// Order quantity.
    pub quantity: i64,
    /// Order price, omitted when unset.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub price: Option<f64>,
    /// Trigger price, omitted when unset.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub trigger_price: Option<f64>,
    /// Market protection percentage, omitted when unset.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub market_protection: Option<f64>,
}

impl OrderMarginParam {
    /// Creates an order with the required fields.
    pub fn new(
        exchange: Exchange,
        tradingsymbol: impl Into<String>,
        transaction_type: TransactionType,
        variety: Variety,
        product: Product,
        order_type: OrderType,
        quantity: i64,
    ) -> Self {
        Self {
            exchange,
            tradingsymbol: tradingsymbol.into(),
            transaction_type,
            variety,
            product,
            order_type,
            quantity,
            price: None,
            trigger_price: None,
            market_protection: None,
        }
    }
    /// Sets the order price.
    pub fn price(mut self, price: f64) -> Self {
        self.price = Some(price);
        self
    }
    /// Sets the trigger price.
    pub fn trigger_price(mut self, price: f64) -> Self {
        self.trigger_price = Some(price);
        self
    }
    /// Set the market protection percentage for the margin request.
    pub fn market_protection(mut self, protection: f64) -> Self {
        self.market_protection = Some(protection);
        self
    }
}

/// Order in the `POST /charges/orders` body.
#[derive(Debug, Clone, Serialize)]
#[non_exhaustive]
pub struct OrderChargesParam {
    /// ID of the order.
    pub order_id: String,
    /// Exchange of the order.
    pub exchange: Exchange,
    /// Trading symbol of the order.
    pub tradingsymbol: String,
    /// Transaction type (buy or sell).
    pub transaction_type: TransactionType,
    /// Order variety.
    pub variety: Variety,
    /// Product type.
    pub product: Product,
    /// Order type.
    pub order_type: OrderType,
    /// Order quantity.
    pub quantity: i64,
    /// Average price of the order.
    pub average_price: f64,
}

impl OrderChargesParam {
    /// Creates an order with the required fields.
    #[expect(
        clippy::too_many_arguments,
        reason = "Required fields of the Kite charges endpoint"
    )]
    pub fn new(
        order_id: impl Into<String>,
        exchange: Exchange,
        tradingsymbol: impl Into<String>,
        transaction_type: TransactionType,
        variety: Variety,
        product: Product,
        order_type: OrderType,
        quantity: i64,
        average_price: f64,
    ) -> Self {
        Self {
            order_id: order_id.into(),
            exchange,
            tradingsymbol: tradingsymbol.into(),
            transaction_type,
            variety,
            product,
            order_type,
            quantity,
            average_price,
        }
    }
}

/// Parameters for `POST /margins/orders`.
#[derive(Debug, Clone)]
#[non_exhaustive]
#[must_use]
pub struct MarginParams {
    /// Orders to compute margins for.
    pub order_params: Vec<OrderMarginParam>,
    /// Whether to request compact output (`mode=compact`).
    pub compact: bool,
}

impl MarginParams {
    /// Creates parameters for the given orders.
    pub fn new(order_params: Vec<OrderMarginParam>) -> Self {
        Self {
            order_params,
            compact: false,
        }
    }
    /// Sets compact output.
    pub fn compact(mut self, compact: bool) -> Self {
        self.compact = compact;
        self
    }
}

/// Parameters for `POST /margins/basket`.
#[derive(Debug, Clone)]
#[non_exhaustive]
#[must_use]
pub struct BasketParams {
    /// Orders to compute basket margins for.
    pub order_params: Vec<OrderMarginParam>,
    /// Whether to request compact output (`mode=compact`).
    pub compact: bool,
    /// Whether to consider existing positions.
    pub consider_positions: bool,
}

impl BasketParams {
    /// Creates parameters for the given orders.
    pub fn new(order_params: Vec<OrderMarginParam>) -> Self {
        Self {
            order_params,
            compact: false,
            consider_positions: false,
        }
    }
    /// Sets compact output.
    pub fn compact(mut self, compact: bool) -> Self {
        self.compact = compact;
        self
    }
    /// Sets whether existing positions are considered.
    pub fn consider_positions(mut self, consider_positions: bool) -> Self {
        self.consider_positions = consider_positions;
        self
    }
}

/// Parameters for `POST /charges/orders`.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct ChargesParams {
    /// Orders to compute charges for.
    pub order_params: Vec<OrderChargesParam>,
}

impl ChargesParams {
    /// Creates parameters for the given orders.
    #[must_use]
    pub fn new(order_params: Vec<OrderChargesParam>) -> Self {
        Self { order_params }
    }
}

/// Profit and loss returned by the Kite margin endpoints.
#[derive(Debug, Clone, Deserialize, Serialize)]
#[non_exhaustive]
pub struct MarginPnl {
    /// Realised profit and loss.
    pub realised: f64,
    /// Unrealised profit and loss.
    pub unrealised: f64,
}

/// GST breakdown returned by the Kite margin and charges endpoints.
#[derive(Debug, Clone, Deserialize, Serialize)]
#[non_exhaustive]
pub struct Gst {
    /// Integrated GST.
    pub igst: f64,
    /// Central GST.
    pub cgst: f64,
    /// State GST.
    pub sgst: f64,
    /// Total GST.
    pub total: f64,
}

/// Charges breakdown returned by the Kite margin and charges endpoints.
#[derive(Debug, Clone, Deserialize, Serialize)]
#[non_exhaustive]
pub struct Charges {
    /// Transaction tax (for example STT).
    pub transaction_tax: f64,
    /// Type of transaction tax.
    pub transaction_tax_type: String,
    /// Exchange turnover charge.
    pub exchange_turnover_charge: f64,
    /// SEBI turnover charge.
    pub sebi_turnover_charge: f64,
    /// Brokerage charge.
    pub brokerage: f64,
    /// Stamp duty.
    pub stamp_duty: f64,
    /// GST breakdown.
    pub gst: Gst,
    /// Total charges.
    pub total: f64,
}

/// Order or basket totals returned by POST /margins/orders and /margins/basket.
#[derive(Debug, Clone, Deserialize, Serialize)]
#[non_exhaustive]
pub struct OrderMargins {
    /// Margin type (`type` in the response).
    #[serde(rename = "type")]
    pub margin_type: Option<String>,
    /// Trading symbol of the order.
    pub tradingsymbol: Option<String>,
    /// Exchange of the order.
    pub exchange: Option<String>,
    /// SPAN margin.
    pub span: Option<f64>,
    /// Exposure margin.
    pub exposure: Option<f64>,
    /// Option premium.
    pub option_premium: Option<f64>,
    /// Additional margin.
    pub additional: Option<f64>,
    /// Bracket order margin.
    pub bo: Option<f64>,
    /// Cash margin.
    pub cash: Option<f64>,
    /// VAR margin.
    pub var: Option<f64>,
    /// Profit and loss figures.
    pub pnl: Option<MarginPnl>,
    /// Leverage applied.
    pub leverage: Option<f64>,
    /// Charges breakdown, if any.
    pub charges: Option<Charges>,
    /// Total margin required.
    pub total: f64,
}

/// Initial, final and per-order totals returned by POST /margins/basket.
#[derive(Debug, Clone, Deserialize, Serialize)]
#[non_exhaustive]
pub struct BasketMargins {
    /// Initial margin totals.
    pub initial: OrderMargins,
    /// Final margin totals (`final` in the response).
    #[serde(rename = "final")]
    pub final_margin: OrderMargins,
    /// Per-order margin totals.
    #[serde(default)]
    pub orders: Vec<OrderMargins>,
    /// Charges breakdown for the basket, if any.
    pub charges: Option<Charges>,
}

/// Order charges returned by POST /charges/orders.
#[derive(Debug, Clone, Deserialize, Serialize)]
#[non_exhaustive]
pub struct OrderCharges {
    /// Exchange of the order.
    pub exchange: String,
    /// Trading symbol of the order.
    pub tradingsymbol: String,
    /// Transaction type (buy or sell).
    pub transaction_type: String,
    /// Order variety.
    pub variety: String,
    /// Product type.
    pub product: String,
    /// Order type.
    pub order_type: String,
    /// Order quantity.
    pub quantity: f64,
    /// Order price.
    pub price: f64,
    /// Charges breakdown for the order.
    pub charges: Charges,
}

impl KiteConnect {
    /// `POST /margins/orders`.
    pub fn order_margins(&self, params: MarginParams) -> Call<Vec<OrderMargins>> {
        let MarginParams {
            order_params,
            compact,
        } = params;
        let mut query = Vec::new();
        if compact {
            query.push(("mode", "compact"));
        }
        Call::new(
            self.clone(),
            Request::post("/margins/orders")
                .query(query)
                .json(serde_json::json!(order_params)),
        )
    }
    /// `POST /margins/basket`.
    pub fn basket_margins(&self, params: BasketParams) -> Call<BasketMargins> {
        let BasketParams {
            order_params,
            compact,
            consider_positions,
        } = params;
        let mut query = Vec::new();
        if consider_positions {
            query.push(("consider_positions", "true"));
        }
        if compact {
            query.push(("mode", "compact"));
        }
        Call::new(
            self.clone(),
            Request::post("/margins/basket")
                .query(query)
                .json(serde_json::json!(order_params)),
        )
    }
    /// `POST /charges/orders`.
    pub fn order_charges(&self, params: ChargesParams) -> Call<Vec<OrderCharges>> {
        let ChargesParams { order_params } = params;
        Call::new(
            self.clone(),
            Request::post("/charges/orders").json(serde_json::json!(order_params)),
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use httpmock::prelude::*;
    use serde_json::{Value, json};

    fn fixture<T: serde::de::DeserializeOwned>(body: &str) -> T {
        let value: Value = serde_json::from_str(body).unwrap();
        serde_json::from_value(value["data"].clone()).unwrap()
    }
    fn client(server: &MockServer) -> KiteConnect {
        KiteConnect::builder("key")
            .base_url(server.base_url())
            .build()
            .unwrap()
    }
    fn order() -> OrderMarginParam {
        OrderMarginParam::new(
            Exchange::Nse,
            "INFY",
            TransactionType::Buy,
            Variety::Regular,
            Product::Cnc,
            OrderType::Market,
            1,
        )
    }
    #[test]
    fn fixtures_decode_all_margin_and_charge_types() {
        let margins: Vec<OrderMargins> =
            fixture(include_str!("../mock_responses/order_margins.json"));
        assert_eq!(margins.len(), 1);
        assert_eq!(margins[0].total.to_bits(), f64::to_bits(1_498.0));
        assert_eq!(margins[0].tradingsymbol.as_deref(), Some("INFY"));
        assert_eq!(margins[0].var, Some(1498.0));
        assert_eq!(
            margins[0].pnl.as_ref().unwrap().realised.to_bits(),
            f64::to_bits(0.0)
        );
        assert_eq!(
            margins[0].pnl.as_ref().unwrap().unrealised.to_bits(),
            f64::to_bits(0.0)
        );
        let charges = margins[0].charges.as_ref().unwrap();
        assert_eq!(charges.transaction_tax.to_bits(), f64::to_bits(1.498));
        assert_eq!(charges.transaction_tax_type, "stt");
        assert_eq!(charges.brokerage.to_bits(), f64::to_bits(0.01));
        assert_eq!(charges.total.to_bits(), f64::to_bits(1.792_551_22));
        assert!((charges.gst.total - 0.011_372_219_999_999_999).abs() < 1e-15);
        assert_eq!(charges.gst.cgst.to_bits(), f64::to_bits(0.0));
        assert_eq!(charges.gst.sgst.to_bits(), f64::to_bits(0.0));
        let basket: BasketMargins = fixture(include_str!("../mock_responses/basket_margins.json"));
        assert_eq!(basket.initial.total.to_bits(), f64::to_bits(96_504.975));
        assert!((basket.final_margin.total - 34_786.725_000_000_006).abs() < 1e-9);
        assert_eq!(basket.orders.len(), 2);
        assert_eq!(basket.orders[0].total.to_bits(), f64::to_bits(95_983.725));
        assert_eq!(basket.orders[1].total.to_bits(), f64::to_bits(521.25));
        assert_eq!(
            basket.charges.as_ref().unwrap().brokerage.to_bits(),
            f64::to_bits(40.0)
        );
        let charges: Vec<OrderCharges> =
            fixture(include_str!("../mock_responses/virtual_contract_note.json"));
        assert_eq!(charges.len(), 3);
        assert_eq!(charges[0].tradingsymbol, "SBIN");
        assert_eq!(charges[0].price.to_bits(), f64::to_bits(560.0));
        assert_eq!(
            charges[0].charges.total.to_bits(),
            f64::to_bits(0.582_696_8)
        );
        assert_eq!(charges[1].exchange, "MCX");
        assert_eq!(charges[1].charges.transaction_tax_type, "ctt");
        assert_eq!(charges[2].quantity.to_bits(), f64::to_bits(100.0));
    }
    #[test]
    fn compact_totals_decode_and_missing_totals_fail() {
        let margin: OrderMargins = serde_json::from_value(json!({"total":0})).unwrap();
        assert_eq!(margin.total.to_bits(), f64::to_bits(0.0));
        assert!(margin.charges.is_none());
        for value in [json!({}), json!({"total":null}), json!({"total":"1"})] {
            assert!(serde_json::from_value::<OrderMargins>(value).is_err());
        }
        let basket: BasketMargins =
            serde_json::from_value(json!({"initial":{"total":1}, "final":{"total":2}})).unwrap();
        assert_eq!(basket.initial.total.to_bits(), f64::to_bits(1.0));
        assert_eq!(basket.final_margin.total.to_bits(), f64::to_bits(2.0));
        assert!(basket.orders.is_empty());
        let mut data: Value =
            serde_json::from_str(include_str!("../mock_responses/order_margins.json")).unwrap();
        data["data"][0]["charges"]
            .as_object_mut()
            .unwrap()
            .remove("total");
        assert!(serde_json::from_value::<Vec<OrderMargins>>(data["data"].clone()).is_err());
        let mut data: Value =
            serde_json::from_str(include_str!("../mock_responses/order_margins.json")).unwrap();
        data["data"][0]["charges"]["gst"]
            .as_object_mut()
            .unwrap()
            .remove("total");
        assert!(serde_json::from_value::<Vec<OrderMargins>>(data["data"].clone()).is_err());
        for section in ["initial", "final"] {
            let mut data: Value =
                serde_json::from_str(include_str!("../mock_responses/basket_margins.json"))
                    .unwrap();
            data["data"][section]
                .as_object_mut()
                .unwrap()
                .remove("total");
            assert!(serde_json::from_value::<BasketMargins>(data["data"].clone()).is_err());
        }
    }
    #[test]
    fn params_serialize_go_fields_and_omit_unset_prices() {
        assert_eq!(
            serde_json::to_value(order()).unwrap(),
            json!({
                "exchange":"NSE", "tradingsymbol":"INFY", "transaction_type":"BUY", "variety":"regular", "product":"CNC", "order_type":"MARKET", "quantity":1
            })
        );
        let priced = order().price(10.0).trigger_price(9.0);
        let value = serde_json::to_value(priced).unwrap();
        assert_eq!(value["price"], 10.0);
        assert_eq!(value["trigger_price"], 9.0);
        let charge = OrderChargesParam::new(
            "OID-1",
            Exchange::Nse,
            "INFY",
            TransactionType::Sell,
            Variety::Regular,
            Product::Cnc,
            OrderType::Limit,
            2,
            1498.0,
        );
        assert_eq!(
            serde_json::to_value(charge).unwrap(),
            json!({
                "order_id":"OID-1", "exchange":"NSE", "tradingsymbol":"INFY", "transaction_type":"SELL", "variety":"regular", "product":"CNC", "order_type":"LIMIT", "quantity":2,"average_price":1498.0
            })
        );
    }
    #[tokio::test]
    async fn order_margins_request() {
        let server = MockServer::start_async().await;
        let mock = server.mock_async(|when, then| {
            when.method(POST).path("/margins/orders").query_param("mode", "compact")
                .header("content-type", "application/json")
                .json_body(json!([{"exchange":"NSE", "tradingsymbol":"INFY", "transaction_type":"BUY", "variety":"regular", "product":"CNC", "order_type":"MARKET", "quantity":1}]));
            then.status(200).json_body(json!({"status":"success", "data":[{"total":1498}]}));
        }).await;
        let margins = client(&server)
            .order_margins(MarginParams::new(vec![order()]).compact(true))
            .await
            .unwrap();
        assert_eq!(margins[0].total.to_bits(), f64::to_bits(1_498.0));
        mock.assert_async().await;
        assert_eq!(
            client(&server)
                .order_margins(MarginParams::new(vec![]))
                .request()
                .query_pairs()
                .len(),
            0
        );
    }
    #[tokio::test]
    async fn basket_margins_request() {
        let server = MockServer::start_async().await;
        let mock = server
            .mock_async(|when, then| {
                when.method(POST)
                    .path("/margins/basket")
                    .query_param("mode", "compact")
                    .query_param("consider_positions", "true")
                    .json_body(json!([{"exchange":"NSE", "tradingsymbol":"INFY", "transaction_type":"BUY", "variety":"regular", "product":"CNC", "order_type":"MARKET", "quantity":1, "price":0.0}]));
                then.status(200)
                    .body(include_str!("../mock_responses/basket_margins.json"));
            })
            .await;
        let basket = client(&server)
            .basket_margins(
                BasketParams::new(vec![order().price(0.0)])
                    .compact(true)
                    .consider_positions(true),
            )
            .await
            .unwrap();
        assert_eq!(basket.initial.total.to_bits(), f64::to_bits(96_504.975));
        mock.assert_async().await;
        assert_eq!(
            client(&server)
                .basket_margins(BasketParams::new(vec![order()]))
                .request()
                .query_pairs()
                .len(),
            0
        );
    }
    #[tokio::test]
    async fn order_charges_request() {
        let server = MockServer::start_async().await;
        let mock = server.mock_async(|when, then| {
            when.method(POST).path("/charges/orders").header("content-type", "application/json")
                .json_body(json!([{"order_id":"OID-1", "exchange":"NSE", "tradingsymbol":"SBIN", "transaction_type":"BUY", "variety":"regular", "product":"CNC", "order_type":"MARKET", "quantity":1,"average_price":560.0}]));
            then.status(200).body(include_str!("../mock_responses/virtual_contract_note.json"));
        }).await;
        let orders = vec![OrderChargesParam::new(
            "OID-1",
            Exchange::Nse,
            "SBIN",
            TransactionType::Buy,
            Variety::Regular,
            Product::Cnc,
            OrderType::Market,
            1,
            560.0,
        )];
        let charges = client(&server)
            .order_charges(ChargesParams::new(orders))
            .await
            .unwrap();
        assert_eq!(
            charges[0].charges.total.to_bits(),
            f64::to_bits(0.582_696_8)
        );
        mock.assert_async().await;
    }
}