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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
use serde::Deserialize;
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct AttachAlgoOrderDetail {
#[serde(default)]
pub attach_algo_cl_ord_id: Option<String>,
#[serde(default)]
pub tp_trigger_px: Option<String>,
#[serde(default)]
pub tp_trigger_ratio: Option<String>,
#[serde(default)]
pub tp_ord_px: Option<String>,
#[serde(default)]
pub tp_ord_kind: Option<String>,
#[serde(default)]
pub sl_trigger_px: Option<String>,
#[serde(default)]
pub sl_trigger_ratio: Option<String>,
#[serde(default)]
pub sl_ord_px: Option<String>,
#[serde(default)]
pub tp_trigger_px_type: Option<String>,
#[serde(default)]
pub sl_trigger_px_type: Option<String>,
#[serde(default)]
pub sz: Option<String>,
#[serde(default)]
pub amend_px_on_trigger_type: Option<String>,
#[serde(default)]
pub callback_ratio: Option<String>,
#[serde(default)]
pub callback_spread: Option<String>,
#[serde(default)]
pub active_px: Option<String>,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct AdvChaseDetail {
#[serde(default)]
pub chase_type: Option<String>,
#[serde(default)]
pub chase_val: Option<String>,
#[serde(default)]
pub max_chase_type: Option<String>,
#[serde(default)]
pub max_chase_val: Option<String>,
}
/// Result from placing a single order.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct OrderResult {
/// Client Order ID as assigned by the client.
pub cl_ord_id: String,
/// Order ID assigned by OKX.
pub ord_id: String,
/// Order tag.
pub tag: String,
/// Timestamp when the order request was received, Unix timestamp in milliseconds.
pub ts: String,
/// The code of the event execution result, 0 means success.
pub s_code: String,
#[serde(default)]
pub sub_code: Option<String>,
/// Rejection or success message of event execution.
pub s_msg: String,
}
/// Result from cancelling a single order.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct CancelledOrder {
/// Client Order ID as assigned by the client.
pub cl_ord_id: String,
/// Order ID.
pub ord_id: String,
/// The code of the event execution result, 0 means success.
pub s_code: String,
#[serde(default)]
pub sub_code: Option<String>,
/// Rejection or success message of event execution.
pub s_msg: String,
}
/// Result from amending an order.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct AmendedOrder {
/// Client Order ID as assigned by the client.
pub cl_ord_id: String,
/// Order ID.
pub ord_id: String,
/// Client Request ID as assigned by the client for order amendment.
pub req_id: String,
/// The code of the event execution result, 0 means success.
pub s_code: String,
#[serde(default)]
pub sub_code: Option<String>,
/// Rejection or success message of event execution.
pub s_msg: String,
}
/// Full details of an order.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct OrderDetails {
/// Instrument type.
pub inst_type: String,
/// Instrument ID.
pub inst_id: String,
/// Margin currency.
pub ccy: String,
/// Order ID.
pub ord_id: String,
/// Client Order ID as assigned by the client.
pub cl_ord_id: String,
/// Order tag.
pub tag: String,
/// Price.
pub px: String,
/// Quantity to buy or sell.
pub sz: String,
/// Profit and loss, applicable to closing orders.
pub pnl: String,
/// Order type.
pub ord_type: String,
/// Order side: buy, sell.
pub side: String,
/// Position side: net, long, short.
pub pos_side: String,
/// Trade mode: cross, isolated, cash.
pub td_mode: String,
/// Accumulated fill quantity.
pub acc_fill_sz: String,
/// Last filled price.
pub fill_px: String,
/// Last trade ID.
pub trade_id: String,
/// Last filled quantity.
pub fill_sz: String,
/// Last filled time.
pub fill_time: String,
/// Order state: canceled, live, partially_filled, filled, mmp_canceled.
pub state: String,
/// Average filled price. If none is filled, it will return "".
pub avg_px: String,
/// Leverage. Not applicable to SPOT, empty if not applicable.
pub lever: String,
/// Fee currency.
pub fee_ccy: String,
/// Fee and target rebate. Negative value means fee charged; positive means rebate.
pub fee: String,
/// Rebate currency.
pub rebate_ccy: String,
/// Rebate amount.
pub rebate: String,
/// Order source.
pub source: String,
/// Category: normal, twap, adl, full_liquidation, partial_liquidation, delivery, ddh.
pub category: String,
#[serde(default)]
pub outcome: Option<String>,
/// Update time, Unix timestamp in milliseconds.
pub u_time: String,
/// Creation time, Unix timestamp in milliseconds.
pub c_time: String,
/// Cancel source. Valid when the order is canceled.
pub cancel_source: String,
/// Take-profit trigger price.
pub tp_trigger_px: String,
/// Take-profit trigger price type: last, index, mark.
pub tp_trigger_px_type: String,
/// Take-profit order price.
pub tp_ord_px: String,
/// Stop-loss trigger price.
pub sl_trigger_px: String,
/// Stop-loss trigger price type: last, index, mark.
pub sl_trigger_px_type: String,
/// Stop-loss order price.
pub sl_ord_px: String,
/// Self trade prevention ID.
pub stp_id: String,
/// Self trade prevention mode.
pub stp_mode: String,
/// Whether the order can only reduce position size.
pub reduce_only: String,
#[serde(default)]
pub attach_algo_ords: Option<Vec<AttachAlgoOrderDetail>>,
}
/// Fill / trade record.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Fill {
/// Instrument type.
pub inst_type: String,
/// Instrument ID.
pub inst_id: String,
/// Trade ID.
pub trade_id: String,
/// Order ID.
pub ord_id: String,
/// Client Order ID as assigned by the client.
pub cl_ord_id: String,
/// Bill ID.
pub bill_id: String,
/// Order tag.
pub tag: String,
/// Last filled price.
pub fill_px: String,
/// Last filled quantity.
pub fill_sz: String,
/// Order side: buy, sell.
pub side: String,
/// Position side: net, long, short.
pub pos_side: String,
/// Execution type: T (taker), M (maker).
pub exec_type: String,
/// Fee currency.
pub fee_ccy: String,
/// Fee. Negative means fee charged; positive means rebate.
pub fee: String,
/// Timestamp of the data generation, Unix timestamp in milliseconds.
pub ts: String,
/// Last filled time.
pub fill_time: String,
/// Last filled profit and loss.
pub fill_pnl: String,
/// Implied volatility when filled, only applicable to options.
pub fill_px_vol: String,
/// Options price when filled, in USD, only applicable to options.
pub fill_px_usd: String,
/// Mark volatility when filled, only applicable to options.
pub fill_mark_vol: String,
/// Forward price when filled, only applicable to options.
pub fill_fwd_px: String,
/// Mark price when filled.
pub fill_mark_px: String,
}
/// Result from placing an algo order.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct AlgoOrderResult {
/// Algo order ID.
pub algo_id: String,
/// Client-supplied Algo Order ID.
pub algo_cl_ord_id: String,
/// The code of the event execution result, 0 means success.
pub s_code: String,
/// Rejection or success message of event execution.
pub s_msg: String,
}
/// Full details of an algo order.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct AlgoOrderDetails {
/// Instrument type.
pub inst_type: String,
/// Instrument ID.
pub inst_id: String,
/// Order ID. Only applicable to TP/SL order that has been triggered.
pub ord_id: String,
/// Algo order ID.
pub algo_id: String,
/// Client Order ID as assigned by the client.
pub cl_ord_id: String,
/// Margin currency.
pub ccy: String,
/// Quantity to buy or sell.
pub sz: String,
/// Algo order type.
pub ord_type: String,
/// Order side: buy, sell.
pub side: String,
/// Position side: net, long, short.
pub pos_side: String,
/// Trade mode: cross, isolated, cash.
pub td_mode: String,
/// Algo order state.
pub state: String,
/// Leverage.
pub lever: String,
/// Take-profit trigger price.
pub tp_trigger_px: String,
/// Take-profit order price.
pub tp_ord_px: String,
/// Stop-loss trigger price.
pub sl_trigger_px: String,
/// Stop-loss order price.
pub sl_ord_px: String,
/// Trigger price.
pub trigger_px: String,
/// Order price.
pub ord_px: String,
/// Actual order quantity.
pub actual_sz: String,
/// Actual order price.
pub actual_px: String,
/// Actual order side.
pub actual_side: String,
/// Trigger time, Unix timestamp in milliseconds.
pub trigger_time: String,
/// Creation time, Unix timestamp in milliseconds.
pub c_time: String,
#[serde(default)]
pub attach_algo_ords: Option<Vec<AttachAlgoOrderDetail>>,
#[serde(default)]
pub advance_ord_type: Option<String>,
#[serde(default)]
pub adv_chase_params: Option<Vec<AdvChaseDetail>>,
#[serde(default)]
pub sub_algo_id_list: Option<Vec<String>>,
}
/// Result from mass cancel operation.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct MassCancelResult {
/// Whether the mass cancel was successful. "true" or "false".
pub result: String,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn current_trade_response_fields_deserialize() {
let result: OrderResult = serde_json::from_value(serde_json::json!({
"clOrdId": "client-1",
"ordId": "1",
"tag": "tag",
"ts": "1",
"sCode": "1",
"subCode": "51008",
"sMsg": "failed"
}))
.unwrap();
let algo: AlgoOrderDetails = serde_json::from_value(serde_json::json!({
"instType": "SWAP",
"instId": "BTC-USDT-SWAP",
"ordId": "1",
"algoId": "2",
"clOrdId": "client-1",
"ccy": "USDT",
"sz": "1",
"ordType": "trigger",
"side": "buy",
"posSide": "net",
"tdMode": "cross",
"state": "effective",
"lever": "1",
"tpTriggerPx": "",
"tpOrdPx": "",
"slTriggerPx": "",
"slOrdPx": "",
"triggerPx": "100000",
"ordPx": "-1",
"actualSz": "1",
"actualPx": "100001",
"actualSide": "buy",
"triggerTime": "1",
"cTime": "1",
"attachAlgoOrds": [{
"callbackRatio": "0.05",
"activePx": "100000"
}],
"advanceOrdType": "chase",
"advChaseParams": [{
"chaseType": "distance",
"chaseVal": "1"
}],
"subAlgoIdList": ["3", "4"]
}))
.unwrap();
assert_eq!(result.sub_code.as_deref(), Some("51008"));
assert_eq!(algo.advance_ord_type.as_deref(), Some("chase"));
assert_eq!(
algo.attach_algo_ords.unwrap()[0].callback_ratio.as_deref(),
Some("0.05")
);
assert_eq!(algo.sub_algo_id_list, Some(vec!["3".into(), "4".into()]));
}
}