Fix status and filtered positions for metrics
This commit is contained in:
@@ -10,7 +10,6 @@ using Managing.Domain.Accounts;
|
||||
using Managing.Domain.Bots;
|
||||
using Managing.Domain.Indicators;
|
||||
using Managing.Domain.Scenarios;
|
||||
using Managing.Domain.Shared.Helpers;
|
||||
using Managing.Domain.Trades;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
@@ -277,53 +276,6 @@ namespace Managing.Application.ManageBot
|
||||
return await grain.ClosePositionAsync(positionId);
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateBotStatisticsAsync(Guid identifier)
|
||||
{
|
||||
try
|
||||
{
|
||||
var grain = _grainFactory.GetGrain<ILiveTradingBotGrain>(identifier);
|
||||
var botData = await grain.GetBotDataAsync();
|
||||
|
||||
// Get the current bot from database
|
||||
var existingBot = await _botRepository.GetBotByIdentifierAsync(identifier);
|
||||
if (existingBot == null)
|
||||
{
|
||||
_tradingBotLogger.LogWarning("Bot {Identifier} not found in database for statistics update",
|
||||
identifier);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Calculate statistics using TradingBox helpers
|
||||
var (tradeWins, tradeLosses) = TradingBox.GetWinLossCount(botData.Positions);
|
||||
var pnl = botData.ProfitAndLoss;
|
||||
var fees = botData.Positions.Values.Sum(p => p.CalculateTotalFees());
|
||||
var volume = TradingBox.GetTotalVolumeTraded(botData.Positions);
|
||||
|
||||
// Calculate ROI based on total investment
|
||||
var totalInvestment = botData.Positions.Values
|
||||
.Where(p => p.IsValidForMetrics())
|
||||
.Sum(p => p.Open.Quantity * p.Open.Price);
|
||||
var netPnl = pnl - fees;
|
||||
var roi = totalInvestment > 0 ? (netPnl / totalInvestment) * 100 : 0;
|
||||
|
||||
// Update bot statistics
|
||||
existingBot.TradeWins = tradeWins;
|
||||
existingBot.TradeLosses = tradeLosses;
|
||||
existingBot.Pnl = pnl;
|
||||
existingBot.Roi = roi;
|
||||
existingBot.Volume = volume;
|
||||
existingBot.Fees = fees;
|
||||
|
||||
// Use the new SaveBotStatisticsAsync method
|
||||
return await SaveBotStatisticsAsync(existingBot);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_tradingBotLogger.LogError(e, "Error updating bot statistics for {Identifier}", identifier);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> SaveBotStatisticsAsync(Bot bot)
|
||||
{
|
||||
try
|
||||
|
||||
Reference in New Issue
Block a user