QuantDinger icon indicating copy to clipboard operation
QuantDinger copied to clipboard

Exchange-native stop loss is silently never placed when the trigger price is not on a tick boundary

Open vivissippi opened this issue 3 weeks ago • 0 comments

Describe the bug

Crypto swap strategies that declare percentage protection (stop_loss_pct / take_profit_pct / trailing_stop_pct) are supposed to get an exchange-native STOP_MARKET right after the entry fills. On Binance USDT-M futures that order is rejected every time, so the position runs with no exchange-side stop loss and silently falls back to QuantDinger's own 1s runtime protection.

Cause: native_protection.py sends the computed trigger price through plain str(price) without snapping it to tickSize. A percentage stop is entry * (1 ± pct), which almost never lands on a tick boundary:

76049.9 * 1.025  # -> 77951.14749999999   (4 decimals + float noise)
                 # BTCUSDT tickSize = 0.1

Binance returns {"code":-1111,"msg":"Precision is over the maximum defined for this asset."}.

It stays silent because the placement is attempted only once at fill time, with no retry, and the exception is only logged — the pending order is still marked filled (pending_order_worker.py:2274, :2304).

Affected code (main / v5.8.5)

backend_api_python/app/services/live_trading/native_protection.py

Line Code
118 "quantity": str(request.quantity) — not snapped to stepSize
130 triggerPrice=str(price) — not snapped to tickSize
142 body["activatePrice"] = str(activation)

This is not a blanket gap: the regular order path already normalizes correctly via BinanceFuturesClient._normalize_price() and the shared instrument_rules provider (which already parses Binance tickSize). The protection path just never calls them.

Steps to reproduce

  1. Docker Compose + Binance USDT-M futures credentials.

  2. Deploy a Strategy API V2 swap strategy on Crypto:BTC/USDT@binance:swap that opens with:

    order_target_percent(SYMBOL, 0.5, reason="entry",
                         stop_loss_pct=0.025,
                         trailing_stop_pct=0.05,
                         trailing_activation_pct=0.05)
    
  3. Run it in live mode and let the entry fill.

  4. GET /fapi/v1/openAlgoOrders returns [] although the position is open.

Expected behavior

Snap triggerPrice / activatePrice to tickSize and quantity to stepSize (reuse instrument_rules), so the order is sent as 77951.1 instead of 77951.14749999999. Also worth surfacing native_protection_error in the UI, or retrying, so an unprotected position is visible rather than silent.

Deployment method

Docker Compose

Relevant logs

{"timestamp":"2026-11-08T13:25:40.590830+00:00","level":"ERROR",
 "logger":"app.services.pending_order_worker",
 "message":"Native protection failed pending_id=3 strategy_id=4 symbol=BTC/USDT:
 Binance HTTP 400: {\"code\":-1111,\"msg\":\"Precision is over the maximum defined for this asset.\"}",
 "process_role":"trading"}


Same order in the strategy log — note it still ends up `status=filled`:


13:25:40.621  trade  Trade executed: open_short BTC/USDT filled=0.001000 @ 76049.900000
13:25:40.591  error  Native protection placement failed; runtime protection remains active: BTC/USDT: Binance HTTP 400: {"code":-1111,...}


Environment: v5.6.1 image, BTC/USDT 4h swap, 5x, `stop_loss_pct=0.025`,
`trailing_stop_pct=0.05`, `trailing_activation_pct=0.05`. Reproduced identically on
`main` / v5.8.5, so upgrading does not help.

vivissippi avatar Nov 09 '26 11:11 vivissippi