Skip to main content

bybit_api/models/
position.rs

1//! Position models.
2
3use crate::models::common::*;
4use serde::{Deserialize, Serialize};
5
6/// Position list response.
7#[derive(Debug, Clone, Serialize, Deserialize)]
8#[serde(rename_all = "camelCase")]
9pub struct PositionList {
10    /// Category
11    pub category: String,
12    /// List of positions
13    pub list: Vec<Position>,
14    /// Next page cursor
15    #[serde(default)]
16    pub next_page_cursor: String,
17}
18
19/// Position info.
20#[derive(Debug, Clone, Serialize, Deserialize)]
21#[serde(rename_all = "camelCase")]
22pub struct Position {
23    /// Position index
24    pub position_idx: i32,
25    /// Risk ID
26    #[serde(default)]
27    pub risk_id: i32,
28    /// Risk limit value
29    #[serde(default)]
30    pub risk_limit_value: String,
31    /// Symbol
32    pub symbol: String,
33    /// Side
34    pub side: String,
35    /// Size
36    pub size: String,
37    /// Average entry price
38    #[serde(default)]
39    pub avg_price: String,
40    /// Position value
41    #[serde(default)]
42    pub position_value: String,
43    /// Trade mode (0=cross, 1=isolated)
44    #[serde(default)]
45    pub trade_mode: i32,
46    /// Position status
47    #[serde(default)]
48    pub position_status: String,
49    /// Leverage
50    #[serde(default)]
51    pub leverage: String,
52    /// Mark price
53    #[serde(default)]
54    pub mark_price: String,
55    /// Liquidation price
56    #[serde(default)]
57    pub liq_price: String,
58    /// Bust price
59    #[serde(default)]
60    pub bust_price: String,
61    /// Position margin
62    #[serde(default)]
63    pub position_mm: String,
64    /// Position initial margin
65    #[serde(default)]
66    pub position_im: String,
67    /// Take profit price
68    #[serde(default)]
69    pub take_profit: String,
70    /// Stop loss price
71    #[serde(default)]
72    pub stop_loss: String,
73    /// Trailing stop
74    #[serde(default)]
75    pub trailing_stop: String,
76    /// Unrealised PnL
77    #[serde(default)]
78    pub unrealised_pnl: String,
79    /// Cumulative realised PnL
80    #[serde(default)]
81    pub cum_realised_pnl: String,
82    /// Created time
83    #[serde(default)]
84    pub created_time: String,
85    /// Updated time
86    #[serde(default)]
87    pub updated_time: String,
88}
89
90/// Set leverage request.
91#[derive(Debug, Clone, Serialize, Deserialize)]
92#[serde(rename_all = "camelCase")]
93pub struct SetLeverageParams {
94    /// Category
95    pub category: Category,
96    /// Symbol
97    pub symbol: String,
98    /// Buy leverage
99    pub buy_leverage: String,
100    /// Sell leverage
101    pub sell_leverage: String,
102}
103
104/// Trading stop request.
105#[derive(Debug, Clone, Serialize, Deserialize)]
106#[serde(rename_all = "camelCase")]
107pub struct TradingStopParams {
108    /// Category
109    pub category: Category,
110    /// Symbol
111    pub symbol: String,
112    /// Take profit price
113    #[serde(skip_serializing_if = "Option::is_none")]
114    pub take_profit: Option<String>,
115    /// Stop loss price
116    #[serde(skip_serializing_if = "Option::is_none")]
117    pub stop_loss: Option<String>,
118    /// Trailing stop
119    #[serde(skip_serializing_if = "Option::is_none")]
120    pub trailing_stop: Option<String>,
121    /// Take profit trigger
122    #[serde(skip_serializing_if = "Option::is_none")]
123    pub tp_trigger_by: Option<TriggerBy>,
124    /// Stop loss trigger
125    #[serde(skip_serializing_if = "Option::is_none")]
126    pub sl_trigger_by: Option<TriggerBy>,
127    /// Position index
128    #[serde(skip_serializing_if = "Option::is_none")]
129    pub position_idx: Option<i32>,
130}
131
132/// Switch position mode request.
133#[derive(Debug, Clone, Serialize, Deserialize)]
134#[serde(rename_all = "camelCase")]
135pub struct SwitchPositionModeParams {
136    /// Category
137    pub category: Category,
138    /// Symbol (optional)
139    #[serde(skip_serializing_if = "Option::is_none")]
140    pub symbol: Option<String>,
141    /// Coin (optional)
142    #[serde(skip_serializing_if = "Option::is_none")]
143    pub coin: Option<String>,
144    /// Mode (0=merged, 3=both sides)
145    pub mode: i32,
146}
147
148/// Set risk limit request.
149#[derive(Debug, Clone, Serialize, Deserialize)]
150#[serde(rename_all = "camelCase")]
151pub struct SetRiskLimitParams {
152    /// Category
153    pub category: Category,
154    /// Symbol
155    pub symbol: String,
156    /// Risk ID
157    pub risk_id: i32,
158    /// Position index
159    #[serde(skip_serializing_if = "Option::is_none")]
160    pub position_idx: Option<i32>,
161}
162
163/// Add margin request.
164#[derive(Debug, Clone, Serialize, Deserialize)]
165#[serde(rename_all = "camelCase")]
166pub struct AddMarginParams {
167    /// Category
168    pub category: Category,
169    /// Symbol
170    pub symbol: String,
171    /// Margin amount
172    pub margin: String,
173    /// Position index
174    #[serde(skip_serializing_if = "Option::is_none")]
175    pub position_idx: Option<i32>,
176}
177
178/// Closed PnL response.
179#[derive(Debug, Clone, Serialize, Deserialize)]
180#[serde(rename_all = "camelCase")]
181pub struct ClosedPnlList {
182    /// Category
183    pub category: String,
184    /// List of closed PnL records
185    pub list: Vec<ClosedPnl>,
186    /// Next page cursor
187    #[serde(default)]
188    pub next_page_cursor: String,
189}
190
191/// Closed PnL record.
192#[derive(Debug, Clone, Serialize, Deserialize)]
193#[serde(rename_all = "camelCase")]
194pub struct ClosedPnl {
195    /// Symbol
196    pub symbol: String,
197    /// Order ID
198    pub order_id: String,
199    /// Side
200    pub side: String,
201    /// Qty
202    pub qty: String,
203    /// Order price
204    pub order_price: String,
205    /// Order type
206    pub order_type: String,
207    /// Exec type
208    pub exec_type: String,
209    /// Closed size
210    pub closed_size: String,
211    /// Cumulative entry value
212    pub cum_entry_value: String,
213    /// Average entry price
214    pub avg_entry_price: String,
215    /// Cumulative exit value
216    pub cum_exit_value: String,
217    /// Average exit price
218    pub avg_exit_price: String,
219    /// Closed PnL
220    pub closed_pnl: String,
221    /// Fill count
222    pub fill_count: String,
223    /// Leverage
224    pub leverage: String,
225    /// Created time
226    pub created_time: String,
227    /// Updated time
228    pub updated_time: String,
229}
230
231/// Execution list response.
232#[derive(Debug, Clone, Serialize, Deserialize)]
233#[serde(rename_all = "camelCase")]
234pub struct ExecutionList {
235    /// Category
236    pub category: String,
237    /// List of executions
238    pub list: Vec<Execution>,
239    /// Next page cursor
240    #[serde(default)]
241    pub next_page_cursor: String,
242}
243
244/// Execution record.
245#[derive(Debug, Clone, Serialize, Deserialize)]
246#[serde(rename_all = "camelCase")]
247pub struct Execution {
248    /// Symbol
249    pub symbol: String,
250    /// Order ID
251    pub order_id: String,
252    /// Order link ID
253    #[serde(default)]
254    pub order_link_id: String,
255    /// Side
256    pub side: String,
257    /// Order price
258    pub order_price: String,
259    /// Order qty
260    pub order_qty: String,
261    /// Order type
262    pub order_type: String,
263    /// Exec ID
264    pub exec_id: String,
265    /// Exec price
266    pub exec_price: String,
267    /// Exec qty
268    pub exec_qty: String,
269    /// Exec fee
270    pub exec_fee: String,
271    /// Exec type
272    pub exec_type: String,
273    /// Exec value
274    pub exec_value: String,
275    /// Fee rate
276    #[serde(default)]
277    pub fee_rate: String,
278    /// Exec time
279    pub exec_time: String,
280}
281
282pub type AddReduceMarginResponse = AddReduceMarginResult;
283
284#[derive(Debug, Clone, Serialize, Deserialize)]
285#[serde(rename_all = "camelCase")]
286pub struct AddReduceMarginResult {
287    #[serde(default)]
288    pub category: String,
289    #[serde(default)]
290    pub symbol: String,
291    #[serde(default)]
292    pub position_idx: i32,
293    #[serde(default)]
294    pub risk_id: i32,
295    #[serde(default)]
296    pub risk_limit_value: String,
297    #[serde(default)]
298    pub size: String,
299    #[serde(default)]
300    pub avg_price: String,
301    #[serde(default)]
302    pub liq_price: String,
303    #[serde(default)]
304    pub bust_price: String,
305    #[serde(default)]
306    pub mark_price: String,
307    #[serde(default)]
308    pub position_value: String,
309    #[serde(default)]
310    pub leverage: String,
311    #[serde(default)]
312    pub auto_add_margin: i32,
313    #[serde(default)]
314    pub position_status: String,
315    #[serde(rename = "positionIM", default)]
316    pub position_im: String,
317    #[serde(rename = "positionMM", default)]
318    pub position_mm: String,
319    #[serde(default)]
320    pub take_profit: String,
321    #[serde(default)]
322    pub stop_loss: String,
323    #[serde(default)]
324    pub trailing_stop: String,
325    #[serde(default)]
326    pub unrealised_pnl: String,
327    #[serde(default)]
328    pub cum_realised_pnl: String,
329    #[serde(default)]
330    pub created_time: String,
331    #[serde(default)]
332    pub updated_time: String,
333}
334
335pub type ConfirmNewRiskLimitResponse = serde_json::Value;
336
337#[derive(Debug, Clone, Serialize, Deserialize)]
338#[serde(rename_all = "camelCase")]
339pub struct ConfirmNewRiskLimitParams {
340    pub category: String,
341    pub symbol: String,
342}
343
344pub type GetClosePositionResponse = GetClosePositionResult;
345
346#[derive(Debug, Clone, Serialize, Deserialize)]
347#[serde(rename_all = "camelCase")]
348pub struct GetClosePositionResult {
349    #[serde(default)]
350    pub category: String,
351    #[serde(default)]
352    pub list: Vec<GetClosePositionItem>,
353    #[serde(default)]
354    pub next_page_cursor: String,
355}
356
357#[derive(Debug, Clone, Serialize, Deserialize)]
358#[serde(rename_all = "camelCase")]
359pub struct GetClosePositionItem {
360    #[serde(default)]
361    pub symbol: String,
362    #[serde(default)]
363    pub side: String,
364    #[serde(default)]
365    pub qty: String,
366    #[serde(default)]
367    pub avg_entry_price: String,
368    #[serde(default)]
369    pub avg_exit_price: String,
370    #[serde(default)]
371    pub delivery_price: String,
372    #[serde(default)]
373    pub total_open_fee: String,
374    #[serde(default)]
375    pub total_close_fee: String,
376    #[serde(default)]
377    pub delivery_fee: String,
378    #[serde(default)]
379    pub total_pnl: String,
380    #[serde(default)]
381    pub open_time: i64,
382    #[serde(default)]
383    pub close_time: i64,
384}
385
386pub type GetClosedPnlResponse = GetClosedPnlResult;
387
388#[derive(Debug, Clone, Serialize, Deserialize)]
389#[serde(rename_all = "camelCase")]
390pub struct GetClosedPnlResult {
391    #[serde(default)]
392    pub category: String,
393    #[serde(default)]
394    pub list: Vec<ClosedPnlItem>,
395    #[serde(default)]
396    pub next_page_cursor: String,
397}
398
399#[derive(Debug, Clone, Serialize, Deserialize)]
400#[serde(rename_all = "camelCase")]
401pub struct ClosedPnlItem {
402    #[serde(default)]
403    pub symbol: String,
404    #[serde(default)]
405    pub order_id: String,
406    #[serde(default)]
407    pub side: String,
408    #[serde(default)]
409    pub qty: String,
410    #[serde(default)]
411    pub order_price: String,
412    #[serde(default)]
413    pub order_type: String,
414    #[serde(default)]
415    pub exec_type: String,
416    #[serde(default)]
417    pub closed_size: String,
418    #[serde(default)]
419    pub cum_entry_value: String,
420    #[serde(default)]
421    pub avg_entry_price: String,
422    #[serde(default)]
423    pub cum_exit_value: String,
424    #[serde(default)]
425    pub avg_exit_price: String,
426    #[serde(default)]
427    pub closed_pnl: String,
428    #[serde(default)]
429    pub fill_count: String,
430    #[serde(default)]
431    pub leverage: String,
432    #[serde(default)]
433    pub open_fee: String,
434    #[serde(default)]
435    pub close_fee: String,
436    #[serde(default)]
437    pub created_time: String,
438    #[serde(default)]
439    pub updated_time: String,
440}
441
442pub type GetMovePositionHistoryResponse = GetMovePositionHistoryResult;
443
444#[derive(Debug, Clone, Serialize, Deserialize)]
445#[serde(rename_all = "camelCase")]
446pub struct GetMovePositionHistoryResult {
447    #[serde(default)]
448    pub list: Vec<GetMovePositionHistoryItem>,
449    #[serde(default)]
450    pub next_page_cursor: String,
451}
452
453#[derive(Debug, Clone, Serialize, Deserialize)]
454#[serde(rename_all = "camelCase")]
455pub struct GetMovePositionHistoryItem {
456    #[serde(default)]
457    pub block_trade_id: String,
458    #[serde(default)]
459    pub category: String,
460    #[serde(default)]
461    pub order_id: String,
462    #[serde(default)]
463    pub user_id: i64,
464    #[serde(default)]
465    pub symbol: String,
466    #[serde(default)]
467    pub side: String,
468    #[serde(default)]
469    pub price: String,
470    #[serde(default)]
471    pub qty: String,
472    #[serde(default)]
473    pub exec_fee: String,
474    #[serde(default)]
475    pub status: String,
476    #[serde(default)]
477    pub exec_id: String,
478    #[serde(default)]
479    pub result_code: i32,
480    #[serde(default)]
481    pub result_message: String,
482    #[serde(default)]
483    pub created_at: i64,
484    #[serde(default)]
485    pub updated_at: i64,
486    #[serde(default)]
487    pub reject_party: String,
488}
489
490pub type GetPositionInfoResponse = GetPositionInfoResult;
491
492#[derive(Debug, Clone, Serialize, Deserialize)]
493#[serde(rename_all = "camelCase")]
494pub struct GetPositionInfoResult {
495    #[serde(default)]
496    pub category: String,
497    #[serde(default)]
498    pub list: Vec<PositionInfo>,
499    #[serde(default)]
500    pub next_page_cursor: String,
501}
502
503#[derive(Debug, Clone, Serialize, Deserialize)]
504#[serde(rename_all = "camelCase")]
505pub struct PositionInfo {
506    #[serde(default)]
507    pub position_idx: i32,
508    #[serde(default)]
509    pub risk_id: i32,
510    #[serde(default)]
511    pub risk_limit_value: String,
512    #[serde(default)]
513    pub symbol: String,
514    #[serde(default)]
515    pub side: String,
516    #[serde(default)]
517    pub size: String,
518    #[serde(default)]
519    pub avg_price: String,
520    #[serde(default)]
521    pub position_value: String,
522    #[serde(default)]
523    pub trade_mode: i32,
524    #[serde(default)]
525    pub auto_add_margin: i32,
526    #[serde(default)]
527    pub position_status: String,
528    #[serde(default)]
529    pub leverage: String,
530    #[serde(default)]
531    pub mark_price: String,
532    #[serde(default)]
533    pub liq_price: String,
534    #[serde(default)]
535    pub bust_price: String,
536    #[serde(rename = "positionIM", default)]
537    pub position_im: String,
538    #[serde(rename = "positionMM", default)]
539    pub position_mm: String,
540    #[serde(default)]
541    pub position_balance: String,
542    #[serde(default)]
543    pub tpsl_mode: String,
544    #[serde(default)]
545    pub take_profit: String,
546    #[serde(default)]
547    pub stop_loss: String,
548    #[serde(default)]
549    pub trailing_stop: String,
550    #[serde(default)]
551    pub unrealised_pnl: String,
552    #[serde(default)]
553    pub cur_realised_pnl: String,
554    #[serde(default)]
555    pub cum_realised_pnl: String,
556    #[serde(default)]
557    pub break_even_price: String,
558    #[serde(default)]
559    pub adl_rank_indicator: i32,
560    #[serde(default)]
561    pub is_reduce_only: bool,
562    #[serde(default)]
563    pub mmr_sys_updated_time: String,
564    #[serde(default)]
565    pub leverage_sys_updated_time: String,
566    #[serde(rename = "positionIMByMp", default)]
567    pub position_im_by_mp: String,
568    #[serde(rename = "positionMMByMp", default)]
569    pub position_mm_by_mp: String,
570    #[serde(default)]
571    pub session_avg_price: String,
572    #[serde(default)]
573    pub delta: String,
574    #[serde(default)]
575    pub gamma: String,
576    #[serde(default)]
577    pub vega: String,
578    #[serde(default)]
579    pub theta: String,
580    #[serde(default)]
581    pub seq: i64,
582    #[serde(default)]
583    pub created_time: String,
584    #[serde(default)]
585    pub updated_time: String,
586}
587
588pub type MovePositionResponse = MovePositionResult;
589
590#[derive(Debug, Clone, Serialize, Deserialize)]
591#[serde(rename_all = "camelCase")]
592pub struct MovePositionResult {
593    #[serde(default)]
594    pub block_trade_id: String,
595    #[serde(default)]
596    pub status: String,
597    #[serde(default)]
598    pub reject_party: String,
599}
600
601#[derive(Debug, Clone, Serialize, Deserialize)]
602#[serde(rename_all = "camelCase")]
603pub struct MovePositionLeg {
604    pub category: String,
605    pub symbol: String,
606    pub price: String,
607    pub side: String,
608    pub qty: String,
609}
610
611#[derive(Debug, Clone, Serialize, Deserialize)]
612#[serde(rename_all = "camelCase")]
613pub struct MovePositionParams {
614    pub from_uid: String,
615    pub to_uid: String,
616    pub list: Vec<MovePositionLeg>,
617}
618
619pub type SetAutoAddMarginResponse = serde_json::Value;
620
621#[derive(Debug, Clone, Serialize, Deserialize)]
622#[serde(rename_all = "camelCase")]
623pub struct SetAutoAddMarginParams {
624    pub category: String,
625    pub symbol: String,
626    pub auto_add_margin: i32,
627    #[serde(skip_serializing_if = "Option::is_none")]
628    pub position_idx: Option<i32>,
629}
630
631pub type SetLeverageResponse = serde_json::Value;
632
633pub type SwitchPositionModeResponse = serde_json::Value;