dhan-rs 0.1.7

Unofficial Rust client library for the DhanHQ Broker API v2 — orders, market data, WebSocket feeds, and more
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
#![allow(missing_docs)]
//! Shared enum types that map directly to DhanHQ API string values.
//!
//! Variant names use `SCREAMING_SNAKE_CASE` to match the JSON wire format
//! expected by the DhanHQ API, so we suppress the Rust naming convention lint.
#![allow(non_camel_case_types)]

use serde::{Deserialize, Serialize};

// ---------------------------------------------------------------------------
// Exchange Segment
// ---------------------------------------------------------------------------

/// Exchange and segment identifier used across all DhanHQ APIs.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ExchangeSegment {
    /// Index value (segment code 0).
    IDX_I,
    /// NSE Equity Cash (segment code 1).
    NSE_EQ,
    /// NSE Futures & Options (segment code 2).
    NSE_FNO,
    /// NSE Currency (segment code 3).
    NSE_CURRENCY,
    /// BSE Equity Cash (segment code 4).
    BSE_EQ,
    /// MCX Commodity (segment code 5).
    MCX_COMM,
    /// NSE Commodity. The linked REST OpenAPI publishes this value, but Dhan's
    /// binary-feed annexure does not assign it a segment code.
    NSE_COMM,
    /// BSE Currency (segment code 7).
    BSE_CURRENCY,
    /// BSE Futures & Options (segment code 8).
    BSE_FNO,
}

impl ExchangeSegment {
    /// Returns the documented numeric segment code used in binary WebSocket
    /// packets, or `None` for REST-only values such as `NSE_COMM`.
    pub fn segment_code(self) -> Option<u8> {
        match self {
            Self::IDX_I => Some(0),
            Self::NSE_EQ => Some(1),
            Self::NSE_FNO => Some(2),
            Self::NSE_CURRENCY => Some(3),
            Self::BSE_EQ => Some(4),
            Self::MCX_COMM => Some(5),
            Self::NSE_COMM => None,
            Self::BSE_CURRENCY => Some(7),
            Self::BSE_FNO => Some(8),
        }
    }

    /// Construct from a numeric segment code (as found in binary feed packets).
    pub fn from_segment_code(code: u8) -> Option<Self> {
        match code {
            0 => Some(Self::IDX_I),
            1 => Some(Self::NSE_EQ),
            2 => Some(Self::NSE_FNO),
            3 => Some(Self::NSE_CURRENCY),
            4 => Some(Self::BSE_EQ),
            5 => Some(Self::MCX_COMM),
            7 => Some(Self::BSE_CURRENCY),
            8 => Some(Self::BSE_FNO),
            _ => None,
        }
    }
}

// ---------------------------------------------------------------------------
// Transaction Type
// ---------------------------------------------------------------------------

/// Buy or sell side of a transaction.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum TransactionType {
    BUY,
    SELL,
}

// ---------------------------------------------------------------------------
// Product Type
// ---------------------------------------------------------------------------

/// Product type for an order.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ProductType {
    /// Cash & Carry for equity deliveries.
    CNC,
    /// Intraday for Equity, Futures & Options.
    INTRADAY,
    /// Carry Forward in Futures & Options.
    MARGIN,
    /// Margin Trading Facility.
    MTF,
    /// Cover Order (intraday only).
    CO,
    /// Bracket Order (intraday only).
    BO,
}

// ---------------------------------------------------------------------------
// Order Type
// ---------------------------------------------------------------------------

/// Type of order.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum OrderType {
    LIMIT,
    MARKET,
    STOP_LOSS,
    STOP_LOSS_MARKET,
}

// ---------------------------------------------------------------------------
// Order Status
// ---------------------------------------------------------------------------

/// Status of an order in the order book.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum OrderStatus {
    /// Did not reach the exchange server.
    TRANSIT,
    /// Awaiting execution.
    PENDING,
    /// Used for Super Order: both entry and exit legs placed.
    CLOSED,
    /// Used for Super Order: target or stop-loss leg triggered.
    TRIGGERED,
    /// Rejected by broker or exchange.
    REJECTED,
    /// Cancelled by user.
    CANCELLED,
    /// Partial quantity traded successfully.
    PART_TRADED,
    /// Executed successfully.
    TRADED,
    /// Order expired.
    EXPIRED,
    /// Confirmed (used for Forever Orders).
    CONFIRM,
}

