From bb4db3deff74dd2e0a42b62950a0ea45e1e873a2 Mon Sep 17 00:00:00 2001 From: cryptooda Date: Mon, 15 Sep 2025 01:27:06 +0700 Subject: [PATCH] Fix scenario exec --- .../Bots/TradingBotBase.cs | 28 ++++++++++--------- .../Grains/PriceFetcherInitializer.cs | 5 ++-- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/Managing.Application/Bots/TradingBotBase.cs b/src/Managing.Application/Bots/TradingBotBase.cs index 60bcb018..5381fa56 100644 --- a/src/Managing.Application/Bots/TradingBotBase.cs +++ b/src/Managing.Application/Bots/TradingBotBase.cs @@ -94,9 +94,10 @@ public class TradingBotBase : ITradingBot $"📢 I'll notify you when signals are triggered."; await LogInformation(startupMessage); - + // Notify AgentGrain about bot startup - await NotifyAgentGrainAsync(AgentSummaryEventType.BotStarted, $"Bot: {Config.Name}, Ticker: {Config.Ticker}"); + await NotifyAgentGrainAsync(AgentSummaryEventType.BotStarted, + $"Bot: {Config.Name}, Ticker: {Config.Ticker}"); break; case BotStatus.Running: @@ -170,15 +171,11 @@ public class TradingBotBase : ITradingBot public async Task Run() { - if (!Config.IsForBacktest) - { - await LoadLastCandle(); - } - // Update signals for live trading only if (!Config.IsForBacktest) { await UpdateSignals(); + await LoadLastCandle(); } if (!Config.IsForWatchingOnly) @@ -770,7 +767,8 @@ public class TradingBotBase : ITradingBot } // Notify AgentGrain about position opening - await NotifyAgentGrainAsync(AgentSummaryEventType.PositionOpened, $"Signal: {signal.Identifier}"); + await NotifyAgentGrainAsync(AgentSummaryEventType.PositionOpened, + $"Signal: {signal.Identifier}"); Logger.LogInformation($"Position requested"); return position; // Return the created position without adding to list @@ -1204,8 +1202,11 @@ public class TradingBotBase : ITradingBot } // Notify AgentGrain about position closing - var pnlInfo = position.ProfitAndLoss?.Realized != null ? $"PnL: {position.ProfitAndLoss.Realized:F2}" : "PnL: Unknown"; - await NotifyAgentGrainAsync(AgentSummaryEventType.PositionClosed, $"Signal: {position.SignalIdentifier}, {pnlInfo}"); + var pnlInfo = position.ProfitAndLoss?.Realized != null + ? $"PnL: {position.ProfitAndLoss.Realized:F2}" + : "PnL: Unknown"; + await NotifyAgentGrainAsync(AgentSummaryEventType.PositionClosed, + $"Signal: {position.SignalIdentifier}, {pnlInfo}"); } else { @@ -1904,7 +1905,7 @@ public class TradingBotBase : ITradingBot await ServiceScopeHelpers.WithScopedService(_scopeFactory, async grainFactory => { var agentGrain = grainFactory.GetGrain(Account.User.Id); - + var updateEvent = new AgentSummaryUpdateEvent { UserId = Account.User.Id, @@ -1915,13 +1916,14 @@ public class TradingBotBase : ITradingBot }; await agentGrain.OnAgentSummaryUpdateAsync(updateEvent); - + Logger.LogDebug("Sent agent notification: {EventType} for bot {BotId}", eventType, Identifier); }); } catch (Exception ex) { - Logger.LogError(ex, "Failed to send agent notification: {EventType} for bot {BotId}", eventType, Identifier); + Logger.LogError(ex, "Failed to send agent notification: {EventType} for bot {BotId}", eventType, + Identifier); } } } \ No newline at end of file diff --git a/src/Managing.Application/Grains/PriceFetcherInitializer.cs b/src/Managing.Application/Grains/PriceFetcherInitializer.cs index ad8a13ef..785a4555 100644 --- a/src/Managing.Application/Grains/PriceFetcherInitializer.cs +++ b/src/Managing.Application/Grains/PriceFetcherInitializer.cs @@ -16,8 +16,9 @@ public class PriceFetcherInitializer : IHostedService public async Task StartAsync(CancellationToken cancellationToken) { // Initialize grains for different timeframes - var timeframes = new[] - { + var timeframes = new[] + { + Timeframe.FiveMinutes, Timeframe.FifteenMinutes, Timeframe.OneHour, Timeframe.FourHour,