rsbit 0.5.4

This is a library for the Bybit API.
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
use crate::{
    v5::api::{
        BybitApi,
        get::Get,
    },
    utils::{
        deserialize_f64,
        deserialize_string_to_u64,
    },
};

use serde::{
    Serialize,
    Deserialize,
};
use serde_json::Value;
use anyhow::Result;

const PATH: &'static str = "/v5/position/closed-pnl";

impl BybitApi {
    /// Retrieves the closed pnl based on the provided parameters.
    ///
    /// # Arguments
    ///
    /// * `params` - The parameters for retrieving closed pnl.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use rsbit::v5::api::{
    ///     get::position::get_closed_pnl::{
    ///         GetClosedPnlParameters,
    ///         GetClosedPnlCategory
    ///     },
    ///     BybitApi,
    /// };
    /// #[tokio::main]
    /// async fn main() {
    ///     let api = BybitApi::new();
    ///     let params = GetClosedPnlParameters::new(GetClosedPnlCategory::Linear);
    ///     let response = api.get_closed_pnl(params).await;
    ///     match response {
    ///         Ok(info) => {
    ///             // Handle the data
    ///         },
    ///         Err(err) => {
    ///             // Handle the error
    ///         }
    ///     }
    /// }
    /// ```
    pub async fn get_closed_pnl(&self, params: GetClosedPnlParameters) -> Result<GetClosedPnlResponse> {
        self.get(PATH, Some(params), true).await
    }
}

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum GetClosedPnlCategory {
    Linear,
    Inverse,
}

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GetClosedPnlParameters {
    category: GetClosedPnlCategory,
    symbol: Option<String>,
    start_time: Option<u64>,
    end_time: Option<u64>,
    limit: Option<u32>,
    cursor: Option<String>,
}

impl GetClosedPnlParameters {
    /// Creates a new instance of `GetClosedPnlParameters` with the specified category.
    ///
    /// # Arguments
    ///
    /// * `category` - The category of the execution.
    ///
    /// # Returns
    ///
    /// A new instance of `GetClosedPnlParameters`.
    pub fn new(category: GetClosedPnlCategory) -> Self {
        Self {
            category,
            symbol: None,
            start_time: None,
            end_time: None,
            limit: None,
            cursor: None,
        }
    }

    /// Sets the symbol for the execution parameters.
    ///
    /// # Arguments
    ///
    /// * `symbol` - The symbol to set.
    ///
    /// # Returns
    ///
    /// The modified `GetExecutionParameters` instance.
    pub fn with_symbol(mut self, symbol: String) -> Self {
        self.symbol = Some(symbol);
        self
    }

    /// Sets the start time for the execution parameters.
    ///
    /// # Arguments
    ///
    /// * `start time` - The start time to set.
    ///
    /// # Returns
    ///
    /// The modified `GetExecutionParameters` instance.
    pub fn with_start_time(mut self, start_time: u64) -> Self {
        self.start_time = Some(start_time);
        self
    }

    /// Sets the end time for the execution parameters.
    ///
    /// # Arguments
    ///
    /// * `end time` - The end time to set.
    ///
    /// # Returns
    ///
    /// The modified `GetExecutionParameters` instance.
    pub fn with_end_time(mut self, end_time: u64) -> Self {
        self.end_time = Some(end_time);
        self
    }

    /// Sets the limit for the execution parameters.
    ///
    /// # Arguments
    ///
    /// * `limit` - The limit to set.
    ///
    /// # Returns
    ///
    /// The modified `GetExecutionParameters` instance.
    pub fn with_limit(mut self, limit: u32) -> Self {
        self.limit = Some(limit);
        self
    }

    /// Sets the cursor for the execution parameters.
    ///
    /// # Arguments
    ///
    /// * `cursor` - The cursor to set.
    ///
    /// # Returns
    ///
    /// The modified `GetExecutionParameters` instance.
    pub fn with_cursor(mut self, cursor: String) -> Self {
        self.cursor = Some(cursor);
        self
    }
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GetClosedPnlResponse {
    ret_code: i32,
    ret_msg: String,
    result: ClosedPnlResult,
    ret_ext_info: Value,
    time: u64,
}
impl GetClosedPnlResponse {
    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) -> &ClosedPnlResult {
        &self.result
    }

    pub fn set_result(&mut self, result: ClosedPnlResult) {
        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;
    }
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ClosedPnlResult {
    category: String,
    next_page_cursor: String,
    list: Vec<ClosedPnl>
}
impl ClosedPnlResult {
    pub fn category(&self) -> &str {
        &self.category
    }

    pub fn set_category(&mut self, category: String) {
        self.category = category;
    }

    pub fn next_page_cursor(&self) -> &str {
        &self.next_page_cursor
    }

