Match fractional share lots and preserve order quantities across restart

This commit is contained in:
2026-12-03 07:01:24 +08:00
parent 2c4179a64e
commit 064b301ebd
27 changed files with 343 additions and 111 deletions
+3 -3
View File
@@ -1,6 +1,6 @@
# matching
每个 OrderBook 保存一个交易品的常驻盘口。买卖双方各有一棵按价格、原始受理序号排序的 BTreeMap。每本簿的价格均用同一个最小价格步长的整数倍表示,数量单位为份。现有 exchange 以一美分为一步。exchange 按市场和结果持有这些簿,并把已提交的订单余量逐笔应用到对应的树。
每个 OrderBook 保存一个交易品的常驻盘口。买卖双方各有一棵按价格、原始受理序号排序的 BTreeMap。每本簿的价格均用同一个最小价格步长的整数倍表示,数量用整数表示最小交易单位。exchange 的价格使用美分,每个数量单位为 0.01 份。exchange 按市场和结果持有这些簿,并把已提交的订单余量逐笔应用到对应的树。
## 文件依赖
@@ -24,8 +24,8 @@ sequenceDiagram
B->>M: match_incoming,传入一条新订单
M->>M: 从对手方最优价起遍历,同价按原序号分配
else 盘中按金额买入
B->>M: match_budget_buy,传入买单序号、最高价、以同一步长折算的总预算
M->>M: 在同一卖盘上逐单扣减可用预算,末档取可负担的整份
B->>M: match_budget_buy,传入买单序号、最高价、以美分乘数量单位折算的总预算
M->>M: 在同一卖盘上逐单扣减可用预算,末档取预算可负担的最大整数单位数
else 集合竞价
B->>M: opening,在买一与卖一之间计算统一成交价
M->>M: 从卖一起,只访问新的卖价或买价之后的第一个价格步长
+2 -2
View File
@@ -42,8 +42,8 @@ impl OrderBook {
}
}
/// 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).
/// 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> {
let mut cash = budget;
let mut capacity = i64::MAX;
+2
View File
@@ -10,6 +10,7 @@ pub struct Order {
pub side: Side,
/// Integer price ticks per share, in the same unit throughout this book.
pub price: i64,
/// Number of minimum quantity lots left on the book.
pub remaining: i64,
}
@@ -19,5 +20,6 @@ pub struct Fill {
pub ask: i64,
/// Integer price ticks per share, in the same unit throughout this book.
pub price: i64,
/// Executed lots, using the same scale as Order::remaining.
pub quantity: i64,
}