diff --git a/src/Managing.Application/Bots/SpotBot.cs b/src/Managing.Application/Bots/SpotBot.cs index 31fa2bb2..4f2c4c7b 100644 --- a/src/Managing.Application/Bots/SpotBot.cs +++ b/src/Managing.Application/Bots/SpotBot.cs @@ -970,8 +970,25 @@ public class SpotBot : TradingBotBase // SHORT signal closes the open LONG position await LogInformationAsync( $"🔻 Short Signal - Closing Long Position\nClosing position `{openedPosition.Identifier}` due to SHORT signal\nSignal: `{signal.Identifier}`"); - await CloseTrade(previousSignal, openedPosition, openedPosition.Open, lastPrice, true); - await SetPositionStatus(previousSignal.Identifier, PositionStatus.Finished); + + try + { + await CloseTrade(previousSignal, openedPosition, openedPosition.Open, lastPrice, true); + // Only mark as Finished if close was successful + await SetPositionStatus(previousSignal.Identifier, PositionStatus.Finished); + } + catch (Exception ex) + { + await LogWarningAsync( + $"❌ Failed to Close Position on SHORT Signal\n" + + $"Position: `{openedPosition.Identifier}`\n" + + $"Signal: `{signal.Identifier}`\n" + + $"Error: {ex.Message}\n" + + $"Position status NOT changed - will retry on next cycle"); + // Don't change position status if close failed + // The position will be retried on the next bot cycle + } + SetSignalStatus(signal.Identifier, SignalStatus.Expired); return null; // No new position opened for SHORT signals } @@ -1084,6 +1101,11 @@ public class SpotBot : TradingBotBase await HandleClosedPosition(position, forceMarketClose ? lastPrice : null, forceMarketClose); } + else + { + // Re-throw exception for other cases so caller knows the operation failed + throw; + } } }