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
use crate::{
v5::api::{
BybitApi,
post::Post,
},
utils::serialize_option_as_string,
};
use serde::{
Serialize,
Deserialize,
};
use serde_json::Value;
use anyhow::Result;
const PATH: &'static str = "/v5/position/trading-stop";
impl BybitApi {
/// set trading stop.
///
/// # Arguments
///
/// * `params` - The parameters for set trading stop.
///
/// # Examples
///
/// ```rust
/// use rsbit::v5::api::{
/// post::position::set_trading_stop::{
/// SetTradingStopParameters,
/// SetTradingStopCategory
/// },
/// BybitApi,
/// };
/// #[tokio::main]
/// async fn main() {
/// let api = BybitApi::new();
/// let params = SetTradingStopParameters::new(
/// SetTradingStopCategory::Linear,
/// "BTCUSDT".to_string(),
/// 1,
/// ).with_take_profit(50000.0);
/// let response = api.set_trading_stop(params).await;
/// match response {
/// Ok(info) => {
/// // Handle the data
/// },
/// Err(err) => {
/// // Handle the error
/// }
/// }
/// }
/// ```
pub async fn set_trading_stop(&self, params: SetTradingStopParameters) -> Result<SetTradingStopResponse> {
self.post(PATH, Some(params)).await
}
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum SetTradingStopCategory {
Linear,
Inverse,
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SetTradingStopParameters {
category: SetTradingStopCategory,
symbol: String,
#[serde(serialize_with = "serialize_option_as_string")]
take_profit: Option<f64>,
#[serde(serialize_with = "serialize_option_as_string")]
stop_loss: Option<f64>,
#[serde(serialize_with = "serialize_option_as_string")]
trailing_stop: Option<f64>,
tp_trigger_by: Option<String>,
sl_trigger_by: Option<String>,
#[serde(serialize_with = "serialize_option_as_string")]
active_price: Option<f64>,
tpsl_mode: Option<String>,
#[serde(serialize_with = "serialize_option_as_string")]
tp_size: Option<f64>,
#[serde(serialize_with = "serialize_option_as_string")]
sl_size: Option<f64>,
#[serde(serialize_with = "serialize_option_as_string")]
tp_limit_price: Option<f64>,
#[serde(serialize_with = "serialize_option_as_string")]
sl_limit_price: Option<f64>,
tp_order_type: Option<String>,
sl_order_type: Option<String>,
position_idx: u8,
}
impl SetTradingStopParameters {
/// Creates a new instance of `SetTradingStopParameters`.
///
/// # Arguments
///
/// * `category` - The category.
/// * `symbol` - The symbol.
/// * `position_idx` - The position idx.
///
/// # Returns
///
/// A new instance of `SetTradingStopParameters`.
pub fn new(category: SetTradingStopCategory, symbol: String, position_idx: u8) -> Self {
Self {
category,
symbol,
take_profit: None,
stop_loss: None,
trailing_stop: None,
tp_trigger_by: None,
sl_trigger_by: None,
active_price: None,
tpsl_mode: None,
tp_size: None,
sl_size: None,
tp_limit_price: None,
sl_limit_price: None,
tp_order_type: None,
sl_order_type: None,
position_idx,
}
}
/// Sets the take profit for the set trading stop.
///
/// # Arguments
///
/// * `take_profit` - The take profit to set.
///
/// # Returns
///
/// The modified `SetTradingStopParameters` instance.
pub fn with_take_profit(mut self, take_profit: f64) -> Self {
self.take_profit = Some(take_profit);
self
}
/// Sets the stop loss for the set trading stop.
///
/// # Arguments
///
/// * `stop_loss` - The stop loss to set.
///
/// # Returns
///
/// The modified `SetTradingStopParameters` instance.
pub fn with_stop_loss(mut self, stop_loss: f64) -> Self {
self.stop_loss = Some(stop_loss);
self
}
/// Sets the trailing stop for the set trading stop.
///
/// # Arguments
///
/// * `trailing_stop` - The trailing stop to set.
///
/// # Returns
///
/// The modified `SetTradingStopParameters` instance.
pub fn with_trailing_stop(mut self, trailing_stop: f64) -> Self {
self.trailing_stop = Some(trailing_stop);
self
}
/// Sets the tp trigger by for the set trading stop.
///
/// # Arguments
///
/// * `tp_trigger_by` - The tp trigger by to set.
///
/// # Returns
///
/// The modified `SetTradingStopParameters` instance.
pub fn with_tp_trigger_by(mut self, tp_trigger_by: String) -> Self {
self.tp_trigger_by = Some(tp_trigger_by);
self
}
/// Sets the sl trigger by for the set trading stop.
///
/// # Arguments
///
/// * `sl_trigger_by` - The sl trigger by to set.
///
/// # Returns
///
/// The modified `SetTradingStopParameters` instance.
pub fn with_sl_trigger_by(mut self, sl_trigger_by: String) -> Self {
self.sl_trigger_by = Some(sl_trigger_by);
self
}
/// Sets the active price for the set trading stop.
///
/// # Arguments
///
/// * `active_price` - The active price to set.
///
/// # Returns
///
/// The modified `SetTradingStopParameters` instance.
pub fn with_active_price(mut self, active_price: f64) -> Self {
self.active_price = Some(active_price);
self
}
/// Sets the tpsl mode for the set trading stop.
///
/// # Arguments
///
/// * `tpsl_mode` - The tpsl mode to set.
///
/// # Returns
///
/// The modified `SetTradingStopParameters` instance.
pub fn with_tpsl_mode(mut self, tpsl_mode: String) -> Self {
self.tpsl_mode = Some(tpsl_mode);
self
}
/// Sets the tp size for the set trading stop.
///
/// # Arguments
///
/// * `tp_size` - The tp size to set.
///
/// # Returns
///
/// The modified `SetTradingStopParameters` instance.
pub fn with_tp_size(mut self, tp_size: f64) -> Self {
self.tp_size = Some(tp_size);
self
}
/// Sets the sl size for the set trading stop.
///
/// # Arguments
///
/// * `sl_size` - The sl size to set.
///
/// # Returns
///
/// The modified `SetTradingStopParameters` instance.
pub fn with_sl_size(mut self, sl_size: f64) -> Self {
self.sl_size = Some(sl_size);
self
}
/// Sets the tp limit price for the set trading stop.
///
/// # Arguments
///
/// * `tp_limit_price` - The tp limit price to set.
///
/// # Returns
///
/// The modified `SetTradingStopParameters` instance.
pub fn with_tp_limit_price(mut self, tp_limit_price: f64) -> Self {
self.tp_limit_price = Some(tp_limit_price);
self
}
/// Sets the sl limit price for the set trading stop.
///
/// # Arguments
///
/// * `sl_limit_price` - The sl limit price to set.
///
/// # Returns
///
/// The modified `SetTradingStopParameters` instance.
pub fn with_sl_limit_price(mut self, sl_limit_price: f64) -> Self {
self.sl_limit_price = Some(sl_limit_price);
self
}
/// Sets the tp order type for the set trading stop.
///
/// # Arguments
///
/// * `tp_order_type` - The tp order type to set.
///
/// # Returns
///
/// The modified `SetTradingStopParameters` instance.
pub fn with_tp_order_type(mut self, tp_order_type: String) -> Self {
self.tp_order_type = Some(tp_order_type);
self
}
/// Sets the sl order type for the set trading stop.
///
/// # Arguments
///
/// * `sl_order_type` - The sl order type to set.
///
/// # Returns
///
/// The modified `SetTradingStopParameters` instance.
pub fn with_sl_order_type(mut self, sl_order_type: String) -> Self {
self.sl_order_type = Some(sl_order_type);
self
}
/// Sets the position idx for the set trading stop.
///
/// # Arguments
///
/// * `position_idx` - The position idx to set.
///
/// # Returns
///
/// The modified `SetTradingStopParameters` instance.
pub fn with_position_idx(mut self, position_idx: u8) -> Self {
self.position_idx = position_idx;
self
}
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SetTradingStopResponse {
ret_code: i32,
ret_msg: String,
result: Value,
ret_ext_info: Value,
time: u64,
}
impl SetTradingStopResponse {
pub fn ret_code(&self) -> i32 {
self.ret_code
}
pub fn set_ret_code(&mut self, ret_code: i32) {
self.ret_code = ret_code;
}
pub fn ret_msg(&self) -> &str {
&self.ret_msg
}
pub fn set_ret_msg(&mut self, ret_msg: String) {
self.ret_msg = ret_msg;
}
pub fn result(&self) -> &Value {
&self.result
}
pub fn set_result(&mut self, result: Value) {
self.result = result;
}
pub fn ret_ext_info(&self) -> &Value {
&self.ret_ext_info
}
pub fn set_ret_ext_info(&mut self, ret_ext_info: Value) {
self.ret_ext_info = ret_ext_info;
}
pub fn time(&self) -> u64 {
self.time
}
pub fn set_time(&mut self, time: u64) {
self.time = time;
}
}