Skip to content

Fix Bybit order-book stream: map depth to valid v5 spot tiers (1/50/200/1000) - #55

Merged
morpheums merged 4 commits into
mainfrom
fix/bybit-orderbook-depth
Jun 26, 2026
Merged

Fix Bybit order-book stream: map depth to valid v5 spot tiers (1/50/200/1000)#55
morpheums merged 4 commits into
mainfrom
fix/bybit-orderbook-depth

Conversation

@morpheums

@morpheums morpheums commented Jun 26, 2026

Copy link
Copy Markdown
Collaborator

Fixes a shipped bug: Bybit order-book WebSocket delivered zero updates for any non-tier depth.

Root cause

BybitStreamProtocol.BuildTopic built orderbook.{depth}.{symbol} from the raw requested depth. Bybit v5 spot only supports order-book depths 1 / 50 / 200 / 1000, so a consumer requesting e.g. depth: 20 produced the invalid topic orderbook.20.BTCUSDT → Bybit replies {"success":false} (classified as an error) → no book data, reconnect.

Fix

Map the requested depth up to the nearest supported Bybit tier before building the topic (20→50, 5→50, null→50, 1→1, 200→200, 500→1000, …). The consumer still gets at least the depth requested. RoutingKeyFor and Classify stay consistent (both derive from the topic; Bybit echoes the subscribed topic in data frames).

Testing

  • Unit: [Theory] covering all tier mappings + a routing-parity test (RoutingKeyFor(20) == subscribe topic == orderbook.50.BTCUSDT). dotnet build CryptoExchanges.Net.slnx 0W/0E; non-integration suite green.
  • Live integration: the multi-symbol order-book gate passed against real Bybit — and was re-run with depth 20 to directly prove orderbook.20.X now maps to orderbook.50.X and delivers updates.

Other exchanges, Core, and Http are untouched. Ships as v0.6.0-preview.3.

Summary by CodeRabbit

  • Bug Fixes

    • Improved order book subscription depth handling by mapping any requested depth to the next supported Bybit v5 spot tier (1/50/200/1000).
    • Default depth is now applied consistently when depth isn’t specified.
    • Invalid depths above the supported maximum now fail with a clear argument error.
    • Subscription topics and routing keys remain consistent for delta frames.
  • Tests

    • Added/updated unit tests covering rounding behavior, null-depth defaults, max-depth validation, and routing-key alignment.

morpheums and others added 2 commits June 26, 2026 22:01
Bybit v5 spot publishes order books only at depths 1/50/200/1000. The
topic builder used the raw requested depth, so a non-tier depth (e.g. 20)
produced an invalid topic that Bybit rejected (success:false), and no
book updates were ever delivered. Round each requested depth UP to the
nearest supported tier in BuildTopic, keeping RoutingKeyFor and Classify
consistent since both derive from the same topic.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…000 tier

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jun 26, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: ba8e9e30-a76d-4a69-89a1-e5266bbebeb3

📥 Commits

Reviewing files that changed from the base of the PR and between f3b5ced and a28fe8b.

📒 Files selected for processing (2)
  • src/CryptoExchanges.Net.Bybit/Streaming/BybitStreamProtocol.cs
  • tests/CryptoExchanges.Net.Bybit.Tests.Unit/Streaming/BybitStreamProtocolTests.cs
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/CryptoExchanges.Net.Bybit/Streaming/BybitStreamProtocol.cs
  • tests/CryptoExchanges.Net.Bybit.Tests.Unit/Streaming/BybitStreamProtocolTests.cs

📝 Walkthrough

Walkthrough

Bybit order book topic generation now maps requested depths to supported tiers, and unit tests were updated to cover rounding, null-depth defaulting, and routing-key consistency.

Changes

Bybit order book depth mapping

Layer / File(s) Summary
Protocol depth mapping
src/CryptoExchanges.Net.Bybit/Streaming/BybitStreamProtocol.cs
BuildTopic now routes order book depths through MapOrderBookDepth, which maps null to the default tier and other values to supported Bybit tiers.
Depth mapping tests
tests/CryptoExchanges.Net.Bybit.Tests.Unit/Streaming/BybitStreamProtocolTests.cs
The order book tests now cover rounded-up depths, null depth mapping to the default topic, matching RoutingKeyFor/subscribe topic behavior, and the updated 1000-tier comment.

