Skip to main content

rust_okx/api/market/
requests.rs

1use serde::Serialize;
2
3use crate::model::InstType;
4
5/// Request for [`get_ticker`](crate::api::market::Market::get_ticker) and
6/// [`get_block_ticker`](crate::api::market::Market::get_block_ticker).
7#[derive(Debug, Clone, Serialize)]
8pub struct InstIdRequest<'a> {
9    /// Instrument ID, e.g. `"BTC-USDT"`.
10    #[serde(rename = "instId")]
11    pub inst_id: &'a str,
12}
13
14/// Request for [`get_tickers`](crate::api::market::Market::get_tickers) and
15/// [`get_block_tickers`](crate::api::market::Market::get_block_tickers).
16#[derive(Debug, Clone, Serialize)]
17pub struct TickersRequest<'a> {
18    /// Instrument type.
19    #[serde(rename = "instType")]
20    pub inst_type: &'a InstType,
21    /// Instrument family filter (optional).
22    #[serde(rename = "instFamily", skip_serializing_if = "Option::is_none")]
23    pub inst_family: Option<&'a str>,
24}
25
26/// Request for [`get_index_tickers`](crate::api::market::Market::get_index_tickers).
27#[derive(Debug, Clone, Default, Serialize)]
28pub struct IndexTickersRequest<'a> {
29    /// Quote currency filter, e.g. `Some("USD")`.
30    #[serde(rename = "quoteCcy", skip_serializing_if = "Option::is_none")]
31    pub quote_ccy: Option<&'a str>,
32    /// Index instrument ID filter.
33    #[serde(rename = "instId", skip_serializing_if = "Option::is_none")]
34    pub inst_id: Option<&'a str>,
35}
36
37/// Request for [`get_orderbook`](crate::api::market::Market::get_orderbook).
38#[derive(Debug, Clone, Serialize)]
39pub struct OrderBookRequest<'a> {
40    /// Instrument ID.
41    #[serde(rename = "instId")]
42    pub inst_id: &'a str,
43    /// Depth (number of price levels per side). OKX default 1, max 400.
44    #[serde(skip_serializing_if = "Option::is_none")]
45    pub sz: Option<u32>,
46}
47
48/// Request for [`get_candlesticks`](crate::api::market::Market::get_candlesticks).
49#[derive(Debug, Clone, Serialize)]
50pub struct CandlesRequest<'a> {
51    /// Instrument ID.
52    #[serde(rename = "instId")]
53    pub inst_id: &'a str,
54    /// Bar size, e.g. `"1m"`, `"1H"`, `"1D"`. OKX default `"1m"`.
55    #[serde(skip_serializing_if = "Option::is_none")]
56    pub bar: Option<&'a str>,
57    /// Maximum number of bars (max 300).
58    #[serde(skip_serializing_if = "Option::is_none")]
59    pub limit: Option<u32>,
60}
61
62/// Request for [`get_trades`](crate::api::market::Market::get_trades).
63#[derive(Debug, Clone, Serialize)]
64pub struct TradesRequest<'a> {
65    /// Instrument ID.
66    #[serde(rename = "instId")]
67    pub inst_id: &'a str,
68    /// Maximum number of trades to return.
69    #[serde(skip_serializing_if = "Option::is_none")]
70    pub limit: Option<u32>,
71}
72
73/// Request for [`get_option_instrument_family_trades`](crate::api::market::Market::get_option_instrument_family_trades).
74#[derive(Debug, Clone, Serialize)]
75pub struct InstFamilyRequest<'a> {
76    /// Instrument family, e.g. `"BTC-USD"`.
77    #[serde(rename = "instFamily")]
78    pub inst_family: &'a str,
79}
80
81/// Request for [`get_index_components`](crate::api::market::Market::get_index_components).
82#[derive(Debug, Clone, Serialize)]
83pub struct IndexRequest<'a> {
84    /// Index symbol, e.g. `"BTC-USD"`.
85    pub index: &'a str,
86}
87
88/// Query parameters for historical/index/mark-price candlestick endpoints.
89#[derive(Debug, Clone, Serialize)]
90pub struct CandlesticksRequest {
91    #[serde(rename = "instId")]
92    inst_id: String,
93    #[serde(skip_serializing_if = "Option::is_none")]
94    after: Option<String>,
95    #[serde(skip_serializing_if = "Option::is_none")]
96    before: Option<String>,
97    #[serde(skip_serializing_if = "Option::is_none")]
98    bar: Option<String>,
99    #[serde(skip_serializing_if = "Option::is_none")]
100    limit: Option<u32>,
101}
102
103impl CandlesticksRequest {
104    /// Create a candlestick query for an instrument.
105    pub fn new(inst_id: impl Into<String>) -> Self {
106        Self {
107            inst_id: inst_id.into(),
108            after: None,
109            before: None,
110            bar: None,
111            limit: None,
112        }
113    }
114
115    /// Return records after this pagination cursor.
116    pub fn after(mut self, after: impl Into<String>) -> Self {
117        self.after = Some(after.into());
118        self
119    }
120
121    /// Return records before this pagination cursor.
122    pub fn before(mut self, before: impl Into<String>) -> Self {
123        self.before = Some(before.into());
124        self
125    }
126
127    /// Set the bar size, e.g. `1m`, `1H`, or `1D`.
128    pub fn bar(mut self, bar: impl Into<String>) -> Self {
129        self.bar = Some(bar.into());
130        self
131    }
132
133    /// Set the maximum number of rows to return.
134    pub fn limit(mut self, limit: u32) -> Self {
135        self.limit = Some(limit);
136        self
137    }
138}
139
140/// Query parameters for historical trades.
141#[derive(Debug, Clone, Serialize)]
142pub struct HistoryTradesRequest {
143    #[serde(rename = "instId")]
144    inst_id: String,
145    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
146    trade_type: Option<String>,
147    #[serde(skip_serializing_if = "Option::is_none")]
148    after: Option<String>,
149    #[serde(skip_serializing_if = "Option::is_none")]
150    before: Option<String>,
151    #[serde(skip_serializing_if = "Option::is_none")]
152    limit: Option<u32>,
153}
154
155impl HistoryTradesRequest {
156    /// Create a historical trades query for an instrument.
157    pub fn new(inst_id: impl Into<String>) -> Self {
158        Self {
159            inst_id: inst_id.into(),
160            trade_type: None,
161            after: None,
162            before: None,
163            limit: None,
164        }
165    }
166
167    /// Set the OKX trade type filter.
168    pub fn trade_type(mut self, trade_type: impl Into<String>) -> Self {
169        self.trade_type = Some(trade_type.into());
170        self
171    }
172
173    /// Return records after this pagination cursor.
174    pub fn after(mut self, after: impl Into<String>) -> Self {
175        self.after = Some(after.into());
176        self
177    }
178
179    /// Return records before this pagination cursor.
180    pub fn before(mut self, before: impl Into<String>) -> Self {
181        self.before = Some(before.into());
182        self
183    }
184
185    /// Set the maximum number of rows to return.
186    pub fn limit(mut self, limit: u32) -> Self {
187        self.limit = Some(limit);
188        self
189    }
190}