From b25f0be083b51401b91be218e379d718d07a9358 Mon Sep 17 00:00:00 2001 From: cryptooda Date: Sun, 5 Oct 2025 02:49:33 +0700 Subject: [PATCH] Remove extra message on the low usdc while position open --- .../Bots/Grains/LiveTradingBotGrain.cs | 40 ++++++++----------- 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/src/Managing.Application/Bots/Grains/LiveTradingBotGrain.cs b/src/Managing.Application/Bots/Grains/LiveTradingBotGrain.cs index 02589f63..4e77951a 100644 --- a/src/Managing.Application/Bots/Grains/LiveTradingBotGrain.cs +++ b/src/Managing.Application/Bots/Grains/LiveTradingBotGrain.cs @@ -259,9 +259,10 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable var hasOpenPositions = await HasOpenPositionsInDatabaseAsync(); if (hasOpenPositions) { - _logger.LogWarning("Cannot stop LiveTradingBotGrain {GrainId} - bot has open positions in database", - this.GetPrimaryKey()); - throw new InvalidOperationException("Cannot stop bot while it has open positions. Please close all positions first."); + _logger.LogWarning("Cannot stop strategy {Name} : strategy has open positions in database", + _tradingBot?.Config.Name); + throw new InvalidOperationException( + "Cannot stop bot while it has open positions. Please close all positions first."); } } @@ -372,24 +373,12 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable if (!balanceCheckResult.IsSuccessful) { - // Log the specific reason for the failure - await _tradingBot.LogWarning( - $"Balance check failed: {balanceCheckResult.Message} (Reason: {balanceCheckResult.FailureReason})"); - // Check if the bot should stop due to this failure if (balanceCheckResult.ShouldStopBot) { - await _tradingBot.LogWarning( - $"Stopping bot due to balance check failure: {balanceCheckResult.Message}"); await StopAsync(); return; } - else - { - // Skip this execution cycle but continue running - await _tradingBot.LogInformation("Skipping this execution cycle due to balance check failure."); - return; - } } } catch (Exception ex) @@ -616,9 +605,10 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable var hasOpenPositions = await HasOpenPositionsInDatabaseAsync(); if (hasOpenPositions) { - _logger.LogWarning("Cannot delete LiveTradingBotGrain {GrainId} - bot has open positions in database", + _logger.LogWarning("Cannot delete LiveTradingBotGrain {GrainId} - bot has open positions in database", this.GetPrimaryKey()); - throw new InvalidOperationException("Cannot delete bot while it has open positions. Please close all positions first."); + throw new InvalidOperationException( + "Cannot delete bot while it has open positions. Please close all positions first."); } try @@ -880,20 +870,21 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable { // For non-running bots, check grain state positions var hasOpenPositions = _state.State.Positions?.Values.Any(p => !p.IsFinished()) ?? false; - _logger.LogDebug("Bot {GrainId} has open positions: {HasOpenPositions} (from grain state)", + _logger.LogDebug("Bot {GrainId} has open positions: {HasOpenPositions} (from grain state)", this.GetPrimaryKey(), hasOpenPositions); return Task.FromResult(hasOpenPositions); } // For running bots, check live positions var hasLiveOpenPositions = _tradingBot.Positions?.Values.Any(p => !p.IsFinished()) ?? false; - _logger.LogDebug("Bot {GrainId} has open positions: {HasOpenPositions} (from live data)", + _logger.LogDebug("Bot {GrainId} has open positions: {HasOpenPositions} (from live data)", this.GetPrimaryKey(), hasLiveOpenPositions); return Task.FromResult(hasLiveOpenPositions); } catch (Exception ex) { - _logger.LogError(ex, "Error checking open positions for LiveTradingBotGrain {GrainId}", this.GetPrimaryKey()); + _logger.LogError(ex, "Error checking open positions for LiveTradingBotGrain {GrainId}", + this.GetPrimaryKey()); return Task.FromResult(false); // Default to false on error to avoid blocking autoswap } } @@ -912,16 +903,17 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable async tradingService => await tradingService.GetPositionsByInitiatorIdentifierAsync(botId)); var hasOpenPositions = positions?.Any(p => !p.IsFinished()) ?? false; - _logger.LogDebug("Bot {GrainId} has open positions in database: {HasOpenPositions}", + _logger.LogDebug("Bot {GrainId} has open positions in database: {HasOpenPositions}", botId, hasOpenPositions); - + if (hasOpenPositions) { var openPositions = positions?.Where(p => !p.IsFinished()).ToList() ?? new List(); - _logger.LogWarning("Bot {GrainId} cannot be stopped - has {Count} open positions in database: {Positions}", + _logger.LogWarning( + "Bot {GrainId} cannot be stopped - has {Count} open positions in database: {Positions}", botId, openPositions.Count, string.Join(", ", openPositions.Select(p => p.Identifier))); } - + return hasOpenPositions; } catch (Exception ex)