Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: [depth]support binance futures orderbook depth buffer #1902

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

anywhy
Copy link
Contributor

@anywhy anywhy commented Feb 12, 2025

No description provided.

Copy link

codecov bot commented Feb 12, 2025

Codecov Report

Attention: Patch coverage is 0% with 11 lines in your changes missing coverage. Please review.

Project coverage is 22.71%. Comparing base (992001d) to head (6242fa6).
Report is 88 commits behind head on main.

Files with missing lines Patch % Lines
pkg/exchange/binance/parse.go 0.00% 6 Missing ⚠️
pkg/exchange/binance/stream.go 0.00% 4 Missing ⚠️
pkg/exchange/binance/futures.go 0.00% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1902      +/-   ##
==========================================
- Coverage   23.04%   22.71%   -0.33%     
==========================================
  Files         664      667       +3     
  Lines       50301    51185     +884     
==========================================
+ Hits        11590    11626      +36     
- Misses      37846    38692     +846     
- Partials      865      867       +2     
Files with missing lines Coverage Δ
pkg/exchange/binance/exchange.go 5.08% <ø> (ø)
pkg/exchange/binance/futures.go 0.00% <0.00%> (ø)
pkg/exchange/binance/stream.go 0.00% <0.00%> (ø)
pkg/exchange/binance/parse.go 19.53% <0.00%> (-0.06%) ⬇️

... and 10 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cd3eebf...6242fa6. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

// skip old events
if u.FinalUpdateID <= finalUpdateID {
continue
if b.isFutures {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the difference between futures and non-futures?

Copy link
Contributor Author

@anywhy anywhy Mar 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For futures, packet loss detection is stricter: it requires pu == previous u. If this condition isn't met, it means data might be missing, so you need to reinitialize the order book.
https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/How-to-manage-a-local-order-book-correctly
For spot, as long as U <= lastUpdateId <= u, the order book update continues. But if U > local order book update ID, you have to rebuild it from scratch.

}
if u.FirstUpdateID <= finalUpdateID && u.FinalUpdateID >= finalUpdateID {
pushUpdates = append(pushUpdates, u)
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this logic is the same as non-futures? it's just reversed ?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And the field PreviousUpdateID is not used here?

Copy link
Contributor Author

@anywhy anywhy Feb 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, it's indeed the same logic. I'll merge it.

@anywhy anywhy force-pushed the futures_depth_buffer branch from 2691d3d to 6b709e5 Compare February 15, 2025 09:32
@anywhy anywhy requested a review from c9s March 4, 2025 02:51
b.logger.Info("try fetching the snapshot due to missing update")
go b.tryFetch()
})
b.EmitReset()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EmitReset needs to be called after the mutex Unlock in order to avoid nested locking.

In your case, when calling this method, the mutex is still in the locked mode, did you verify this behavior? does it cause other effect?

@@ -236,7 +265,7 @@ func (b *Buffer) fetchAndPush() error {
continue
}

if u.FirstUpdateID > finalUpdateID+1 {
if u.FirstUpdateID > finalUpdateID+1 || (b.isFutures && idx > 0 && u.PreviousUpdateID != b.buffer[idx-1].FinalUpdateID) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please explain the reason in the comment we need the isFutures flag for the previous id check?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was originally planning to validate that the event in the buffer needs to satisfy U <= lastUpdateId <= u, but it seems unnecessary.

@@ -253,6 +282,10 @@ func (b *Buffer) fetchAndPush() error {
b.buffer = nil

// set the final update ID so that we will know if there is an update missing
if b.isFutures && len(pushUpdates) > 0 {
// reset finalUpdateId to first update event
finalUpdateID = pushUpdates[0].FinalUpdateID
Copy link
Owner

@c9s c9s Mar 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, why do we need to reset the finalUpdateId here? I don't quiet understand

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems I misunderstood the code logic here; this is completely unnecessary.

@anywhy anywhy force-pushed the futures_depth_buffer branch 2 times, most recently from 5a1422d to 7dfb507 Compare March 26, 2025 12:43
@anywhy anywhy force-pushed the futures_depth_buffer branch from 7dfb507 to 6242fa6 Compare March 26, 2025 12:45
@anywhy anywhy changed the title [FEATURE]: support binance futures orderbook depth buffer FEATURE: [depth]support binance futures orderbook depth buffer Mar 26, 2025
@anywhy anywhy requested a review from c9s March 27, 2025 09:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants