okx-client 0.1.2

Async Rust client for the OKX exchange API v5
Documentation
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
use serde::Serialize;

use crate::types::enums::*;

fn serialize_csv<S>(values: &[String], serializer: S) -> Result<S::Ok, S::Error>
where
    S: serde::Serializer,
{
    serializer.serialize_str(&values.join(","))
}

#[derive(Debug, Clone, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct AttachAlgoOrdRequest {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub attach_algo_cl_ord_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tp_trigger_px: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tp_trigger_ratio: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tp_ord_px: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tp_ord_kind: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sl_trigger_px: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sl_trigger_ratio: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sl_ord_px: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tp_trigger_px_type: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sl_trigger_px_type: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sz: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub amend_px_on_trigger_type: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub callback_ratio: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub callback_spread: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub active_px: Option<String>,
}

#[derive(Debug, Clone, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct AmendAttachedTrailingStop {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub new_callback_ratio: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub new_callback_spread: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub new_active_px: Option<String>,
}

#[derive(Debug, Clone, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct AdvChaseParams {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub chase_type: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub chase_val: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_chase_type: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_chase_val: Option<String>,
}

#[derive(Debug, Clone, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct AdvChaseAmendParams {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub new_chase_val: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub new_max_chase_val: Option<String>,
}

/// Place a single order.
#[derive(Debug, Clone, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct OrderRequest {
    /// Instrument ID, e.g. "BTC-USDT".
    pub inst_id: String,
    /// Instrument ID code for low-latency WebSocket requests.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub inst_id_code: Option<u64>,
    /// Trade mode: cross, isolated, cash, spot_isolated.
    pub td_mode: TradeMode,
    /// Margin currency. Only applicable to cross MARGIN orders in single-currency margin.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ccy: Option<String>,
    /// Client Order ID as assigned by the client.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cl_ord_id: Option<String>,
    /// Order tag. A combination of case-sensitive alphanumerics, all numbers, or all letters of up to 16 characters.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tag: Option<String>,
    /// Order side: buy or sell.
    pub side: OrderSide,
    /// Position side: net, long, or short.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pos_side: Option<PositionSide>,
    /// Order type: market, limit, post_only, fok, ioc, etc.
    pub ord_type: OrderType,
    /// Quantity to buy or sell.
    pub sz: String,
    /// Order price. Only applicable to limit, post_only, fok, ioc order types.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub px: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub px_usd: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub px_vol: Option<String>,
    /// Whether orders can only reduce position size.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reduce_only: Option<bool>,
    /// Target currency for the quantity: base_ccy or quote_ccy.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tgt_ccy: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub slippage_pct: Option<String>,
    /// Whether to disallow the system from amending the size of the SPOT Market Order.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ban_amend: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub px_amend_type: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub trade_quote_ccy: Option<String>,
    /// Self trade prevention ID.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stp_id: Option<String>,
    /// Self trade prevention mode.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stp_mode: Option<StpMode>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rpi_taker_access: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub is_elp_taker_access: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rpi_px_round: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub speed_bump: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub outcome: Option<String>,
    /// Take-profit trigger price.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tp_trigger_px: Option<String>,
    /// Take-profit order price. If the price is -1, take-profit will be executed at market price.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tp_ord_px: Option<String>,
    /// Stop-loss trigger price.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sl_trigger_px: Option<String>,
    /// Stop-loss order price. If the price is -1, stop-loss will be executed at market price.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sl_ord_px: Option<String>,
    /// Take-profit trigger price type: last, index, mark.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tp_trigger_px_type: Option<String>,
    /// Stop-loss trigger price type: last, index, mark.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sl_trigger_px_type: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub attach_algo_ords: Option<Vec<AttachAlgoOrdRequest>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub quick_mgn_type: Option<String>,
    /// Expiration time, a Unix timestamp in milliseconds. Order will be cancelled after expiry.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub exp_time: Option<String>,
}

/// Cancel a single order.
#[derive(Debug, Clone, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct CancelOrderRequest {
    /// Instrument ID, e.g. "BTC-USDT".
    pub inst_id: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub inst_id_code: Option<u64>,
    /// Order ID. Either ordId or clOrdId is required; if both are passed, ordId will be used.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ord_id: Option<String>,
    /// Client Order ID as assigned by the client.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cl_ord_id: Option<String>,
}

