How to implement an efficient rolling order book imbalance signal
This medium-difficulty coding problem tests whether you can translate a real market-microstructure concept into clean, performant Python. Order book imbalance is a foundational alpha signal in high-frequency trading; interviewers at quant firms use it to assess both algorithm design and practical coding discipline.
The core challenge is computing a rolling average of a derived metric (OBI) without recomputing the entire window at each step. A naive O(n×W) loop will be too slow for the scale of order book data in production. The key insight is to maintain running state—cumulative sums or a deque—so that each new snapshot update takes constant time. You must also handle edge cases: the warm-up phase before the window fills, division-by-zero in the imbalance formula, and correct indexing of the rolling slice.
- Sliding window and running aggregates
- Handling undefined values and boundary conditions
- Time complexity trade-offs (space for speed)
- Numeric stability in financial calculations