dhan_rs/types/option_chain.rs
1#![allow(missing_docs)]
2//! Option Chain types — chain data, greeks, expiry list.
3
4use std::collections::HashMap;
5
6use serde::{Deserialize, Serialize};
7
8// ---------------------------------------------------------------------------
9// Request
10// ---------------------------------------------------------------------------
11
12/// Request body for fetching the option chain.
13///
14/// Used by `POST /v2/optionchain`.
15///
16/// Note: field names use PascalCase in the API.
17#[derive(Debug, Clone, Serialize)]
18#[allow(non_snake_case)]
19pub struct OptionChainRequest {
20 /// Security ID of the underlying instrument.
21 pub UnderlyingScrip: u64,
22 /// Exchange & segment of the underlying.
23 pub UnderlyingSeg: String,
24 /// Expiry date (YYYY-MM-DD).
25 pub Expiry: String,
26}
27
28/// Request body for fetching the expiry list.
29///
30/// Used by `POST /v2/optionchain/expirylist`.
31#[derive(Debug, Clone, Serialize)]
32#[allow(non_snake_case)]
33pub struct ExpiryListRequest {
34 /// Security ID of the underlying instrument.
35 pub UnderlyingScrip: u64,
36 /// Exchange & segment of the underlying.
37 pub UnderlyingSeg: String,
38}
39
40// ---------------------------------------------------------------------------
41// Greeks
42// ---------------------------------------------------------------------------
43
44/// Option greeks for a single strike.
45#[derive(Debug, Clone, Deserialize)]
46pub struct Greeks {
47 pub delta: f64,
48 pub theta: f64,
49 pub gamma: f64,
50 pub vega: f64,
51}
52
53// ---------------------------------------------------------------------------
54// Option Data (per CE/PE)
55// ---------------------------------------------------------------------------
56
57/// Data for a single call or put at a given strike.
58#[derive(Debug, Clone, Deserialize)]
59pub struct OptionData {
60 #[serde(default)]
61 pub average_price: Option<f64>,
62 pub greeks: Option<Greeks>,
63 #[serde(default)]
64 pub implied_volatility: Option<f64>,
65 pub last_price: f64,
66 #[serde(default)]
67 pub oi: Option<i64>,
68 #[serde(default)]
69 pub previous_close_price: Option<f64>,
70 #[serde(default)]
71 pub previous_oi: Option<i64>,
72 #[serde(default)]
73 pub previous_volume: Option<i64>,
74 #[serde(default)]
75 pub security_id: Option<u64>,
76 #[serde(default)]
77 pub top_ask_price: Option<f64>,
78 #[serde(default)]
79 pub top_ask_quantity: Option<i64>,
80 #[serde(default)]
81 pub top_bid_price: Option<f64>,
82 #[serde(default)]
83 pub top_bid_quantity: Option<i64>,
84 #[serde(default)]
85 pub volume: Option<i64>,
86}
87
88// ---------------------------------------------------------------------------
89// Strike Data
90// ---------------------------------------------------------------------------
91
92/// Call and Put data at a given strike price.
93#[derive(Debug, Clone, Deserialize)]
94pub struct StrikeData {
95 /// Call option data (may be absent if no CE at this strike).
96 pub ce: Option<OptionData>,
97 /// Put option data (may be absent if no PE at this strike).
98 pub pe: Option<OptionData>,
99}
100
101// ---------------------------------------------------------------------------
102// Option Chain Response
103// ---------------------------------------------------------------------------
104
105/// Inner data envelope of the option chain response.
106#[derive(Debug, Clone, Deserialize)]
107pub struct OptionChainData {
108 /// LTP of the underlying.
109 pub last_price: f64,
110 /// Strike-wise option chain. Keys are strike prices as strings (e.g. `"25650.000000"`).
111 pub oc: HashMap<String, StrikeData>,
112}
113
114/// Response from `POST /v2/optionchain`.
115#[derive(Debug, Clone, Deserialize)]
116pub struct OptionChainResponse {
117 pub data: OptionChainData,
118 pub status: String,
119}
120
121// ---------------------------------------------------------------------------
122// Expiry List Response
123// ---------------------------------------------------------------------------
124
125/// Response from `POST /v2/optionchain/expirylist`.
126#[derive(Debug, Clone, Deserialize)]
127pub struct ExpiryListResponse {
128 /// List of expiry dates (YYYY-MM-DD).
129 pub data: Vec<String>,
130 pub status: String,
131}