返回题库

多腿订单构建器

Multi Leg Order Builder

专题
Systems & Architecture / 系统与架构
难度
L3
来源
MyntBit

题目详情

多腿期权策略(如蝶式价差或跨式组合)需要同时执行多个期权腿以实现特定风险特征。量化交易系统的订单构建器必须跟踪这些腿的关系、确保原子提交,并在部分腿失败时管理取消和回滚。

任务:实现多腿订单构建器类,构建多腿订单。支持定义腿之间的比率关系、设置总腿价格约束、原子提交所有腿,以及在任一腿失败时取消其余腿。

英文原题

Multi-leg options strategies, such as butterfly spreads or straddles, require executing multiple option legs simultaneously to achieve specific risk profiles. A critical component in quantitative trading systems is an order builder that tracks these legs, prices them against real-time market data, and validates constraints like delta neutrality and limit prices before execution.
Task
Implement a MultiLegOrderBuilder class that processes a stream of market data and order operations. You must sup

解析

问题分析

多腿订单(如期货跨期价差、期权组合)需要多个子订单同时成交或全部取消。核心是原子性保证:要么所有腿都成交,要么都不成交。

解法

struct Leg { std::string symbol; int qty; bool is_buy; double limit_price; };
class MultiLegOrder {
    std::vector<Leg> legs_;
public:
    enum Status { PENDING, PARTIAL, FILLED, CANCELLED };
    Status execute(std::function<bool(const Leg&)> tryFill) {
        std::vector<Leg> filled;
        for (auto& leg : legs_) {
            if (!tryFill(leg)) { for (auto& f : filled) cancelFill(f); return CANCELLED; }
            filled.push_back(leg);
        }
        return FILLED;
    }
};

复杂度与边界

  • 时间复杂度:O(腿数) 顺序执行
  • 边界条件:(1) 部分成交后市场价格变化导致后续腿无法成交→回滚 (2) 跨交易所多腿需考虑延迟差异 (3) 腿间比例关系可能动态变化

英文解析

Analysis

Multi-leg orders (futures calendar spreads, options combinations) require multiple sub-orders to fill simultaneously or all cancel. The core is atomicity: either all legs fill or none do.

Solution

struct Leg { std::string symbol; int qty; bool is_buy; double limit_price; };
class MultiLegOrder {
    std::vector<Leg> legs_;
public:
    enum Status { PENDING, PARTIAL, FILLED, CANCELLED };
    Status execute(std::function<bool(const Leg&)> tryFill) {
        std::vector<Leg> filled;
        for (auto& leg : legs_) {
            if (!tryFill(leg)) { for (auto& f : filled) cancelFill(f); return CANCELLED; }
            filled.push_back(leg);
        }
        return FILLED;
    }
};

Complexity & Edge Cases

  • Time complexity: O(number of legs) sequential execution
  • Edge cases: (1) Partial fill + market price change causing subsequent legs to fail triggers rollback (2) Cross-exchange multi-leg must account for latency differences (3) Inter-leg ratio relationships may change dynamically

Key Considerations

  1. Leg synchronization: All legs must be submitted simultaneously or within tight time window; partial leg execution without other legs creates unwanted directional exposure
  2. Price relationship: Leg prices are linked (e.g., spread = leg1_price - leg2_price); must enforce price consistency across legs, not submit independent prices
  3. Fill tracking: Each leg fills independently; must track per-leg fill quantities and average prices, then compute composite strategy fill metrics
  4. Cancel semantics: Canceling multi-leg order cancels all remaining legs; partial cancel (cancel one leg only) typically not supported by exchanges