dhan-rs 0.1.7

Unofficial Rust client library for the DhanHQ Broker API v2 — orders, market data, WebSocket feeds, and more
Documentation
#![allow(missing_docs)]
//! Order management types.

use serde::{Deserialize, Serialize};

use crate::types::enums::*;

// ---------------------------------------------------------------------------
// Place Order
// ---------------------------------------------------------------------------

/// Request body for placing a new order.
///
/// Used by `POST /v2/orders` and `POST /v2/orders/slicing`.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PlaceOrderRequest {
    /// User-specific identification generated by Dhan.
    pub dhan_client_id: String,
    /// User/partner generated tracking ID.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub correlation_id: Option<String>,
    /// Buy or Sell.
    pub transaction_type: TransactionType,
    /// Exchange & segment.
    pub exchange_segment: ExchangeSegment,
    /// Product type.
    pub product_type: ProductType,
    /// Order type.
    pub order_type: OrderType,
    /// Order validity.
    pub validity: Validity,
    /// Exchange standard security ID.
    pub security_id: String,
    /// Number of shares.
    pub quantity: u64,
    /// Number of shares visible (>30% of quantity).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub disclosed_quantity: Option<u64>,
    /// Price at which order is placed.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub price: Option<f64>,
    /// Trigger price for SL/SL-M orders.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub trigger_price: Option<f64>,
    /// Flag for after-market orders.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub after_market_order: Option<bool>,
    /// Timing for after-market order.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub amo_time: Option<AmoTime>,
    /// Bracket order target price change.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bo_profit_value: Option<f64>,
    /// Bracket order stop-loss price change.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bo_stop_loss_value: Option<f64>,
}

// ---------------------------------------------------------------------------
// Modify Order
// ---------------------------------------------------------------------------

/// Request body for modifying a pending order.
///
/// Used by `PUT /v2/orders/{order-id}`.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ModifyOrderRequest {
    /// User-specific identification generated by Dhan.
    pub dhan_client_id: String,
    /// Order ID to modify.
    pub order_id: String,
    /// Order type.
    pub order_type: OrderType,
    /// Leg name (for BO/CO orders).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub leg_name: Option<LegName>,
    /// Quantity to modify.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub quantity: Option<u64>,
    /// Price to modify.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub price: Option<f64>,
    /// Disclosed quantity.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub disclosed_quantity: Option<u64>,
    /// Trigger price for SL/SL-M.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub trigger_price: Option<f64>,
    /// Validity.
    pub validity: Validity,
}

// ---------------------------------------------------------------------------
// Order Response
// ---------------------------------------------------------------------------

/// Response from placing, modifying, or cancelling an order.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OrderResponse {
    /// Order-specific identification generated by Dhan.
    pub order_id: String,
    /// Last updated status of the order.
    pub order_status: String,
}

// ---------------------------------------------------------------------------
// Order Detail (Order Book entry)
// ---------------------------------------------------------------------------

/// Full order detail as returned by the order book.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OrderDetail {
    pub dhan_client_id: Option<String>,
    pub order_id: Option<String>,
    pub exchange_order_id: Option<String>,
    pub correlation_id: Option<String>,
    pub order_status: Option<String>,
    pub transaction_type: Option<String>,
    pub exchange_segment: Option<String>,
    pub product_type: Option<String>,
    pub order_type: Option<String>,
    pub validity: Option<String>,
    pub trading_symbol: Option<String>,
    pub security_id: Option<String>,
    #[serde(default)]
    pub quantity: Option<u64>,
    #[serde(default)]
    pub disclosed_quantity: Option<u64>,
    #[serde(default)]
    pub price: Option<f64>,
    #[serde(default)]
    pub trigger_price: Option<f64>,
    #[serde(default)]
    pub after_market_order: Option<bool>,
    #[serde(default)]
    pub bo_profit_value: Option<f64>,
    #[serde(default)]
    pub bo_stop_loss_value: Option<f64>,
    pub leg_name: Option<String>,
    pub create_time: Option<String>,
    pub update_time: Option<String>,
    pub exchange_time: Option<String>,
    pub drv_expiry_date: Option<String>,
    pub drv_option_type: Option<String>,
    #[serde(default)]
    pub drv_strike_price: Option<f64>,
    pub oms_error_code: Option<String>,
    pub oms_error_description: Option<String>,
    pub algo_id: Option<String>,
    #[serde(default)]
    pub remaining_quantity: Option<u64>,
    #[serde(default)]
    pub average_traded_price: Option<f64>,
    #[serde(default)]
    pub filled_qty: Option<u64>,
}

// ---------------------------------------------------------------------------
// Trade Detail (Trade Book entry)
// ---------------------------------------------------------------------------

/// Trade detail as returned by the trade book.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TradeDetail {
    pub dhan_client_id: Option<String>,
    pub order_id: Option<String>,
    pub exchange_order_id: Option<String>,
    pub exchange_trade_id: Option<String>,
    pub transaction_type: Option<String>,
    pub exchange_segment: Option<String>,
    pub product_type: Option<String>,
    pub order_type: Option<String>,
    pub trading_symbol: Option<String>,
    pub custom_symbol: Option<String>,
    pub security_id: Option<String>,
    #[serde(default)]
    pub traded_quantity: Option<u64>,
    #[serde(default)]
    pub traded_price: Option<f64>,
    pub create_time: Option<String>,
    pub update_time: Option<String>,
    pub exchange_time: Option<String>,
    pub drv_expiry_date: Option<String>,
    pub drv_option_type: Option<String>,
    #[serde(default)]
    pub drv_strike_price: Option<f64>,
}