Review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

A bunny hops, "thump-thump" with glee,
From depth one up to one-zero-zero-zero, see? 🐇
Topics map clean, no stray leaf in sight,
Tests nibble the edges and hold them tight.
Hops to the tier, and all is bright.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 22.22% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: mapping Bybit order-book depths to valid v5 spot tiers.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/bybit-orderbook-depth

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/CryptoExchanges.Net.Bybit/Streaming/BybitStreamProtocol.cs`:
- Around line 156-163: Adjust MapOrderBookDepth in BybitStreamProtocol so it no
longer silently maps unsupported depths to 1000; instead, validate the requested
depth and reject values outside Bybit’s supported range with a failure path or
exception, while only rounding valid in-range inputs to the nearest allowed
topic depth. Keep the existing handling for null/default and the supported
buckets inside the method, but make sure inputs like 0, negative values, and
values above the maximum are surfaced as invalid rather than rewritten.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 4b59898b-7bb4-4fde-ba3a-0e2b7649dedb

📥 Commits

Reviewing files that changed from the base of the PR and between f2e251e and f3b5ced.

📒 Files selected for processing (2)
  • src/CryptoExchanges.Net.Bybit/Streaming/BybitStreamProtocol.cs
  • tests/CryptoExchanges.Net.Bybit.Tests.Unit/Streaming/BybitStreamProtocolTests.cs

Comment thread src/CryptoExchanges.Net.Bybit/Streaming/BybitStreamProtocol.cs
…tead of silent clamp

CodeRabbit PR #55: depths above the deepest Bybit v5 spot tier (1000) were
silently clamped DOWN to 1000, under-delivering a shallower book than requested
with no signal. Since the mapping otherwise rounds UP, an unsatisfiable request
(> 1000) now throws ArgumentOutOfRangeException, matching the file's existing
unsupported-value style. Round-up for 1..1000 unchanged.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Fixes Bybit v5 spot order-book WebSocket subscriptions by mapping requested depths to the nearest supported tier so subscriptions don’t silently fail due to invalid topics (e.g., orderbook.20.*).

Changes:

  • Map non-tier order-book depths to valid Bybit v5 spot tiers (1/50/200/1000) when building the topic.
  • Throw ArgumentOutOfRangeException for depths above the maximum supported tier (1000) instead of subscribing to an invalid/unsatisfiable depth.
  • Add unit tests covering tier mapping, default-depth handling, and routing-key/topic parity.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/CryptoExchanges.Net.Bybit/Streaming/BybitStreamProtocol.cs Centralizes order-book topic building to use a depth-tier mapper (1/50/200/1000) and rejects depths > 1000.
tests/CryptoExchanges.Net.Bybit.Tests.Unit/Streaming/BybitStreamProtocolTests.cs Adds theory-based coverage for depth mapping and verifies RoutingKeyFor matches the subscribe topic for non-tier depths.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/CryptoExchanges.Net.Bybit/Streaming/BybitStreamProtocol.cs
…message

Address Copilot nit on PR #55: the ArgumentOutOfRangeException for depth > 1000
now enumerates the discrete supported Bybit v5 spot tiers (1, 50, 200, 1000)
instead of only stating the maximum, making the failure actionable. Same
exception type, paramName, and actual value preserved.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@morpheums
morpheums merged commit ad829f4 into main Jun 26, 2026
2 checks passed
@morpheums
morpheums deleted the fix/bybit-orderbook-depth branch June 26, 2026 22:28
morpheums added a commit that referenced this pull request Jun 27, 2026
Bump version 0.6.0-preview.2 -> 0.6.0-preview.3 and add the dated
CHANGELOG section for the Bybit order-book WebSocket non-tier depth fix
(PR #55), with comparison links. Build verified 0W/0E.

Co-authored-by: Morpheums <morpheums@users.noreply.github.com>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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