Lee-Ready 算法
Lee Ready Algorithm
题目详情
Lee-Ready 算法是市场微观结构中使用成交价格和报价数据推断交易方向的基础技术。通过将成交分类为买方发起或卖方发起——基于成交价与买卖中间价的关系和前一笔价格变动——研究者可以分析流动性和订单流毒性。
任务:实现 classify_trades 函数,根据成交价与当前报价的关系判定交易方向:成交价高于中间价为买方发起,低于中间价为卖方发起,等于中间价时使用 Tick Test(与前一笔比较)判定。
英文原题
The Lee-Ready algorithm is a foundational technique in market microstructure for inferring the direction of trades using only trade price and quote data. By classifying trades as buyer-initiated or seller-initiated based on their relationship to the bid-ask midpoint and previous price movements, researchers can analyze liquidity and order flow toxicity.
Task
Implement the function classify_trades(trade_prices, quote_bids, quote_asks) to determine trade direction (Buy: 1, Sell: -1, Unknown: 0) u
解析
问题分析
The Lee-Ready algorithm is a foundational technique in market microstructure for inferring the direction of trades using only trade price and quote data. By classifying trades as buyer-initiated or seller-initiated based on their relationship to the bid-ask midpoint and previous price movements, res
解法
根据题目要求实现相应功能。核心逻辑需要:
// 核心数据结构和方法——根据题目 API 约定实现
// 1. 确定状态表示——选择支持所需操作的数据结构
// 2. 实现核心算法——确保 O(·) 时间复杂度和正确性
// 3. 处理边界条件——空输入、极值参数、并发访问验证
用具体输入验证:构造已知输入的测试用例,确认输出匹配预期结果。
复杂度与边界
- 时间复杂度:取决于选用的算法
- 空间复杂度:取决于数据规模
- 关键边界条件:空输入、极值参数、并发场景下的正确性保证
英文解析
Analysis
The Lee-Ready algorithm is a foundational technique in market microstructure for inferring the direction of trades using only trade price and quote data. By classifying trades as buyer-initiated or seller-initiated based on their relationship to the bid-ask midpoint and previous price movements, researchers can estimate order flow imbalance and construct meaningful measures of net buying pressure essential for alpha signal generation.
Solution
enum class TradeDir { BUY, SELL, UNKNOWN };
TradeDir leeReady(double trade_px, double prev_px,
double bid, double ask) {
double mid = (bid + ask) / 2.0;
if (trade_px > mid) return TradeDir::BUY;
if (trade_px < mid) return TradeDir::SELL;
// At midpoint: use tick rule (check against previous trade)
if (trade_px > prev_px) return TradeDir::BUY;
if (trade_px < prev_px) return TradeDir::SELL;
return TradeDir::UNKNOWN;
}Complexity & Edge Cases
- Time complexity: O(1) per trade classification
- Space complexity: O(1) (stores only previous trade price)
- Edge cases: (1) Midpoint trades are ambiguous - tick rule provides fallback (2) Quote staleness: bid/ask may lag actual market (3) Unknown classification reduces effective sample size
Verification
Classify trades in a known order flow dataset. Benchmark Lee-Ready classification against actual order direction (when available from exchange data). Measure accuracy rate and unknown trade percentage.
Key Considerations
The Lee-Ready algorithm combines the quote rule (price vs midpoint) with the tick rule (price change direction) to maximize classification accuracy. When exchange-provided aggressor flags are unavailable (most historical datasets), Lee-Ready provides approximately 85% accuracy. The algorithm's simplicity makes it the standard first step in order flow analysis for alpha research.