// ---------------------------------------------------------------------------
// Validity
// ---------------------------------------------------------------------------

/// Order validity.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Validity {
    /// Valid for the trading day.
    DAY,
    /// Immediate or Cancel.
    IOC,
}

// ---------------------------------------------------------------------------
// Leg Name
// ---------------------------------------------------------------------------

/// Identifies a leg in Super Order / Bracket Order / Cover Order.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum LegName {
    ENTRY_LEG,
    TARGET_LEG,
    STOP_LOSS_LEG,
}

// ---------------------------------------------------------------------------
// Position Type
// ---------------------------------------------------------------------------

/// Position direction.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum PositionType {
    LONG,
    SHORT,
    CLOSED,
}

// ---------------------------------------------------------------------------
// Option Type
// ---------------------------------------------------------------------------

/// Derivative option type.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum OptionType {
    CALL,
    PUT,
}

// ---------------------------------------------------------------------------
// After Market Order Time
// ---------------------------------------------------------------------------

/// Timing for after-market orders.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum AmoTime {
    /// Pumped at pre-market session.
    PRE_OPEN,
    /// Pumped at market open.
    OPEN,
    /// Pumped 30 minutes after market open.
    OPEN_30,
    /// Pumped 60 minutes after market open.
    OPEN_60,
}

// ---------------------------------------------------------------------------
// Instrument
// ---------------------------------------------------------------------------

/// Instrument type identifier.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Instrument {
    INDEX,
    FUTIDX,
    OPTIDX,
    EQUITY,
    FUTSTK,
    OPTSTK,
    FUTCOM,
    OPTFUT,
    FUTCUR,
    OPTCUR,
}

// ---------------------------------------------------------------------------
// Expiry Code
// ---------------------------------------------------------------------------

/// Expiry proximity for derivative instruments.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[repr(u8)]
pub enum ExpiryCode {
    /// Current / near expiry.
    Near = 0,
    /// Next expiry.
    Next = 1,
    /// Far expiry.
    Far = 2,
}

// ---------------------------------------------------------------------------
// Order Flag (Forever Orders)
// ---------------------------------------------------------------------------

/// Forever order flag.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum OrderFlag {
    /// Single forever order.
    SINGLE,
    /// One-Cancels-Other order.
    OCO,
}

// ---------------------------------------------------------------------------
// Kill Switch Status
// ---------------------------------------------------------------------------

/// Kill switch activation status.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum KillSwitchStatus {
    ACTIVATE,
    DEACTIVATE,
}

// ---------------------------------------------------------------------------
// IP Flag
// ---------------------------------------------------------------------------

/// Static IP designation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum IpFlag {
    PRIMARY,
    SECONDARY,
}

// ---------------------------------------------------------------------------
// Feed Request Code (WebSocket market feed)
// ---------------------------------------------------------------------------

/// Request codes sent over the market feed WebSocket.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum FeedRequestCode {
    /// Connect to feed.
    Connect = 11,
    /// Disconnect from feed.
    Disconnect = 12,
    /// Subscribe to Ticker packets.
    SubscribeTicker = 15,
    /// Unsubscribe from Ticker packets.
    UnsubscribeTicker = 16,
    /// Subscribe to Quote packets.
    SubscribeQuote = 17,
    /// Unsubscribe from Quote packets.
    UnsubscribeQuote = 18,
    /// Subscribe to Full packets.
    SubscribeFull = 21,
    /// Unsubscribe from Full packets.
    UnsubscribeFull = 22,
    /// Subscribe to Full Market Depth.
    SubscribeFullMarketDepth = 23,
    /// Unsubscribe from Full Market Depth.
    UnsubscribeFullMarketDepth = 24,
}