/// Amend an existing order.
#[derive(Debug, Clone, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct AmendOrderRequest {
    /// Instrument ID, e.g. "BTC-USDT".
    pub inst_id: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub inst_id_code: Option<u64>,
    /// Whether the order needs to be automatically cancelled when amendment fails.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cxl_on_fail: Option<bool>,
    /// Order ID. Either ordId or clOrdId is required.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ord_id: Option<String>,
    /// Client Order ID as assigned by the client.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cl_ord_id: Option<String>,
    /// Client Request ID as assigned by the client for order amendment.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub req_id: Option<String>,
    /// New quantity after amendment.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub new_sz: Option<String>,
    /// New price after amendment.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub new_px: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub new_px_usd: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub new_px_vol: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub speed_bump: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rpi_taker_access: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub is_elp_taker_access: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rpi_px_round: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub attach_algo_ords: Option<Vec<AmendAttachedTrailingStop>>,
    /// New take-profit trigger price after amendment.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub new_tp_trigger_px: Option<String>,
    /// New take-profit order price after amendment. -1 for market price.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub new_tp_ord_px: Option<String>,
    /// New stop-loss trigger price after amendment.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub new_sl_trigger_px: Option<String>,
    /// New stop-loss order price after amendment. -1 for market price.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub new_sl_ord_px: Option<String>,
    /// New take-profit trigger price type after amendment.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub new_tp_trigger_px_type: Option<String>,
    /// New stop-loss trigger price type after amendment.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub new_sl_trigger_px_type: Option<String>,
}

/// Close a position.
#[derive(Debug, Clone, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct ClosePositionRequest {
    /// Instrument ID, e.g. "BTC-USDT-SWAP".
    pub inst_id: String,
    /// Margin mode: cross or isolated.
    pub mgn_mode: MarginMode,
    /// Position side: net, long, or short. Default is net.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pos_side: Option<PositionSide>,
    /// Margin currency. Required for cross MARGIN orders in single-currency margin.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ccy: Option<String>,
    /// Whether to automatically cancel all pending orders of the instrument after closing.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub auto_cxl: Option<bool>,
    /// Client Order ID as assigned by the client.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cl_ord_id: Option<String>,
    /// Order tag.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tag: Option<String>,
}

/// Get details of a single order.
#[derive(Debug, Clone, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct GetOrderRequest {
    /// Instrument ID, e.g. "BTC-USDT".
    pub inst_id: String,
    /// Order ID. Either ordId or clOrdId is required; if both are passed, ordId will be used.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ord_id: Option<String>,
    /// Client Order ID as assigned by the client.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cl_ord_id: Option<String>,
}

/// Get a list of pending (unfilled/partially filled) orders.
#[derive(Debug, Clone, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct GetOrderListRequest {
    /// Instrument type: SPOT, MARGIN, SWAP, FUTURES, OPTION.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub inst_type: Option<InstrumentType>,
    /// Underlying, e.g. "BTC-USD". Applicable to FUTURES/SWAP/OPTION.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub uly: Option<String>,
    /// Instrument family, e.g. "BTC-USD". Applicable to FUTURES/SWAP/OPTION.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub inst_family: Option<String>,
    /// Instrument ID, e.g. "BTC-USDT".
    #[serde(skip_serializing_if = "Option::is_none")]
    pub inst_id: Option<String>,
    /// Order type: market, limit, post_only, fok, ioc, etc.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ord_type: Option<OrderType>,
    /// Order state: live, partially_filled.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub state: Option<String>,
    /// Pagination of data to return records earlier than the requested ordId.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub after: Option<String>,
    /// Pagination of data to return records newer than the requested ordId.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub before: Option<String>,
    /// Number of results per request. Maximum 100; default 100.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<String>,
}

