Skip to main content

sandbox_quant/model/
order.rs

1use std::fmt;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4pub enum OrderSide {
5    Buy,
6    Sell,
7}
8
9impl OrderSide {
10    pub fn as_binance_str(&self) -> &'static str {
11        match self {
12            OrderSide::Buy => "BUY",
13            OrderSide::Sell => "SELL",
14        }
15    }
16}
17
18impl fmt::Display for OrderSide {
19    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20        match self {
21            OrderSide::Buy => write!(f, "BUY"),
22            OrderSide::Sell => write!(f, "SELL"),
23        }
24    }
25}
26
27#[derive(Debug, Clone, Copy, PartialEq, Eq)]
28pub enum OrderType {
29    Market,
30    Limit,
31}
32
33impl OrderType {
34    pub fn as_binance_str(&self) -> &'static str {
35        match self {
36            OrderType::Market => "MARKET",
37            OrderType::Limit => "LIMIT",
38        }
39    }
40}
41
42#[derive(Debug, Clone, Copy, PartialEq, Eq)]
43pub enum OrderStatus {
44    PendingSubmit,
45    Submitted,
46    PartiallyFilled,
47    Filled,
48    Cancelled,
49    Rejected,
50    Expired,
51}
52
53impl OrderStatus {
54    pub fn from_binance_str(s: &str) -> Self {
55        match s {
56            "NEW" => OrderStatus::Submitted,
57            "PARTIALLY_FILLED" => OrderStatus::PartiallyFilled,
58            "FILLED" => OrderStatus::Filled,
59            "CANCELED" => OrderStatus::Cancelled,
60            "REJECTED" => OrderStatus::Rejected,
61            "EXPIRED" => OrderStatus::Expired,
62            _ => OrderStatus::Rejected,
63        }
64    }
65
66    pub fn is_terminal(&self) -> bool {
67        matches!(
68            self,
69            OrderStatus::Filled
70                | OrderStatus::Cancelled
71                | OrderStatus::Rejected
72                | OrderStatus::Expired
73        )
74    }
75}
76
77impl fmt::Display for OrderStatus {
78    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
79        match self {
80            OrderStatus::PendingSubmit => write!(f, "PENDING"),
81            OrderStatus::Submitted => write!(f, "SUBMITTED"),
82            OrderStatus::PartiallyFilled => write!(f, "PARTIAL"),
83            OrderStatus::Filled => write!(f, "FILLED"),
84            OrderStatus::Cancelled => write!(f, "CANCELLED"),
85            OrderStatus::Rejected => write!(f, "REJECTED"),
86            OrderStatus::Expired => write!(f, "EXPIRED"),
87        }
88    }
89}
90
91#[derive(Debug, Clone)]
92pub struct Fill {
93    pub price: f64,
94    pub qty: f64,
95    pub commission: f64,
96    pub commission_asset: String,
97}
98
99#[derive(Debug, Clone)]
100pub struct Order {
101    pub client_order_id: String,
102    pub server_order_id: Option<u64>,
103    pub symbol: String,
104    pub side: OrderSide,
105    pub order_type: OrderType,
106    pub quantity: f64,
107    pub price: Option<f64>,
108    pub status: OrderStatus,
109    pub created_at: chrono::DateTime<chrono::Utc>,
110    pub updated_at: chrono::DateTime<chrono::Utc>,
111    pub fills: Vec<Fill>,
112}