From f578d8dc228986d524f67de5972ddb651bb7804a Mon Sep 17 00:00:00 2001 From: cryptooda Date: Mon, 5 Jan 2026 18:58:27 +0700 Subject: [PATCH] Refactor SpotBot position history logging - Updated SpotBot to log detailed information when a closing position is found in the history, including position direction and dates. - Enhanced logging for scenarios where no closing position is found or when position history is unavailable, improving clarity on position status. - Removed outdated log messages to streamline the logging process. --- src/Managing.Application/Bots/SpotBot.cs | 31 ++++++++++++++++++------ 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/src/Managing.Application/Bots/SpotBot.cs b/src/Managing.Application/Bots/SpotBot.cs index 5a581f91..f89131ca 100644 --- a/src/Managing.Application/Bots/SpotBot.cs +++ b/src/Managing.Application/Bots/SpotBot.cs @@ -510,23 +510,40 @@ public class SpotBot : TradingBotBase if (positionHistory != null && positionHistory.Count != 0) { - var recentPosition = positionHistory + // Find a SELL/SHORT position that occurred AFTER our open position was created + // This indicates our LONG position was closed + var closingPosition = positionHistory + .Where(p => p.OriginDirection == TradeDirection.Short && + p.Date >= position.Date) .OrderByDescending(p => p.Date) .FirstOrDefault(); - if (recentPosition != null) + if (closingPosition != null) { await LogDebugAsync( - $"✅ Spot Position Found in History\n" + + $"✅ Closing Position Found in History\n" + $"Position: `{position.Identifier}`\n" + - $"Ticker: `{recentPosition.Ticker}`\n" + - $"Date: `{recentPosition.Date}`"); + $"Ticker: `{closingPosition.Ticker}`\n" + + $"Direction: `{closingPosition.OriginDirection}` (SHORT = Sold/Closed)\n" + + $"Date: `{closingPosition.Date}`\n" + + $"Our Position Open Date: `{position.Date}`"); return (true, false); } + + await LogDebugAsync( + $"ℹ️ No Closing Position Found in History\n" + + $"Position: `{position.Identifier}`\n" + + $"Found {positionHistory.Count} history entries but none indicate position was closed\n" + + $"Position is likely still open"); + } + else + { + await LogDebugAsync( + $"ℹ️ No Position History Available\n" + + $"Position: `{position.Identifier}`\n" + + $"Position is likely still open"); } - await LogDebugAsync( - $"❌ No Spot Position Found in Exchange History\nPosition: `{position.Identifier}`\nPosition may still be open or data is delayed"); return (false, false); } catch (Exception ex)