Honor the instrument price step when selecting the clearing price

This commit is contained in:
2026-12-03 06:32:16 +08:00
parent 12e26d6cc9
commit 097138afdd
2 changed files with 36 additions and 2 deletions
+35 -1
View File
@@ -2,6 +2,7 @@ mod order;
pub use order::{Fill, Order, Side};
use std::cmp::Reverse;
use std::collections::BTreeMap;
use std::num::NonZeroU32;
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Phase {
@@ -73,6 +74,11 @@ impl OrderBook {
}
pub fn opening(&self) -> Vec<Fill> {
self.opening_with_step(NonZeroU32::MIN)
}
/// Orders have already been validated as multiples of this price step.
pub fn opening_with_step(&self, step: NonZeroU32) -> Vec<Fill> {
let (Some(bid), Some(ask)) = (self.bids.values().next(), self.asks.values().next()) else {
return Vec::new();
};
@@ -104,7 +110,9 @@ impl OrderBook {
// Supply changes at an ask. Demand changes one tick AFTER a bid.
// Equal scores keep the first (lowest) tick in the interval.
let next_ask = asks.peek().map(|o| o.price);
let after_bid = bids.peek().and_then(|o| o.price.checked_add(1));
let after_bid = bids
.peek()
.and_then(|o| o.price.checked_add(i64::from(step.get())));
let Some(next) = next_ask.into_iter().chain(after_bid).min() else {
break;
};
@@ -334,4 +342,30 @@ mod tests {
);
}
}
#[test]
fn opening_selects_an_admissible_tick_on_the_configured_grid() {
let mut book = OrderBook::default();
for (seq, side, price, remaining) in [
(1, Side::Sell, 50, 2),
(2, Side::Buy, 50, 3),
(3, Side::Buy, 70, 2),
] {
book.update(Order {
seq,
side,
price,
remaining,
});
}
assert_eq!(book.opening()[0].price, 51);
assert_eq!(
book.opening_with_step(NonZeroU32::new(10).unwrap()),
vec![Fill {
bid: 3,
ask: 1,
price: 60,
quantity: 2
}]
);
}
}