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
//! Position API endpoints.
use crate::client::BybitClient;
use crate::error::Result;
use crate::models::position::*;
use crate::models::Category;
use crate::models::*;
impl BybitClient {
/// Get position list.
///
/// # Arguments
/// * `category` - Product category
/// * `symbol` - Optional symbol filter
/// * `settle_coin` - Optional settle coin filter (e.g., "USDT")
pub async fn get_positions(
&self,
category: Category,
symbol: Option<&str>,
settle_coin: Option<&str>,
) -> Result<PositionList> {
let cat_str = category.to_string();
let mut params = vec![("category", cat_str.as_str())];
if let Some(s) = symbol {
params.push(("symbol", s));
}
if let Some(sc) = settle_coin {
params.push(("settleCoin", sc));
}
self.get("/v5/position/list", ¶ms).await
}
/// Set leverage.
///
/// # Arguments
/// * `category` - Product category
/// * `symbol` - Symbol name
/// * `buy_leverage` - Buy leverage
/// * `sell_leverage` - Sell leverage
// FIXME(typed-signature): falls back to `serde_json::Value` because the
// OpenAPI spec referenced a response/request type that gen-sdk-rust could
// not auto-resolve. Replace with a typed struct in a follow-up PR.
pub async fn set_leverage(
&self,
category: Category,
symbol: &str,
buy_leverage: &str,
sell_leverage: &str,
) -> Result<serde_json::Value> {
let params = SetLeverageParams {
category,
symbol: symbol.to_string(),
buy_leverage: buy_leverage.to_string(),
sell_leverage: sell_leverage.to_string(),
};
self.post("/v5/position/set-leverage", ¶ms).await
}
/// Set trading stop (TP/SL).
///
/// # Arguments
/// * `params` - Trading stop parameters
// FIXME(typed-signature): falls back to `serde_json::Value` because the
// OpenAPI spec referenced a response/request type that gen-sdk-rust could
// not auto-resolve. Replace with a typed struct in a follow-up PR.
pub async fn set_trading_stop(&self, params: TradingStopParams) -> Result<serde_json::Value> {
self.post("/v5/position/trading-stop", ¶ms).await
}
/// Switch position mode.
///
/// # Arguments
/// * `category` - Product category
/// * `mode` - Position mode (0=merged, 3=both sides)
// FIXME(typed-signature): falls back to `serde_json::Value` because the
// OpenAPI spec referenced a response/request type that gen-sdk-rust could
// not auto-resolve. Replace with a typed struct in a follow-up PR.
pub async fn switch_position_mode(
&self,
category: Category,
mode: PositionMode,
) -> Result<serde_json::Value> {
let params = SwitchPositionModeParams {
category,
symbol: None,
coin: None,
mode: mode as i32,
};
self.post("/v5/position/switch-mode", ¶ms).await
}
/// Set risk limit.
///
/// # Arguments
/// * `category` - Product category
/// * `symbol` - Symbol name
/// * `risk_id` - Risk limit ID
// FIXME(typed-signature): falls back to `serde_json::Value` because the
// OpenAPI spec referenced a response/request type that gen-sdk-rust could
// not auto-resolve. Replace with a typed struct in a follow-up PR.
pub async fn set_risk_limit(
&self,
category: Category,
symbol: &str,
risk_id: i32,
) -> Result<serde_json::Value> {
let params = SetRiskLimitParams {
category,
symbol: symbol.to_string(),
risk_id,
position_idx: None,
};
self.post("/v5/position/set-risk-limit", ¶ms).await
}
/// Add or reduce margin.
///
/// # Arguments
/// * `category` - Product category
/// * `symbol` - Symbol name
/// * `margin` - Margin amount (positive to add, negative to reduce)
// FIXME(typed-signature): falls back to `serde_json::Value` because the
// OpenAPI spec referenced a response/request type that gen-sdk-rust could
// not auto-resolve. Replace with a typed struct in a follow-up PR.
pub async fn add_margin(
&self,
category: Category,
symbol: &str,
margin: &str,
) -> Result<serde_json::Value> {
let params = AddMarginParams {
category,
symbol: symbol.to_string(),
margin: margin.to_string(),
position_idx: None,
};
self.post("/v5/position/add-margin", ¶ms).await
}
/// Get closed PnL history.
///
/// # Arguments
/// * `category` - Product category
/// * `symbol` - Optional symbol filter
/// * `limit` - Optional limit (default 20)
pub async fn get_closed_pnl(
&self,
category: Category,
symbol: Option<&str>,
limit: Option<u32>,
) -> Result<ClosedPnlList> {
let cat_str = category.to_string();
let limit_str = limit.unwrap_or(20).to_string();
let mut params = vec![
("category", cat_str.as_str()),
("limit", limit_str.as_str()),
];
if let Some(s) = symbol {
params.push(("symbol", s));
}
self.get("/v5/position/closed-pnl", ¶ms).await
}
/// Get execution list (trade history).
///
/// # Arguments
/// * `category` - Product category
/// * `symbol` - Optional symbol filter
/// * `limit` - Optional limit (default 50)
pub async fn get_executions(
&self,
category: Category,
symbol: Option<&str>,
limit: Option<u32>,
) -> Result<ExecutionList> {
let cat_str = category.to_string();
let limit_str = limit.unwrap_or(50).to_string();
let mut params = vec![
("category", cat_str.as_str()),
("limit", limit_str.as_str()),
];
if let Some(s) = symbol {
params.push(("symbol", s));
}
self.get("/v5/execution/list", ¶ms).await
}
pub async fn confirm_new_risk_limit(
&self,
params: ConfirmNewRiskLimitParams,
) -> Result<ConfirmNewRiskLimitResponse> {
self.post("/v5/position/confirm-pending-mmr", ¶ms).await
}
pub async fn get_close_position(
&self,
category: Category,
symbol: Option<&str>,
start_time: Option<i64>,
end_time: Option<i64>,
limit: Option<i64>,
cursor: Option<&str>,
) -> Result<GetClosePositionResponse> {
let cat_str = category.to_string();
let start_time_str = start_time.map(|v| v.to_string());
let end_time_str = end_time.map(|v| v.to_string());
let limit_str = limit.map(|v| v.to_string());
let mut params = vec![("category", cat_str.as_str())];
if let Some(s) = symbol {
params.push(("symbol", s));
}
if let Some(ref st) = start_time_str {
params.push(("startTime", st.as_str()));
}
if let Some(ref et) = end_time_str {
params.push(("endTime", et.as_str()));
}
if let Some(ref l) = limit_str {
params.push(("limit", l.as_str()));
}
if let Some(c) = cursor {
params.push(("cursor", c));
}
self.get("/v5/position/get-closed-positions", ¶ms).await
}
#[allow(clippy::too_many_arguments)] // TODO(api-ergonomics): convert positional args to a typed `*Params` struct
pub async fn get_move_position_history(
&self,
category: Option<Category>,
symbol: Option<&str>,
start_time: Option<i64>,
end_time: Option<i64>,
status: Option<&str>,
block_trade_id: Option<&str>,
limit: Option<&str>,
cursor: Option<&str>,
) -> Result<GetMovePositionHistoryResponse> {
let cat_str = category.map(|c| c.to_string());
let start_time_str = start_time.map(|v| v.to_string());
let end_time_str = end_time.map(|v| v.to_string());
let mut params: Vec<(&str, &str)> = Vec::new();
if let Some(ref c) = cat_str {
params.push(("category", c.as_str()));
}
if let Some(s) = symbol {
params.push(("symbol", s));
}
if let Some(ref st) = start_time_str {
params.push(("startTime", st.as_str()));
}
if let Some(ref et) = end_time_str {
params.push(("endTime", et.as_str()));
}
if let Some(s) = status {
params.push(("status", s));
}
if let Some(b) = block_trade_id {
params.push(("blockTradeId", b));
}
if let Some(l) = limit {
params.push(("limit", l));
}
if let Some(c) = cursor {
params.push(("cursor", c));
}
self.get("/v5/position/move-history", ¶ms).await
}
pub async fn move_position(&self, params: MovePositionParams) -> Result<MovePositionResponse> {
self.post("/v5/position/move-positions", ¶ms).await
}
pub async fn set_auto_add_margin(
&self,
params: SetAutoAddMarginParams,
) -> Result<SetAutoAddMarginResponse> {
self.post("/v5/position/set-auto-add-margin", ¶ms).await
}
}