Derive market order protection from visible opposing depth

This commit is contained in:
2026-12-03 07:18:25 +08:00
parent f90605bfc7
commit 5d82be0091
10 changed files with 201 additions and 33 deletions
+13
View File
@@ -0,0 +1,13 @@
{"op":"list","id":"list","market":"fixture","rules":"Simulated result","tick_size":"0.01"}
{"op":"fund","id":"a","account":"maker","cents":2000}
{"op":"fund","id":"b","account":"taker","cents":1000}
{"op":"split","id":"mint","market":"fixture","account":"maker","qty":7}
{"op":"open","id":"start","market":"fixture"}
{"op":"order","id":"ordinary","asset_id":"eb:YES:fixture","account":"maker","side":"sell","price":40,"qty":2}
{"op":"order","id":"complement","asset_id":"eb:NO:fixture","account":"maker","side":"buy","price":50,"qty":3}
{"op":"estimate","asset_id":"eb:YES:fixture","side":"buy","amount_cents":231,"tif":"FOK"}
{"op":"estimate","asset_id":"eb:YES:fixture","side":"buy","amount_cents":231,"tif":"FAK"}
{"op":"market_buy","id":"take","asset_id":"eb:YES:fixture","account":"taker","amount_cents":180,"tif":"FOK"}
{"op":"order","id":"bid-low","asset_id":"eb:YES:fixture","account":"maker","side":"buy","price":20,"qty":2}
{"op":"order","id":"bid-high","asset_id":"eb:YES:fixture","account":"maker","side":"buy","price":30,"qty":2}
{"op":"market_sell","id":"sell","asset_id":"eb:YES:fixture","account":"taker","qty":3,"tif":"FOK"}
+8
View File
@@ -279,3 +279,11 @@ order、market_sell 和卖出 estimate 可用 size_micros 输入数量,也可
配置价格步长后,`market_amounts` 读出保护价对应的 `makerAmount` 与 `takerAmount`。买单用 `amount_cents` 或 `amount_micros` 指定预算,向下取整到美分。卖单用 `qty` 或 `size_micros` 指定份额,向下取整到 0.01 份。显式保护价必须落在市场网格上。买单编码的有效价格若达到用户上限加 0.0001,预览、估价和下单均拒绝。成交估价及真实模拟成交回执通过 `order_amounts` 给出相同口径。估价还考虑当时盘口、手续费和本地可成交单位。[官方市价单金额计算](https://docs.polymarket.com/trading/place-orders.md)
[回放样例](examples/market-amounts.jsonl)先预览 10 美元预算在最高价 0.52 下的 19.2307 份编码,再按 0.50 卖盘买到 20 份。随后卖出 5.01 份,最低接受 0.48 美元,实际按 0.50 美元得到 2.505 美元。本地成交的最小数量单位仍是 0.01 份。这里复现公开委托金额公式,真实签名、生产成交粒度与资金到账仍待对照。
## 由对手盘口推导市价单保护价
省略最高买价或最低卖价时,使用当前交易品盘口的价格优先顺序。买单逐档累计金额,卖单逐档累计份额,首次达到请求量的一档给出保护价。FAK 在深度不足时取最远可见档。FOK 在深度不足时无法生成委托。空盘两种模式都无法自动定价。估价用 `protection_price_micros: null` 和 `executable: false` 表示无法定价,并返回可见的部分深度。显式保护价继续生效。[官方 Python CLOB v2 定价实现(固定版本)](https://github.com/Polymarket/py-clob-client-v2/blob/9c7c9e89cbbc11528daae90dc66ef65c9380cbdf/py_clob_client_v2/order_builder/builder.py#L307-L351)
[本地回放样例](examples/depth-protection.jsonl)包含普通 YES 卖单和 NO 买单映射出的 YES 卖盘。定价使用费用前的订单簿深度,实际成交另行检查预算、费用和本地数量精度。保护价只对该次读取的盘口有效,提交请求时会重新读取。客户端可显式提交预览过的保护价。相同请求号的重试返回已保存的原始回执。
在固定版本的官方 Python 函数上实测,若依次有 0.10、0.70、0.90 美元各一份卖盘,0.80 美元预算会得到 0.90 美元保护价。原因是前两档的二进制浮点和略低于 0.80。本地在推导委托保护价时复现这一可观察结果,成交和扣款仍按整数计算,两份合计恰好 0.80 美元。样例未向生产环境发单。