1use serde::{Deserialize, Serialize};
2use chrono::{DateTime, Utc};
3
4#[derive(Debug, Clone, Serialize, Deserialize)]
6pub struct Order {
7 pub id: String,
9
10 pub symbol: String,
12
13 pub quantity: f64,
15
16 pub filled_quantity: f64,
18
19 pub price: Option<f64>,
21
22 pub stop_price: Option<f64>,
24
25 pub status: OrderStatus,
27
28 pub side: OrderSide,
30
31 pub order_type: OrderType,
33
34 pub time_in_force: TimeInForce,
36
37 pub extended_hours: bool,
39
40 pub created_at: DateTime<Utc>,
42
43 pub updated_at: DateTime<Utc>,
45
46 pub commission: f64,
48
49 pub rejected_reason: Option<String>,
51
52 pub average_fill_price: Option<f64>,
54}
55
56#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
58#[serde(rename_all = "UPPERCASE")]
59pub enum OrderStatus {
60 New,
62
63 PartiallyFilled,
65
66 Filled,
68
69 Canceled,
71
72 Rejected,
74
75 PendingCancel,
77
78 PendingNew,
80
81 PendingReplace,
83
84 Replaced,
86
87 Suspended,
89
90 Expired,
92}
93
94#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
96#[serde(rename_all = "UPPERCASE")]
97pub enum OrderSide {
98 Buy,
100
101 Sell,
103
104 SellShort,
106
107 BuyToCover,
109}
110
111#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
113#[serde(rename_all = "UPPERCASE")]
114pub enum OrderType {
115 Market,
117
118 Limit,
120
121 Stop,
123
124 StopLimit,
126
127 TrailingStop,
129
130 TrailingStopLimit,
132}
133
134#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
136#[serde(rename_all = "UPPERCASE")]
137pub enum TimeInForce {
138 Day,
140
141 Gtc,
143
144 Gtd,
146
147 Ioc,
149
150 Fok,
152}
153
154#[derive(Debug, Clone, Serialize, Deserialize)]
156pub struct OrderRequest {
157 pub symbol: String,
159
160 pub quantity: f64,
162
163 pub price: Option<f64>,
165
166 pub stop_price: Option<f64>,
168
169 pub side: OrderSide,
171
172 pub order_type: OrderType,
174
175 pub time_in_force: TimeInForce,
177
178 pub extended_hours: bool,
180}
181
182impl OrderRequest {
183 pub fn new() -> Self {
185 Self {
186 symbol: String::new(),
187 quantity: 0.0,
188 price: None,
189 stop_price: None,
190 side: OrderSide::Buy,
191 order_type: OrderType::Market,
192 time_in_force: TimeInForce::Day,
193 extended_hours: false,
194 }
195 }
196
197 pub fn symbol(mut self, symbol: impl Into<String>) -> Self {
199 self.symbol = symbol.into();
200 self
201 }
202
203 pub fn quantity(mut self, quantity: f64) -> Self {
205 self.quantity = quantity;
206 self
207 }
208
209 pub fn price(mut self, price: f64) -> Self {
211 self.price = Some(price);
212 self
213 }
214
215 pub fn stop_price(mut self, stop_price: f64) -> Self {
217 self.stop_price = Some(stop_price);
218 self
219 }
220
221 pub fn side(mut self, side: OrderSide) -> Self {
223 self.side = side;
224 self
225 }
226
227 pub fn order_type(mut self, order_type: OrderType) -> Self {
229 self.order_type = order_type;
230 self
231 }
232
233 pub fn time_in_force(mut self, time_in_force: TimeInForce) -> Self {
235 self.time_in_force = time_in_force;
236 self
237 }
238
239 pub fn extended_hours(mut self, extended_hours: bool) -> Self {
241 self.extended_hours = extended_hours;
242 self
243 }
244}
245
246impl Default for OrderRequest {
247 fn default() -> Self {
248 Self::new()
249 }
250}
251
252#[derive(Debug, Clone, Serialize, Deserialize)]
254pub struct OrderResponse {
255 pub id: String,
257
258 pub status: OrderStatus,
260
261 pub symbol: String,
263
264 pub quantity: f64,
266
267 pub price: Option<f64>,
269
270 pub stop_price: Option<f64>,
272
273 pub side: OrderSide,
275
276 pub order_type: OrderType,
278
279 pub time_in_force: TimeInForce,
281
282 pub extended_hours: bool,
284
285 pub created_at: DateTime<Utc>,
287}
288
289#[derive(Debug, Clone, Serialize, Deserialize)]
291pub struct OrderQueryParams {
292 pub status: Option<OrderStatus>,
294
295 pub symbol: Option<String>,
297
298 pub start_date: Option<DateTime<Utc>>,
300
301 pub end_date: Option<DateTime<Utc>>,
303
304 pub limit: Option<u32>,
306}
307
308impl OrderQueryParams {
309 pub fn new() -> Self {
311 Self {
312 status: None,
313 symbol: None,
314 start_date: None,
315 end_date: None,
316 limit: None,
317 }
318 }
319
320 pub fn status(mut self, status: OrderStatus) -> Self {
322 self.status = Some(status);
323 self
324 }
325
326 pub fn symbol(mut self, symbol: impl Into<String>) -> Self {
328 self.symbol = Some(symbol.into());
329 self
330 }
331
332 pub fn start_date(mut self, start_date: DateTime<Utc>) -> Self {
334 self.start_date = Some(start_date);
335 self
336 }
337
338 pub fn end_date(mut self, end_date: DateTime<Utc>) -> Self {
340 self.end_date = Some(end_date);
341 self
342 }
343
344 pub fn limit(mut self, limit: u32) -> Self {
346 self.limit = Some(limit);
347 self
348 }
349}
350
351impl Default for OrderQueryParams {
352 fn default() -> Self {
353 Self::new()
354 }
355}