Skip to main content

dhan_rs/types/
conditional.rs

1#![allow(missing_docs)]
2//! Conditional Trigger types.
3
4use serde::{Deserialize, Serialize};
5
6use crate::types::enums::*;
7
8// ---------------------------------------------------------------------------
9// Alert Condition
10// ---------------------------------------------------------------------------
11
12/// Condition configuration for a conditional trigger.
13#[derive(Debug, Clone, Serialize, Deserialize)]
14#[serde(rename_all = "camelCase")]
15pub struct AlertCondition {
16    /// Type of comparison (e.g. `TECHNICAL_WITH_VALUE`).
17    pub comparison_type: String,
18    /// Exchange where condition is evaluated.
19    pub exchange_segment: ExchangeSegment,
20    /// Security ID of the instrument.
21    pub security_id: String,
22    /// Technical indicator name (e.g. `SMA_5`, `LTP`).
23    #[serde(skip_serializing_if = "Option::is_none")]
24    pub indicator_name: Option<String>,
25    /// Timeframe for indicator evaluation (`DAY`, `ONE_MIN`, `FIVE_MIN`, `FIFTEEN_MIN`).
26    #[serde(skip_serializing_if = "Option::is_none")]
27    pub time_frame: Option<String>,
28    /// Condition operator (e.g. `CROSSING_UP`, `GREATER_THAN`).
29    pub operator: String,
30    /// Value to compare indicator/price against.
31    #[serde(skip_serializing_if = "Option::is_none")]
32    pub comparing_value: Option<serde_json::Value>,
33    /// Second indicator name for indicator-vs-indicator comparisons.
34    #[serde(skip_serializing_if = "Option::is_none")]
35    pub comparing_indicator_name: Option<String>,
36    /// Alert expiry date (YYYY-MM-DD). Defaults to 1 year.
37    #[serde(skip_serializing_if = "Option::is_none")]
38    pub exp_date: Option<String>,
39    /// Trigger frequency (e.g. `ONCE`).
40    #[serde(skip_serializing_if = "Option::is_none")]
41    pub frequency: Option<String>,
42    /// User-provided note.
43    #[serde(skip_serializing_if = "Option::is_none")]
44    pub user_note: Option<String>,
45}
46
47// ---------------------------------------------------------------------------
48// Alert Order
49// ---------------------------------------------------------------------------
50
51/// Order to execute when the alert condition is met.
52#[derive(Debug, Clone, Serialize, Deserialize)]
53#[serde(rename_all = "camelCase")]
54pub struct AlertOrder {
55    pub transaction_type: TransactionType,
56    pub exchange_segment: ExchangeSegment,
57    pub product_type: ProductType,
58    pub order_type: OrderType,
59    pub security_id: String,
60    pub quantity: u64,
61    pub validity: Validity,
62    /// Price at which order is placed (as string in API).
63    pub price: String,
64    /// Disclosed quantity.
65    #[serde(skip_serializing_if = "Option::is_none")]
66    pub disc_quantity: Option<String>,
67    /// Trigger price for SL/SL-M.
68    #[serde(skip_serializing_if = "Option::is_none")]
69    pub trigger_price: Option<String>,
70}
71
72// ---------------------------------------------------------------------------
73// Place / Modify Conditional Trigger
74// ---------------------------------------------------------------------------
75
76/// Request body for placing or modifying a conditional trigger.
77///
78/// Used by `POST /v2/alerts/orders` and `PUT /v2/alerts/orders/{alertId}`.
79#[derive(Debug, Clone, Serialize)]
80#[serde(rename_all = "camelCase")]
81pub struct ConditionalTriggerRequest {
82    pub dhan_client_id: String,
83    /// Alert ID (only for modify requests).
84    #[serde(skip_serializing_if = "Option::is_none")]
85    pub alert_id: Option<String>,
86    pub condition: AlertCondition,
87    pub orders: Vec<AlertOrder>,
88}
89
90// ---------------------------------------------------------------------------
91// Conditional Trigger Response
92// ---------------------------------------------------------------------------
93
94/// Response from placing, modifying, or deleting a conditional trigger.
95#[derive(Debug, Clone, Deserialize)]
96#[serde(rename_all = "camelCase")]
97pub struct ConditionalTriggerResponse {
98    pub alert_id: String,
99    pub alert_status: String,
100}
101
102// ---------------------------------------------------------------------------
103// Conditional Trigger Detail
104// ---------------------------------------------------------------------------
105
106/// Full conditional trigger detail as returned by get endpoints.
107#[derive(Debug, Clone, Deserialize)]
108#[serde(rename_all = "camelCase")]
109pub struct ConditionalTriggerDetail {
110    pub alert_id: Option<String>,
111    pub alert_status: Option<String>,
112    pub created_time: Option<String>,
113    pub triggered_time: Option<String>,
114    #[serde(default)]
115    pub last_price: Option<serde_json::Value>,
116    pub condition: Option<AlertCondition>,
117    #[serde(default)]
118    pub orders: Vec<AlertOrder>,
119}