nkaz001 / hftbacktest

A high-frequency trading and market-making backtesting tool in Python and Rust, which accounts for limit orders, queue positions, and latencies, utilizing full tick data for trades and order books, with real-world crypto market-making examples for Binance Futures
MIT License
1.78k stars 357 forks source link

The issue of hbt.bid_depth and hbt.ask_depth not aligning with hbt.best_bid and hbt.best_ask #125

Open kasperlmc opened 4 weeks ago

kasperlmc commented 4 weeks ago

I have noticed that the data for hbt.bid_depth and hbt.ask_depth seems inconsistent with hbt.best_bid and hbt.best_ask. During my examination, I found that the minimum price in hbt.ask_depth is lower than hbt.best_ask. What might be causing this discrepancy, and is there any way to resolve it?

kasperlmc commented 4 weeks ago

version is 1.84

nkaz001 commented 4 weeks ago

Crypto exchanges often experience unstable data feeds, leading to gaps in the feed that cannot always be fully recovered. This can result in incomplete market depth data, where certain price levels may not be properly updated. Consequently, the best bid may cross the best ask in the market depth, or vice versa. Since HftBacktest determines the best bid and ask from the Level 2 market depth, this can cause discrepancy.

To resolve this issue, HftBacktest adjusts by finding the best ask price above the best bid price when the best bid is updated, and finding the best bid price below the best ask price when the best ask is updated. As a result, there may be residual "afterimages" in the market depth, and these are naturally refreshed over time.

When you access the market depth, I recommend bounding the depth to the best bid and the best ask using hbt.best_bid, hbt.best_bid_tick, hbt.best_ask, and hbt.best_ask_tick. Please see Working with Market Depth and Trades tutorial.

kasperlmc commented 3 weeks ago

Alright, thank you for the explanation.