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

Question-Explanation of the differences between `LOCAL` and `EXCH` #88

Closed bohblue2 closed 2 months ago

bohblue2 commented 2 months ago

https://github.com/nkaz001/hftbacktest/blob/780bfa178bcbec85446fe11b2bd4ce57c776ef64/rust/src/types.rs#L106-L118

https://github.com/nkaz001/hftbacktest/blob/780bfa178bcbec85446fe11b2bd4ce57c776ef64/rust/src/types.rs#L140-L145

In the above two codes, I have seen that there are two types of LOCAL_EVENT, EXCH_EVENT and the processor is also handling for two event types(LOCAL, and EXCH).

Could you please explain the difference between LOCAL and EXCH ?

The official documentation doesn't explain it either, so it's hard to interpret the code.

Thank you.

nkaz001 commented 2 months ago

These two documents and the data util code may not provide all the information you need, but they will help you understand the concepts.

These two flags indicate whether an event needs to be processed at the local processor or at the exchange processor. If an event has the LOCAL_EVENT flag, it should be processed by the local processor at local_ts. If it has the EXCH_EVENT flag, it should be processed by the exchange processor at exch_ts.

These flags are necessary because the order of local_ts and exch_ts can differ. In different feed streams, an event occurring at the exchange before another event might be received locally after the latter event.

To maintain the correct order of events on each side, the data conversion utility duplicates the event, assigning one the EXCH_EVENT flag and the other the LOCAL_EVENT flag. This way, events with the LOCAL_EVENT flag are sorted by local_ts, and events with the EXCH_EVENT flag are sorted by exch_ts.

bohblue2 commented 2 months ago

It's important to look at the Local_ts and exch_ts fields and process the events in chronological order. I'm getting the idea now.

I'll analyze the source code a bit more and raise the question again.

I'll close the issue when I'm done with the question in a couple weeks.

Thank you.

bohblue2 commented 2 months ago

@nkaz001 Sorry to ask a similar question again, but can you explain a little more?

I'm reverse engineering a project and the exact definitions of LOCAL_EVENT and EXCH_EVENT are still vague to me.

Have a great day.

Thank you.

Brian Bae.

nkaz001 commented 2 months ago

Sure, but could you please elaborate on your question? Which part do you find ambiguous?

bohblue2 commented 2 months ago

@nkaz001 Sorry for late response, I am still reverse engineering your code.

Can I ask the question again when I'm clearer about what I know and what I don't know?

Thank you.

Brian Bae