CRITICAL: Bot now recovers orphaned trades on restart. Rebuilds active_trades from Binance balances (2026-07-09 20:16)

This commit is contained in:
Marc Blatter 2026-07-09 20:16:51 +02:00
parent 572bfdfb0d
commit 51220ba8cf
1 changed files with 25 additions and 0 deletions

View File

@ -41,6 +41,30 @@ class TradingBotV03:
self.client = Client(API_KEY, API_SECRET) self.client = Client(API_KEY, API_SECRET)
self.price_history = {sym: [] for sym in SYMBOLS} self.price_history = {sym: [] for sym in SYMBOLS}
self.active_trades = {} # {symbol: {'entry_price': float, 'qty': float}} 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") logger.info("Bot V0.3 initialized | Fresh Cache + Local Min + Hard TP/SL")
def get_fresh_balance(self): def get_fresh_balance(self):
@ -254,3 +278,4 @@ if __name__ == '__main__':
bot = TradingBotV03() bot = TradingBotV03()
bot.run() bot.run()