智能订单路由
Smart Order Router
题目详情
智能订单路由(SOR)根据流动性、延迟、费用和执行概率动态选择最优执行场所。核心算法需要实时评估多个交易所的订单簿深度并按照总成本排序。
任务:实现一个智能订单路由器,计算并排序到各交易所的最优路由。
英文原题
Smart Order Routing (SOR) is a critical component in modern algorithmic trading systems designed to achieve best execution. By splitting a parent order across multiple liquidity venues, an SOR optimizes execution costs by dynamically evaluating available order book depth and venue-specific fee structures.
Task
Implement a SmartOrderRouter class that splits a parent order across multiple venues to minimize costs for buy orders and maximize revenue for sell orders.
You must implement the followin
解析
问题分析
智能订单路由(SOR)根据流动性、延迟、费用和执行概率动态选择最优执行场所。核心算法需要实时评估多个交易所的订单簿深度,计算到达各场所的网络延迟,并按照总成本(显性费用 + 隐性滑点)排序。
实现
struct Venue { std::string name; double latency_us; FeeSchedule fees; };
struct Route { const Venue* venue; double est_cost; double fill_prob; };
class SmartOrderRouter {
std::vector<Venue> venues_;
std::unordered_map<std::string, OrderBook> books_;
public:
std::vector<Route> route(const std::string& symbol, int qty, bool is_buy) {
std::vector<Route> routes;
for (auto& v : venues_) {
auto& book = books_[v.name];
double price = is_buy ? book.bestAsk(qty) : book.bestBid(qty);
double fee = v.fees.calculate(is_buy ? 0 : 1, price * qty);
double latency_cost = v.latency_us * 1e-6 * price * qty * 0.0001;
double prob = estimateFillProb(book, qty, is_buy);
routes.push_back({&v, price * qty + fee + latency_cost, prob});
}
std::sort(routes.begin(), routes.end(),
[] (auto &a, auto &b) { return a.est_cost * a.fill_prob < b.est_cost * b.fill_prob; });
return routes;
}
};复杂度与边界
- 时间复杂度:O(V log V),V 为交易场所数
- 空间复杂度:O(V)
- 边界条件:(1) 无流动性时返回空路由 (2) 延迟为 0 的特殊处理 (3) 成交概率为 0 时跳过 (4) 支持分批路由到多个场所
英文解析
Analysis
Smart Order Routing (SOR) dynamically selects the optimal execution venue based on liquidity, latency, fees, and fill probability. The core algorithm must real-time assess order book depth across multiple exchanges, compute network latency to each venue, and rank by total cost (explicit fees + implicit slippage).
Solution
struct Venue { std::string name; double latency_us; FeeSchedule fees; };
struct Route { const Venue* venue; double est_cost; double fill_prob; };
class SmartOrderRouter {
std::vector<Venue> venues_;
std::unordered_map<std::string, OrderBook> books_;
public:
std::vector<Route> route(const std::string& symbol, int qty, bool is_buy) {
std::vector<Route> routes;
for (auto& v : venues_) {
auto& book = books_[v.name];
double price = is_buy ? book.bestAsk(qty) : book.bestBid(qty);
double fee = v.fees.calculate(is_buy ? 0 : 1, price * qty);
double latency_cost = v.latency_us * 1e-6 * price * qty * 0.0001;
double prob = estimateFillProb(book, qty, is_buy);
routes.push_back({&v, price * qty + fee + latency_cost, prob});
}
std::sort(routes.begin(), routes.end(),
[] (auto &a, auto &b) { return a.est_cost * a.fill_prob < b.est_cost * b.fill_prob; });
return routes;
}
};Complexity & Edge Cases
- Time complexity: O(V log V), V = number of venues
- Space complexity: O(V)
- Edge cases: (1) Zero liquidity venues excluded (2) Latency spikes change rankings dynamically (3) Fee structure varies by product and tier
Key Considerations
- Latency modeling: Venue latency varies dynamically; router must weight recent latency measurements more than historical averages
- Fee-adjusted ranking: Net cost = execution price + venue fee - maker rebate; router must compute fee-adjusted cost for each venue
- Fill probability: Not all displayed liquidity is accessible; dark pool fill rates differ from lit exchanges — weight by historical fill probability
- Regulatory constraints: Some markets prohibit routing to specific venue types (e.g., dark pools for certain order types); compliance rules must be enforced