/// Get order history (last 7 days or last 3 months depending on endpoint).
#[derive(Debug, Clone, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct GetOrderHistoryRequest {
    /// Instrument type: SPOT, MARGIN, SWAP, FUTURES, OPTION.
    pub inst_type: InstrumentType,
    /// Underlying, e.g. "BTC-USD". Applicable to FUTURES/SWAP/OPTION.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub uly: Option<String>,
    /// Instrument family, e.g. "BTC-USD". Applicable to FUTURES/SWAP/OPTION.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub inst_family: Option<String>,
    /// Instrument ID, e.g. "BTC-USDT".
    #[serde(skip_serializing_if = "Option::is_none")]
    pub inst_id: Option<String>,
    /// Order type: market, limit, post_only, fok, ioc, etc.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ord_type: Option<OrderType>,
    /// Order state: canceled, filled, mmp_canceled.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub state: Option<String>,
    /// Category: twap, adl, full_liquidation, partial_liquidation, delivery, ddh.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub category: Option<String>,
    /// Pagination of data to return records earlier than the requested ordId.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub after: Option<String>,
    /// Pagination of data to return records newer than the requested ordId.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub before: Option<String>,
    /// Number of results per request. Maximum 100; default 100.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<String>,
    /// Filter with a begin timestamp (Unix timestamp in milliseconds).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub begin: Option<String>,
    /// Filter with an end timestamp (Unix timestamp in milliseconds).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub end: Option<String>,
}

/// Get transaction (fill) details.
#[derive(Debug, Clone, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct GetFillsRequest {
    /// Instrument type: SPOT, MARGIN, SWAP, FUTURES, OPTION.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub inst_type: Option<InstrumentType>,
    /// Underlying, e.g. "BTC-USD". Applicable to FUTURES/SWAP/OPTION.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub uly: Option<String>,
    /// Instrument family, e.g. "BTC-USD". Applicable to FUTURES/SWAP/OPTION.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub inst_family: Option<String>,
    /// Instrument ID, e.g. "BTC-USDT".
    #[serde(skip_serializing_if = "Option::is_none")]
    pub inst_id: Option<String>,
    /// Order ID.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ord_id: Option<String>,
    /// Pagination of data to return records earlier than the requested billId.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub after: Option<String>,
    /// Pagination of data to return records newer than the requested billId.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub before: Option<String>,
    /// Number of results per request. Maximum 100; default 100.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<String>,
    /// Filter with a begin timestamp (Unix timestamp in milliseconds).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub begin: Option<String>,
    /// Filter with an end timestamp (Unix timestamp in milliseconds).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub end: Option<String>,
}

/// Place an algo order (e.g. trigger, OCO, conditional, iceberg, TWAP).
#[derive(Debug, Clone, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct AlgoOrderRequest {
    /// Instrument ID, e.g. "BTC-USDT".
    pub inst_id: String,
    /// Trade mode: cross, isolated, cash, spot_isolated.
    pub td_mode: TradeMode,
    /// Margin currency.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ccy: Option<String>,
    /// Order side: buy or sell.
    pub side: OrderSide,
    /// Position side: net, long, or short.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pos_side: Option<PositionSide>,
    /// Algo order type: conditional, oco, trigger, move_order_stop, iceberg, twap, chase.
    pub ord_type: String,
    /// Quantity to buy or sell.
    pub sz: String,
    /// Order tag.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tag: Option<String>,
    /// Target currency for the quantity.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tgt_ccy: Option<String>,
    /// Whether orders can only reduce position size.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reduce_only: Option<bool>,
    /// Take-profit trigger price.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tp_trigger_px: Option<String>,
    /// Take-profit order price. -1 for market price.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tp_ord_px: Option<String>,
    /// Stop-loss trigger price.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sl_trigger_px: Option<String>,
    /// Stop-loss order price. -1 for market price.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sl_ord_px: Option<String>,
    /// Trigger price for trigger orders.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub trigger_px: Option<String>,
    /// Order price for trigger orders. -1 for market price.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub order_px: Option<String>,
    /// Take-profit trigger price type: last, index, mark.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tp_trigger_px_type: Option<String>,
    /// Stop-loss trigger price type: last, index, mark.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sl_trigger_px_type: Option<String>,
    /// Trigger price type: last, index, mark.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub trigger_px_type: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub chase_type: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub chase_val: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_chase_type: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_chase_val: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub callback_ratio: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub callback_spread: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub active_px: Option<String>,
    /// Price ratio for iceberg/TWAP. Either pxVar or pxSpread is required.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub px_var: Option<String>,
    /// Price variance for iceberg/TWAP. Either pxVar or pxSpread is required.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub px_spread: Option<String>,
    /// Average amount for each order in iceberg/TWAP.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sz_limit: Option<String>,
    /// Price limit for iceberg/TWAP.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub px_limit: Option<String>,
    /// Time interval in seconds for TWAP orders.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub time_interval: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub advance_ord_type: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub adv_chase_params: Option<Vec<AdvChaseParams>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub attach_algo_ords: Option<Vec<AttachAlgoOrdRequest>>,
}

