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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
use crate::{OrderBook, OrderBookError};
use pricelevel::{OrderId, OrderType, OrderUpdate, PriceLevel, Side};
use std::sync::Arc;
use tracing::trace;
impl OrderBook {
/// Update an order's price and/or quantity
pub fn update_order(
&self,
update: OrderUpdate,
) -> Result<Option<Arc<OrderType>>, OrderBookError> {
trace!("Order book {}: Updating order {:?}", self.symbol, update);
match update {
OrderUpdate::UpdatePrice {
order_id,
new_price,
} => {
// Get the order location without locking
let location = self.order_locations.get(&order_id).map(|val| *val);
if let Some((old_price, _)) = location {
// If price doesn't change, do nothing
if old_price == new_price {
return Err(OrderBookError::InvalidOperation {
message: "Cannot update price to the same value".to_string(),
});
}
// Get the original order without holding locks
let original_order = if let Some(order) = self.get_order(order_id) {
// Create a copy of the order
Arc::try_unwrap(order.clone()).unwrap_or_else(|arc| (*arc))
} else {
return Ok(None); // Order not found
};
// Cancel the original order
self.cancel_order(order_id)?;
// Create a new order with the updated price
let mut new_order = original_order;
// Update the price based on order type
match &mut new_order {
OrderType::Standard { price, .. } => *price = new_price,
OrderType::IcebergOrder { price, .. } => *price = new_price,
OrderType::PostOnly { price, .. } => *price = new_price,
OrderType::TrailingStop { price, .. } => *price = new_price,
OrderType::PeggedOrder { price, .. } => *price = new_price,
OrderType::MarketToLimit { price, .. } => *price = new_price,
OrderType::ReserveOrder { price, .. } => *price = new_price,
}
// Add the updated order
let result = self.add_order(new_order)?;
Ok(Some(result))
} else {
Ok(None) // Order not found
}
}
OrderUpdate::UpdateQuantity {
order_id,
new_quantity,
} => {
// Get order location without locking
let location = self.order_locations.get(&order_id).map(|val| *val);
if let Some((price, side)) = location {
// Get the appropriate price levels map
let price_levels = match side {
Side::Buy => &self.bids,
Side::Sell => &self.asks,
};
// Use entry() to safely modify the price level without deadlocks
let mut result = None;
let mut is_empty = false;
price_levels.entry(price).and_modify(|price_level| {
// Create update operation
let update = OrderUpdate::UpdateQuantity {
order_id,
new_quantity,
};
// Try to update the order
if let Ok(updated_order) = price_level.update_order(update) {
result = updated_order;
is_empty = price_level.order_count() == 0;
}
});
// If the price level is now empty, remove it
if is_empty {
price_levels.remove(&price);
self.order_locations.remove(&order_id);
}
Ok(result)
} else {
Ok(None) // Order not found
}
}
OrderUpdate::UpdatePriceAndQuantity {
order_id,
new_price,
new_quantity,
} => {
// Get order location without locking
let location = self.order_locations.get(&order_id).map(|val| *val);
if let Some((_, _)) = location {
// Get the original order without holding locks
let original_order = if let Some(order) = self.get_order(order_id) {
// Create a copy of the order
Arc::try_unwrap(order.clone()).unwrap_or_else(|arc| (*arc))
} else {
return Ok(None); // Order not found
};
// Cancel the original order
self.cancel_order(order_id)?;
// Create a new order with the updated price and quantity
let mut new_order = original_order;
// Update the price and quantity based on order type
match &mut new_order {
OrderType::Standard {
price, quantity, ..
} => {
*price = new_price;
*quantity = new_quantity;
}
OrderType::IcebergOrder {
price,
visible_quantity,
..
} => {
*price = new_price;
*visible_quantity = new_quantity;
}
OrderType::PostOnly {
price, quantity, ..
} => {
*price = new_price;
*quantity = new_quantity;
}
OrderType::TrailingStop {
price, quantity, ..
} => {
*price = new_price;
*quantity = new_quantity;
}
OrderType::PeggedOrder {
price, quantity, ..
} => {
*price = new_price;
*quantity = new_quantity;
}
OrderType::MarketToLimit {
price, quantity, ..
} => {
*price = new_price;
*quantity = new_quantity;
}
OrderType::ReserveOrder {
price,
visible_quantity,
..
} => {
*price = new_price;
*visible_quantity = new_quantity;
}
}
// Add the updated order
let result = self.add_order(new_order)?;
Ok(Some(result))
} else {
Ok(None) // Order not found
}
}
OrderUpdate::Cancel { order_id } => {
// Get order location without locking
let location = self.order_locations.get(&order_id).map(|val| *val);
if let Some((price, side)) = location {
// Get the appropriate price levels map
let price_levels = match side {
Side::Buy => &self.bids,
Side::Sell => &self.asks,
};
// Use entry() to safely modify the price level
let mut result = None;
let mut is_empty = false;
price_levels.entry(price).and_modify(|price_level| {
// Create cancel operation
let update = OrderUpdate::Cancel { order_id };
// Try to cancel the order
if let Ok(cancelled_order) = price_level.update_order(update) {
result = cancelled_order;
is_empty = price_level.order_count() == 0;
}
});
// If we cancelled an order, remove it from tracking
if result.is_some() {
self.order_locations.remove(&order_id);
// If price level is empty, remove it
if is_empty {
price_levels.remove(&price);
}
}
Ok(result)
} else {
Ok(None) // Order not found
}
}
OrderUpdate::Replace {
order_id,
price,
quantity,
side,
} => {
// Get the original order without holding locks
let original_opt = self.get_order(order_id);
if let Some(original) = original_opt {
// Extract what we need from the original order
let timestamp = original.timestamp();
let time_in_force = original.time_in_force();
// Check which order type we need to create
let new_order = match &*original {
OrderType::Standard { .. } => OrderType::Standard {
id: order_id,
price,
quantity,
side,
timestamp,
time_in_force,
},
OrderType::IcebergOrder {
hidden_quantity, ..
} => OrderType::IcebergOrder {
id: order_id,
price,
visible_quantity: quantity,
hidden_quantity: *hidden_quantity,
side,
timestamp,
time_in_force,
},
OrderType::PostOnly { .. } => OrderType::PostOnly {
id: order_id,
price,
quantity,
side,
timestamp,
time_in_force,
},
OrderType::ReserveOrder {
hidden_quantity,
replenish_threshold,
replenish_amount,
auto_replenish,
..
} => OrderType::ReserveOrder {
id: order_id,
price,
visible_quantity: quantity,
hidden_quantity: *hidden_quantity,
side,
timestamp,
time_in_force,
replenish_threshold: *replenish_threshold,
replenish_amount: *replenish_amount,
auto_replenish: *auto_replenish,
},
// Add cases for other order types if needed
_ => {
return Err(OrderBookError::InvalidOperation {
message: "Replace operation not supported for this order type"
.to_string(),
});
}
};
// Cancel the original order
self.cancel_order(order_id)?;
// Add the new order
let result = self.add_order(new_order)?;
Ok(Some(result))
} else {
Ok(None) // Original order not found
}
}
}
}
/// Cancel an order by ID
pub fn cancel_order(
&self,
order_id: OrderId,
) -> Result<Option<Arc<OrderType>>, OrderBookError> {
// Primero encontramos la ubicación de la orden (precio y lado) sin bloquear
let location = self.order_locations.get(&order_id).map(|val| *val);
if let Some((price, side)) = location {
// Obtener el mapa de niveles de precio apropiado
let price_levels = match side {
Side::Buy => &self.bids,
Side::Sell => &self.asks,
};
// Crear la actualización para cancelar
let update = OrderUpdate::Cancel { order_id };
// Utilizar entry() para modificar el nivel de precio de manera segura
let mut result = None;
let mut empty_level = false;
price_levels.entry(price).and_modify(|price_level| {
// Intentar cancelar la orden
if let Ok(cancelled) = price_level.update_order(update) {
result = cancelled;
// Verificar si el nivel quedó vacío
empty_level = price_level.order_count() == 0;
}
});
// Si obtuvimos un resultado y la orden fue cancelada
if result.is_some() {
// Eliminar la orden del mapa de ubicaciones
self.order_locations.remove(&order_id);
// Si el nivel quedó vacío, eliminarlo
if empty_level {
price_levels.remove(&price);
}
}
Ok(result)
} else {
// La orden no se encontró
Ok(None)
}
}
/// Add a new order to the book
pub fn add_order(&self, order: OrderType) -> Result<Arc<OrderType>, OrderBookError> {
trace!(
"Order book {}: Adding order {} at price {}",
self.symbol,
order.id(),
order.price()
);
let price = order.price();
let side = order.side();
// Check if the order has expired before adding
if self.has_expired(&order) {
return Err(OrderBookError::InvalidOperation {
message: "Order has already expired".to_string(),
});
}
// For post-only orders, check for price crossing
if order.is_post_only() && self.will_cross_market(price, side) {
let opposite_price = match side {
Side::Buy => self.best_ask().unwrap(),
Side::Sell => self.best_bid().unwrap(),
};
return Err(OrderBookError::PriceCrossing {
price,
side,
opposite_price,
});
}
// Handle immediate-or-cancel and fill-or-kill orders
if order.is_immediate() {
return self.handle_immediate_order(order);
}
// Standard limit order processing
let price_levels = match side {
Side::Buy => &self.bids,
Side::Sell => &self.asks,
};
// Get or create the price level
let price_level = price_levels
.entry(price)
.or_insert_with(|| Arc::new(PriceLevel::new(price)));
// Add the order to the price level
let order_arc = price_level.add_order(order);
// Track the order's location
self.order_locations.insert(order_arc.id(), (price, side));
Ok(order_arc)
}
}