rusty_bybit/
trade.rs

1//! Trading endpoints for order management
2//!
3//! Provides functionality for creating, canceling, and querying orders.
4//!
5//! # Example
6//!
7//! ````rust,no_run
8//! use rusty_bybit::BybitClient;
9//! use rusty_bybit::CreateOrderRequest;
10//!
11//! #[tokio::main]
12//! async fn main() {
13//!     let client = BybitClient::testnet();
14//!     let request = CreateOrderRequest {
15//!         category: "linear".to_string(),
16//!         symbol: "BTCUSDT".to_string(),
17//!         side: "Buy".to_string(),
18//!         order_type: "Limit".to_string(),
19//!         qty: Some("0.001".to_string()),
20//!         price: Some("28000".to_string()),
21//!         ..Default::default()
22//!     };
23//!     let response = client.create_order(&request).await.unwrap();
24//!     println!("Order ID: {}", response.order_id);
25//! }
26//! ```
27
28use crate::client::BybitClient;
29use crate::error::Result;
30use crate::types::{CreateOrderRequest, CreateOrderResponse, OrderList};
31
32impl BybitClient {
33    pub async fn create_order(&self, request: &CreateOrderRequest) -> Result<CreateOrderResponse> {
34        let body = serde_json::to_value(request)?;
35        self.post("/v5/order/create", Some(body)).await
36    }
37
38    pub async fn cancel_order(
39        &self,
40        category: &str,
41        order_id: &str,
42        symbol: &str,
43    ) -> Result<serde_json::Value> {
44        let body = serde_json::json!({
45            "category": category,
46            "orderId": order_id,
47            "symbol": symbol,
48        });
49        self.post("/v5/order/cancel", Some(body)).await
50    }
51
52    pub async fn cancel_all_orders(
53        &self,
54        category: &str,
55        symbol: &str,
56    ) -> Result<serde_json::Value> {
57        let body = serde_json::json!({
58            "category": category,
59            "symbol": symbol,
60        });
61        self.post("/v5/order/cancel-all", Some(body)).await
62    }
63
64    pub async fn get_order(&self, category: &str, order_id: &str) -> Result<OrderList> {
65        let query = vec![("category", category), ("orderId", order_id)];
66        self.get("/v5/order/realtime", Some(query)).await
67    }
68
69    pub async fn get_open_orders(&self, category: &str) -> Result<OrderList> {
70        let query = vec![("category", category)];
71        self.get("/v5/order/realtime", Some(query)).await
72    }
73}
74
75#[cfg(test)]
76mod tests {
77    #[test]
78    fn test_trade_module_exists() {}
79}