1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
use crate::client::OkxClient;
use crate::error::Error;
use crate::model::InstType;
use crate::transport::Transport;
use super::endpoints::*;
use super::internal::*;
use super::requests::*;
use super::responses::*;
/// Accessor for the public market-data endpoints.
///
/// Obtain one via [`OkxClient::market`](crate::OkxClient::market).
pub struct Market<'a, T> {
client: &'a OkxClient<T>,
}
impl<'a, T: Transport> Market<'a, T> {
pub(crate) fn new(client: &'a OkxClient<T>) -> Self {
Self { client }
}
/// Retrieve the latest ticker for a single instrument.
///
/// `GET /api/v5/market/ticker`. Public (unauthenticated). The returned
/// vector contains exactly one [`Ticker`].
///
/// # Errors
///
/// Returns [`Error::Api`] on a non-zero OKX code, or
/// [`Error::Transport`]/[`Error::Decode`] on transport/parsing failure.
pub async fn get_ticker(&self, inst_id: &str) -> Result<Vec<Ticker>, Error> {
let query = InstIdQuery { inst_id };
self.client.get(TICKER, &query, false).await
}
/// Retrieve tickers for an instrument type.
///
/// `GET /api/v5/market/tickers`. Public. `underlying` and `inst_family`
/// are useful for derivatives and omitted when `None`.
///
/// # Errors
///
/// See [`get_ticker`](Self::get_ticker).
pub async fn get_tickers(
&self,
inst_type: InstType,
inst_family: Option<&str>,
) -> Result<Vec<Ticker>, Error> {
let query = TickersQuery {
inst_type: &inst_type,
inst_family,
};
self.client.get(TICKERS, &query, false).await
}
/// Retrieve index tickers.
///
/// `GET /api/v5/market/index-tickers`. Public. Filter by quote currency,
/// index instrument ID, or neither.
///
/// # Errors
///
/// See [`get_ticker`](Self::get_ticker).
pub async fn get_index_tickers(
&self,
quote_ccy: Option<&str>,
inst_id: Option<&str>,
) -> Result<Vec<IndexTicker>, Error> {
let query = IndexTickersQuery { quote_ccy, inst_id };
self.client.get(INDEX_TICKERS, &query, false).await
}
/// Retrieve the order book for an instrument.
///
/// `GET /api/v5/market/books`. `depth` is the number of levels per side
/// (OKX default 1, max 400). Public.
///
/// # Errors
///
/// See [`get_ticker`](Self::get_ticker).
pub async fn get_orderbook(
&self,
inst_id: &str,
depth: Option<u32>,
) -> Result<Vec<OrderBook>, Error> {
let query = OrderBookQuery { inst_id, sz: depth };
self.client.get(BOOKS, &query, false).await
}
/// Retrieve candlestick (OHLCV) data.
///
/// `GET /api/v5/market/candles`. `bar` is the bar size, e.g. `1m`, `1H`,
/// `1D` (OKX default `1m`). `limit` caps the number of bars (max 300).
/// Public.
///
/// # Errors
///
/// See [`get_ticker`](Self::get_ticker).
pub async fn get_candlesticks(
&self,
inst_id: &str,
bar: Option<&str>,
limit: Option<u32>,
) -> Result<Vec<Candle>, Error> {
let query = CandlesQuery {
inst_id,
bar,
limit,
};
self.client.get(CANDLES, &query, false).await
}
/// Retrieve historical candlestick data for top currencies.
///
/// `GET /api/v5/market/history-candles`. Public.
///
/// # Errors
///
/// See [`get_ticker`](Self::get_ticker).
pub async fn get_history_candlesticks(
&self,
request: &CandlesticksRequest,
) -> Result<Vec<Candle>, Error> {
self.client.get(HISTORY_CANDLES, request, false).await
}
/// Retrieve index candlestick data.
///
/// `GET /api/v5/market/index-candles`. Public.
///
/// # Errors
///
/// See [`get_ticker`](Self::get_ticker).
pub async fn get_index_candlesticks(
&self,
request: &CandlesticksRequest,
) -> Result<Vec<IndexCandle>, Error> {
self.client.get(INDEX_CANDLES, request, false).await
}
/// Retrieve mark-price candlestick data.
///
/// `GET /api/v5/market/mark-price-candles`. Public.
///
/// # Errors
///
/// See [`get_ticker`](Self::get_ticker).
pub async fn get_mark_price_candlesticks(
&self,
request: &CandlesticksRequest,
) -> Result<Vec<IndexCandle>, Error> {
self.client.get(MARK_PRICE_CANDLES, request, false).await
}
/// Retrieve recent trades for an instrument.
///
/// `GET /api/v5/market/trades`. Public.
///
/// # Errors
///
/// See [`get_ticker`](Self::get_ticker).
pub async fn get_trades(
&self,
inst_id: &str,
limit: Option<u32>,
) -> Result<Vec<MarketTrade>, Error> {
let query = TradesQuery { inst_id, limit };
self.client.get(TRADES, &query, false).await
}
/// Retrieve historical trades for an instrument.
///
/// `GET /api/v5/market/history-trades`. Public.
///
/// # Errors
///
/// See [`get_ticker`](Self::get_ticker).
pub async fn get_history_trades(
&self,
request: &HistoryTradesRequest,
) -> Result<Vec<MarketTrade>, Error> {
self.client.get(HISTORY_TRADES, request, false).await
}
/// Retrieve OKX platform 24-hour volume.
///
/// `GET /api/v5/market/platform-24-volume`. Public.
///
/// # Errors
///
/// See [`get_ticker`](Self::get_ticker).
pub async fn get_platform_24_volume(&self) -> Result<Vec<PlatformVolume>, Error> {
self.client.get(PLATFORM_24_VOLUME, &NoQuery, false).await
}
/// Retrieve index components.
///
/// `GET /api/v5/market/index-components`. Public.
///
/// # Errors
///
/// See [`get_ticker`](Self::get_ticker).
pub async fn get_index_components(&self, index: &str) -> Result<Vec<IndexComponents>, Error> {
let query = IndexComponentsQuery { index };
self.client.get(INDEX_COMPONENTS, &query, false).await
}
/// Retrieve the USD/CNY exchange rate used by OKX.
///
/// `GET /api/v5/market/exchange-rate`. Public.
///
/// # Errors
///
/// See [`get_ticker`](Self::get_ticker).
pub async fn get_exchange_rate(&self) -> Result<Vec<ExchangeRate>, Error> {
self.client.get(EXCHANGE_RATE, &NoQuery, false).await
}
/// Retrieve a block-trading ticker for a single instrument.
///
/// `GET /api/v5/market/block-ticker`. Public.
///
/// # Errors
///
/// See [`get_ticker`](Self::get_ticker).
pub async fn get_block_ticker(&self, inst_id: &str) -> Result<Vec<BlockTicker>, Error> {
let query = InstIdQuery { inst_id };
self.client.get(BLOCK_TICKER, &query, false).await
}
/// Retrieve block-trading tickers for an instrument type.
///
/// `GET /api/v5/market/block-tickers`. Public.
///
/// # Errors
///
/// See [`get_ticker`](Self::get_ticker).
pub async fn get_block_tickers(
&self,
inst_type: InstType,
inst_family: Option<&str>,
) -> Result<Vec<BlockTicker>, Error> {
let query = TickersQuery {
inst_type: &inst_type,
inst_family,
};
self.client.get(BLOCK_TICKERS, &query, false).await
}
/// Retrieve option trades aggregated by instrument family.
///
/// `GET /api/v5/market/option/instrument-family-trades`. Public.
///
/// # Errors
///
/// See [`get_ticker`](Self::get_ticker).
pub async fn get_option_instrument_family_trades(
&self,
inst_family: &str,
) -> Result<Vec<OptionFamilyTradeGroup>, Error> {
let query = InstFamilyQuery { inst_family };
self.client
.get(OPTION_INSTRUMENT_FAMILY_TRADES, &query, false)
.await
}
}