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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
use crate::errors::PriceLevelError;
use crate::orders::{Id, Side};
use crate::utils::{Price, Quantity};
use serde::{Deserialize, Serialize};
use std::str::FromStr;
/// Represents a request to update an existing order
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum OrderUpdate {
/// Update the price of an order
UpdatePrice {
/// ID of the order to update
order_id: Id,
/// New price for the order
new_price: Price,
},
/// Update the quantity of an order
///
/// For two-tranche orders (iceberg / reserve) `new_quantity` applies to the
/// **visible** tranche only; the hidden tranche is left untouched, so the
/// order's new total is `new_quantity + hidden`. Reducing hidden depth is
/// not reachable through this update — cancel and re-submit instead. The
/// increase-vs-decrease priority policy (documented on the price level's
/// `update_order`) is likewise driven by the visible delta.
UpdateQuantity {
/// ID of the order to update
order_id: Id,
/// New quantity for the order (visible tranche for iceberg / reserve)
new_quantity: Quantity,
},
/// Update both price and quantity of an order
UpdatePriceAndQuantity {
/// ID of the order to update
order_id: Id,
/// New price for the order
new_price: Price,
/// New quantity for the order
new_quantity: Quantity,
},
/// Cancel an order
Cancel {
/// ID of the order to cancel
order_id: Id,
},
/// Replace an order entirely with a new one
Replace {
/// ID of the order to replace
order_id: Id,
/// New price for the replacement order
price: Price,
/// New quantity for the replacement order
quantity: Quantity,
/// Side of the market (unchanged)
side: Side,
},
}
impl FromStr for OrderUpdate {
type Err = PriceLevelError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let parts: Vec<&str> = s.split(':').collect();
if parts.len() != 2 {
return Err(PriceLevelError::InvalidFormat);
}
let update_type = parts[0];
let fields_str = parts[1];
let mut fields = std::collections::HashMap::new();
for field_pair in fields_str.split(';') {
let kv: Vec<&str> = field_pair.split('=').collect();
if kv.len() == 2 {
fields.insert(kv[0], kv[1]);
}
}
let get_field = |field: &str| -> Result<&str, PriceLevelError> {
match fields.get(field) {
Some(result) => Ok(*result),
None => Err(PriceLevelError::MissingField(field.to_string())),
}
};
let parse_price = |field: &str, value: &str| -> Result<Price, PriceLevelError> {
Price::from_str(value).map_err(|_| PriceLevelError::InvalidFieldValue {
field: field.to_string(),
value: value.to_string(),
})
};
let parse_quantity = |field: &str, value: &str| -> Result<Quantity, PriceLevelError> {
Quantity::from_str(value).map_err(|_| PriceLevelError::InvalidFieldValue {
field: field.to_string(),
value: value.to_string(),
})
};
// Parse order_id field which is common to all update types
let order_id_str = get_field("order_id")?;
let order_id =
Id::from_str(order_id_str).map_err(|_| PriceLevelError::InvalidFieldValue {
field: "order_id".to_string(),
value: order_id_str.to_string(),
})?;
match update_type {
"UpdatePrice" => {
let new_price_str = get_field("new_price")?;
let new_price = parse_price("new_price", new_price_str)?;
Ok(OrderUpdate::UpdatePrice {
order_id,
new_price,
})
}
"UpdateQuantity" => {
let new_quantity_str = get_field("new_quantity")?;
let new_quantity = parse_quantity("new_quantity", new_quantity_str)?;
Ok(OrderUpdate::UpdateQuantity {
order_id,
new_quantity,
})
}
"UpdatePriceAndQuantity" => {
let new_price_str = get_field("new_price")?;
let new_price = parse_price("new_price", new_price_str)?;
let new_quantity_str = get_field("new_quantity")?;
let new_quantity = parse_quantity("new_quantity", new_quantity_str)?;
Ok(OrderUpdate::UpdatePriceAndQuantity {
order_id,
new_price,
new_quantity,
})
}
"Cancel" => Ok(OrderUpdate::Cancel { order_id }),
"Replace" => {
let price_str = get_field("price")?;
let price = parse_price("price", price_str)?;
let quantity_str = get_field("quantity")?;
let quantity = parse_quantity("quantity", quantity_str)?;
let side_str = get_field("side")?;
let side =
Side::from_str(side_str).map_err(|_| PriceLevelError::InvalidFieldValue {
field: "side".to_string(),
value: side_str.to_string(),
})?;
Ok(OrderUpdate::Replace {
order_id,
price,
quantity,
side,
})
}
_ => Err(PriceLevelError::UnknownOrderType(update_type.to_string())),
}
}
}
impl std::fmt::Display for OrderUpdate {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
OrderUpdate::UpdatePrice {
order_id,
new_price,
} => {
write!(f, "UpdatePrice:order_id={order_id};new_price={new_price}")
}
OrderUpdate::UpdateQuantity {
order_id,
new_quantity,
} => {
write!(
f,
"UpdateQuantity:order_id={order_id};new_quantity={new_quantity}"
)
}
OrderUpdate::UpdatePriceAndQuantity {
order_id,
new_price,
new_quantity,
} => {
write!(
f,
"UpdatePriceAndQuantity:order_id={order_id};new_price={new_price};new_quantity={new_quantity}"
)
}
OrderUpdate::Cancel { order_id } => {
write!(f, "Cancel:order_id={order_id}")
}
OrderUpdate::Replace {
order_id,
price,
quantity,
side,
} => {
write!(
f,
"Replace:order_id={order_id};price={price};quantity={quantity};side={side}"
)
}
}
}
}