Fix status and filtered positions for metrics

This commit is contained in:
2025-10-08 18:37:38 +07:00
parent 86dd6849ea
commit 76b087a6e4
8 changed files with 57 additions and 89 deletions

View File

@@ -169,7 +169,8 @@ public class AgentGrain : Grain, IAgentGrain
try
{
// Get all positions for this agent's bots as initiator
var positions = (await _tradingService.GetPositionByUserIdAsync((int)this.GetPrimaryKeyLong())).ToList();
var positions = (await _tradingService.GetPositionByUserIdAsync((int)this.GetPrimaryKeyLong()))
.Where(p => p.IsValidForMetrics()).ToList();
// Calculate aggregated statistics from position data
var totalPnL = positions.Sum(p => p.ProfitAndLoss?.Realized ?? 0);

View File

@@ -791,22 +791,24 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
}
}
var positionForMetrics = _tradingBot.Positions.Where(p => p.Value.IsValidForMetrics())
.Select(p => p.Value).ToList();
// Calculate statistics using TradingBox helpers
var (tradeWins, tradeLosses) = TradingBox.GetWinLossCount(_tradingBot.Positions);
var (tradeWins, tradeLosses) = TradingBox.GetWinLossCount(positionForMetrics);
var pnl = _tradingBot.GetProfitAndLoss();
var fees = _tradingBot.GetTotalFees();
var netPnl = pnl - fees; // Net PnL after fees
var volume = TradingBox.GetTotalVolumeTraded(_tradingBot.Positions);
var volume = TradingBox.GetTotalVolumeTraded(positionForMetrics);
// Calculate ROI based on total investment (Net PnL)
var totalInvestment = _tradingBot.Positions.Values
var totalInvestment = positionForMetrics
.Sum(p => p.Open.Quantity * p.Open.Price);
var roi = totalInvestment > 0 ? (netPnl / totalInvestment) * 100 : 0;
// Calculate long and short position counts
var longPositionCount = _tradingBot.Positions.Values
var longPositionCount = positionForMetrics
.Count(p => p.OriginDirection == TradeDirection.Long);
var shortPositionCount = _tradingBot.Positions.Values
var shortPositionCount = positionForMetrics
.Count(p => p.OriginDirection == TradeDirection.Short);
// Create complete Bot object with all statistics