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
use serde::Deserialize;

/// Full account balance information.
///
/// Contains overall account equity, margin, and per-currency balance details.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct AccountBalance {
    /// Update time, Unix timestamp in milliseconds.
    #[serde(default)]
    pub u_time: String,
    /// Total equity in USD.
    #[serde(default)]
    pub total_eq: String,
    /// Isolated margin equity in USD.
    #[serde(default)]
    pub iso_eq: String,
    /// Adjusted / effective equity in USD.
    #[serde(default)]
    pub adj_eq: String,
    /// Cross margin frozen for pending orders.
    #[serde(default)]
    pub ord_froz: String,
    /// Initial margin requirement.
    #[serde(default)]
    pub imr: String,
    /// Maintenance margin requirement.
    #[serde(default)]
    pub mmr: String,
    /// Notional value of positions in USD.
    #[serde(default)]
    pub notional_usd: String,
    /// Per-currency balance details.
    #[serde(default)]
    pub details: Vec<BalanceDetail>,
}

/// Per-currency balance detail.
///
/// Provides detailed balance information for a single currency within the
/// trading account.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct BalanceDetail {
    /// Currency, e.g. "BTC".
    #[serde(default)]
    pub ccy: String,
    /// Equity of the currency.
    #[serde(default)]
    pub eq: String,
    /// Cash balance.
    #[serde(default)]
    pub cash_bal: String,
    /// Update time, Unix timestamp in milliseconds.
    #[serde(default)]
    pub u_time: String,
    /// Isolated margin equity of the currency.
    #[serde(default)]
    pub iso_eq: String,
    /// Available equity of the currency.
    #[serde(default)]
    pub avail_eq: String,
    /// Discount equity of the currency in USD.
    #[serde(default)]
    pub dis_eq: String,
    /// Available balance of the currency.
    #[serde(default)]
    pub avail_bal: String,
    /// Frozen balance of the currency.
    #[serde(default)]
    pub frozen_bal: String,
    /// Margin frozen for open orders.
    #[serde(default)]
    pub ord_frozen: String,
    /// Liabilities of the currency.
    #[serde(default)]
    pub liab: String,
    /// Unrealized profit and loss.
    #[serde(default)]
    pub upl: String,
    /// Unrealized profit and loss for liabilities.
    #[serde(default)]
    pub upl_liab: String,
    /// Cross liabilities of the currency.
    #[serde(default)]
    pub cross_liab: String,
    /// Isolated liabilities of the currency.
    #[serde(default)]
    pub iso_liab: String,
    /// Margin ratio of the currency.
    #[serde(default)]
    pub mgn_ratio: String,
    /// Accrued interest of the currency.
    #[serde(default)]
    pub interest: String,
    /// TWAP value.
    #[serde(default)]
    pub twap: String,
    /// Maximum loan of the currency.
    #[serde(default)]
    pub max_loan: String,
    /// Equity in USD.
    #[serde(default)]
    pub eq_usd: String,
    /// Leverage used in the notional value of the currency.
    #[serde(default)]
    pub notional_lever: String,
}

/// Position information.
///
/// Represents a single open position in the trading account.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Position {
    /// Instrument type.
    #[serde(default)]
    pub inst_type: String,
    /// Margin mode: "cross" or "isolated".
    #[serde(default)]
    pub mgn_mode: String,
    /// Position ID.
    #[serde(default)]
    pub pos_id: String,
    /// Position side: "net", "long", or "short".
    #[serde(default)]
    pub pos_side: String,
    /// Quantity of positions.
    #[serde(default)]
    pub pos: String,
    /// Base currency balance (applicable to SPOT/MARGIN positions in isolated margin).
    #[serde(default)]
    pub base_bal: String,
    /// Quote currency balance (applicable to SPOT/MARGIN positions in isolated margin).
    #[serde(default)]
    pub quote_bal: String,
    /// Base currency borrowed (applicable to SPOT/MARGIN).
    #[serde(default)]
    pub base_borrowed: String,
    /// Quote currency borrowed (applicable to SPOT/MARGIN).
    #[serde(default)]
    pub quote_borrowed: String,
    /// Position currency, only applicable to MARGIN positions.
    #[serde(default)]
    pub pos_ccy: String,
    /// Position that can be closed.
    #[serde(default)]
    pub avail_pos: String,
    /// Average open price.
    #[serde(default)]
    pub avg_px: String,
    /// Unrealized profit and loss.
    #[serde(default)]
    pub upl: String,
    /// Unrealized profit and loss ratio.
    #[serde(default)]
    pub upl_ratio: String,
    /// Instrument ID, e.g. "BTC-USDT-SWAP".
    #[serde(default)]
    pub inst_id: String,
    /// Leverage.
    #[serde(default)]
    pub lever: String,
    /// Estimated liquidation price.
    #[serde(default)]
    pub liq_px: String,
    /// Mark price.
    #[serde(default)]
    pub mark_px: String,
    /// Initial margin requirement.
    #[serde(default)]
    pub imr: String,
    /// Margin, can be added or reduced.
    #[serde(default)]
    pub margin: String,
    /// Margin ratio.
    #[serde(default)]
    pub mgn_ratio: String,
    /// Maintenance margin requirement.
    #[serde(default)]
    pub mmr: String,
    /// Liabilities. Only applicable to MARGIN.
    #[serde(default)]
    pub liab: String,
    /// Liabilities currency. Only applicable to MARGIN.
    #[serde(default)]
    pub liab_ccy: String,
    /// Interest. Only applicable to MARGIN.
    #[serde(default)]
    pub interest: String,
    /// Last trade ID.
    #[serde(default)]
    pub trade_id: String,
    /// Notional value of positions in USD.
    #[serde(default)]
    pub notional_usd: String,
    /// Auto-deleveraging indicator (1-5, higher means more risk).
    #[serde(default)]
    pub adl: String,
    /// Currency used for margin.
    #[serde(default)]
    pub ccy: String,
    /// Latest traded price.
    #[serde(default)]
    pub last: String,
    /// Update time, Unix timestamp in milliseconds.
    #[serde(default)]
    pub u_time: String,
    /// Creation time, Unix timestamp in milliseconds.
    #[serde(default)]
    pub c_time: String,
    /// Accumulated PnL of closing orders for the position.
    #[serde(default)]
    pub pnl: String,
    /// Accumulated fee. Negative means user transaction fee charged by the platform.
    #[serde(default)]
    pub fee: String,
    /// Accumulated funding fee.
    #[serde(default)]
    pub funding_fee: String,
    /// Realized profit and loss.
    #[serde(default)]
    pub real_pnl: String,
}

