use jiff::Timestamp;
use reqwest::Method;
use serde::{Deserialize, Serialize};
use crate::{
enums::{
OrderSide, OrderStatus, OrderType, ResponseType, SecurityType, SelfTradePreventionMode,
TimeInForce, WorkingFloor,
},
rest_api::endpoint,
web_socket_api::web_socket,
};
endpoint!(
"/api/v3/order",
Method::POST,
SecurityType::Trade,
NewOrderEndpoint,
NewOrderParams,
NewOrderResponse
);
pub struct NewOrderEndpoint<'r> {
client: &'r crate::rest_api::RestApiClient,
}
impl<'r> NewOrderEndpoint<'r> {
pub fn new(client: &'r crate::rest_api::RestApiClient) -> Self {
Self { client }
}
}
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct NewOrderParams {
symbol: String,
side: OrderSide,
r#type: OrderType,
#[serde(skip_serializing_if = "Option::is_none")]
time_in_force: Option<TimeInForce>,
#[serde(skip_serializing_if = "Option::is_none")]
quantity: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
quote_order_qty: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
price: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
new_client_order_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
strategy_id: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
strategy_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
stop_price: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
trailing_delta: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
iceberg_qty: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
new_order_resp_type: Option<ResponseType>,
#[serde(skip_serializing_if = "Option::is_none")]
self_trade_prevention_mode: Option<SelfTradePreventionMode>,
#[serde(skip_serializing_if = "Option::is_none")]
recv_window: Option<i64>,
timestamp: i64,
}
impl NewOrderParams {
pub fn new(symbol: &str, side: OrderSide, r#type: OrderType) -> Self {
Self {
symbol: symbol.to_owned(),
side,
r#type,
time_in_force: None,
quantity: None,
quote_order_qty: None,
price: None,
new_client_order_id: None,
strategy_id: None,
strategy_type: None,
stop_price: None,
trailing_delta: None,
iceberg_qty: None,
new_order_resp_type: None,
self_trade_prevention_mode: None,
recv_window: None,
timestamp: Timestamp::now().as_millisecond(),
}
}
pub fn time_in_force(mut self, time_in_force: TimeInForce) -> Self {
self.time_in_force = Some(time_in_force);
self
}
pub fn quantity(mut self, quantity: f64) -> Self {
self.quantity = Some(quantity);
self
}
pub fn quote_order_qty(mut self, quote_order_qty: f64) -> Self {
self.quote_order_qty = Some(quote_order_qty);
self
}
pub fn price(mut self, price: f64) -> Self {
self.price = Some(price);
self
}
pub fn new_client_order_id(mut self, new_client_order_id: &str) -> Self {
self.new_client_order_id = Some(new_client_order_id.to_owned());
self
}
pub fn strategy_id(mut self, strategy_id: i64) -> Self {
self.strategy_id = Some(strategy_id);
self
}
pub fn strategy_type(mut self, strategy_type: &str) -> Self {
self.strategy_type = Some(strategy_type.to_owned());
self
}
pub fn stop_price(mut self, stop_price: f64) -> Self {
self.stop_price = Some(stop_price);
self
}
pub fn trailing_delta(mut self, trailing_delta: f64) -> Self {
self.trailing_delta = Some(trailing_delta);
self
}
pub fn iceberg_qty(mut self, iceberg_qty: f64) -> Self {
self.iceberg_qty = Some(iceberg_qty);
self
}
pub fn new_order_resp_type(mut self, new_order_resp_type: ResponseType) -> Self {
self.new_order_resp_type = Some(new_order_resp_type);
self
}
pub fn self_trade_prevention_mode(
mut self,
self_trade_prevention_mode: SelfTradePreventionMode,
) -> Self {
self.self_trade_prevention_mode = Some(self_trade_prevention_mode);
self
}
pub fn recv_window(mut self, recv_window: i64) -> Self {
self.recv_window = Some(recv_window);
self
}
}
#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum NewOrderResponse {
Ack(Box<NewOrderAck>),
Result(Box<NewOrderResult>),
Full(Box<NewOrderFull>),
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct NewOrderAck {
pub symbol: String,
pub order_id: i64,
pub order_list_id: i64,
pub client_order_id: String,
pub transact_time: i64,
#[serde(flatten)]
pub conditional: ConditionalFields,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct NewOrderResult {
pub symbol: String,
pub order_id: i64,
pub order_list_id: i64,
pub client_order_id: String,
pub transact_time: i64,
pub price: String,
pub orig_qty: String,
pub executed_qty: String,
pub cummulative_quote_qty: String,
pub status: OrderStatus,
pub time_in_force: TimeInForce,
pub r#type: OrderType,
pub side: OrderSide,
pub working_type: i64,
pub self_trade_prevention_mode: SelfTradePreventionMode,
#[serde(flatten)]
pub conditional: ConditionalFields,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct NewOrderFull {
pub symbol: String,
pub order_id: i64,
pub order_list_id: i64,
pub client_order_id: String,
pub transact_time: i64,
pub price: String,
pub orig_qty: String,
pub executed_qty: String,
pub cummulative_quote_qty: String,
pub status: OrderStatus,
pub time_in_force: TimeInForce,
pub r#type: OrderType,
pub side: String,
pub working_type: i64,
pub self_trade_prevention_mode: SelfTradePreventionMode,
pub fills: Vec<OrderFill>,
#[serde(flatten)]
pub conditional: ConditionalFields,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OrderFill {
pub price: String,
pub qty: String,
pub commission: String,
pub commission_asset: String,
pub trade_id: i64,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConditionalFields {
pub iceberg_qty: Option<String>,
pub prevented_match_id: Option<i64>,
pub prevented_quantity: Option<String>,
pub stop_price: Option<String>,
pub strategy_id: Option<i64>,
pub strategy_type: Option<i64>,
pub trailing_delta: Option<i64>,
pub trailing_time: Option<i64>,
pub used_sor: Option<bool>,
pub working_floor: Option<WorkingFloor>,
}
web_socket!(
"order.place",
NewOrderWebSocket,
NewOrderParams,
NewOrderResponse
);
pub struct NewOrderWebSocket<'w> {
client: &'w crate::web_socket_api::WebSocketApiClient,
}
impl<'w> NewOrderWebSocket<'w> {
pub fn new(client: &'w crate::web_socket_api::WebSocketApiClient) -> Self {
Self { client }
}
}