diff --git a/src/Managing.Application/Bots/TradingBotBase.cs b/src/Managing.Application/Bots/TradingBotBase.cs index c3395de4..af3a2dbe 100644 --- a/src/Managing.Application/Bots/TradingBotBase.cs +++ b/src/Managing.Application/Bots/TradingBotBase.cs @@ -2935,8 +2935,8 @@ public class TradingBotBase : ITradingBot return; // No positions at all } - // Only attempt recovery if the last position is cancelled - if (lastPosition.Status != PositionStatus.Canceled) + // Only attempt recovery if the last position is cancelled and recovery hasn't been attempted yet + if (lastPosition.Status != PositionStatus.Canceled || lastPosition.RecoveryAttempted) { return; } @@ -2955,6 +2955,9 @@ public class TradingBotBase : ITradingBot return; } + // Mark recovery as attempted before proceeding + lastPosition.RecoveryAttempted = true; + // Attempt recovery for the last position only bool recovered = await RecoverOpenPositionFromBroker(signal, lastPosition); if (recovered) diff --git a/src/Managing.Domain/Trades/Position.cs b/src/Managing.Domain/Trades/Position.cs index 092807fd..f644e5d7 100644 --- a/src/Managing.Domain/Trades/Position.cs +++ b/src/Managing.Domain/Trades/Position.cs @@ -77,6 +77,13 @@ namespace Managing.Domain.Trades [Required] public Guid InitiatorIdentifier { get; set; } + /// + /// Indicates whether position recovery has been attempted for this cancelled position + /// Used to prevent repeated recovery attempts for positions that were never filled + /// + [Id(18)] + public bool RecoveryAttempted { get; set; } + /// /// Return true if position is finished even if the position was canceled or rejected ///