diff --git a/src/Managing.Application/Bots/Grains/LiveTradingBotGrain.cs b/src/Managing.Application/Bots/Grains/LiveTradingBotGrain.cs index 735b28e2..0a84a60c 100644 --- a/src/Managing.Application/Bots/Grains/LiveTradingBotGrain.cs +++ b/src/Managing.Application/Bots/Grains/LiveTradingBotGrain.cs @@ -398,27 +398,39 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable return; } - // Use coordinated balance checking and swap management through AgentGrain - try + if (_tradingBot.Positions.Any(p => p.Value.IsOpen() || p.Value.Status.Equals(PositionStatus.New))) { - var agentGrain = GrainFactory.GetGrain(_state.State.User.Id); - var balanceCheckResult = - await agentGrain.CheckAndEnsureEthBalanceAsync(_state.State.Identifier, _tradingBot.Account.Name); - - if (!balanceCheckResult.IsSuccessful) + _logger.LogInformation( + "Bot {BotId} has open positions. Trading loop will continue managing existing positions.", + _state.State.Identifier); + } + else + { + // If no open positions, ensure ETH balance is sufficient for new positions + // Use coordinated balance checking and swap management through AgentGrain + try { - // Check if the bot should stop due to this failure - if (balanceCheckResult.ShouldStopBot) + var agentGrain = GrainFactory.GetGrain(_state.State.User.Id); + var balanceCheckResult = + await agentGrain.CheckAndEnsureEthBalanceAsync(_state.State.Identifier, + _tradingBot.Account.Name); + + if (!balanceCheckResult.IsSuccessful) { - await StopAsync(balanceCheckResult.Message); - return; + // Check if the bot should stop due to this failure + if (balanceCheckResult.ShouldStopBot) + { + await StopAsync(balanceCheckResult.Message); + return; + } } } - } - catch (Exception ex) - { - _logger.LogError(ex, "Error during coordinated balance check for bot {BotId}", _state.State.Identifier); - // Continue execution to avoid stopping the bot due to coordination errors + catch (Exception ex) + { + _logger.LogError(ex, "Error during coordinated balance check for bot {BotId}", + _state.State.Identifier); + // Continue execution to avoid stopping the bot due to coordination errors + } } // Execute the bot's Run method @@ -973,7 +985,7 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable _scopeFactory, async tradingService => await tradingService.GetPositionsByInitiatorIdentifierAsync(botId)); - var hasOpenPositions = positions?.Any(p => p.IsOpen()) ?? false; + var hasOpenPositions = positions?.Any(p => p.IsOpen() || p.Status.Equals(PositionStatus.New)) ?? false; _logger.LogDebug("Bot {GrainId} has open positions in database: {HasOpenPositions}", botId, hasOpenPositions); diff --git a/src/Managing.Application/Trading/Handlers/OpenPositionCommandHandler.cs b/src/Managing.Application/Trading/Handlers/OpenPositionCommandHandler.cs index bd1fd847..2431bd42 100644 --- a/src/Managing.Application/Trading/Handlers/OpenPositionCommandHandler.cs +++ b/src/Managing.Application/Trading/Handlers/OpenPositionCommandHandler.cs @@ -121,7 +121,7 @@ namespace Managing.Application.Trading.Handlers request.Date, TradeStatus.Requested); - position.Status = IsOpenTradeHandled(position.Open.Status, account.Exchange) + position.Status = IsOpenTradeHandled(position.Open.Status) ? position.Status : PositionStatus.Rejected; @@ -133,10 +133,10 @@ namespace Managing.Application.Trading.Handlers return position; } - private static bool IsOpenTradeHandled(TradeStatus tradeStatus, TradingExchanges exchange) + private static bool IsOpenTradeHandled(TradeStatus tradeStatus) { return tradeStatus == TradeStatus.Filled - || (exchange == TradingExchanges.Evm && tradeStatus == TradeStatus.Requested); + || tradeStatus == TradeStatus.Requested; } } } \ No newline at end of file