nkaz001 / collect-binancefutures

Collect BinanceFutures's trade and orderbook(depth) feeds.
94 stars 27 forks source link

bid_price == -9.223372036854776e+16? #8

Closed LJingnan closed 3 months ago

LJingnan commented 3 months ago
hbt = HftBacktest(
    [
        'btcusdt_20240522.npz',
    ],
    tick_size=0.01,
    lot_size=0.001,
    maker_fee=-0.00005,
    taker_fee=0.0007,
    order_latency=FeedLatency(),
    queue_model=SquareProbQueueModel(),
    asset_type=Linear,
    trade_list_size=10_000,

)

@njit
def print_bbo(hbt):
    t = 0
    bid_prices = np.zeros(11721077)
    ask_prices = np.zeros(11721077)

    while hbt.elapse(100000):
        bid_prices[t] = hbt.best_bid
        ask_prices[t] = hbt.best_ask
        t = t + 1
        print(t)

        # !!!!
        hbt.clear_last_trades()

        if not hbt.run and t >= bid_prices :
            break
    return bid_prices, ask_prices
bid_prices, ask_prices = print_bbo(hbt)
max(bid_prices)  # 70642.7
min(bid_prices)  # ???? -9.223372036854776e+16

I download data using python, But I fould some bid_price where strange. Is it a collection error? Here is the data sample image

nkaz001 commented 3 months ago

At the start of the backtesting or right after a depth clear event, the market depth is empty, resulting in no bid or ask. In such cases, it returns INVALID_MIN or INVALID_MAX based on the side, defined as -sys.maxsize or sys.maxsize in marketdepth.py. You can check this by using from hftbacktest.marketdepth import INVALID_MIN, INVALID_MAX.

LJingnan commented 3 months ago

At the start of the backtesting or right after a depth clear event, the market depth is empty, resulting in no bid or ask. In such cases, it returns INVALID_MIN or INVALID_MAX based on the side, defined as -sys.maxsize or sys.maxsize in marketdepth.py. You can check this by using from hftbacktest.marketdepth import INVALID_MIN, INVALID_MAX.

Would this affect backtesting,because I find out that my collecting program restart often.

nkaz001 commented 3 months ago

Collecting program data doesn't have to do with backtesting. Connection loss often happens. It's recommended to handle invalid values to ensure that erroneous actions are not taken in backtesting.

LJingnan commented 3 months ago

Collecting program data doesn't have to do with backtesting. Connection loss often happens. It's recommended to handle invalid values to ensure that erroneous actions are not taken in backtesting.

I see, could you please give me some advise? When I encounter INVALID_MIN or INVALID_MAX then I clear all position and wait until a DEPTH_SNAPSHOT_EVENT? Is this logic ok?

Now I use the method: I split data on depth_clear event, and bacltest them one by one and collect them together at the end. Is this a good method?

nkaz001 commented 3 months ago

I'd rather wait until both a valid bid and ask are received. Ultimately, the same situation will occur in live trading, so there's no use in applying methods that only work in backtesting.

LJingnan commented 3 months ago

I'd rather wait until both a valid bid and ask are received. Ultimately, the same situation will occur in live trading, so there's no use in applying methods that only work in backtesting.

I see, Thank You!