Skip to main content

dhan_rs/types/
enums.rs

1#![allow(missing_docs)]
2//! Shared enum types that map directly to DhanHQ API string values.
3//!
4//! Variant names use `SCREAMING_SNAKE_CASE` to match the JSON wire format
5//! expected by the DhanHQ API, so we suppress the Rust naming convention lint.
6#![allow(non_camel_case_types)]
7
8use serde::{Deserialize, Serialize};
9
10// ---------------------------------------------------------------------------
11// Exchange Segment
12// ---------------------------------------------------------------------------
13
14/// Exchange and segment identifier used across all DhanHQ APIs.
15#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
16pub enum ExchangeSegment {
17    /// Index value (segment code 0).
18    IDX_I,
19    /// NSE Equity Cash (segment code 1).
20    NSE_EQ,
21    /// NSE Futures & Options (segment code 2).
22    NSE_FNO,
23    /// NSE Currency (segment code 3).
24    NSE_CURRENCY,
25    /// BSE Equity Cash (segment code 4).
26    BSE_EQ,
27    /// MCX Commodity (segment code 5).
28    MCX_COMM,
29    /// BSE Currency (segment code 7).
30    BSE_CURRENCY,
31    /// BSE Futures & Options (segment code 8).
32    BSE_FNO,
33}
34
35impl ExchangeSegment {
36    /// Returns the numeric segment code used in binary WebSocket packets.
37    pub fn segment_code(self) -> u8 {
38        match self {
39            Self::IDX_I => 0,
40            Self::NSE_EQ => 1,
41            Self::NSE_FNO => 2,
42            Self::NSE_CURRENCY => 3,
43            Self::BSE_EQ => 4,
44            Self::MCX_COMM => 5,
45            Self::BSE_CURRENCY => 7,
46            Self::BSE_FNO => 8,
47        }
48    }
49
50    /// Construct from a numeric segment code (as found in binary feed packets).
51    pub fn from_segment_code(code: u8) -> Option<Self> {
52        match code {
53            0 => Some(Self::IDX_I),
54            1 => Some(Self::NSE_EQ),
55            2 => Some(Self::NSE_FNO),
56            3 => Some(Self::NSE_CURRENCY),
57            4 => Some(Self::BSE_EQ),
58            5 => Some(Self::MCX_COMM),
59            7 => Some(Self::BSE_CURRENCY),
60            8 => Some(Self::BSE_FNO),
61            _ => None,
62        }
63    }
64}
65
66// ---------------------------------------------------------------------------
67// Transaction Type
68// ---------------------------------------------------------------------------
69
70/// Buy or sell side of a transaction.
71#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
72pub enum TransactionType {
73    BUY,
74    SELL,
75}
76
77// ---------------------------------------------------------------------------
78// Product Type
79// ---------------------------------------------------------------------------
80
81/// Product type for an order.
82#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
83pub enum ProductType {
84    /// Cash & Carry for equity deliveries.
85    CNC,
86    /// Intraday for Equity, Futures & Options.
87    INTRADAY,
88    /// Carry Forward in Futures & Options.
89    MARGIN,
90    /// Margin Trading Facility.
91    MTF,
92    /// Cover Order (intraday only).
93    CO,
94    /// Bracket Order (intraday only).
95    BO,
96}
97
98// ---------------------------------------------------------------------------
99// Order Type
100// ---------------------------------------------------------------------------
101
102/// Type of order.
103#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
104pub enum OrderType {
105    LIMIT,
106    MARKET,
107    STOP_LOSS,
108    STOP_LOSS_MARKET,
109}
110
111// ---------------------------------------------------------------------------
112// Order Status
113// ---------------------------------------------------------------------------
114
115/// Status of an order in the order book.
116#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
117pub enum OrderStatus {
118    /// Did not reach the exchange server.
119    TRANSIT,
120    /// Awaiting execution.
121    PENDING,
122    /// Used for Super Order: both entry and exit legs placed.
123    CLOSED,
124    /// Used for Super Order: target or stop-loss leg triggered.
125    TRIGGERED,
126    /// Rejected by broker or exchange.
127    REJECTED,
128    /// Cancelled by user.
129    CANCELLED,
130    /// Partial quantity traded successfully.
131    PART_TRADED,
132    /// Executed successfully.
133    TRADED,
134    /// Order expired.
135    EXPIRED,
136    /// Confirmed (used for Forever Orders).
137    CONFIRM,
138}
139
140// ---------------------------------------------------------------------------
141// Validity
142// ---------------------------------------------------------------------------
143
144/// Order validity.
145#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
146pub enum Validity {
147    /// Valid for the trading day.
148    DAY,
149    /// Immediate or Cancel.
150    IOC,
151}
152
153// ---------------------------------------------------------------------------
154// Leg Name
155// ---------------------------------------------------------------------------
156
157/// Identifies a leg in Super Order / Bracket Order / Cover Order.
158#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
159pub enum LegName {
160    ENTRY_LEG,
161    TARGET_LEG,
162    STOP_LOSS_LEG,
163}
164
165// ---------------------------------------------------------------------------
166// Position Type
167// ---------------------------------------------------------------------------
168
169/// Position direction.
170#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
171pub enum PositionType {
172    LONG,
173    SHORT,
174    CLOSED,
175}
176
177// ---------------------------------------------------------------------------
178// Option Type
179// ---------------------------------------------------------------------------
180
181/// Derivative option type.
182#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
183pub enum OptionType {
184    CALL,
185    PUT,
186}
187
188// ---------------------------------------------------------------------------
189// After Market Order Time
190// ---------------------------------------------------------------------------
191
192/// Timing for after-market orders.
193#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
194pub enum AmoTime {
195    /// Pumped at pre-market session.
196    PRE_OPEN,
197    /// Pumped at market open.
198    OPEN,
199    /// Pumped 30 minutes after market open.
200    OPEN_30,
201    /// Pumped 60 minutes after market open.
202    OPEN_60,
203}
204
205// ---------------------------------------------------------------------------
206// Instrument
207// ---------------------------------------------------------------------------
208
209/// Instrument type identifier.
210#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
211pub enum Instrument {
212    INDEX,
213    FUTIDX,
214    OPTIDX,
215    EQUITY,
216    FUTSTK,
217    OPTSTK,
218    FUTCOM,
219    OPTFUT,
220    FUTCUR,
221    OPTCUR,
222}
223
224// ---------------------------------------------------------------------------
225// Expiry Code
226// ---------------------------------------------------------------------------
227
228/// Expiry proximity for derivative instruments.
229#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
230#[repr(u8)]
231pub enum ExpiryCode {
232    /// Current / near expiry.
233    Near = 0,
234    /// Next expiry.
235    Next = 1,
236    /// Far expiry.
237    Far = 2,
238}
239
240// ---------------------------------------------------------------------------
241// Order Flag (Forever Orders)
242// ---------------------------------------------------------------------------
243
244/// Forever order flag.
245#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
246pub enum OrderFlag {
247    /// Single forever order.
248    SINGLE,
249    /// One-Cancels-Other order.
250    OCO,
251}
252
253// ---------------------------------------------------------------------------
254// Kill Switch Status
255// ---------------------------------------------------------------------------
256
257/// Kill switch activation status.
258#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
259pub enum KillSwitchStatus {
260    ACTIVATE,
261    DEACTIVATE,
262}
263
264// ---------------------------------------------------------------------------
265// IP Flag
266// ---------------------------------------------------------------------------
267
268/// Static IP designation.
269#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
270pub enum IpFlag {
271    PRIMARY,
272    SECONDARY,
273}
274
275// ---------------------------------------------------------------------------
276// Feed Request Code (WebSocket market feed)
277// ---------------------------------------------------------------------------
278
279/// Request codes sent over the market feed WebSocket.
280#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
281#[repr(u8)]
282pub enum FeedRequestCode {
283    /// Connect to feed.
284    Connect = 11,
285    /// Disconnect from feed.
286    Disconnect = 12,
287    /// Subscribe to Ticker packets.
288    SubscribeTicker = 15,
289    /// Unsubscribe from Ticker packets.
290    UnsubscribeTicker = 16,
291    /// Subscribe to Quote packets.
292    SubscribeQuote = 17,
293    /// Unsubscribe from Quote packets.
294    UnsubscribeQuote = 18,
295    /// Subscribe to Full packets.
296    SubscribeFull = 21,
297    /// Unsubscribe from Full packets.
298    UnsubscribeFull = 22,
299    /// Subscribe to Full Market Depth.
300    SubscribeFullMarketDepth = 23,
301    /// Unsubscribe from Full Market Depth.
302    UnsubscribeFullMarketDepth = 24,
303}
304
305impl Serialize for FeedRequestCode {
306    fn serialize<S: serde::Serializer>(
307        &self,
308        serializer: S,
309    ) -> std::result::Result<S::Ok, S::Error> {
310        serializer.serialize_u8(*self as u8)
311    }
312}
313
314// ---------------------------------------------------------------------------
315// Feed Response Code (WebSocket market feed)
316// ---------------------------------------------------------------------------
317
318/// Response codes received in binary market feed packets.
319#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
320#[repr(u8)]
321pub enum FeedResponseCode {
322    /// Index packet.
323    Index = 1,
324    /// Ticker packet (LTP + LTT).
325    Ticker = 2,
326    /// Quote packet (LTP, qty, ATP, volume, OHLC, etc.).
327    Quote = 4,
328    /// Open Interest packet.
329    OI = 5,
330    /// Previous close packet.
331    PrevClose = 6,
332    /// Market status packet.
333    MarketStatus = 7,
334    /// Full packet (quote + depth + OI).
335    Full = 8,
336    /// Feed disconnect packet.
337    Disconnect = 50,
338}
339
340impl FeedResponseCode {
341    /// Parse a response code from the first byte of a binary packet header.
342    pub fn from_byte(b: u8) -> Option<Self> {
343        match b {
344            1 => Some(Self::Index),
345            2 => Some(Self::Ticker),
346            4 => Some(Self::Quote),
347            5 => Some(Self::OI),
348            6 => Some(Self::PrevClose),
349            7 => Some(Self::MarketStatus),
350            8 => Some(Self::Full),
351            50 => Some(Self::Disconnect),
352            _ => None,
353        }
354    }
355}
356
357// ---------------------------------------------------------------------------
358// Conditional Trigger — Comparison Type
359// ---------------------------------------------------------------------------
360
361/// How the condition in a conditional trigger is evaluated.
362#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
363pub enum ComparisonType {
364    /// Compare technical indicator against a fixed numeric value.
365    TECHNICAL_WITH_VALUE,
366    /// Compare technical indicator against another indicator.
367    TECHNICAL_WITH_INDICATOR,
368    /// Compare a technical indicator with closing price.
369    TECHNICAL_WITH_CLOSE,
370    /// Continuously scans live market data.
371    LIVE_SCAN_ALERT,
372    /// Compare market price against a fixed value.
373    PRICE_WITH_VALUE,
374    /// Compare price change by percentage.
375    PRICE_WITH_PERCENT_CHANGE,
376}
377
378// ---------------------------------------------------------------------------
379// Conditional Trigger — Indicator Name
380// ---------------------------------------------------------------------------
381
382/// Technical indicator names supported by conditional triggers.
383#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
384pub enum IndicatorName {
385    SMA_5,
386    SMA_10,
387    SMA_20,
388    SMA_50,
389    SMA_100,
390    SMA_200,
391    EMA_5,
392    EMA_10,
393    EMA_20,
394    EMA_50,
395    EMA_100,
396    EMA_200,
397    /// Upper Bollinger Band.
398    BB_UPPER,
399    /// Lower Bollinger Band.
400    BB_LOWER,
401    /// Relative Strength Index (14-period).
402    RSI_14,
403    /// Average True Range (14-period).
404    ATR_14,
405    /// Stochastic Oscillator.
406    STOCHASTIC,
407    /// Stochastic RSI (14-period).
408    STOCHRSI_14,
409    /// MACD long-term component (26-period).
410    MACD_26,
411    /// MACD short-term component (12-period).
412    MACD_12,
413    /// MACD histogram.
414    MACD_HIST,
415}
416
417// ---------------------------------------------------------------------------
418// Conditional Trigger — Operator
419// ---------------------------------------------------------------------------
420
421/// Comparison operator for conditional trigger conditions.
422#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
423pub enum Operator {
424    CROSSING_UP,
425    CROSSING_DOWN,
426    CROSSING_ANY_SIDE,
427    GREATER_THAN,
428    LESS_THAN,
429    GREATER_THAN_EQUAL,
430    LESS_THAN_EQUAL,
431    EQUAL,
432    NOT_EQUAL,
433}
434
435// ---------------------------------------------------------------------------
436// Conditional Trigger — Alert Status
437// ---------------------------------------------------------------------------
438
439/// Status of a conditional trigger alert.
440#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
441pub enum AlertStatus {
442    /// Alert is currently active and monitoring.
443    ACTIVE,
444    /// Alert condition has been met.
445    TRIGGERED,
446    /// Alert has expired.
447    EXPIRED,
448    /// Alert was cancelled by the user.
449    CANCELLED,
450}