Skip to content

fix(trading): snap Bitget and Bybit native protection prices to the tick - #363

Closed
aliassar wants to merge 1 commit into
OpenByteInc:mainfrom
aliassar:fix/native-protection-tick-size
Closed

aliassar wants to merge 1 commit into
OpenByteInc:mainfrom
aliassar:fix/native-protection-tick-size

Conversation

@aliassar

Copy link
Copy Markdown
Contributor

Summary

#362 fixed the Binance side of #361, but Bitget (UTA and classic) and Bybit still send native protection prices with plain str(). A 2.5% stop on a 76049.9 short goes out as 77951.14749999999, which isn't on the contract tick, the same precision problem Binance rejected in #361. This applies the same treatment #362 gave Binance to the other two venues.

Related issue

Changes

  • Bitget UTA: stopLoss, takeProfit and the trailing activationPrice go through client._normalize_price + client._dec_str, same pattern as fix(trading): normalize Binance native protection orders #362.
  • Bitget classic: the loss_plan/profit_plan and moving_plan triggerPrice do too, so priceEndStep is respected (e.g. step 0.5).
  • Bybit: conditional triggerPrice, the trailing activePrice, and the trailingStop distance (entry * pct, also a price) are snapped to tickSize.
  • Each raises <venue>_native_protection_price_below_minimum if a price snaps to 0, like the Binance path. As with fix(trading): normalize Binance native protection orders #362, a leg that fails this raises after earlier legs are placed; the worker logs it and runtime protection stays on.
  • OKX and Gate still send str(price); their clients have no price-normalize helper, so I left them out.

API documentation

  • No route or schema changes

Screenshots

Not applicable (backend-only fix).

Test plan

  • Tested locally with docker compose up -d --build
  • Backend logs show no errors
  • Relevant pytest tests pass

python -m pytest tests/test_native_protection.py -q passes (16). The three new tests use the issue's numbers and fail on main. The existing Bitget UTA assertion changed from "90.0" to "90" since prices now go through _dec_str, and the existing classic test stubs get_contract so it doesn't hit the network. ruff check and scripts/backend_quality_check.py pass. I also ran PendingOrderWorker._attach_native_protection against Bybit and Bitget clients with a faked HTTP layer that rejects off-tick prices: all three were rejected on main, and after the change they place 77951.1 (Bybit, UTA) and 77951 (classic, 0.5 step).

OpenByteInc#362 normalized the Binance protection orders, but Bitget (UTA and
classic) and Bybit still sent percentage-derived prices with str(), e.g.
stopLoss=77951.14749999999 for a 2.5% stop on a 76049.9 short. Those are
off the contract tick, so the venue rejects the protection order.

Run the stop-loss, take-profit and trailing activation prices (and the
Bybit trailingStop distance) through each client's _normalize_price, the
same way OpenByteInc#362 does for Binance, and raise when the result falls below the
minimum.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: aliassar <[email protected]>
@sonarqubecloud

Copy link
Copy Markdown

@codecov

codecov Bot commented Nov 11, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 41.66667% with 14 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...hon/app/services/live_trading/native_protection.py 41.66% 7 Missing and 7 partials ⚠️

📢 Thoughts on this report? Let us know!

@brokermr810

Copy link
Copy Markdown
Collaborator

Thank you for the fix and the thorough tests. We preserved your commit and authorship in test, then extended the same tick-size and minimum-quantity safeguards to OKX, Gate, and HTX. The integrated changes are now tracked in #365 (test → main), so I’m closing this PR as superseded. Your contribution remains part of the new PR’s history.

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