冰山订单逻辑
Iceberg Order Logic
题目详情
冰山订单在量化金融和算法交易中用于执行大额交易而不扰动市场。它隐藏真实委托量,仅向公开订单簿暴露较小的"峰值"数量,成交后从隐藏储备中补充可见部分。
任务:实现 IcebergOrder 类,维护订单的可见量和隐藏量。当可见量被成交消耗后,自动从隐藏储备补充至峰值大小,直到隐藏量耗尽。
英文原题
Iceberg orders are crucial in quantitative finance and algorithmic trading for executing large trades without moving the market. They hide the true order quantity by exposing only a smaller "peak" size to the public order book, replenishing the visible part from a hidden reserve as trades execute.
Task
Implement an IcebergOrder class that maintains the visible and hidden quantities of an order.
- IcebergOrder(double total_qty, double peak_size): Initializes the order. The initial visible quanti
解析
问题分析
冰山订单只显示总数量的一小部分(可见量),其余隐藏。当可见量被成交后,自动从隐藏量中补充新的可见量。用于大订单隐藏真实意图,减少市场冲击。
解法
class IcebergOrder {
int total_qty_, visible_qty_, hidden_qty_, displayed_;
public:
IcebergOrder(int total, int visible) : total_qty_(total), visible_qty_(visible), hidden_qty_(total - visible), displayed_(std::min(visible, total)) {}
int displayed() const { return displayed_; }
void onFill(int qty) {
total_qty_ -= qty; displayed_ -= qty;
if (displayed_ <= 0 && hidden_qty_ > 0) {
int refill = std::min(visible_qty_, hidden_qty_);
displayed_ += refill; hidden_qty_ -= refill;
}
}
int remaining() const { return total_qty_; }
};复杂度与边界
- 时间复杂度:所有操作 O(1)
- 边界条件:(1) 最后一次补充可能小于 visible_qty (2) 全部成交后 remaining=0 (3) 市价单可能一次吃掉多片
英文解析
Analysis
Iceberg orders display only a small portion (visible quantity) of the total order, keeping the remainder hidden. When the visible quantity is filled, a new visible slice is automatically replenished from the hidden reserve. Used to conceal true trading intent and reduce market impact for large orders.
Solution
class IcebergOrder {
int total_qty_, visible_qty_, hidden_qty_, displayed_;
public:
IcebergOrder(int total, int visible) : total_qty_(total), visible_qty_(visible), hidden_qty_(total - visible), displayed_(std::min(visible, total)) {}
int displayed() const { return displayed_; }
void onFill(int qty) {
total_qty_ -= qty; displayed_ -= qty;
if (displayed_ <= 0 && hidden_qty_ > 0) {
int refill = std::min(visible_qty_, hidden_qty_);
displayed_ += refill; hidden_qty_ -= refill;
}
}
int remaining() const { return total_qty_; }
};Complexity & Edge Cases
- Time complexity: All operations O(1)
- Edge cases: (1) Final replenishment may be smaller than visible_qty (2) All filled: remaining=0 (3) visible_qty >= total: behaves as regular order
Key Considerations
- Display quantity: Visible quantity must be small relative to total order size to minimize market impact; typical display is 5-10% of total quantity
- Refresh mechanism: When displayed quantity fills, iceberg refreshes from hidden reserve; refresh must not reveal total remaining quantity in book data
- Priority reset: Some exchanges reset price-time priority when iceberg refreshes — effectively reinserting at back of queue at same price level
- Detection risk: Iceberg patterns are detectable through repeated fills at same price level; sophisticated participants can infer hidden reserve size from fill frequency