Fix stop-loss placement failure after cancel by increasing delay and adding retry

Bitfinex needs time to release locked balance after cancelling a stop order.
Increased wait from 0.3s to 1.0s, and added a one-time retry with 2s delay
if the first attempt fails.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
kroutony 2026-03-16 04:11:34 +00:00
parent 36225df832
commit 41ad549ae2

View File

@ -413,7 +413,7 @@ def run_cycle():
cancelled_any = True
logger.info("Cancelled old stop %s for %s (position size changed)", s["id"], sym)
if cancelled_any:
time.sleep(0.3) # Wait for Bitfinex to release locked balance
time.sleep(1.0) # Wait for Bitfinex to release locked balance
# Place new stop-loss for TOTAL position amount at new avg entry
total_amount = pos.get("amount", amount)
@ -433,6 +433,11 @@ def run_cycle():
logger.info("ATR unavailable for %s, using fixed %.0f%% stop", sym, config.STOP_LOSS_PCT * 100)
sl = trader.place_stop_loss_order(sym, total_amount, entry_price, stop_price=atr_stop)
if sl is None and cancelled_any:
# Retry once — Bitfinex may need more time to release locked balance
time.sleep(2.0)
logger.info("Retrying stop-loss for %s after balance release delay", sym)
sl = trader.place_stop_loss_order(sym, total_amount, entry_price, stop_price=atr_stop)
if sl:
stop_price = sl["stop_price"]
# Update stop_orders_by_sym so subsequent actions in this cycle see it