Stream auction price discovery and benchmark matching workloads
This commit is contained in:
+111
-16
@@ -32,8 +32,12 @@ impl OrderBook {
|
||||
/// Read only the opposite side and stop at the first untouched price level.
|
||||
pub fn match_incoming(&self, incoming: &Order) -> Vec<Fill> {
|
||||
match incoming.side {
|
||||
Side::Buy => plan(std::iter::once(incoming), self.asks.values(), None),
|
||||
Side::Sell => plan(self.bids.values(), std::iter::once(incoming), None),
|
||||
Side::Buy => sweep(incoming, self.asks.values(), |maker| {
|
||||
maker.price <= incoming.price
|
||||
}),
|
||||
Side::Sell => sweep(incoming, self.bids.values(), |maker| {
|
||||
maker.price >= incoming.price
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,27 +48,62 @@ impl OrderBook {
|
||||
if bid.price < ask.price {
|
||||
return Vec::new();
|
||||
}
|
||||
// Move each price boundary once as the candidate tick rises.
|
||||
let mut demand: i128 = self.bids.values().map(|o| i128::from(o.remaining)).sum();
|
||||
let mut supply = 0i128;
|
||||
let mut bids = self.bids.values().rev().peekable();
|
||||
let mut asks = self.asks.values().peekable();
|
||||
let price = (0..=100)
|
||||
.min_by_key(|&p| {
|
||||
let demand: i128 = self
|
||||
.bids
|
||||
.values()
|
||||
.take_while(|o| o.price >= p)
|
||||
.map(|o| i128::from(o.remaining))
|
||||
.sum();
|
||||
let supply: i128 = self
|
||||
.asks
|
||||
.values()
|
||||
.take_while(|o| o.price <= p)
|
||||
.map(|o| i128::from(o.remaining))
|
||||
.sum();
|
||||
(Reverse(demand.min(supply)), (demand - supply).abs(), p)
|
||||
.min_by_key(|&tick| {
|
||||
while bids.peek().is_some_and(|o| o.price < tick) {
|
||||
demand -= i128::from(bids.next().unwrap().remaining);
|
||||
}
|
||||
while asks.peek().is_some_and(|o| o.price <= tick) {
|
||||
supply += i128::from(asks.next().unwrap().remaining);
|
||||
}
|
||||
(Reverse(demand.min(supply)), (demand - supply).abs(), tick)
|
||||
})
|
||||
.expect("nonempty tick range");
|
||||
plan(self.bids.values(), self.asks.values(), Some(price))
|
||||
}
|
||||
}
|
||||
|
||||
fn sweep<'a>(
|
||||
order: &Order,
|
||||
makers: impl Iterator<Item = &'a Order>,
|
||||
crosses: impl Fn(&Order) -> bool,
|
||||
) -> Vec<Fill> {
|
||||
let mut remaining = order.remaining;
|
||||
let mut fills = Vec::new();
|
||||
for maker in makers {
|
||||
if !crosses(maker) {
|
||||
break;
|
||||
}
|
||||
let quantity = remaining.min(maker.remaining);
|
||||
let (bid, ask) = if order.side == Side::Buy {
|
||||
(order.seq, maker.seq)
|
||||
} else {
|
||||
(maker.seq, order.seq)
|
||||
};
|
||||
let price = if maker.seq < order.seq {
|
||||
maker.price
|
||||
} else {
|
||||
order.price
|
||||
};
|
||||
fills.push(Fill {
|
||||
bid,
|
||||
ask,
|
||||
price,
|
||||
quantity,
|
||||
});
|
||||
remaining -= quantity;
|
||||
if remaining == 0 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
fills
|
||||
}
|
||||
|
||||
fn set<K: Ord>(side: &mut BTreeMap<K, Order>, key: K, order: Order) {
|
||||
if order.remaining == 0 {
|
||||
side.remove(&key);
|
||||
@@ -117,3 +156,59 @@ pub fn cross(orders: Vec<Order>, phase: Phase) -> Vec<Fill> {
|
||||
Phase::Continuous => plan(book.bids.values(), book.asks.values(), None),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn opening_agrees_with_exhaustive_price_discovery() {
|
||||
let mut seed = 13u64;
|
||||
for _ in 0..96 {
|
||||
let mut book = OrderBook::default();
|
||||
for seq in 0..64 {
|
||||
seed = seed.wrapping_mul(6364136223846793005).wrapping_add(1);
|
||||
book.update(Order {
|
||||
seq,
|
||||
side: if seq % 2 == 0 { Side::Buy } else { Side::Sell },
|
||||
price: ((seed >> 32) % 101) as i64,
|
||||
remaining: 1 + (seed % 17) as i64,
|
||||
});
|
||||
}
|
||||
let p = (0..=100)
|
||||
.min_by_key(|&p| {
|
||||
let demand: i64 = book
|
||||
.bids
|
||||
.values()
|
||||
.filter(|o| o.price >= p)
|
||||
.map(|o| o.remaining)
|
||||
.sum();
|
||||
let supply: i64 = book
|
||||
.asks
|
||||
.values()
|
||||
.filter(|o| o.price <= p)
|
||||
.map(|o| o.remaining)
|
||||
.sum();
|
||||
(Reverse(demand.min(supply)), (demand - supply).abs(), p)
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
book.opening(),
|
||||
plan(book.bids.values(), book.asks.values(), Some(p))
|
||||
);
|
||||
for side in [Side::Buy, Side::Sell] {
|
||||
let incoming = Order {
|
||||
seq: 90,
|
||||
side,
|
||||
price: 50,
|
||||
remaining: 16,
|
||||
};
|
||||
let expected = match side {
|
||||
Side::Buy => plan(std::iter::once(&incoming), book.asks.values(), None),
|
||||
Side::Sell => plan(book.bids.values(), std::iter::once(&incoming), None),
|
||||
};
|
||||
assert_eq!(book.match_incoming(&incoming), expected);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user