Match immediate buys against a cash budget and price cap
This commit is contained in:
@@ -37,6 +37,36 @@ impl OrderBook {
|
||||
}
|
||||
}
|
||||
|
||||
/// Buy whole shares at maker prices, walking the same FIFO ask tree as a limit buy.
|
||||
pub fn match_budget_buy(&self, seq: i64, limit: i64, budget: i64) -> Vec<Fill> {
|
||||
let mut cash = budget;
|
||||
let mut capacity = i64::MAX;
|
||||
let mut fills = Vec::new();
|
||||
for ask in self.asks.values() {
|
||||
if cash <= 0 || ask.price > limit || capacity == 0 {
|
||||
break;
|
||||
}
|
||||
let affordable = if ask.price == 0 {
|
||||
capacity
|
||||
} else {
|
||||
cash / ask.price
|
||||
};
|
||||
let quantity = ask.remaining.min(affordable).min(capacity);
|
||||
if quantity <= 0 {
|
||||
break;
|
||||
}
|
||||
fills.push(Fill {
|
||||
bid: seq,
|
||||
ask: ask.seq,
|
||||
price: ask.price,
|
||||
quantity,
|
||||
});
|
||||
cash -= ask.price * quantity;
|
||||
capacity -= quantity;
|
||||
}
|
||||
fills
|
||||
}
|
||||
|
||||
pub fn opening(&self) -> Vec<Fill> {
|
||||
let (Some(bid), Some(ask)) = (self.bids.values().next(), self.asks.values().next()) else {
|
||||
return Vec::new();
|
||||
|
||||
Reference in New Issue
Block a user