nkaz001 / hftbacktest

A high-frequency trading and market-making backtesting and trading bot 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
2k stars 392 forks source link

In grid trading example, the best_ask price is always NaN #127

Closed lli839 closed 2 months ago

lli839 commented 3 months ago

I'm using 2.0.0 version. Data: Use the BTCUSDC from https://github.com/nkaz001/hftbacktest/issues/69#issuecomment-2273239534

I'm testing the plain grid trading example: https://hftbacktest.readthedocs.io/en/latest/tutorials/High-Frequency%20Grid%20Trading.html

TickSize=0.1, LotSize=0.001.

The dataframe seems alright. However, after running the entire example, I notice the best_ask price is always NaN. So, no order is submitted.

Any reason?

nkaz001 commented 3 months ago

Which date of BTCUSDT data is used?

lli839 commented 2 months ago

Which date of BTCUSDT data is used?

I'm using btcusdc 20240730 - 20240804.

Step 1. Create npz

def data_prepare(filename):
    _ = binancefutures.convert(
        filename + '.gz',
        output_filename=filename+'.npz',
        combined_stream=True
    )
data_prepare('btcusdc_20240730')
data_prepare('btcusdc_20240801')
data_prepare('btcusdc_20240802')
data_prepare('btcusdc_20240803')
data_prepare('btcusdc_20240804')

The data seems ok.

┌────────────┬─────────────────────┬─────────────────────┬─────────┬───────┬──────────┬──────┬──────┐
│ ev         ┆ exch_ts             ┆ local_ts            ┆ px      ┆ qty   ┆ order_id ┆ ival ┆ fval │
│ ---        ┆ ---                 ┆ ---                 ┆ ---     ┆ ---   ┆ ---      ┆ ---  ┆ ---  │
│ u64        ┆ i64                 ┆ i64                 ┆ f64     ┆ f64   ┆ u64      ┆ i64  ┆ f64  │
╞════════════╪═════════════════════╪═════════════════════╪═════════╪═══════╪══════════╪══════╪══════╡
│ 3758096385 ┆ 1722346072132000000 ┆ 1722346072346101388 ┆ 65658.0 ┆ 2.036 ┆ 0        ┆ 0    ┆ 0.0  │
│ 3758096385 ┆ 1722346072132000000 ┆ 1722346072346101388 ┆ 66530.5 ┆ 0.0   ┆ 0        ┆ 0    ┆ 0.0  │
│ 3758096385 ┆ 1722346072132000000 ┆ 1722346072346101388 ┆ 66531.0 ┆ 3.324 ┆ 0        ┆ 0    ┆ 0.0  │
│ 3489660929 ┆ 1722346072132000000 ┆ 1722346072346101388 ┆ 67908.1 ┆ 1.442 ┆ 0        ┆ 0    ┆ 0.0  │
│ 3758096385 ┆ 1722346072211000000 ┆ 1722346072346358866 ┆ 65108.6 ┆ 4.599 ┆ 0        ┆ 0    ┆ 0.0  │
│ …          ┆ …                   ┆ …                   ┆ …       ┆ …     ┆ …        ┆ …    ┆ …    │
│ 3489660929 ┆ 1722383999979000000 ┆ 1722383999982625576 ┆ 66158.5 ┆ 0.0   ┆ 0        ┆ 0    ┆ 0.0  │
│ 3489660929 ┆ 1722383999979000000 ┆ 1722383999982625576 ┆ 66159.9 ┆ 0.028 ┆ 0        ┆ 0    ┆ 0.0  │
│ 3489660929 ┆ 1722383999979000000 ┆ 1722383999982625576 ┆ 66160.9 ┆ 0.03  ┆ 0        ┆ 0    ┆ 0.0  │
│ 3489660929 ┆ 1722383999979000000 ┆ 1722383999982625576 ┆ 66161.7 ┆ 0.55  ┆ 0        ┆ 0    ┆ 0.0  │
│ 3489660929 ┆ 1722383999979000000 ┆ 1722383999982625576 ┆ 66176.2 ┆ 0.453 ┆ 0        ┆ 0    ┆ 0.0  │
└────────────┴─────────────────────┴─────────────────────┴─────────┴───────┴──────────┴──────┴──────┘

Step 2. Create eod snapshot.

create_last_snapshot(
    ['btcusdc_20240730.npz'],
    tick_size=TICK_SIZE_BTC,
    lot_size=LOT_SIZE_BTC,
    output_snapshot_filename='btcusdc_20240730_eod.npz',
)
create_last_snapshot(
    ['btcusdt_20240731.npz'],
    tick_size=0.1,
    lot_size=0.001,
    output_snapshot_filename='btcusdt_20240731_eod.npz',
    initial_snapshot='btcusdt_20240730_eod.npz',
)

Step 3. Run the plain grid trading: https://hftbacktest.readthedocs.io/en/latest/tutorials/High-Frequency%20Grid%20Trading.html

I added a print(bid_price, ask_price) at each iteration.

61.0 nan
61.1 nan
61.1 nan

Two interesting things are:

  1. bid_price seems incorrect.
  2. ask_price is always nan.
nkaz001 commented 2 months ago

Did you use ROIVectorMarketDepthBacktest? If so, could you check whether you set the correct roi_lb and roi_ub values? roi_lb is the lower bound of the market depth, and roi_ub is the upper bound.

lli839 commented 2 months ago

Did you use ROIVectorMarketDepthBacktest? If so, could you check whether you set the correct roi_lb and roi_ub values? roi_lb is the lower bound of the market depth, and roi_ub is the upper bound.

Thanks for the reply. After setting roi_lb and roi_ub to the correct range, it works fine now.