/// Account configuration.
///
/// Contains account-level settings and metadata.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct AccountConfig {
    /// User ID.
    #[serde(default)]
    pub uid: String,
    /// Account level: "1" Simple, "2" Single-currency margin,
    /// "3" Multi-currency margin, "4" Portfolio margin.
    #[serde(default)]
    pub acct_lv: String,
    /// Position mode: "long_short_mode" or "net_mode".
    #[serde(default)]
    pub pos_mode: String,
    /// Whether to borrow coins automatically: "true" or "false".
    #[serde(default)]
    pub auto_loan: String,
    /// Current display type of Greeks: "PA" (coins) or "BS" (Black-Scholes).
    #[serde(default)]
    pub greeks_type: String,
    /// User level.
    #[serde(default)]
    pub level: String,
    /// Temporary user level experience.
    #[serde(default)]
    pub level_tmp: String,
    /// Contract isolated margin trading settings.
    #[serde(default)]
    pub ct_iso_mode: String,
    /// Margin isolated margin trading settings.
    #[serde(default)]
    pub mgn_iso_mode: String,
    /// Spot offset type.
    #[serde(default)]
    pub spot_offset_type: String,
    /// Current request IP.
    #[serde(default)]
    pub ip: String,
    /// Permission of the current API Key.
    #[serde(default)]
    pub perm: String,
    /// Label of the current API Key.
    #[serde(default)]
    pub label: String,
}

/// Leverage information.
///
/// Contains the leverage setting for a given instrument and margin mode.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct LeverageInfo {
    /// Instrument ID, e.g. "BTC-USDT-SWAP".
    #[serde(default)]
    pub inst_id: String,
    /// Margin mode: "cross" or "isolated".
    #[serde(default)]
    pub mgn_mode: String,
    /// Position side: "net", "long", or "short".
    #[serde(default)]
    pub pos_side: String,
    /// Leverage value.
    #[serde(default)]
    pub lever: String,
}

/// Maximum buy/sell amount.
///
/// Maximum tradeable buy and sell amounts for an instrument.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct MaxBuySellAmount {
    /// Instrument ID, e.g. "BTC-USDT".
    #[serde(default)]
    pub inst_id: String,
    /// Currency, e.g. "BTC".
    #[serde(default)]
    pub ccy: String,
    /// Maximum quantity to buy.
    #[serde(default)]
    pub max_buy: String,
    /// Maximum quantity to sell.
    #[serde(default)]
    pub max_sell: String,
}

/// Fee rate information.
///
/// Contains maker/taker fee rates and other fee-related details for an
/// instrument type.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct FeeRate {
    /// Fee rate level.
    #[serde(default)]
    pub level: String,
    /// Taker fee rate. Negative means the platform charges a fee;
    /// positive means the platform pays a rebate.
    #[serde(default)]
    pub taker: String,
    /// Maker fee rate.
    #[serde(default)]
    pub maker: String,
    /// Delivery fee rate.
    #[serde(default)]
    pub delivery: String,
    /// Fee rate for exercising options.
    #[serde(default)]
    pub exercise: String,
    /// Instrument type.
    #[serde(default)]
    pub inst_type: String,
    /// Timestamp, Unix timestamp in milliseconds.
    #[serde(default)]
    pub ts: String,
}

