From 51220ba8cfeae4eb288d8011ff2aeecf152651ec Mon Sep 17 00:00:00 2001 From: Marc Blatter Date: Thu, 9 Jul 2026 20:16:51 +0200 Subject: [PATCH] CRITICAL: Bot now recovers orphaned trades on restart. Rebuilds active_trades from Binance balances (2026-07-09 20:16) --- src/main_ml.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/main_ml.py b/src/main_ml.py index e127359..e97b6e4 100644 --- a/src/main_ml.py +++ b/src/main_ml.py @@ -41,6 +41,30 @@ class TradingBotV03: self.client = Client(API_KEY, API_SECRET) self.price_history = {sym: [] for sym in SYMBOLS} self.active_trades = {} # {symbol: {'entry_price': float, 'qty': float}} + + # CRITICAL: Recover orphaned trades from Binance balance (bot got restarted!) + try: + account = self.client.get_account() + for b in account['balances']: + asset = b['asset'] + free = float(b['free']) + + # If we hold a symbol's coin, reconstruct it + for symbol in SYMBOLS: + if symbol.replace('USDT', '') == asset and free > 0.0001: + # Get current price to estimate entry + try: + current_price = float(self.get_current_price(symbol)) + self.active_trades[symbol] = { + 'entry_price': current_price, # Reconstructed (not exact, but better than 0) + 'qty': free, + 'entry_time': datetime.now().isoformat() + } + logger.warning(f"RECOVERED orphaned trade: {symbol} {free} coins @ ${current_price:.2f}") + except: + pass + except Exception as e: + logger.warning(f"Trade recovery failed: {e}") logger.info("Bot V0.3 initialized | Fresh Cache + Local Min + Hard TP/SL") def get_fresh_balance(self): @@ -254,3 +278,4 @@ if __name__ == '__main__': bot = TradingBotV03() bot.run() +