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
#![allow(unused)]
use async_trait::async_trait;
use std::{
collections::{BTreeMap, HashMap},
pin::Pin,
sync::Arc,
};
use futures::Future;
use reqwest::Method;
use serde_json::Value;
use crate::endpoints::v5trade;
use super::http_manager::{HttpManager, Manager};
#[async_trait]
pub trait Trade {
fn new(http_manager: Arc<HttpManager>) -> Self;
async fn place_order(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn amend_order(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn cancel_order(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn get_open_orders(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn cancel_all_orders(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn get_order_history(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn amend_batch_order(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn cancel_batch_order(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn get_borrow_quota(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn set_dcp(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
}
pub struct TradeHTTP {
http_manager: Arc<HttpManager>,
}
#[async_trait]
impl Trade for TradeHTTP {
///
///
//// Initialize the TradeHTTP by passing the HttpManager
///
///
fn new(http_manager: Arc<HttpManager>) -> Self {
TradeHTTP { http_manager }
}
////
/// This method supports to create the order for spot, spot margin, linear perpetual, inverse futures and options.
/// Required args:
/// category (string): Product type Unified account: spot, linear, optionNormal account: linear, inverse. Please note that category is not involved with business logic
/// symbol (string): Symbol name
/// side (string): Buy, Sell
/// orderType (string): Market, Limit
/// qty (string): Order quantity
/// Returns:
/// Request results as JSON data.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/order/create-order
async fn place_order(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
let endpoint = v5trade::Trade::PlaceOrder.to_string();
self.http_manager
.submit_post_request(Method::POST, &endpoint, true, query)
.await
}
/// Unified account covers: Linear contract / Options
/// Normal account covers: USDT perpetual / Inverse perpetual / Inverse futures
/// Required args:
/// category (string): Product type Unified account: spot, linear, optionNormal account: linear, inverse. Please note that category is not involved with business logic
/// symbol (string): Symbol name
/// Returns:
/// Request results as JSON data.
/// Additional information:
async fn amend_order(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
self.http_manager
.submit_post_request(
Method::POST,
&v5trade::Trade::AmendOrder.to_string(),
true,
query,
)
.await
}
/// Unified account covers: Spot / Linear contract / Options
/// Normal account covers: USDT perpetual / Inverse perpetual / Inverse futures
/// Required args:
/// category (string): Product type Unified account: spot, linear, optionNormal account: linear, inverse. Please note that category is not involved with business logic
/// symbol (string): Symbol name
/// orderId (string): Order ID. Either orderId or orderLinkId is required
/// orderLinkId (string): User customised order ID. Either orderId or orderLinkId is required
/// Returns:
/// Request results as JSON data.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/order/cancel-order
async fn cancel_order(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
self.http_manager
.submit_post_request(
Method::POST,
&v5trade::Trade::CancelOrder.to_string(),
true,
query,
)
.await
}
/// Query unfilled or partially filled orders in real-time. To query older order records, please use the order history interface.
/// Required args:
/// category (string): Product type Unified account: spot, linear, optionNormal account: linear, inverse. Please note that category is not involved with business logic
/// Returns:
/// Request results as JSON data.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/order/open-order
async fn get_open_orders(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
self.http_manager
.submit_request(
Method::GET,
&v5trade::Trade::GetOpenOrders.to_string(),
query,
true,
)
.await
}
/// Cancel all open orders
/// Required args:
/// category (string): Product type
/// Unified account: spot, linear, option
/// Normal account: linear, inverse.
/// Please note that category is not involved with business logic. If cancel all by baseCoin, it will cancel all linear & inverse orders
/// Returns:
/// Request results as JSON data.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/order/cancel-all
async fn cancel_all_orders(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
self.http_manager
.submit_post_request(
Method::POST,
&v5trade::Trade::CancelAllOrders.to_string(),
true,
query,
)
.await
}
/// Query order history. As order creation/cancellation is asynchronous, the data returned from this endpoint may delay.
/// If you want to get real-time order information, you could query this endpoint or rely on the websocket stream (recommended).
/// Required args:
/// category (string): Product type
/// Unified account: spot, linear, option
/// Normal account: linear, inverse.
/// Please note that category is not involved with business logic
/// Returns:
/// Request results as JSON data.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/order/order-list
async fn get_order_history(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
self.http_manager
.submit_request(
Method::GET,
&v5trade::Trade::GetOrderHistory.to_string(),
query,
true,
)
.await
}
/// Covers: Option (Unified Account)
/// Required args:
/// category (string): Product type. option
/// request (array): Object
/// > symbol (string): Symbol name
/// > side (string): Buy, Sell
/// > orderType (string): Market, Limit
/// > qty (string): Order quantity
/// Returns:
/// Request results as JSON data.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/order/batch-place
async fn amend_batch_order(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
self.http_manager
.submit_post_request(
Method::POST,
&v5trade::Trade::BatchAmendOrder.to_string(),
true,
query,
)
.await
}
/// This endpoint allows you to cancel more than one open order in a single request.
/// Required args:
/// category (string): Product type. option
/// request (array): Object
/// > symbol (string): Symbol name
/// Returns:
/// Request results as JSON data.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/order/batch-cancel
async fn cancel_batch_order(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
self.http_manager
.submit_post_request(
Method::POST,
&v5trade::Trade::CancelAllOrders.to_string(),
true,
query,
)
.await
}
/// Query the qty and amount of borrowable coins in spot account.
/// Required args:
/// category (string): Product type. spot
/// symbol (string): Symbol name
/// side (string): Transaction side. Buy,Sell
/// Returns:
/// Request results as JSON data.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/order/spot-borrow-quota
async fn get_borrow_quota(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
self.http_manager
.submit_request(
Method::GET,
&v5trade::Trade::GetBorrowQuota.to_string(),
query,
true,
)
.await
}
/// Covers: Option (Unified Account)
/// Required args:
/// timeWindow (integer): Disconnection timing window time. [10, 300], unit: second
/// Returns:
/// Request results as JSON data.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/order/dcp
async fn set_dcp(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
self.http_manager
.submit_post_request(
Method::POST,
&v5trade::Trade::SetDcp.to_string(),
true,
query,
)
.await
}
}