Skip to content

fix(trading): derive Gate native protection trigger rule from position side and role - #384

Open
huuan26 wants to merge 1 commit into
OpenByteInc:mainfrom
huuan26:fix/gate-protection-trigger-rule
Open

huuan26 wants to merge 1 commit into
OpenByteInc:mainfrom
huuan26:fix/gate-protection-trigger-rule

Conversation

@huuan26

@huuan26 huuan26 commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

Summary

Derive Gate /api/v4/futures/usdt/price_orders trigger rule from (role, pos_side) rather than price > request.entry_price in _place_gate().

Previously, _place_gate() set rule = 1 if price > request.entry_price else 2, which inverted the trigger condition in two realistic cases:

  1. When request.entry_price <= 0 (e.g., queued protection orders placed before entry_price is resolved), both SL and TP received rule = 1 (>= trigger.price), causing a long stop-loss or short take-profit to trigger immediately at market.
  2. When a stop-loss is trailed or locked into profit past entry_price (long SL above entry or short SL below entry), rule flipped to the opposite inequality and triggered an immediate close, while gate_protection_orders() in quick_trade/position_activity.py misclassified long SL orders (rule == 1) as "tp".

This aligns _place_gate() with _place_bybit() (native_protection.py) and gate_protection_orders() (quick_trade/position_activity.py).

Changes

  • backend_api_python/app/services/live_trading/native_protection.py: compute rises = (role == "take_profit" and request.pos_side == "long") or (role == "stop_loss" and request.pos_side == "short") and set rule = 1 if rises else 2.
  • backend_api_python/tests/test_native_protection.py: add parametrized regression tests covering long/short SL and TP with entry_price=0.0 and profit-locked stop-loss prices past entry, including round-trip classification via gate_protection_orders().

Test plan

  • pytest tests/test_native_protection.py -k "not test_partial_fill_protection_client_ids_are_stable_and_distinct" --cov=app.services.live_trading.native_protection --cov-branch -v -> 29 passed, 1 deselected (0 missed lines/branches on modified lines; deselected test imports pending_order_worker which requires cryptography in local env).
  • ruff check app scripts tests, compileall -q app scripts tests, backend_quality_check.py, check_requirements_lock.py, check_docs.py, check_version.py, check_mojibake.py all pass.

API documentation

  • No route or schema changes.

…n side and role

Signed-off-by: huuan26 <phanbuihuuan261103@gmail.com>
@huuan26
huuan26 requested a review from brokermr810 as a code owner November 15, 2026 08:19
@sonarqubecloud

Copy link
Copy Markdown

@codecov

codecov Bot commented Nov 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

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.

1 participant