/// Maximum withdrawal amount.
///
/// Contains the maximum amount that can be withdrawn for a given currency.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct MaxWithdrawal {
    /// Currency, e.g. "BTC".
    #[serde(default)]
    pub ccy: String,
    /// Maximum withdrawal amount.
    #[serde(default)]
    pub max_wd: String,
    /// Maximum withdrawal amount (excluding borrowed assets).
    #[serde(default)]
    pub max_wd_ex: String,
    /// Max withdrawal with spot offset.
    #[serde(default)]
    pub spot_offset_max_wd: String,
    /// Max withdrawal with spot offset (excluding borrowed assets).
    #[serde(default)]
    pub spot_offset_max_wd_ex: String,
}

/// MMP (Market Maker Protection) configuration.
///
/// Contains the MMP configuration settings for an instrument family.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct MmpConfig {
    /// Instrument family, e.g. "BTC-USD".
    #[serde(default)]
    pub inst_family: String,
    /// Time window (ms).
    #[serde(default)]
    pub time_interval: String,
    /// Frozen period (ms).
    #[serde(default)]
    pub frozen_interval: String,
    /// Quantity limit.
    #[serde(default)]
    pub qty_limit: String,
}

/// Account risk state.
///
/// Represents the current risk state of the account.
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct AccountRiskState {
    /// Whether the account is at risk.
    #[serde(default, rename = "atRisk")]
    pub at_risk: String,
    /// Index-based risk indicator.
    #[serde(default, rename = "atRiskIdx")]
    pub at_risk_idx: String,
    /// Margin-based risk indicator.
    #[serde(default, rename = "atRiskMgn")]
    pub at_risk_mgn: String,
    /// Timestamp, Unix timestamp in milliseconds.
    #[serde(default)]
    pub ts: String,
}

/// Generic result for set operations.
///
/// Used for responses from endpoints like `setPositionMode`, `setLeverage`,
/// `setGreeks`, etc. Since the response fields vary by endpoint, this uses
/// `serde_json::Value` to flexibly capture any returned fields.
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct SetResult {
    /// The raw result data. Different set operations return different fields
    /// (e.g. `posMode` for set position mode, `lever` for set leverage).
    #[serde(flatten)]
    pub data: serde_json::Value,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct BillSubtypeDetail {
    #[serde(default)]
    pub sub_type: String,
    #[serde(default)]
    pub sub_type_desc: String,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct AccountBillTypeDefinition {
    #[serde(default, rename = "type")]
    pub type_: String,
    #[serde(default)]
    pub type_desc: String,
    #[serde(default)]
    pub sub_type_details: Vec<BillSubtypeDetail>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct AccountHistoryBill {
    #[serde(default)]
    pub file_href: Option<String>,
    #[serde(default)]
    pub result: Option<String>,
    #[serde(default)]
    pub state: Option<String>,
    #[serde(default)]
    pub ts: String,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct SetTradingConfigResult {
    #[serde(default, rename = "type")]
    pub type_: String,
    #[serde(default)]
    pub stgy_type: String,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct UnmatchedInfo {
    #[serde(default, rename = "type")]
    pub type_: String,
    #[serde(default)]
    pub delta_lever: Option<String>,
    #[serde(default)]
    pub ord_list: Vec<String>,
    #[serde(default)]
    pub pos_list: Vec<String>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct PrecheckSetDeltaNeutralResult {
    #[serde(default)]
    pub unmatched_info_check: Vec<UnmatchedInfo>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct MovePositionResultFrom {
    #[serde(default)]
    pub pos_id: String,
    #[serde(default)]
    pub inst_id: String,
    #[serde(default)]
    pub px: String,
    #[serde(default)]
    pub side: String,
    #[serde(default)]
    pub sz: String,
    #[serde(default)]
    pub s_code: String,
    #[serde(default)]
    pub s_msg: String,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct MovePositionResultTo {
    #[serde(default)]
    pub inst_id: String,
    #[serde(default)]
    pub px: String,
    #[serde(default)]
    pub side: String,
    #[serde(default)]
    pub sz: String,
    #[serde(default)]
    pub td_mode: String,
    #[serde(default)]
    pub pos_side: String,
    #[serde(default)]
    pub ccy: String,
    #[serde(default)]
    pub s_code: String,
    #[serde(default)]
    pub s_msg: String,
}

#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct MovePositionResultLeg {
    pub from: MovePositionResultFrom,
    pub to: MovePositionResultTo,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct MovePositionsResult {
    #[serde(default)]
    pub client_id: String,
    #[serde(default)]
    pub block_td_id: String,
    #[serde(default)]
    pub state: String,
    #[serde(default)]
    pub ts: String,
    #[serde(default)]
    pub from_acct: String,
    #[serde(default)]
    pub to_acct: String,
    #[serde(default)]
    pub legs: Vec<MovePositionResultLeg>,
}