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
use exc_core::types;
use rust_decimal::Decimal;
use serde::Serialize;
use crate::{
http::{
error::RestError,
request::{Payload, Rest, RestEndpoint},
},
types::trading::{OrderSide, OrderType, PositionSide, TimeInForce},
};
#[derive(Debug, Clone, Copy, Serialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum RespType {
Ack,
Result,
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PlaceOrder {
pub symbol: String,
pub side: OrderSide,
pub position_side: PositionSide,
#[serde(rename = "type")]
pub order_type: OrderType,
#[serde(skip_serializing_if = "Option::is_none")]
pub reduce_only: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub quantity: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none")]
pub price: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none")]
pub new_client_order_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub stop_price: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none")]
pub close_position: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub activation_price: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none")]
pub callback_rate: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none")]
pub time_in_force: Option<TimeInForce>,
#[serde(skip_serializing_if = "Option::is_none")]
pub working_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub price_protect: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub new_order_resp_type: Option<RespType>,
}
impl<'a> TryFrom<&'a types::PlaceOrder> for PlaceOrder {
type Error = RestError;
fn try_from(req: &'a exc_core::types::PlaceOrder) -> Result<Self, Self::Error> {
let place = req.place;
let side = if place.size.is_zero() {
return Err(RestError::PlaceZeroSize);
} else if place.size.is_sign_positive() {
OrderSide::Buy
} else {
OrderSide::Sell
};
let (order_type, price, tif) = match place.kind {
types::OrderKind::Market => (OrderType::Market, None, None),
types::OrderKind::Limit(price, tif) => {
let tif = match tif {
types::TimeInForce::GoodTilCancelled => Some(TimeInForce::Gtc),
types::TimeInForce::FillOrKill => Some(TimeInForce::Fok),
types::TimeInForce::ImmediateOrCancel => Some(TimeInForce::Ioc),
};
(OrderType::Limit, Some(price), tif)
}
types::OrderKind::PostOnly(price) => {
(OrderType::Limit, Some(price), Some(TimeInForce::Gtx))
}
};
Ok(Self {
symbol: req.instrument.to_uppercase(),
side,
position_side: PositionSide::Both,
order_type,
reduce_only: None,
quantity: Some(place.size.abs()),
price,
new_client_order_id: req.client_id.clone(),
stop_price: None,
close_position: None,
activation_price: None,
callback_rate: None,
time_in_force: tif,
working_type: None,
price_protect: None,
new_order_resp_type: None,
})
}
}
impl Rest for PlaceOrder {
fn method(&self, _endpoint: &RestEndpoint) -> Result<http::Method, RestError> {
Ok(http::Method::POST)
}
fn to_path(&self, endpoint: &RestEndpoint) -> Result<String, RestError> {
match endpoint {
RestEndpoint::UsdMarginFutures => Ok(format!("/fapi/v1/order")),
_ => Err(RestError::UnsupportedEndpoint(anyhow::anyhow!(
"only support usd-margin futures"
))),
}
}
fn need_apikey(&self) -> bool {
true
}
fn need_sign(&self) -> bool {
true
}
fn serialize(&self, _endpoint: &RestEndpoint) -> Result<serde_json::Value, RestError> {
Ok(serde_json::to_value(self)?)
}
fn to_payload(&self) -> Payload {
Payload::new(self.clone())
}
}