bybit_api/api/
position.rs1use crate::client::BybitClient;
4use crate::error::Result;
5use crate::models::position::*;
6use crate::models::*;
7
8impl BybitClient {
9 pub async fn get_positions(
16 &self,
17 category: Category,
18 symbol: Option<&str>,
19 settle_coin: Option<&str>,
20 ) -> Result<PositionList> {
21 let cat_str = category.to_string();
22 let mut params = vec![("category", cat_str.as_str())];
23
24 if let Some(s) = symbol {
25 params.push(("symbol", s));
26 }
27 if let Some(sc) = settle_coin {
28 params.push(("settleCoin", sc));
29 }
30
31 self.get("/v5/position/list", ¶ms).await
32 }
33
34 pub async fn set_leverage(
42 &self,
43 category: Category,
44 symbol: &str,
45 buy_leverage: &str,
46 sell_leverage: &str,
47 ) -> Result<serde_json::Value> {
48 let params = SetLeverageParams {
49 category,
50 symbol: symbol.to_string(),
51 buy_leverage: buy_leverage.to_string(),
52 sell_leverage: sell_leverage.to_string(),
53 };
54
55 self.post("/v5/position/set-leverage", ¶ms).await
56 }
57
58 pub async fn set_trading_stop(&self, params: TradingStopParams) -> Result<serde_json::Value> {
63 self.post("/v5/position/trading-stop", ¶ms).await
64 }
65
66 pub async fn switch_position_mode(
72 &self,
73 category: Category,
74 mode: PositionMode,
75 ) -> Result<serde_json::Value> {
76 let params = SwitchPositionModeParams {
77 category,
78 symbol: None,
79 coin: None,
80 mode: mode as i32,
81 };
82
83 self.post("/v5/position/switch-mode", ¶ms).await
84 }
85
86 pub async fn set_risk_limit(
93 &self,
94 category: Category,
95 symbol: &str,
96 risk_id: i32,
97 ) -> Result<serde_json::Value> {
98 let params = SetRiskLimitParams {
99 category,
100 symbol: symbol.to_string(),
101 risk_id,
102 position_idx: None,
103 };
104
105 self.post("/v5/position/set-risk-limit", ¶ms).await
106 }
107
108 pub async fn add_margin(
115 &self,
116 category: Category,
117 symbol: &str,
118 margin: &str,
119 ) -> Result<serde_json::Value> {
120 let params = AddMarginParams {
121 category,
122 symbol: symbol.to_string(),
123 margin: margin.to_string(),
124 position_idx: None,
125 };
126
127 self.post("/v5/position/add-margin", ¶ms).await
128 }
129
130 pub async fn get_closed_pnl(
137 &self,
138 category: Category,
139 symbol: Option<&str>,
140 limit: Option<u32>,
141 ) -> Result<ClosedPnlList> {
142 let cat_str = category.to_string();
143 let limit_str = limit.unwrap_or(20).to_string();
144 let mut params = vec![
145 ("category", cat_str.as_str()),
146 ("limit", limit_str.as_str()),
147 ];
148
149 if let Some(s) = symbol {
150 params.push(("symbol", s));
151 }
152
153 self.get("/v5/position/closed-pnl", ¶ms).await
154 }
155
156 pub async fn get_executions(
163 &self,
164 category: Category,
165 symbol: Option<&str>,
166 limit: Option<u32>,
167 ) -> Result<ExecutionList> {
168 let cat_str = category.to_string();
169 let limit_str = limit.unwrap_or(50).to_string();
170 let mut params = vec![
171 ("category", cat_str.as_str()),
172 ("limit", limit_str.as_str()),
173 ];
174
175 if let Some(s) = symbol {
176 params.push(("symbol", s));
177 }
178
179 self.get("/v5/execution/list", ¶ms).await
180 }
181}