Skip to content

fix: clear the correct ask range in ROIVectorMarketDepth::clear_depth#318

Open
23f3001135 wants to merge 1 commit into
nkaz001:masterfrom
23f3001135:fix/roivector-ask-clear-depth
Open

fix: clear the correct ask range in ROIVectorMarketDepth::clear_depth#318
23f3001135 wants to merge 1 commit into
nkaz001:masterfrom
23f3001135:fix/roivector-ask-clear-depth

Conversation

@23f3001135

Copy link
Copy Markdown

Summary

ROIVectorMarketDepth::clear_depth does not clear the ask side when given a price bound. The bid side works correctly; the ask side leaves stale quantity in the book.

Details

The depth is stored in a flat vector indexed by tick - roi_lb. The ask branch computes the upper bound of the loop as:

let to = (clear_upto + 1 - self.roi_ub).min(self.ask_depth.len() as i64);

but the index offset is roi_lb, not roi_ub. Since roi_ub is the top of the range (typically far above roi_lb), to ends up below from, so for t in from..to never runs and no ask level is cleared. The bid branch just above already uses roi_lb:

let to = self.best_bid_tick + 1 - self.roi_lb;

After an ASK_DEPTH_CLEAR event the levels that should have been removed stay in ask_depth, sitting behind the recomputed best ask. best_ask_tick is recomputed separately, so the top of book still looks correct and the problem is easy to miss, but ask_qty_at_tick, ask_depth(), and the queue model that seeds order positions from those quantities all see phantom liquidity. HashMapMarketDepth and BTreeMarketDepth both clear best_ask_tick..=clear_upto, confirming the intended behaviour.

Fix

Use roi_lb for the upper bound, matching the bid side and the rest of the file.

Test

Added test_l2_clear_depth_upto_price: it clears the ask side up to a price and checks the levels are gone (plus the mirror bid case). It fails before the change and passes after.

The ask branch computed the upper bound of the cleared range with `roi_ub`
instead of `roi_lb`. Depth vector indices are offset by `roi_lb`, so the
bound collapsed below `from` and the loop never ran, leaving stale ask
quantity behind the best ask after an ASK_DEPTH_CLEAR event. The bid branch
already used `roi_lb`.
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 0 complexity

Metric Results
Complexity 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant