Files
big-qmt/py-client/strategy/zt/ownership.py
2026-09-15 20:02:05 +08:00

39 lines
1.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""ZT 委托与成交的归属判定。
本地订单号由 ``OrderBook.new_order_id`` 生成,形如 ``zt-base-<hex>``、
``zt-added-<hex>``、``zt-SELL-<hex>``。QMT 把提交时传入的 ``userOrderId``
原样写进委托和成交的 ``remark``,客户端的
``OrderItem.local_order_id`` / ``DealItem.local_order_id`` 取 ``remark`` 的
``|`` 前段,因此它们等于当时的本地订单号。
2026-09-15 的生产库 ``zt_86037237_state.db`` 已核实:``remark`` 与
``order_local_id`` 完全相同(``zt-base-8e9da97a42e957408489``),没有
``|策略名`` 后缀。
手工单、IPO 单和其他策略单不带的 ``zt-`` 前缀,属于别人的成交,
绝不能进入本策略账本。
"""
# ZT 本地订单号前缀,与 `OrderBook.new_order_id("zt", ...)` 的调用保持一致。
OWNED_PREFIX = "zt-"
def owns_local_order_id(local_order_id: str) -> bool:
"""判断本地订单号是否属于 ZT 策略。
取严格前缀匹配:本地订单号始终是 ``zt-<角色>-<随机>``。
若上游改动了备注契约,这里会整体判定为"非本策略"
``_sync_state`` 会逐轮打印忽略数量,便于立刻发现。
"""
return str(local_order_id or "").strip().startswith(OWNED_PREFIX)
def owned_deals(deals: list) -> tuple[list, int]:
"""把成交分成"本策略""非本策略"两组。
Returns:
(归属本策略的成交列表, 被忽略的成交笔数)
"""
owned = [deal for deal in deals if owns_local_order_id(deal.get_local_order_id)]
return owned, len(deals) - len(owned)