Fix ROI calculation for Strategy

This commit is contained in:
2025-10-08 19:37:24 +07:00
parent 76b087a6e4
commit 1a99224d18
5 changed files with 41 additions and 36 deletions

View File

@@ -182,8 +182,8 @@ public class AgentGrain : Grain, IAgentGrain
_state.State.TotalFees = totalFees;
// 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 totalWins = positions.Count(p => (p.ProfitAndLoss?.Net ?? 0) > 0);
var totalLosses = positions.Count(p => (p.ProfitAndLoss?.Net ?? 0) <= 0);
// Calculate ROI based on PnL minus fees
var netPnL = totalPnL - totalFees;

View File

@@ -791,12 +791,18 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
}
}
var positionForMetrics = _tradingBot.Positions.Where(p => p.Value.IsValidForMetrics())
.Select(p => p.Value).ToList();
var positionForMetrics = await ServiceScopeHelpers.WithScopedService<ITradingService, List<Position>>(
_scopeFactory,
async tradingService =>
{
return (await tradingService.GetPositionsByInitiatorIdentifierAsync(this.GetPrimaryKey()))
.Where(p => p.IsValidForMetrics()).ToList();
});
// Calculate statistics using TradingBox helpers
var (tradeWins, tradeLosses) = TradingBox.GetWinLossCount(positionForMetrics);
var pnl = _tradingBot.GetProfitAndLoss();
var fees = _tradingBot.GetTotalFees();
var pnl = positionForMetrics.Sum(p => p.ProfitAndLoss.Realized);
var fees = positionForMetrics.Sum(p => p.CalculateTotalFees());
var netPnl = pnl - fees; // Net PnL after fees
var volume = TradingBox.GetTotalVolumeTraded(positionForMetrics);