Drop-Copy 对账器
Drop Copy Reconciler
题目详情
在高频交易系统中,"Drop Copy" 行情流提供独立于主交易网关的成交报告流。实时对账主成交流与 Drop Copy 流对风控和检测遗漏、重复或错配成交至关重要。确定性低延迟的对账器可即时发现成交异常。
任务:实现 DropCopyReconciler 类,逐笔比对主成交流和 Drop Copy 流,检测遗漏、重复和价格不一致,并输出异常报告。
英文原题
In high-frequency trading systems, a "drop copy" feed provides an independent stream of execution reports directly from the exchange to bypass the primary trading gateway. Reconciling the primary fill feed against this drop-copy feed in real-time is critical for risk management and detecting missing, duplicate, or mismatched fills. Deterministic, low-latency reconcilers are essential to instantly flag these discrepancies and prevent catastrophic positional errors.
Task
Implement a DropCopyRecon
解析
问题分析
Drop-Copy 是交易所发送给清算会员的订单/成交副本流。对账引擎比较内部记录与交易所副本,检测丢失的成交、重复订单或状态不一致。核心是双指针遍历两个已排序的 ID 流。
解法
class Reconciler {
std::set<uint64_t> internal_ids_, exchange_ids_;
public:
struct Diff { uint64_t id; enum { MISSING_INTERNAL, MISSING_EXCHANGE, QTY_MISMATCH } type; };
std::vector<Diff> reconcile() {
std::vector<Diff> diffs;
for (auto id : internal_ids_) if (!exchange_ids_.count(id)) diffs.push_back({id, Diff::MISSING_EXCHANGE});
for (auto id : exchange_ids_) if (!internal_ids_.count(id)) diffs.push_back({id, Diff::MISSING_INTERNAL});
return diffs;
}
};复杂度与边界
- 时间复杂度:O(N log N) 使用 set 或 O(N) 双指针(预排序流)
- 边界条件:(1) 重复 ID 只计一次 (2) 大成交量时按摘要对账而非逐笔 (3) 定期全量对账 + 实时增量
英文解析
Analysis
Drop-Copy is the order/fill replica stream sent by exchanges to clearing members. The reconciliation engine compares internal records against exchange copies to detect missing fills, duplicate orders, or status inconsistencies. The core uses dual-pointer traversal of two sorted ID streams.
Solution
class Reconciler {
std::set<uint64_t> internal_ids_, exchange_ids_;
public:
struct Diff { uint64_t id; enum { MISSING_INTERNAL, MISSING_EXCHANGE, QTY_MISMATCH } type; };
std::vector<Diff> reconcile() {
std::vector<Diff> diffs;
for (auto id : internal_ids_) if (!exchange_ids_.count(id)) diffs.push_back({id, Diff::MISSING_EXCHANGE});
for (auto id : exchange_ids_) if (!internal_ids_.count(id)) diffs.push_back({id, Diff::MISSING_INTERNAL});
return diffs;
}
};Complexity & Edge Cases
- Time complexity: O(N log N) using set, or O(N) with dual pointers on pre-sorted streams
- Edge cases: (1) Duplicate IDs counted only once (2) Large volumes reconciled by summary rather than per-trade (3) Periodic full reconciliation + real-time incremental
Key Considerations
- Sequence gap detection: Drop copy may miss messages during network interruption; gap in ExecID sequence triggers reconciliation request for missing executions
- Latency tolerance: Drop copy arrives after execution acknowledgment; reconciliation must tolerate up to several seconds of delay between order state and drop copy
- Duplicate handling: Exchange may retransmit drop copy messages with PossDupFlag; reconciler must detect and skip duplicates without double-counting fills
- Partial fill accumulation: Multiple partial fills on same order accumulate in reconciler; must track cumulative filled qty, not just individual fill quantities