obrewin_data_structures/
order.rs

1use serde::{Deserialize, Serialize};
2
3use crate::market::{Price, Quantity};
4use crate::misc::DateTimeUTC;
5
6/// Type alias for an order ID.
7pub type OrderID = String;
8
9/// Type alias for symbol.
10pub type Symbol = String;
11
12/// Represents an order content.
13#[derive(Clone, Serialize)]
14pub enum OrderContent {
15    /// Represents a new direct order.
16    /// Negative `quantity` means sell order.
17    NewDirect { price: Price, quantity: Quantity },
18
19    /// Represents a market order.
20    /// Negative `quantity` means sell order.
21    NewMarket { quantity: Quantity },
22
23    /// Represents a cancel order.
24    Cancel { original_client_order_id: OrderID },
25}
26
27/// Represents a TIF.
28#[derive(Clone, Serialize)]
29pub enum TimeInForce {
30    /// Good Til Cancel (with provided `expiration_time`)
31    GTC { expiration_time: DateTimeUTC },
32    /// Immediate or cancel
33    IoC,
34    /// Fill or Kill = IoC + AoN (All or None)
35    FoK,
36}
37
38/// Represents an order request.
39#[derive(Clone, Serialize)]
40pub struct OrderRequest {
41    /// Main content of this order request.
42    pub content: OrderContent,
43    /// Target symbol.
44    pub symbol: Symbol,
45    /// Order request ID from client's side.
46    pub client_order_id: OrderID,
47}
48
49/// Represents an order response status.
50#[derive(Clone, Deserialize)]
51pub enum OrderResponseStatus {
52    Ok,
53    Filled {
54        executed_price: Price,
55        executed_quantity: Quantity,
56    },
57    Rejected {
58        code: Option<i32>,
59        message: Option<String>,
60    },
61}
62
63/// Represents an order response.
64#[derive(Clone, Deserialize)]
65pub struct OrderResponse {
66    pub status: OrderResponseStatus,
67    /// Same as `OrderRequest::client_order_id`.
68    pub client_order_id: OrderID,
69}
70
71impl OrderResponse {
72    /// Get notional value of this execution.
73    pub fn notional_value(&self) -> Quantity {
74        match self.status {
75            OrderResponseStatus::Filled {
76                executed_price,
77                executed_quantity,
78            } => executed_price * executed_quantity,
79            _ => Quantity::ZERO,
80        }
81    }
82}