Derive market order protection from visible opposing depth

This commit is contained in:
2026-12-03 07:18:25 +08:00
parent f90605bfc7
commit 5d82be0091
10 changed files with 201 additions and 33 deletions
+18
View File
@@ -42,6 +42,24 @@ impl OrderBook {
}
}
/// Visible opposing price levels, best first. Sum orders at one level
/// before exposing it so a client sees the same depth regardless of FIFO splits.
pub fn opposing_levels(&self, side: Side) -> impl Iterator<Item = (i64, i128)> + '_ {
let orders: Box<dyn Iterator<Item = &Order> + '_> = match side {
Side::Buy => Box::new(self.asks.values()),
Side::Sell => Box::new(self.bids.values()),
};
let mut orders = orders.peekable();
std::iter::from_fn(move || {
let first = orders.next()?;
let mut size = i128::from(first.remaining);
while let Some(order) = orders.next_if(|o| o.price == first.price) {
size += i128::from(order.remaining);
}
Some((first.price, size))
})
}
/// Buy integer lots at maker prices. Express the budget in price ticks
/// multiplied by quantity lots, the same units as price * remaining.
pub fn match_budget_buy(&self, seq: i64, limit: i64, budget: i64) -> Vec<Fill> {