Honor the instrument price step when selecting the clearing price

This commit is contained in:
2026-12-03 06:32:16 +08:00
parent 12e26d6cc9
commit 097138afdd
2 changed files with 36 additions and 2 deletions
+1 -1
View File
@@ -43,7 +43,7 @@ sequenceDiagram
Gamma 市场 559651 的一份公开目录记录中,最小步长为 0.001 美元,买一为 0.012,卖一为 0.013。以这个步长建立内存簿,两个价位分别用整数 12、13 表示。假设盘中在 12 上有两张各 2 份的卖单,在 13 上有一张 2 份卖单,以 13 为限价买入 5 份,会按受理顺序以 12、12、13 成交,合计 61 个步长,即 0.061 美元。这个成交场景使用模拟订单。exchange 的命令、预留资金和持久化价格仍按整分工作,接入细步长需要一起迁移这些字段。[官方目录](https://gamma-api.polymarket.com/markets/559651) Gamma 市场 559651 的一份公开目录记录中,最小步长为 0.001 美元,买一为 0.012,卖一为 0.013。以这个步长建立内存簿,两个价位分别用整数 12、13 表示。假设盘中在 12 上有两张各 2 份的卖单,在 13 上有一张 2 份卖单,以 13 为限价买入 5 份,会按受理顺序以 12、12、13 成交,合计 61 个步长,即 0.061 美元。这个成交场景使用模拟订单。exchange 的命令、预留资金和持久化价格仍按整分工作,接入细步长需要一起迁移这些字段。[官方目录](https://gamma-api.polymarket.com/markets/559651)
开盘沿用 Eventbook 的集合竞价规则。若价位 812 有 2 份卖单,812 和 817 分别有 3 份和 2 份买单,在 812 可成交 2 份,需求多出 3 份;在 813 至 817 均可成交 2 份,供需相等。最终以最低的 813 清算。813 上可以没有挂单。遍历工作量取决于订单和有供需变化的价位数,即使最小步长细化,空价位也无需逐档扫描。 开盘沿用 Eventbook 的集合竞价规则。若价位 812 有 2 份卖单,812 和 817 分别有 3 份和 2 份买单,在 812 可成交 2 份,需求多出 3 份;在 813 至 817 均可成交 2 份,供需相等。最终以最低的 813 清算。813 上可以没有挂单。遍历工作量取决于订单和有供需变化的价位数,即使最小步长细化,空价位也无需逐档扫描。若调用方价格单位为分、市场仅允许每 10 分一档,使用 opening_with_step(10),输入委托须已对齐。同样的供需变化在买价 50 分之后要到 60 分才生效。
## 吞吐基准 ## 吞吐基准
+35 -1
View File
@@ -2,6 +2,7 @@ mod order;
pub use order::{Fill, Order, Side}; pub use order::{Fill, Order, Side};
use std::cmp::Reverse; use std::cmp::Reverse;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::num::NonZeroU32;
#[derive(Clone, Copy, PartialEq, Eq)] #[derive(Clone, Copy, PartialEq, Eq)]
pub enum Phase { pub enum Phase {
@@ -73,6 +74,11 @@ impl OrderBook {
} }
pub fn opening(&self) -> Vec<Fill> { pub fn opening(&self) -> Vec<Fill> {
self.opening_with_step(NonZeroU32::MIN)
}
/// Orders have already been validated as multiples of this price step.
pub fn opening_with_step(&self, step: NonZeroU32) -> Vec<Fill> {
let (Some(bid), Some(ask)) = (self.bids.values().next(), self.asks.values().next()) else { let (Some(bid), Some(ask)) = (self.bids.values().next(), self.asks.values().next()) else {
return Vec::new(); return Vec::new();
}; };
@@ -104,7 +110,9 @@ impl OrderBook {
// Supply changes at an ask. Demand changes one tick AFTER a bid. // Supply changes at an ask. Demand changes one tick AFTER a bid.
// Equal scores keep the first (lowest) tick in the interval. // Equal scores keep the first (lowest) tick in the interval.
let next_ask = asks.peek().map(|o| o.price); let next_ask = asks.peek().map(|o| o.price);
let after_bid = bids.peek().and_then(|o| o.price.checked_add(1)); let after_bid = bids
.peek()
.and_then(|o| o.price.checked_add(i64::from(step.get())));
let Some(next) = next_ask.into_iter().chain(after_bid).min() else { let Some(next) = next_ask.into_iter().chain(after_bid).min() else {
break; break;
}; };
@@ -334,4 +342,30 @@ mod tests {
); );
} }
} }
#[test]
fn opening_selects_an_admissible_tick_on_the_configured_grid() {
let mut book = OrderBook::default();
for (seq, side, price, remaining) in [
(1, Side::Sell, 50, 2),
(2, Side::Buy, 50, 3),
(3, Side::Buy, 70, 2),
] {
book.update(Order {
seq,
side,
price,
remaining,
});
}
assert_eq!(book.opening()[0].price, 51);
assert_eq!(
book.opening_with_step(NonZeroU32::new(10).unwrap()),
vec![Fill {
bid: 3,
ask: 1,
price: 60,
quantity: 2
}]
);
}
} }