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
151
152
//! Result types for Exchange operations.
use crate::stop::StopStatus;
use crate::{OrderId, OrderStatus, Quantity, Trade};
/// Result of submitting an order.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SubmitResult {
/// The order ID assigned by the exchange
pub order_id: OrderId,
/// Final status of the order
pub status: OrderStatus,
/// Trades that occurred (if any)
pub trades: Vec<Trade>,
/// Quantity that was filled
pub filled_quantity: Quantity,
/// Quantity that is resting on the book (GTC only)
pub resting_quantity: Quantity,
/// Quantity that was cancelled (IOC remainder, FOK rejection)
pub cancelled_quantity: Quantity,
}
impl SubmitResult {
/// Returns true if any trades occurred.
pub fn has_trades(&self) -> bool {
!self.trades.is_empty()
}
/// Returns true if the order is resting on the book.
pub fn is_resting(&self) -> bool {
self.resting_quantity > 0
}
/// Returns true if the order was fully filled.
pub fn is_fully_filled(&self) -> bool {
self.status == OrderStatus::Filled
}
}
/// Result of cancelling an order.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CancelResult {
/// Whether the cancellation succeeded
pub success: bool,
/// Quantity that was cancelled (0 if failed)
pub cancelled_quantity: Quantity,
/// Error if cancellation failed
pub error: Option<CancelError>,
}
impl CancelResult {
/// Create a successful cancel result.
pub fn success(cancelled_quantity: Quantity) -> Self {
Self {
success: true,
cancelled_quantity,
error: None,
}
}
/// Create a failed cancel result.
pub fn failure(error: CancelError) -> Self {
Self {
success: false,
cancelled_quantity: 0,
error: Some(error),
}
}
}
/// Errors that can occur when cancelling an order.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum CancelError {
/// Order ID not found
OrderNotFound,
/// Order already filled or cancelled
OrderNotActive,
}
/// Result of modifying an order.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ModifyResult {
/// Whether the modification succeeded
pub success: bool,
/// The old order ID (always set)
pub old_order_id: OrderId,
/// The new order ID (if successful)
pub new_order_id: Option<OrderId>,
/// Quantity cancelled from old order
pub cancelled_quantity: Quantity,
/// Trades from the new order (if any)
pub trades: Vec<Trade>,
/// Error if modification failed
pub error: Option<ModifyError>,
}
impl ModifyResult {
/// Create a successful modify result.
pub fn success(
old_order_id: OrderId,
new_order_id: OrderId,
cancelled_quantity: Quantity,
trades: Vec<Trade>,
) -> Self {
Self {
success: true,
old_order_id,
new_order_id: Some(new_order_id),
cancelled_quantity,
trades,
error: None,
}
}
/// Create a failed modify result.
pub fn failure(old_order_id: OrderId, error: ModifyError) -> Self {
Self {
success: false,
old_order_id,
new_order_id: None,
cancelled_quantity: 0,
trades: Vec::new(),
error: Some(error),
}
}
}
/// Errors that can occur when modifying an order.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ModifyError {
/// Order ID not found
OrderNotFound,
/// Order already filled or cancelled
OrderNotActive,
/// New quantity is zero
InvalidQuantity,
}
/// Result of submitting a stop order.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct StopSubmitResult {
/// The order ID assigned by the exchange.
pub order_id: OrderId,
/// Status of the stop order (Pending or Triggered if immediate).
pub status: StopStatus,
}