Skip to main content

rust_okx/api/account/requests/
risk.rs

1use serde::Serialize;
2
3use crate::model::InstType;
4
5/// A simulated position used by position-builder and simulated-margin requests.
6#[derive(Debug, Clone, Serialize)]
7pub struct SimulatedPosition {
8    #[serde(rename = "instId")]
9    inst_id: String,
10    #[serde(skip_serializing_if = "Option::is_none")]
11    pos: Option<String>,
12    #[serde(rename = "avgPx", skip_serializing_if = "Option::is_none")]
13    avg_px: Option<String>,
14    #[serde(skip_serializing_if = "Option::is_none")]
15    lever: Option<String>,
16}
17
18impl SimulatedPosition {
19    /// Create a simulated position for an instrument.
20    pub fn new(inst_id: impl Into<String>) -> Self {
21        Self {
22            inst_id: inst_id.into(),
23            pos: None,
24            avg_px: None,
25            lever: None,
26        }
27    }
28
29    /// Set the simulated position size.
30    pub fn position(mut self, pos: impl Into<String>) -> Self {
31        self.pos = Some(pos.into());
32        self
33    }
34
35    /// Set the simulated average price.
36    pub fn average_price(mut self, avg_px: impl Into<String>) -> Self {
37        self.avg_px = Some(avg_px.into());
38        self
39    }
40
41    /// Set the simulated leverage.
42    pub fn leverage(mut self, lever: impl Into<String>) -> Self {
43        self.lever = Some(lever.into());
44        self
45    }
46}
47
48/// A simulated asset used by position-builder requests.
49#[derive(Debug, Clone, Serialize)]
50pub struct SimulatedAsset {
51    ccy: String,
52    #[serde(skip_serializing_if = "Option::is_none")]
53    eq: Option<String>,
54}
55
56impl SimulatedAsset {
57    /// Create a simulated asset for a currency.
58    pub fn new(ccy: impl Into<String>) -> Self {
59        Self {
60            ccy: ccy.into(),
61            eq: None,
62        }
63    }
64
65    /// Set the simulated equity.
66    pub fn equity(mut self, eq: impl Into<String>) -> Self {
67        self.eq = Some(eq.into());
68        self
69    }
70}
71
72/// Request body for simulated margin calculation.
73#[derive(Debug, Clone, Default, Serialize)]
74pub struct SimulatedMarginRequest {
75    #[serde(rename = "instType", skip_serializing_if = "Option::is_none")]
76    inst_type: Option<InstType>,
77    #[serde(rename = "inclRealPos", skip_serializing_if = "Option::is_none")]
78    include_real_positions: Option<bool>,
79    #[serde(rename = "spotOffsetType", skip_serializing_if = "Option::is_none")]
80    spot_offset_type: Option<String>,
81    #[serde(rename = "simPos", skip_serializing_if = "Option::is_none")]
82    simulated_positions: Option<Vec<SimulatedPosition>>,
83}
84
85impl SimulatedMarginRequest {
86    /// Create an empty simulated-margin request.
87    pub fn new() -> Self {
88        Self::default()
89    }
90
91    /// Set the instrument type.
92    pub fn inst_type(mut self, inst_type: InstType) -> Self {
93        self.inst_type = Some(inst_type);
94        self
95    }
96
97    /// Set whether real positions and equity are included.
98    pub fn include_real_positions(mut self, include_real_positions: bool) -> Self {
99        self.include_real_positions = Some(include_real_positions);
100        self
101    }
102
103    /// Set the spot offset type.
104    pub fn spot_offset_type(mut self, spot_offset_type: impl Into<String>) -> Self {
105        self.spot_offset_type = Some(spot_offset_type.into());
106        self
107    }
108
109    /// Set simulated positions.
110    pub fn simulated_positions(mut self, simulated_positions: Vec<SimulatedPosition>) -> Self {
111        self.simulated_positions = Some(simulated_positions);
112        self
113    }
114}
115
116/// Query parameters for account position tiers.
117#[derive(Debug, Clone, Default, Serialize)]
118pub struct AccountPositionTiersRequest {
119    #[serde(rename = "instType", skip_serializing_if = "Option::is_none")]
120    inst_type: Option<InstType>,
121    #[serde(rename = "uly", skip_serializing_if = "Option::is_none")]
122    underlying: Option<String>,
123    #[serde(rename = "instFamily", skip_serializing_if = "Option::is_none")]
124    inst_family: Option<String>,
125}
126
127impl AccountPositionTiersRequest {
128    /// Create an empty account position-tiers query.
129    pub fn new() -> Self {
130        Self::default()
131    }
132
133    /// Set the instrument type filter.
134    pub fn inst_type(mut self, inst_type: InstType) -> Self {
135        self.inst_type = Some(inst_type);
136        self
137    }
138
139    /// Set the underlying filter.
140    pub fn underlying(mut self, underlying: impl Into<String>) -> Self {
141        self.underlying = Some(underlying.into());
142        self
143    }
144
145    /// Set the instrument family filter.
146    pub fn inst_family(mut self, inst_family: impl Into<String>) -> Self {
147        self.inst_family = Some(inst_family.into());
148        self
149    }
150}
151
152/// Request body for position builder.
153#[derive(Debug, Clone, Default, Serialize)]
154pub struct PositionBuilderRequest {
155    #[serde(rename = "acctLv", skip_serializing_if = "Option::is_none")]
156    acct_lv: Option<String>,
157    #[serde(rename = "inclRealPosAndEq", skip_serializing_if = "Option::is_none")]
158    include_real_positions_and_equity: Option<bool>,
159    #[serde(skip_serializing_if = "Option::is_none")]
160    lever: Option<String>,
161    #[serde(rename = "greeksType", skip_serializing_if = "Option::is_none")]
162    greeks_type: Option<String>,
163    #[serde(rename = "simPos", skip_serializing_if = "Option::is_none")]
164    simulated_positions: Option<Vec<SimulatedPosition>>,
165    #[serde(rename = "simAsset", skip_serializing_if = "Option::is_none")]
166    simulated_assets: Option<Vec<SimulatedAsset>>,
167    #[serde(rename = "idxVol", skip_serializing_if = "Option::is_none")]
168    index_volatility: Option<String>,
169}
170
171impl PositionBuilderRequest {
172    /// Create an empty position-builder request.
173    pub fn new() -> Self {
174        Self::default()
175    }
176
177    /// Set the account level.
178    pub fn account_level(mut self, acct_lv: impl Into<String>) -> Self {
179        self.acct_lv = Some(acct_lv.into());
180        self
181    }
182
183    /// Set whether real positions and equity are included.
184    pub fn include_real_positions_and_equity(mut self, include: bool) -> Self {
185        self.include_real_positions_and_equity = Some(include);
186        self
187    }
188
189    /// Set leverage.
190    pub fn leverage(mut self, lever: impl Into<String>) -> Self {
191        self.lever = Some(lever.into());
192        self
193    }
194
195    /// Set greeks display type.
196    pub fn greeks_type(mut self, greeks_type: impl Into<String>) -> Self {
197        self.greeks_type = Some(greeks_type.into());
198        self
199    }
200
201    /// Set simulated positions.
202    pub fn simulated_positions(mut self, simulated_positions: Vec<SimulatedPosition>) -> Self {
203        self.simulated_positions = Some(simulated_positions);
204        self
205    }
206
207    /// Set simulated assets.
208    pub fn simulated_assets(mut self, simulated_assets: Vec<SimulatedAsset>) -> Self {
209        self.simulated_assets = Some(simulated_assets);
210        self
211    }
212
213    /// Set index volatility.
214    pub fn index_volatility(mut self, index_volatility: impl Into<String>) -> Self {
215        self.index_volatility = Some(index_volatility.into());
216        self
217    }
218}