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

Why MarketDepth.clear_depth not just clear all level depth #78

Closed LJingnan closed 4 months ago

LJingnan commented 4 months ago
`    def clear_depth(
            self,
            side,
            clear_upto_price,
    ):
        clear_upto = round(clear_upto_price / self.tick_size)
        if side == BUY:
            if self.best_bid_tick != INVALID_MIN:
                for t in range(self.best_bid_tick, clear_upto - 1, -1):
                    if t in self.bid_depth:
                        del self.bid_depth[t]
`

Here we clear the depth from best_bid_tick to clear_upto (clear_upto_price is the lowest bid price from restful api). This approach would miss the level below clear_upto_price. Suppose the data project loss the data for 10 minutes due to Internet problem, when project restart to work, it would get a restful depth-1000, but clearly all past data is useless.

nkaz001 commented 4 months ago

It depends on the user's intention. Since in the case of Binance, there is no way to fetch the complete full market depth, the market depth is always incomplete in a strict sense. But, over time, with the diff. book depth stream, the market depth becomes more complete. Essentially, it depends on the natural refresh.

If the connection is lost, one can delete all market depth data. In that case, reaching a state close to the complete market depth will take long time. So, I decided to leave the market depth behind the snapshot that can be fetched.

If you want to clear all the market depth in the backtesting, modifying the market depth clear event with an invalid side will do.

LJingnan commented 4 months ago

It depends on the user's intention. Since in the case of Binance, there is no way to fetch the complete full market depth, the market depth is always incomplete in a strict sense. But, over time, with the diff. book depth stream, the market depth becomes more complete. Essentially, it depends on the natural refresh.

If the connection is lost, one can delete all market depth data. In that case, reaching a state close to the complete market depth will take long time. So, I decided to leave the market depth behind the snapshot that can be fetched.

If you want to clear all the market depth in the backtesting, modifying the market depth clear event with an invalid side will do.

Thank you very much