Update agent stats

This commit is contained in:
2025-09-28 13:55:24 +07:00
parent 6e07bac6ae
commit a8d09c36b7
5 changed files with 37 additions and 20 deletions

View File

@@ -7,6 +7,7 @@ using Managing.Application.Orleans;
using Managing.Common;
using Managing.Core;
using Managing.Core.Exceptions;
using Managing.Domain.Bots;
using Managing.Domain.Statistics;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
@@ -154,32 +155,25 @@ public class AgentGrain : Grain, IAgentGrain
try
{
// Get all positions for this agent's bots as initiator
var positions = await _tradingService.GetPositionsByInitiatorIdentifiersAsync(_state.State.BotIds);
var positions = await _tradingService.GetPositionByUserIdAsync((int)this.GetPrimaryKeyLong());
// Calculate aggregated statistics from position data
var totalPnL = positions.Sum(p => p.ProfitAndLoss?.Realized ?? 0);
var totalVolume = positions.Sum(p => p.Open.Price * p.Open.Quantity * p.Open.Leverage);
var totalVolumeWithoutLeverage = positions.Sum(p => p.Open.Price * p.Open.Quantity);
var collateral = positions.Sum(p => p.Open.Price * p.Open.Quantity);
var totalFees = positions.Sum(p => p.CalculateTotalFees());
// Calculate wins/losses from position PnL
var totalWins = positions.Count(p => (p.ProfitAndLoss?.Realized ?? 0) > 0);
var totalLosses = positions.Count(p => (p.ProfitAndLoss?.Realized ?? 0) <= 0);
var totalROI = totalVolume switch
var totalROI = collateral switch
{
> 0 => (totalPnL / totalVolumeWithoutLeverage) * 100,
> 0 => (totalPnL / collateral) * 100,
>= 0 => 0,
_ => 0
};
// Calculate Runtime based on the earliest position date
DateTime? runtime = null;
if (positions.Any())
{
runtime = positions.Min(p => p.Date);
}
// Calculate total balance (USDC + open positions value)
decimal totalBalance = 0;
try
@@ -199,11 +193,7 @@ public class AgentGrain : Grain, IAgentGrain
totalBalance += usdcBalance;
}
// Get positions for all bots using their GUIDs as InitiatorIdentifier
var botPositions =
await _tradingService.GetPositionsByInitiatorIdentifiersAsync(_state.State.BotIds);
foreach (var position in botPositions.Where(p => !p.IsFinished()))
foreach (var position in positions.Where(p => !p.IsFinished()))
{
totalBalance += position.Open.Price * position.Open.Quantity;
totalBalance += position.ProfitAndLoss?.Realized ?? 0;
@@ -216,12 +206,17 @@ public class AgentGrain : Grain, IAgentGrain
totalBalance = 0; // Set to 0 if calculation fails
}
// Get active strategies count from bot data (this is still needed for running bots)
var bots = await ServiceScopeHelpers.WithScopedService<IGrainFactory, List<BotRegistryEntry>> (_scopeFactory, async (grainFactory) => {
var registry = grainFactory.GetGrain<ILiveBotRegistryGrain>(0);
return await registry.GetBotsForUser((int)this.GetPrimaryKeyLong());
var bots = await ServiceScopeHelpers.WithScopedService<IBotService, IEnumerable<Bot>> (_scopeFactory, async (botService) => {
return await botService.GetBotsByUser((int)this.GetPrimaryKeyLong());
});
// Calculate Runtime based on the earliest position date
DateTime? runtime = null;
if (positions.Any())
{
runtime = bots.Min(p => p.StartupTime);
}
var activeStrategiesCount = bots.Count(b => b.Status == BotStatus.Running);
var summary = new AgentSummary