/// Cancel an algo order.
#[derive(Debug, Clone, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct CancelAlgoOrderRequest {
    /// Instrument ID, e.g. "BTC-USDT".
    pub inst_id: String,
    /// Algo order ID.
    pub algo_id: String,
}

/// Amend an algo order.
#[derive(Debug, Clone, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct AmendAlgoOrderRequest {
    /// Instrument ID, e.g. "BTC-USDT".
    pub inst_id: String,
    /// Algo order ID.
    pub algo_id: String,
    /// Whether the order needs to be automatically cancelled when amendment fails.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cxl_on_fail: Option<bool>,
    /// Client Request ID as assigned by the client for order amendment.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub req_id: Option<String>,
    /// New quantity after amendment.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub new_sz: Option<String>,
    /// New take-profit trigger price after amendment.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub new_tp_trigger_px: Option<String>,
    /// New take-profit order price after amendment. -1 for market price.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub new_tp_ord_px: Option<String>,
    /// New stop-loss trigger price after amendment.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub new_sl_trigger_px: Option<String>,
    /// New stop-loss order price after amendment. -1 for market price.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub new_sl_ord_px: Option<String>,
    /// New take-profit trigger price type after amendment.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub new_tp_trigger_px_type: Option<String>,
    /// New stop-loss trigger price type after amendment.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub new_sl_trigger_px_type: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub new_trigger_px: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub new_ord_px: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub new_trigger_px_type: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub adv_chase_params: Option<Vec<AdvChaseAmendParams>>,
}

/// Get details of a single algo order.
#[derive(Debug, Clone, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct GetAlgoOrderRequest {
    /// Algo order ID. Either algoId or algoClOrdId is required.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub algo_id: Option<String>,
    /// Client-supplied Algo Order ID. Either algoId or algoClOrdId is required.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub algo_cl_ord_id: Option<String>,
}

/// Get a list of pending algo orders.
#[derive(Debug, Clone, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct GetAlgoOrderListRequest {
    /// Algo order type: conditional, oco, trigger, move_order_stop, iceberg, twap, chase.
    pub ord_type: String,
    /// Algo order ID.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub algo_id: Option<String>,
    /// Instrument type: SPOT, MARGIN, SWAP, FUTURES, OPTION.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub inst_type: Option<InstrumentType>,
    /// Instrument ID, e.g. "BTC-USDT".
    #[serde(skip_serializing_if = "Option::is_none")]
    pub inst_id: Option<String>,
    /// Pagination of data to return records earlier than the requested algoId.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub after: Option<String>,
    /// Pagination of data to return records newer than the requested algoId.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub before: Option<String>,
    /// Number of results per request. Maximum 100; default 100.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<String>,
}

/// Mass cancel orders for an instrument type.
#[derive(Debug, Clone, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct MassCancelRequest {
    /// Instrument type: SWAP, FUTURES, OPTION.
    pub inst_type: InstrumentType,
    /// Instrument family, e.g. "BTC-USD".
    #[serde(skip_serializing_if = "Option::is_none")]
    pub inst_family: Option<String>,
}

/// Cancel all orders after a specified countdown timer.
#[derive(Debug, Clone, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct CancelAllAfterRequest {
    /// The countdown timer value in seconds. Setting "0" will effectively cancel the timer.
    pub time_out: String,
    /// Order tag.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tag: Option<String>,
}

/// Easy convert request.
///
/// Convert small assets into OKB.
#[derive(Debug, Clone, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct EasyConvertRequest {
    /// Source currencies to convert from. Comma-separated list.
    #[serde(serialize_with = "serialize_csv")]
    pub from_ccy: Vec<String>,
    /// Target currency to convert to.
    pub to_ccy: String,
}

/// Get easy convert history request.
#[derive(Debug, Clone, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct GetEasyConvertHistoryRequest {
    /// Pagination of data to return records earlier than the requested timestamp.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub after: Option<String>,
    /// Pagination of data to return records newer than the requested timestamp.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub before: Option<String>,
    /// Number of results per request. Maximum 100; default 100.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<String>,
}

