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

Negative values for best_bid_tick and best_bid #97

Closed isslerman closed 1 month ago

isslerman commented 1 month ago

I am loading some data into the simulator and printing some debug data and found some negative values or non-initialized values for best_bid_tick and best_bid. Ask is fine but bid is wrong. Not sure yet if is some data wrong.

I am using python here.

Here is the code:

while hbt.elapse(100_000):
    print(f'ts: {hbt.current_timestamp}, best_bid_tick: {hbt.best_bid_tick}, low_bid_tick: {hbt.low_bid_tick}, best_bid: {hbt.best_bid}, bid_depth: {hbt.bid_depth}')
    print(f'ts: {hbt.current_timestamp}, best_ask_tick: {hbt.best_ask_tick}, high_ask_tick: {hbt.high_ask_tick}, best_ask: {hbt.best_ask}, ask_depth: {hbt.ask_depth}')

And here is the result:

ts: 1719565471747000, best_bid_tick: -9223372036854775807, low_bid_tick: 25104, best_bid: -4.611686018427388e+19, bid_depth: {27674: 1003.0, 26691: 28.0, 25960: 2.0, 25600: 20.0, ...}

ts: 1719565471747000, best_ask_tick: 25104, high_ask_tick: 25402, best_ask: 125520.0, ask_depth: {25400: 76.0, 25385: 1.0, 25384: 2.0, 25380: 52.0, 25369: 64.0, ...}
nkaz001 commented 1 month ago

When there is no best bid or best ask, it sets INVALID_MIN or INVALID_MAX, respectively. Refer to this.

If the initial snapshot is not provided at the beginning of the backtest or if the depth is cleared and has not yet been recovered by the snapshot or natural refresh, this situation can occur. If the data is valid, Elapsing until the best bid and ask have valid values resolves the issue.