okx 0.2.3

OKX交易所API SDK
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
use crate::dto::common::MarginMode;
use serde::{Deserialize, Serialize};

/// 平仓策略委托订单结构体
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CloseOrderAlgo {
    /// 策略委托单ID
    pub algo_id: String,
    /// 止损触发价
    pub sl_trigger_px: Option<String>,
    /// 止损触发价类型
    pub sl_trigger_px_type: Option<String>,
    /// 止盈委托价
    pub tp_trigger_px: Option<String>,
    /// 止盈触发价类型
    pub tp_trigger_px_type: Option<String>,
    /// 策略委托触发时,平仓的百分比。1 代表100%
    pub close_fraction: Option<String>,
}

/// 持仓信息结构体
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct TradingNumRequestParams {
    pub inst_id: String,              // 产品ID,如 BTC-USDT
    pub td_mode: String,              // 交易模式: cross, isolated, cash, spot_isolated
    pub ccy: Option<String>,          // 保证金币种,仅适用于单币种保证金模式下的全仓杠杆订单
    pub reduce_only: Option<bool>,    // 是否为只减仓模式,仅适用于币币杠杆
    pub px: Option<String>,           // 对应平仓价格下的可用数量,默认为市价,仅适用于杠杆只减仓
    pub un_spot_offset: Option<bool>, // true:禁止现货对冲,false:允许现货对冲,默认为false,仅适用于组合保证金模式
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct TradingNumResponseData {
    pub inst_id: String,    // 产品ID,如 BTC-USDT
    pub avail_buy: String,  //最大买入可用数量
    pub avail_sell: String, //最大卖出可用数量
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct TradingSwapNumRequestParams {
    pub inst_id: String,              // 产品ID,如 BTC-USDT
    pub td_mode: String,              // 交易模式: cross, isolated, cash, spot_isolated
    pub ccy: Option<String>,          // 保证金币种,仅适用于单币种保证金模式下的全仓杠杆订单
    pub px: Option<String>, // 委托价格当不填委托价时,交割和永续会取当前限价计算,其他业务线会按当前最新成交价计算当指定多个产品ID查询时,忽略该参数,当未填写处理
    pub leverage: Option<String>, // 开仓杠杆倍数默认为当前杠杆倍数仅适用于币币杠杆/交割/永续
    pub un_spot_offset: Option<bool>, // true:禁止现货对冲,false:允许现货对冲,默认为false,仅适用于组合保证金模式
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct TradingSwapNumResponseData {
    pub inst_id: String,  // 产品ID,如 BTC-USDT
    pub ccy: String,      //保证金币种
    pub max_buy: String,  //最大买入可用数量
    pub max_sell: String, //最大卖出可用数量
}
/// 账户余额信息
/// uTime	String	账户信息的更新时间,Unix时间戳的毫秒数格式,如 1597026383085
/// totalEq	String	美金层面权益
/// isoEq	String	美金层面逐仓仓位权益
/// 适用于合约模式/跨币种保证金模式/组合保证金模式
/// adjEq	String	美金层面有效保证金
/// 适用于现货模式/跨币种保证金模式/组合保证金模式
/// availEq	String	账户美金层面可用保证金,排除因总质押借币上限而被限制的币种
/// 适用于跨币种保证金模式/组合保证金模式
/// ordFroz	String	美金层面全仓挂单占用保证金
/// 仅适用于现货模式/跨币种保证金模式/组合保证金模式
/// imr	String	美金层面占用保证金
/// 适用于现货模式/跨币种保证金模式/组合保证金模式
/// mmr	String	美金层面维持保证金
/// 适用于现货模式/跨币种保证金模式/组合保证金模式
/// borrowFroz	String	账户美金层面潜在借币占用保证金
/// 仅适用于现货模式/跨币种保证金模式/组合保证金模式。在其他账户模式下为""。
/// mgnRatio	String	美金层面维持保证金率
/// 适用于现货模式/跨币种保证金模式/组合保证金模式
/// notionalUsd	String	以美金价值为单位的持仓数量,即仓位美金价值
/// 适用于现货模式/跨币种保证金模式/组合保证金模式
/// notionalUsdForBorrow	String	借币金额(美元价值)
/// 适用于现货模式/跨币种保证金模式/组合保证金模式
/// notionalUsdForSwap	String	永续合约持仓美元价值
/// 适用于跨币种保证金模式/组合保证金模式
/// notionalUsdForFutures	String	交割合约持仓美元价值
/// 适用于跨币种保证金模式/组合保证金模式
/// notionalUsdForOption	String	期权持仓美元价值
/// 适用于现货模式/跨币种保证金模式/组合保证金模式
/// upl	String	账户层面全仓未实现盈亏(美元单位)
/// 适用于跨币种保证金模式/组合保证金模式
/// details	Array of objects	各币种资产详细信息
/// ccy	String	币种
/// eq	String	币种总权益
/// cashBal	String	币种余额
/// uTime	String	币种余额信息的更新时间,Unix时间戳的毫秒数格式,如 1597026383085
/// isoEq	String	币种逐仓仓位权益
/// 适用于合约模式/跨币种保证金模式/组合保证金模式
/// availEq	String	可用保证金
/// 适用于合约模式/跨币种保证金模式/组合保证金模式
/// disEq	String	美金层面币种折算权益
/// 适用于现货模式(开通了借币功能)/跨币种保证金模式/组合保证金模式
/// fixedBal	String	抄底宝、逃顶宝功能的币种冻结金额
/// availBal	String	可用余额
/// frozenBal	String	币种占用金额
/// ordFrozen	String	挂单冻结数量
/// 适用于现货模式/合约模式/跨币种保证金模式
/// liab	String	币种负债额
/// 值为正数,如 "21625.64"
/// 适用于现货模式/跨币种保证金模式/组合保证金模式
/// upl	String	未实现盈亏
/// 适用于合约模式/跨币种保证金模式/组合保证金模式
/// uplLiab	String	由于仓位未实现亏损导致的负债
/// 适用于跨币种保证金模式/组合保证金模式
/// crossLiab	String	币种全仓负债额
/// 适用于现货模式/跨币种保证金模式/组合保证金模式
/// isoLiab	String	币种逐仓负债额
/// 适用于跨币种保证金模式/组合保证金模式
/// rewardBal	String	体验金余额
/// mgnRatio	String	币种全仓维持保证金率,衡量账户内某项资产风险的指标
/// 适用于合约模式且有全仓仓位时
/// imr	String	币种维度全仓占用保证金
/// 适用于合约模式且有全仓仓位时
/// mmr	String	币种维度全仓维持保证金
/// 适用于合约模式且有全仓仓位时
/// interest	String	计息,应扣未扣利息
/// 值为正数,如 9.01
/// 适用于现货模式/跨币种保证金模式/组合保证金模式
/// twap	String	当前负债币种触发系统自动换币的风险
/// 0、1、2、3、4、5其中之一,数字越大代表您的负债币种触发自动换币概率越高
/// 适用于现货模式/跨币种保证金模式/组合保证金模式
/// maxLoan	String	币种最大可借
/// 适用于现货模式/跨币种保证金模式/组合保证金模式 的全仓
/// eqUsd	String	币种权益美金价值
/// borrowFroz	String	币种美金层面潜在借币占用保证金
/// 仅适用于现货模式/跨币种保证金模式/组合保证金模式。在其他账户模式下为""。
/// notionalLever	String	币种杠杆倍数
/// 适用于合约模式
/// stgyEq	String	策略权益
/// isoUpl	String	逐仓未实现盈亏
/// 适用于合约模式/跨币种保证金模式/组合保证金模式
/// spotInUseAmt	String	现货对冲占用数量
/// 适用于组合保证金模式
/// clSpotInUseAmt	String	用户自定义现货占用数量
/// 适用于组合保证金模式
/// maxSpotInUse	String	系统计算得到的最大可能现货占用数量
/// 适用于组合保证金模式
/// spotIsoBal	String	现货逐仓余额
/// 仅适用于现货带单/跟单
/// 适用于现货模式/合约模式
/// smtSyncEq	String	合约智能跟单权益
/// 默认为0,仅适用于跟单人。
/// spotCopyTradingEq	String	现货智能跟单权益
/// 默认为0,仅适用于跟单人。
/// spotBal	String	现货余额 ,单位为 币种,比如 BTC。详情
/// openAvgPx	String	现货开仓成本价 单位 USD。 详情
/// accAvgPx	String	现货累计成本价 单位 USD。 详情
/// spotUpl	String	现货未实现收益,单位 USD。 详情
/// spotUplRatio	String	现货未实现收益率。详情
/// totalPnl	String	现货累计收益,单位 USD。 详情
/// totalPnlRatio	String	现货累计收益率。详情
/// collateralEnabled	Boolean	true:质押币
/// false:非质押币
/// 适用于跨币种保证金模式
/// collateralRestrict	Boolean	平台维度的质押借币限制
/// true
/// false
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Balance {
    /// 账户信息的更新时间,Unix时间戳的毫秒数格式,如 1597026383085
    #[serde(rename = "uTime")]
    pub u_time: String,
    /// 美金层面权益
    #[serde(rename = "totalEq")]
    pub total_eq: String,
    /// 美金层面逐仓仓位权益
    #[serde(rename = "isoEq")]
    pub iso_eq: String,
    /// 美金层面有效保证金
    #[serde(rename = "adjEq")]
    pub adj_eq: String,
    /// 账户美金层面可用保证金,排除因总质押借币上限而被限制的币种
    #[serde(rename = "availEq")]
    pub avail_eq: String,
    /// 美金层面全仓挂单占用保证金
    #[serde(rename = "ordFroz")]
    pub ord_froz: String,
    /// 美金层面占用保证金
    #[serde(rename = "imr")]
    pub imr: String,
    /// 美金层面维持保证金
    #[serde(rename = "mmr")]
    pub mmr: String,
    /// 美金层面潜在借币占用保证金
    #[serde(rename = "borrowFroz")]
    pub borrow_froz: String,
    /// 美金层面维持保证金率
    #[serde(rename = "mgnRatio")]
    pub mgn_ratio: String,
    /// 以美金价值为单位的持仓数量,即仓位美金价值
    #[serde(rename = "notionalUsd")]
    pub notional_usd: String,
    /// 借币金额(美元价值)
    #[serde(rename = "notionalUsdForBorrow")]
    pub notional_usd_for_borrow: String,
    /// 永续合约持仓美元价值
    #[serde(rename = "notionalUsdForSwap")]
    pub notional_usd_for_swap: String,
    /// 交割合约持仓美元价值
    #[serde(rename = "notionalUsdForFutures")]
    pub notional_usd_for_futures: String,
    /// 期权持仓美元价值
    #[serde(rename = "notionalUsdForOption")]
    pub notional_usd_for_option: String,
    /// 账户层面全仓未实现盈亏(美元单位)
    #[serde(rename = "upl")]
    pub upl: String,
    /// 各币种资产详细信息
    pub details: Vec<BalanceDetail>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BalanceDetail {
    /// 币种
    pub ccy: String,
    /// 币种总权益
    pub eq: String,
    /// 币种余额
    #[serde(rename = "cashBal")]
    pub cash_bal: String,
    /// 币种逐仓仓位权益
    #[serde(rename = "isoEq")]
    pub iso_eq: String,
    /// 可用保证金
    #[serde(rename = "availEq")]
    pub avail_eq: String,
    /// 美金层面币种折算权益
    #[serde(rename = "disEq")]
    pub dis_eq: String,
    /// 抄底宝、逃顶宝功能的币种冻结金额
    #[serde(rename = "fixedBal")]
    pub fixed_bal: String,
    /// 可用余额
    #[serde(rename = "availBal")]
    pub avail_bal: String,
    /// 币种占用金额
    #[serde(rename = "frozenBal")]
    pub frozen_bal: String,
    /// 挂单冻结数量
    #[serde(rename = "ordFrozen")]
    pub ord_frozen: String,
    /// 币种负债额
    #[serde(rename = "liab")]
    pub liab: String,
    /// 未实现盈亏
    #[serde(rename = "upl")]
    pub upl: String,
    /// 由于仓位未实现亏损导致的负债
    #[serde(rename = "uplLiab")]
    pub upl_liab: String,
    /// 币种全仓负债额  
    #[serde(rename = "crossLiab")]
    pub cross_liab: String,
    /// 币种逐仓负债额
    #[serde(rename = "isoLiab")]
    pub iso_liab: String,
    /// 体验金余额
    #[serde(rename = "rewardBal")]
    pub reward_bal: String,
    /// 币种全仓维持保证金率
    #[serde(rename = "mgnRatio")]
    pub mgn_ratio: String,
    /// 币种维度全仓占用保证金
    #[serde(rename = "imr")]
    pub imr: String,
    /// 币种维度全仓维持保证金
    #[serde(rename = "mmr")]
    pub mmr: String,
    /// 计息,应扣未扣利息
    #[serde(rename = "interest")]
    pub interest: String,
    /// 当前负债币种触发系统自动换币的风险
    #[serde(rename = "twap")]
    pub twap: String,
    /// 币种最大可借
    #[serde(rename = "maxLoan")]
    pub max_loan: String,
    /// 币种权益美金价值
    #[serde(rename = "eqUsd")]
    pub eq_usd: String,
    /// 币种美金层面潜在借币占用保证金
    #[serde(rename = "borrowFroz")]
    pub borrow_froz: String,
    /// 币种杠杆倍数
    #[serde(rename = "notionalLever")]
    pub notional_lever: String,
    /// 策略权益
    #[serde(rename = "stgyEq")]
    pub stgy_eq: String,
    /// 逐仓未实现盈亏
    #[serde(rename = "isoUpl")]
    pub iso_upl: String,
    /// 现货对冲占用数量
    #[serde(rename = "spotInUseAmt")]
    pub spot_in_use_amt: String,
    /// 用户自定义现货占用数量
    #[serde(rename = "clSpotInUseAmt")]
    pub cl_spot_in_use_amt: String,
    /// 系统计算得到的最大可能现货占用数量
    #[serde(rename = "maxSpotInUse")]
    pub max_spot_in_use: String,
    /// 现货逐仓余额
    #[serde(rename = "spotIsoBal")]
    pub spot_iso_bal: String,
    /// 合约智能跟单权益
    #[serde(rename = "smtSyncEq")]
    pub smt_sync_eq: String,
    /// 现货智能跟单权益
    #[serde(rename = "spotCopyTradingEq")]
    pub spot_copy_trading_eq: String,
    /// 现货余额
    #[serde(rename = "spotBal")]
    pub spot_bal: String,
    /// 现货开仓成本价
    #[serde(rename = "openAvgPx")]
    pub open_avg_px: String,
    /// 现货累计成本价
    #[serde(rename = "accAvgPx")]
    pub acc_avg_px: String,
    /// 现货未实现收益
    #[serde(rename = "spotUpl")]
    pub spot_upl: String,
    /// 现货未实现收益率
    #[serde(rename = "spotUplRatio")]
    pub spot_upl_ratio: String,
    /// 现货累计收益
    #[serde(rename = "totalPnl")]
    pub total_pnl: String,
    /// 现货累计收益率
    #[serde(rename = "totalPnlRatio")]
    pub total_pnl_ratio: String,
    /// 质押币
    #[serde(rename = "collateralEnabled")]
    pub collateral_enabled: bool,
    /// 质押风险率
    #[serde(rename = "collateralRestrict")]
    pub collateral_restrict: bool,
}

/// 账户配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AccountConfig {
    /// 当前请求账户ID
    #[serde(rename = "uid", alias = "acctId")]
    pub account_id: String,
    /// 当前请求母账户ID
    #[serde(rename = "mainUid")]
    pub main_uid: String,
    /// 账户模式
    #[serde(rename = "acctLv")]
    pub account_level: String,
    /// 账户自成交保护模式
    #[serde(rename = "acctStpMode")]
    pub account_stp_mode: String,
    /// 持仓类型
    #[serde(rename = "posMode")]
    pub position_mode: String,
    /// 是否自动借币
    #[serde(rename = "autoLoan")]
    pub auto_loan: bool,
    /// 当前希腊字母展示方式
    #[serde(rename = "greeksType")]
    pub greeks_type: String,
    /// 手续费类型
    #[serde(rename = "feeType")]
    pub fee_type: String,
    /// 账户级别
    pub level: String,
    /// 特约用户的临时体验用户等级
    #[serde(rename = "levelTmp", default)]
    pub level_tmp: String,
    /// 合约逐仓保证金设置
    #[serde(rename = "ctIsoMode", default)]
    pub contract_isolated_margin_mode: String,
    /// 币币杠杆逐仓保证金设置
    #[serde(rename = "mgnIsoMode", default)]
    pub margin_isolated_margin_mode: String,
    /// 是否开启现货借币
    #[serde(rename = "enableSpotBorrow", default)]
    pub enable_spot_borrow: bool,
    /// 是否开启现货借币自动还币
    #[serde(rename = "spotBorrowAutoRepay", default)]
    pub spot_borrow_auto_repay: bool,
    /// 现货对冲类型
    #[serde(rename = "spotOffsetType", default)]
    pub spot_offset_type: String,
    /// 策略类型
    #[serde(rename = "stgyType", default)]
    pub strategy_type: String,
    /// 跟单角色类型
    #[serde(rename = "roleType", default)]
    pub role_type: String,
    /// 带单合约列表
    #[serde(rename = "traderInsts", default)]
    pub trader_instruments: Vec<String>,
    /// 现货跟单角色类型
    #[serde(rename = "spotRoleType", default)]
    pub spot_role_type: String,
    /// 现货带单产品列表
    #[serde(rename = "spotTraderInsts", default)]
    pub spot_trader_instruments: Vec<String>,
    /// 期权交易权限是否开通
    #[serde(rename = "opAuth", default)]
    pub option_authority: String,
    /// KYC等级
    #[serde(rename = "kycLv", default)]
    pub kyc_level: String,
    /// API Key备注
    #[serde(default)]
    pub label: String,
    /// API Key绑定的IP地址
    #[serde(default)]
    pub ip: String,
    /// API Key类型
    #[serde(rename = "type", default)]
    pub account_type: String,
    /// 爆仓档位
    #[serde(rename = "liquidationGear", default)]
    pub liquidation_gear: String,
    /// API Key权限
    #[serde(default)]
    pub perm: String,
    /// 结算币种
    #[serde(rename = "settleCcy", default)]
    pub settle_ccy: String,
    /// 可选结算币种列表
    #[serde(rename = "settleCcyList", default)]
    pub settle_ccy_list: Vec<String>,
    /// 兼容旧字段,当前账户配置接口不返回mgnMode
    #[serde(rename = "mgnMode", default, skip_serializing_if = "Option::is_none")]
    pub margin_mode: Option<MarginMode>,
}

/// 账户风险数据
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AccountRisk {
    /// 当前风险数据
    pub risk: String,
    /// 风险等级
    #[serde(rename = "riskLvl")]
    pub risk_level: String,
    /// 总权益
    #[serde(rename = "totalEq")]
    pub total_equity: String,
}

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

    #[test]
    fn parses_account_config_from_okx_config_response() {
        let value = serde_json::json!({
            "acctLv": "2",
            "acctStpMode": "cancel_maker",
            "autoLoan": false,
            "ctIsoMode": "automatic",
            "enableSpotBorrow": false,
            "greeksType": "PA",
            "feeType": "0",
            "ip": "",
            "type": "0",
            "kycLv": "3",
            "label": "v5 test",
            "level": "Lv1",
            "levelTmp": "",
            "liquidationGear": "-1",
            "mainUid": "44705892343619584",
            "mgnIsoMode": "automatic",
            "opAuth": "1",
            "perm": "read_only,withdraw,trade",
            "posMode": "long_short_mode",
            "roleType": "0",
            "spotBorrowAutoRepay": false,
            "spotOffsetType": "",
            "spotRoleType": "0",
            "spotTraderInsts": [],
            "stgyType": "0",
            "traderInsts": [],
            "uid": "44705892343619584",
            "settleCcy": "USDC",
            "settleCcyList": ["USD", "USDC", "USDG"]
        });

        let config: AccountConfig = serde_json::from_value(value).unwrap();

        assert_eq!(config.account_id, "44705892343619584");
        assert_eq!(config.main_uid, "44705892343619584");
        assert_eq!(config.account_level, "2");
        assert_eq!(config.account_stp_mode, "cancel_maker");
        assert_eq!(config.position_mode, "long_short_mode");
        assert!(!config.auto_loan);
        assert_eq!(config.greeks_type, "PA");
        assert_eq!(config.fee_type, "0");
        assert_eq!(config.level, "Lv1");
        assert_eq!(config.margin_mode, None);
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct SetLeverageRequest {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub inst_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ccy: Option<String>,
    pub lever: String,
    pub mgn_mode: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pos_side: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SetLeverageData {
    pub lever: String,
    pub mgn_mode: String,
    pub inst_id: String,
    pub pos_side: String,
}

/// 持仓信息结构体
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Position {
    /// 产品类型
    pub inst_type: String,
    /// 保证金模式 (cross: 全仓, isolated: 逐仓)
    pub mgn_mode: String,
    /// 持仓ID
    pub pos_id: String,
    /// 持仓方向 (long: 开平仓模式开多, short: 开平仓模式开空, net: 买卖模式)
    pub pos_side: String,
    /// 持仓数量
    pub pos: String,
    /// 仓位资产币种,仅适用于币币杠杆仓位
    pub pos_ccy: Option<String>,
    /// 可平仓数量,适用于币币杠杆, 交割/永续(开平仓模式),期权
    pub avail_pos: Option<String>,
    /// 开仓平均价
    pub avg_px: Option<String>,
    /// 未实现收益(以标记价格计算)
    pub upl: Option<String>,
    /// 未实现收益率(以标记价格计算)
    pub upl_ratio: Option<String>,
    /// 以最新成交价格计算的未实现收益
    pub upl_last_px: Option<String>,
    /// 以最新成交价格计算的未实现收益率
    pub upl_ratio_last_px: Option<String>,
    /// 产品ID,如 BTC-USD-180216
    pub inst_id: String,
    /// 杠杆倍数,不适用于期权以及组合保证金模式下的全仓仓位
    pub lever: Option<String>,
    /// 预估强平价,不适用于期权
    pub liq_px: Option<String>,
    /// 最新标记价格
    pub mark_px: Option<String>,
    /// 初始保证金,仅适用于全仓
    pub imr: Option<String>,
    /// 保证金余额,可增减,仅适用于逐仓
    pub margin: Option<String>,
    /// 保证金率
    pub mgn_ratio: Option<String>,
    /// 维持保证金
    pub mmr: Option<String>,
    /// 负债额,仅适用于币币杠杆
    pub liab: Option<String>,
    /// 负债币种,仅适用于币币杠杆
    pub liab_ccy: Option<String>,
    /// 利息,已经生成的未扣利息
    pub interest: Option<String>,
    /// 最新成交ID
    pub trade_id: Option<String>,
    /// 期权市值,仅适用于期权
    pub opt_val: Option<String>,
    /// 逐仓杠杆负债对应平仓挂单的数量
    pub pending_close_ord_liab_val: Option<String>,
    /// 以美金价值为单位的持仓数量
    pub notional_usd: Option<String>,
    /// 信号区,分为5档,从1到5,数字越小代表adl强度越弱
    pub adl: Option<String>,
    /// 占用保证金的币种
    pub ccy: Option<String>,
    /// 最新成交价
    pub last: Option<String>,
    /// 最新指数价格
    pub idx_px: Option<String>,
    /// 美金价格
    pub usd_px: Option<String>,
    /// 盈亏平衡价
    pub be_px: Option<String>,
    /// 美金本位持仓仓位delta,仅适用于期权
    pub delta_bs: Option<String>,
    /// 币本位持仓仓位delta,仅适用于期权
    pub delta_pa: Option<String>,
    /// 美金本位持仓仓位gamma,仅适用于期权
    pub gamma_bs: Option<String>,
    /// 币本位持仓仓位gamma,仅适用于期权
    pub gamma_pa: Option<String>,
    /// 美金本位持仓仓位theta,仅适用于期权
    pub theta_bs: Option<String>,
    /// 币本位持仓仓位theta,仅适用于期权
    pub theta_pa: Option<String>,
    /// 美金本位持仓仓位vega,仅适用于期权
    pub vega_bs: Option<String>,
    /// 币本位持仓仓位vega,仅适用于期权
    pub vega_pa: Option<String>,
    /// 现货对冲占用数量,适用于组合保证金模式
    pub spot_in_use_amt: Option<String>,
    /// 现货对冲占用币种,适用于组合保证金模式
    pub spot_in_use_ccy: Option<String>,
    /// 用户自定义现货占用数量,适用于组合保证金模式
    pub cl_spot_in_use_amt: Option<String>,
    /// 系统计算得到的最大可能现货占用数量,适用于组合保证金模式
    pub max_spot_in_use_amt: Option<String>,
    /// 已实现收益
    pub realized_pnl: Option<String>,
    /// 平仓订单累计收益额
    pub pnl: Option<String>,
    /// 累计手续费金额
    pub fee: Option<String>,
    /// 累计资金费用
    pub funding_fee: Option<String>,
    /// 累计爆仓罚金
    pub liq_penalty: Option<String>,
    /// 平仓策略委托订单
    pub close_order_algo: Option<Vec<CloseOrderAlgo>>,
    /// 持仓创建时间,Unix时间戳的毫秒数格式
    pub c_time: Option<String>,
    /// 最近一次持仓更新时间,Unix时间戳的毫秒数格式
    pub u_time: Option<String>,
    /// 外部业务id,e.g. 体验券id
    pub biz_ref_id: Option<String>,
    /// 外部业务类型
    pub biz_ref_type: Option<String>,
}