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 358 forks source link

Order sequence error due to delays #48

Closed xtma closed 10 months ago

xtma commented 11 months ago

Normally, the local process should receive the order acceptance information first and then the order execution information, as shown below:

exch process  2 0 1 1686067320067000
local process  2 1 0 1686067320081000
filled and del  2 1686067324745000.0
local process  2 3 0 1686067324751000
current_timestamp: 1686067380023000 , order_id: 2 , order_status: FILLED , order_req: NONE , order_price: 26097.2 , exec_price: 26097.2 , order_qty: 0.001 , exec_qty: 0.001 , leave_qty: 0.0

However, due to delays, it is possible to receive the FILLED information before the NEW information. In this case, the local process may mistakenly assume that a new order has been submitted, but the exchange process does not have this order because it has already been executed.

exch process  13 0 1 1686067980057500
filled and del  13 1686067980066000.0
local process  13 3 0 1686067980072000
local process  13 1 0 1686067980092000
current_timestamp: 1686068040023000 , order_id: 13 , order_status: NEW , order_req: NONE , order_price: 26159.800000000003 , exec_price: 0.0 , order_qty: 0.001 , exec_qty: 0.0 , leave_qty: 0.001

My suggestion is that when cleaning up the inactive orders (hbt.clear_inactive_orders()), it is necessary to consider deleting the orders that receive both the NEW and FILLED information for the same ID.

nkaz001 commented 11 months ago

Could you check if the fix #49 works as expected?

xtma commented 10 months ago

Yes, it works without any issues.