use crate::client::BybitClient;
use crate::error::Result;
use crate::models::trade::*;
use crate::models::*;
use tracing::info;
impl BybitClient {
pub async fn place_order(&self, params: PlaceOrderParams) -> Result<OrderResponse> {
params.validate()?;
info!(
symbol = %params.symbol,
side = ?params.side,
order_type = ?params.order_type,
qty = %params.qty,
"Placing order"
);
self.post("/v5/order/create", ¶ms).await
}
pub async fn amend_order(&self, params: AmendOrderParams) -> Result<OrderResponse> {
info!(
symbol = %params.symbol,
order_id = ?params.order_id,
"Amending order"
);
self.post("/v5/order/amend", ¶ms).await
}
pub async fn cancel_order(&self, params: CancelOrderParams) -> Result<OrderResponse> {
info!(
symbol = %params.symbol,
order_id = ?params.order_id,
"Cancelling order"
);
self.post("/v5/order/cancel", ¶ms).await
}
pub async fn cancel_all_orders(
&self,
category: Category,
symbol: Option<&str>,
) -> Result<CancelAllResponse> {
let params = CancelAllOrdersParams {
category,
symbol: symbol.map(|s| s.to_string()),
base_coin: None,
settle_coin: None,
};
info!(category = ?category, symbol = ?symbol, "Cancelling all orders");
self.post("/v5/order/cancel-all", ¶ms).await
}
pub async fn place_batch_order(
&self,
category: Category,
orders: Vec<PlaceOrderParams>,
) -> Result<BatchOrderResponse> {
for order in &orders {
order.validate()?;
}
let request = BatchOrderRequest {
category,
request: orders,
};
info!(category = ?category, "Placing batch orders");
self.post("/v5/order/create-batch", &request).await
}
pub async fn amend_batch_order(
&self,
category: Category,
orders: Vec<AmendOrderParams>,
) -> Result<BatchOrderResponse> {
#[derive(serde::Serialize)]
#[serde(rename_all = "camelCase")]
struct BatchAmendRequest {
category: Category,
request: Vec<AmendOrderParams>,
}
let request = BatchAmendRequest {
category,
request: orders,
};
self.post("/v5/order/amend-batch", &request).await
}
pub async fn cancel_batch_order(
&self,
category: Category,
orders: Vec<CancelOrderParams>,
) -> Result<BatchOrderResponse> {
#[derive(serde::Serialize)]
#[serde(rename_all = "camelCase")]
struct BatchCancelRequest {
category: Category,
request: Vec<CancelOrderParams>,
}
let request = BatchCancelRequest {
category,
request: orders,
};
self.post("/v5/order/cancel-batch", &request).await
}
pub async fn get_open_orders(
&self,
category: Category,
symbol: Option<&str>,
) -> Result<OrdersList> {
let cat_str = category.to_string();
let mut params = vec![("category", cat_str.as_str())];
if let Some(s) = symbol {
params.push(("symbol", s));
}
self.get("/v5/order/realtime", ¶ms).await
}
pub async fn get_order_history(
&self,
category: Category,
symbol: Option<&str>,
limit: Option<u32>,
) -> Result<OrdersList> {
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/order/history", ¶ms).await
}
}