Discover auction prices at supply and demand changes on fine tick grids
This commit is contained in:
+106
-13
@@ -41,7 +41,8 @@ impl OrderBook {
|
||||
}
|
||||
}
|
||||
|
||||
/// Buy whole shares at maker prices, walking the same FIFO ask tree as a limit buy.
|
||||
/// Buy whole shares at maker prices. Budget and limit use the instrument's
|
||||
/// integer tick unit (total cash in ticks and ticks per share respectively).
|
||||
pub fn match_budget_buy(&self, seq: i64, limit: i64, budget: i64) -> Vec<Fill> {
|
||||
let mut cash = budget;
|
||||
let mut capacity = i64::MAX;
|
||||
@@ -78,22 +79,40 @@ impl OrderBook {
|
||||
if bid.price < ask.price {
|
||||
return Vec::new();
|
||||
}
|
||||
// Move each price boundary once as the candidate tick rises.
|
||||
// Every crossed book with positive live quantities executes at least one
|
||||
// share between its best ask and best bid. Outside, volume is zero.
|
||||
let mut tick = ask.price;
|
||||
let mut price = tick;
|
||||
let mut best_score = None;
|
||||
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(|&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");
|
||||
loop {
|
||||
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);
|
||||
}
|
||||
let score = (Reverse(demand.min(supply)), (demand - supply).abs());
|
||||
if best_score.map_or(true, |best| score < best) {
|
||||
price = tick;
|
||||
best_score = Some(score);
|
||||
}
|
||||
|
||||
// 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 Some(next) = next_ask.into_iter().chain(after_bid).min() else {
|
||||
break;
|
||||
};
|
||||
if next > bid.price {
|
||||
break;
|
||||
}
|
||||
tick = next;
|
||||
}
|
||||
plan(self.bids.values(), self.asks.values(), Some(price))
|
||||
}
|
||||
}
|
||||
@@ -241,4 +260,78 @@ mod tests {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fine_tick_execution_preserves_maker_price_and_fifo() {
|
||||
let mut book = OrderBook::default();
|
||||
let order = |seq, side, price, remaining| Order {
|
||||
seq,
|
||||
side,
|
||||
price,
|
||||
remaining,
|
||||
};
|
||||
// One tick is $0.001 on this book.
|
||||
for (seq, price) in [(1, 13), (2, 12), (3, 12)] {
|
||||
book.update(order(seq, Side::Sell, price, 2));
|
||||
}
|
||||
let incoming = order(4, Side::Buy, 13, 5);
|
||||
let fills = book.match_incoming(&incoming);
|
||||
let expected = vec![
|
||||
Fill {
|
||||
bid: 4,
|
||||
ask: 2,
|
||||
price: 12,
|
||||
quantity: 2,
|
||||
},
|
||||
Fill {
|
||||
bid: 4,
|
||||
ask: 3,
|
||||
price: 12,
|
||||
quantity: 2,
|
||||
},
|
||||
Fill {
|
||||
bid: 4,
|
||||
ask: 1,
|
||||
price: 13,
|
||||
quantity: 1,
|
||||
},
|
||||
];
|
||||
assert_eq!(fills, expected);
|
||||
assert_eq!(book.match_budget_buy(4, 13, 61), expected);
|
||||
assert_eq!(book.match_budget_buy(4, 13, 60), expected[..2]);
|
||||
assert!(book.match_incoming(&order(5, Side::Buy, 11, 1)).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clearing_can_fall_one_tick_above_a_bid_without_an_order_at_that_price() {
|
||||
for (low, high) in [
|
||||
(12, 17),
|
||||
(812, 817),
|
||||
(120_000, 990_000),
|
||||
(i64::MAX - 5, i64::MAX),
|
||||
] {
|
||||
let mut book = OrderBook::default();
|
||||
for (seq, side, price, remaining) in [
|
||||
(1, Side::Sell, low, 2),
|
||||
(2, Side::Buy, low, 3),
|
||||
(3, Side::Buy, high, 2),
|
||||
] {
|
||||
book.update(Order {
|
||||
seq,
|
||||
side,
|
||||
price,
|
||||
remaining,
|
||||
});
|
||||
}
|
||||
assert_eq!(
|
||||
book.opening(),
|
||||
vec![Fill {
|
||||
bid: 3,
|
||||
ask: 1,
|
||||
price: low + 1,
|
||||
quantity: 2
|
||||
}]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user