impl Serialize for FeedRequestCode {
    fn serialize<S: serde::Serializer>(
        &self,
        serializer: S,
    ) -> std::result::Result<S::Ok, S::Error> {
        serializer.serialize_u8(*self as u8)
    }
}

// ---------------------------------------------------------------------------
// Feed Response Code (WebSocket market feed)
// ---------------------------------------------------------------------------

/// Response codes received in binary market feed packets.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum FeedResponseCode {
    /// Index packet.
    Index = 1,
    /// Ticker packet (LTP + LTT).
    Ticker = 2,
    /// Quote packet (LTP, qty, ATP, volume, OHLC, etc.).
    Quote = 4,
    /// Open Interest packet.
    OI = 5,
    /// Previous close packet.
    PrevClose = 6,
    /// Market status packet.
    MarketStatus = 7,
    /// Full packet (quote + depth + OI).
    Full = 8,
    /// Feed disconnect packet.
    Disconnect = 50,
}

impl FeedResponseCode {
    /// Parse a response code from the first byte of a binary packet header.
    pub fn from_byte(b: u8) -> Option<Self> {
        match b {
            1 => Some(Self::Index),
            2 => Some(Self::Ticker),
            4 => Some(Self::Quote),
            5 => Some(Self::OI),
            6 => Some(Self::PrevClose),
            7 => Some(Self::MarketStatus),
            8 => Some(Self::Full),
            50 => Some(Self::Disconnect),
            _ => None,
        }
    }
}

// ---------------------------------------------------------------------------
// Conditional Trigger — Comparison Type
// ---------------------------------------------------------------------------

/// How the condition in a conditional trigger is evaluated.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ComparisonType {
    /// Compare technical indicator against a fixed numeric value.
    TECHNICAL_WITH_VALUE,
    /// Compare technical indicator against another indicator.
    TECHNICAL_WITH_INDICATOR,
    /// Compare a technical indicator with closing price.
    TECHNICAL_WITH_CLOSE,
    /// Continuously scans live market data.
    LIVE_SCAN_ALERT,
    /// Compare market price against a fixed value.
    PRICE_WITH_VALUE,
    /// Compare price change by percentage.
    PRICE_WITH_PERCENT_CHANGE,
}

// ---------------------------------------------------------------------------
// Conditional Trigger — Indicator Name
// ---------------------------------------------------------------------------

/// Technical indicator names supported by conditional triggers.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum IndicatorName {
    SMA_5,
    SMA_10,
    SMA_20,
    SMA_50,
    SMA_100,
    SMA_200,
    EMA_5,
    EMA_10,
    EMA_20,
    EMA_50,
    EMA_100,
    EMA_200,
    /// Upper Bollinger Band.
    BB_UPPER,
    /// Lower Bollinger Band.
    BB_LOWER,
    /// Relative Strength Index (14-period).
    RSI_14,
    /// Average True Range (14-period).
    ATR_14,
    /// Stochastic Oscillator.
    STOCHASTIC,
    /// Stochastic RSI (14-period).
    STOCHRSI_14,
    /// MACD long-term component (26-period).
    MACD_26,
    /// MACD short-term component (12-period).
    MACD_12,
    /// MACD histogram.
    MACD_HIST,
}

// ---------------------------------------------------------------------------
// Conditional Trigger — Operator
// ---------------------------------------------------------------------------

/// Comparison operator for conditional trigger conditions.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Operator {
    CROSSING_UP,
    CROSSING_DOWN,
    CROSSING_ANY_SIDE,
    GREATER_THAN,
    LESS_THAN,
    GREATER_THAN_EQUAL,
    LESS_THAN_EQUAL,
    EQUAL,
    NOT_EQUAL,
}

// ---------------------------------------------------------------------------
// Conditional Trigger — Alert Status
// ---------------------------------------------------------------------------

/// Status of a conditional trigger alert.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum AlertStatus {
    /// Alert is currently active and monitoring.
    ACTIVE,
    /// Alert condition has been met.
    TRIGGERED,
    /// Alert has expired.
    EXPIRED,
    /// Alert was cancelled by the user.
    CANCELLED,
}