    pub fn set_next_page_cursor(&mut self, next_page_cursor: String) {
        self.next_page_cursor = next_page_cursor;
    }

    pub fn list(&self) -> &Vec<ClosedPnl> {
        &self.list
    }

    pub fn set_list(&mut self, list: Vec<ClosedPnl>) {
        self.list = list;
    }
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ClosedPnl {
    symbol: String,
    order_type: String,
    leverage: String,
    #[serde(deserialize_with = "deserialize_string_to_u64")]
    updated_time: u64,
    side: String,
    order_id: String,
    #[serde(deserialize_with = "deserialize_f64")]
    closed_pnl: f64,
    #[serde(deserialize_with = "deserialize_f64")]
    avg_entry_price: f64,
    #[serde(deserialize_with = "deserialize_f64")]
    qty: f64,
    #[serde(deserialize_with = "deserialize_f64")]
    cum_entry_value: f64,
    #[serde(deserialize_with = "deserialize_string_to_u64")]
    created_time: u64,
    #[serde(deserialize_with = "deserialize_f64")]
    order_price: f64,
    #[serde(deserialize_with = "deserialize_f64")]
    closed_size: f64,
    #[serde(deserialize_with = "deserialize_f64")]
    avg_exit_price: f64,
    exec_type: String,
    #[serde(deserialize_with = "deserialize_string_to_u64")]
    fill_count: u64,
    #[serde(deserialize_with = "deserialize_f64")]
    cum_exit_value: f64,
}

impl ClosedPnl {
    
    pub fn symbol(&self) -> &str {
        &self.symbol
    }

    pub fn set_symbol(&mut self, symbol: String) {
        self.symbol = symbol;
    }

    pub fn order_type(&self) -> &str {
        &self.order_type
    }

    pub fn set_order_type(&mut self, order_type: String) {
        self.order_type = order_type;
    }

    pub fn leverage(&self) -> &str {
        &self.leverage
    }

    pub fn set_leverage(&mut self, leverage: String) {
        self.leverage = leverage;
    }

    pub fn updated_time(&self) -> u64 {
        self.updated_time
    }

    pub fn set_updated_time(&mut self, updated_time: u64) {
        self.updated_time = updated_time;
    }

    pub fn side(&self) -> &str {
        &self.side
    }

    pub fn set_side(&mut self, side: String) {
        self.side = side;
    }

    pub fn order_id(&self) -> &str {
        &self.order_id
    }

    pub fn set_order_id(&mut self, order_id: String) {
        self.order_id = order_id;
    }

    pub fn closed_pnl(&self) -> f64 {
        self.closed_pnl
    }

    pub fn set_closed_pnl(&mut self, closed_pnl: f64) {
        self.closed_pnl = closed_pnl;
    }

    pub fn avg_entry_price(&self) -> f64 {
        self.avg_entry_price
    }

    pub fn set_avg_entry_price(&mut self, avg_entry_price: f64) {
        self.avg_entry_price = avg_entry_price;
    }

    pub fn qty(&self) -> f64 {
        self.qty
    }

    pub fn set_qty(&mut self, qty: f64) {
        self.qty = qty;
    }

    pub fn cum_entry_value(&self) -> f64 {
        self.cum_entry_value
    }

    pub fn set_cum_entry_value(&mut self, cum_entry_value: f64) {
        self.cum_entry_value = cum_entry_value;
    }

    pub fn created_time(&self) -> u64 {
        self.created_time
    }

    pub fn set_created_time(&mut self, created_time: u64) {
        self.created_time = created_time;
    }

    pub fn order_price(&self) -> f64 {
        self.order_price
    }

    pub fn set_order_price(&mut self, order_price: f64) {
        self.order_price = order_price;
    }

    pub fn closed_size(&self) -> f64 {
        self.closed_size
    }

    pub fn set_closed_size(&mut self, closed_size: f64) {
        self.closed_size = closed_size;
    }

    pub fn avg_exit_price(&self) -> f64 {
        self.avg_exit_price
    }

    pub fn set_avg_exit_price(&mut self, avg_exit_price: f64) {
        self.avg_exit_price = avg_exit_price;
    }

    pub fn exec_type(&self) -> &str {
        &self.exec_type
    }

    pub fn set_exec_type(&mut self, exec_type: String) {
        self.exec_type = exec_type;
    }

    pub fn fill_count(&self) -> u64 {
        self.fill_count
    }

    pub fn set_fill_count(&mut self, fill_count: u64) {
        self.fill_count = fill_count;
    }

    pub fn cum_exit_value(&self) -> f64 {
        self.cum_exit_value
    }

    pub fn set_cum_exit_value(&mut self, cum_exit_value: f64) {
        self.cum_exit_value = cum_exit_value;
    }

}