Skip to content

Commit

Permalink
fix(rust): fix the incorrect boundary check in clear_depth.
Browse files Browse the repository at this point in the history
  • Loading branch information
nkaz001 committed Aug 10, 2024
1 parent fc4a38c commit ee325ec
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions hftbacktest/src/depth/roivectormarketdepth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,9 @@ impl L2MarketDepth for ROIVectorMarketDepth {
let clear_upto = (clear_upto_price / self.tick_size).round() as i64;
if side == Side::Buy {
if self.best_bid_tick != INVALID_MIN {
for t in clear_upto.max(self.roi_lb)..(self.best_bid_tick + 1) {
let from = (clear_upto - self.roi_lb).max(0);
let to = self.best_bid_tick + 1 - self.roi_lb;
for t in from..to {
unsafe {
*self.bid_depth.get_unchecked_mut(t as usize) = 0.0;
}
Expand All @@ -269,7 +271,9 @@ impl L2MarketDepth for ROIVectorMarketDepth {
}
} else if side == Side::Sell {
if self.best_ask_tick != INVALID_MAX {
for t in self.best_ask_tick..(clear_upto.min(self.roi_ub) + 1) {
let from = self.best_ask_tick - self.roi_lb;
let to = (clear_upto + 1 - self.roi_ub).min(self.ask_depth.len() as i64);
for t in from..to {
unsafe {
*self.ask_depth.get_unchecked_mut(t as usize) = 0.0;
}
Expand Down Expand Up @@ -343,7 +347,7 @@ impl MarketDepth for ROIVectorMarketDepth {
fn bid_qty_at_tick(&self, price_tick: i64) -> f64 {
if price_tick < self.roi_lb || price_tick > self.roi_ub {
// This is outside the range of interest.
0.0
f64::NAN
} else {
unsafe {
*self
Expand All @@ -357,7 +361,7 @@ impl MarketDepth for ROIVectorMarketDepth {
fn ask_qty_at_tick(&self, price_tick: i64) -> f64 {
if price_tick < self.roi_lb || price_tick > self.roi_ub {
// This is outside the range of interest.
0.0
f64::NAN
} else {
unsafe {
*self
Expand Down

0 comments on commit ee325ec

Please sign in to comment.