/// One-click repay request.
///
/// Repay cross margin debt with a single click.
#[derive(Debug, Clone, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct OneClickRepayRequest {
    /// Currencies with debt to repay. Comma-separated list.
    #[serde(serialize_with = "serialize_csv")]
    pub debt_ccy: Vec<String>,
    /// Currency to use for repayment.
    pub repay_ccy: String,
}

/// Get one-click repay history request.
#[derive(Debug, Clone, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct GetOneClickRepayHistoryRequest {
    /// Pagination of data to return records earlier than the requested timestamp.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub after: Option<String>,
    /// Pagination of data to return records newer than the requested timestamp.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub before: Option<String>,
    /// Number of results per request. Maximum 100; default 100.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<String>,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn easy_convert_serializes_currency_list_as_csv() {
        let req = EasyConvertRequest {
            from_ccy: vec!["BTC".into(), "ETH".into()],
            to_ccy: "USDT".into(),
        };

        let value = serde_json::to_value(req).unwrap();
        assert_eq!(value["fromCcy"], "BTC,ETH");
    }

    #[test]
    fn one_click_repay_serializes_currency_list_as_csv() {
        let req = OneClickRepayRequest {
            debt_ccy: vec!["BTC".into(), "ETH".into()],
            repay_ccy: "USDT".into(),
        };

        let value = serde_json::to_value(req).unwrap();
        assert_eq!(value["debtCcy"], "BTC,ETH");
    }

    #[test]
    fn order_serializes_current_wire_fields() {
        let request = OrderRequest {
            inst_id_code: Some(42),
            slippage_pct: Some("0.01".into()),
            rpi_taker_access: Some(true),
            is_elp_taker_access: Some(false),
            rpi_px_round: Some(true),
            outcome: Some("yes".into()),
            attach_algo_ords: Some(vec![AttachAlgoOrdRequest {
                callback_ratio: Some("0.05".into()),
                active_px: Some("100".into()),
                ..Default::default()
            }]),
            ..Default::default()
        };

        let value = serde_json::to_value(request).unwrap();
        assert_eq!(value["instIdCode"], 42);
        assert_eq!(value["slippagePct"], "0.01");
        assert_eq!(value["rpiTakerAccess"], true);
        assert_eq!(value["isElpTakerAccess"], false);
        assert_eq!(value["rpiPxRound"], true);
        assert_eq!(value["outcome"], "yes");
        assert_eq!(value["attachAlgoOrds"][0]["callbackRatio"], "0.05");
        assert_eq!(value["attachAlgoOrds"][0]["activePx"], "100");
    }

    #[test]
    fn amend_and_chase_requests_serialize_current_wire_fields() {
        let amend = AmendOrderRequest {
            inst_id_code: Some(42),
            rpi_taker_access: Some(true),
            rpi_px_round: Some(true),
            attach_algo_ords: Some(vec![AmendAttachedTrailingStop {
                new_callback_spread: Some("10".into()),
                ..Default::default()
            }]),
            ..Default::default()
        };
        let chase = AlgoOrderRequest {
            advance_ord_type: Some("chase".into()),
            adv_chase_params: Some(vec![AdvChaseParams {
                chase_type: Some("distance".into()),
                chase_val: Some("1".into()),
                ..Default::default()
            }]),
            ..Default::default()
        };
        let chase_amend = AmendAlgoOrderRequest {
            adv_chase_params: Some(vec![AdvChaseAmendParams {
                new_chase_val: Some("2".into()),
                ..Default::default()
            }]),
            ..Default::default()
        };

        let amend = serde_json::to_value(amend).unwrap();
        let chase = serde_json::to_value(chase).unwrap();
        let chase_amend = serde_json::to_value(chase_amend).unwrap();
        assert_eq!(amend["instIdCode"], 42);
        assert_eq!(amend["rpiTakerAccess"], true);
        assert_eq!(amend["rpiPxRound"], true);
        assert_eq!(
            amend["attachAlgoOrds"][0]["newCallbackSpread"],
            "10"
        );
        assert_eq!(chase["advanceOrdType"], "chase");
        assert_eq!(chase["advChaseParams"][0]["chaseVal"], "1");
        assert_eq!(
            chase_amend["advChaseParams"][0]["newChaseVal"],
            "2"
        );
    }
}