binance_client/http_api_v3/data/order/post/
request.rs

1//!
2//! The order POST request.
3//!
4
5use chrono::prelude::*;
6use rust_decimal::Decimal;
7
8use crate::http_api_v3::data::order::post::response::r#type::Type as ResponseType;
9use crate::http_api_v3::data::order_side::OrderSide;
10use crate::http_api_v3::data::order_time_in_force::OrderTimeInForce;
11use crate::http_api_v3::data::order_type::OrderType;
12
13///
14/// The `https://www.binance.com/api/v3/order` POST request query.
15///
16pub struct Query {
17    /// The symbol name.
18    pub symbol: String,
19    /// The order side.
20    pub side: OrderSide,
21    /// The order type.
22    pub r#type: OrderType,
23    /// The order time-in-force.
24    pub time_in_force: Option<OrderTimeInForce>,
25    /// The order quantity in the secondary asset.
26    pub quantity: Option<Decimal>,
27    /// The order quantity in the primary asset.
28    pub quote_order_qty: Option<Decimal>,
29    /// The order price. Required for limit orders.
30    pub price: Option<Decimal>,
31    /// A unique id for the order. Automatically generated if not sent.
32    pub new_client_order_id: Option<String>,
33    /// Used with `STOP_LOSS`, `STOP_LOSS_LIMIT`, `TAKE_PROFIT`, and `TAKE_PROFIT_LIMIT` orders.
34    pub stop_price: Option<Decimal>,
35    /// The iceberg order quantity.
36    pub iceberg_qty: Option<Decimal>,
37    /// Set the response JSON. ACK, RESULT, or FULL;
38    /// MARKET and LIMIT order types default to FULL, all other orders default to ACK.
39    pub new_order_resp_type: Option<ResponseType>,
40    /// The allowed time window between the request and response in milliseconds.
41    pub recv_window: Option<i64>,
42    /// The request time in milliseconds.
43    pub timestamp: i64,
44}
45
46impl Query {
47    /// The query params default capacity.
48    const QUERY_INITIAL_CAPACITY: usize = 255;
49
50    ///
51    /// Creates a market order request.
52    ///
53    pub fn market(
54        symbol: &str,
55        side: OrderSide,
56        quantity: Decimal,
57        use_base_quantity: bool,
58    ) -> Self {
59        let (quantity, quote_order_qty) = if use_base_quantity {
60            (None, Some(quantity))
61        } else {
62            (Some(quantity), None)
63        };
64
65        Self {
66            symbol: symbol.to_owned(),
67            side,
68            r#type: OrderType::Market,
69            time_in_force: None,
70            quantity,
71            quote_order_qty,
72            price: None,
73            new_client_order_id: None,
74            stop_price: None,
75            iceberg_qty: None,
76            new_order_resp_type: Some(ResponseType::Full),
77            recv_window: None,
78            timestamp: Utc::now().timestamp_millis(),
79        }
80    }
81
82    ///
83    /// Creates a limit order request.
84    ///
85    pub fn limit(symbol: &str, side: OrderSide, quantity: Decimal, price: Decimal) -> Self {
86        Self {
87            symbol: symbol.to_owned(),
88            side,
89            r#type: OrderType::Limit,
90            time_in_force: Some(OrderTimeInForce::GoodTilCanceled),
91            quantity: Some(quantity),
92            quote_order_qty: None,
93            price: Some(price),
94            new_client_order_id: None,
95            stop_price: None,
96            iceberg_qty: None,
97            new_order_resp_type: Some(ResponseType::Ack),
98            recv_window: None,
99            timestamp: Utc::now().timestamp_millis(),
100        }
101    }
102}
103
104impl ToString for Query {
105    fn to_string(&self) -> String {
106        let mut params = String::with_capacity(Self::QUERY_INITIAL_CAPACITY);
107        params += &format!("symbol={}", self.symbol.to_owned());
108        params += &format!("&side={}", self.side);
109        params += &format!("&type={}", self.r#type);
110        if let Some(time_in_force) = self.time_in_force {
111            params += &format!("&timeInForce={}", time_in_force);
112        }
113        if let Some(quantity) = self.quantity {
114            params += &format!("&quantity={}", quantity);
115        }
116        if let Some(quote_order_qty) = self.quote_order_qty {
117            params += &format!("&quoteOrderQty={}", quote_order_qty);
118        }
119        if let Some(price) = self.price {
120            params += &format!("&price={}", price);
121        }
122        if let Some(ref new_client_order_id) = self.new_client_order_id {
123            params += &format!("&newClientOrderId={}", new_client_order_id.to_owned());
124        }
125        if let Some(stop_price) = self.stop_price {
126            params += &format!("&stopPrice={}", stop_price);
127        }
128        if let Some(iceberg_qty) = self.iceberg_qty {
129            params += &format!("&icebergQty={}", iceberg_qty);
130        }
131        if let Some(new_order_resp_type) = self.new_order_resp_type {
132            params += &format!("&newOrderRespType={}", new_order_resp_type);
133        }
134        if let Some(recv_window) = self.recv_window {
135            params += &format!("&recvWindow={}", recv_window);
136        }
137        params += &format!("&timestamp={}", self.timestamp);
138        params
139    }
140}