Skip to main content

dhan_rs/api/
traders_control.rs

1//! Trader's Control endpoints — Kill Switch, P&L Based Exit.
2
3use crate::client::DhanClient;
4use crate::error::{DhanError, Result};
5use crate::types::traders_control::*;
6
7impl DhanClient {
8    /// Activate or deactivate the kill switch for the current trading day.
9    ///
10    /// Pass `"ACTIVATE"` or `"DEACTIVATE"` as the `status` parameter.
11    ///
12    /// **Endpoint:** `POST /v2/killswitch?killSwitchStatus={status}`
13    pub async fn manage_kill_switch(&self, status: &str) -> Result<KillSwitchResponse> {
14        if !matches!(status, "ACTIVATE" | "DEACTIVATE") {
15            return Err(DhanError::InvalidArgument(
16                "kill switch status must be ACTIVATE or DEACTIVATE".into(),
17            ));
18        }
19        // The only accepted values are fixed protocol enums, so no caller
20        // input is interpolated into the query string.
21        let path = match status {
22            "ACTIVATE" => "/v2/killswitch?killSwitchStatus=ACTIVATE",
23            "DEACTIVATE" => "/v2/killswitch?killSwitchStatus=DEACTIVATE",
24            _ => unreachable!("status was validated above"),
25        };
26        self.post_without_body(path).await
27    }
28
29    /// Retrieve current kill switch status.
30    ///
31    /// **Endpoint:** `GET /v2/killswitch`
32    pub async fn get_kill_switch_status(&self) -> Result<KillSwitchResponse> {
33        self.get("/v2/killswitch").await
34    }
35
36    /// Configure P&L-based auto-exit for the current trading day.
37    ///
38    /// **Endpoint:** `POST /v2/pnlExit`
39    pub async fn set_pnl_exit(&self, req: &PnlExitRequest) -> Result<PnlExitResponse> {
40        req.validate()
41            .map_err(|message| DhanError::InvalidArgument(message.into()))?;
42        self.post("/v2/pnlExit", req).await
43    }
44
45    /// Disable the active P&L-based exit configuration.
46    ///
47    /// **Endpoint:** `DELETE /v2/pnlExit`
48    pub async fn stop_pnl_exit(&self) -> Result<PnlExitResponse> {
49        self.delete("/v2/pnlExit").await
50    }
51
52    /// Fetch the currently active P&L-based exit configuration.
53    ///
54    /// **Endpoint:** `GET /v2/pnlExit`
55    pub async fn get_pnl_exit(&self) -> Result<PnlExitConfig> {
56        self.get("/v2/pnlExit").await
57    }
58}