Derive market order protection from visible opposing depth
This commit is contained in:
@@ -23,6 +23,10 @@ sequenceDiagram
|
||||
alt 连续交易
|
||||
B->>M: match_incoming,传入一条新订单
|
||||
M->>M: 从对手方最优价起遍历,同价按原序号分配
|
||||
else 未指定市价委托保护价
|
||||
B->>M: opposing_levels,传入买卖方向
|
||||
M->>M: 从最优对手价起,逐档汇总同价订单的剩余份额
|
||||
M-->>B: 逐档返回价格和总量,调用方按需停止读取
|
||||
else 盘中按金额买入
|
||||
B->>M: match_budget_buy,传入买单序号、最高价、以微美元计的总预算
|
||||
M->>M: 在同一卖盘上逐单扣减可用预算,末档取预算可负担的最大整数单位数
|
||||
|
||||
@@ -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> {
|
||||
|
||||
Reference in New Issue
Block a user