From 41ad549ae2aa73e739a90a73edadbb267e8fb74a Mon Sep 17 00:00:00 2001 From: kroutony Date: Mon, 16 Mar 2026 04:11:34 +0000 Subject: [PATCH] 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) --- main.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index fbe61bd..da36461 100644 --- a/main.py +++ b/main.py @@ -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