Skip to main content

rust_okx/api/account/requests/
risk.rs

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