Fix ROI
This commit is contained in:
@@ -11,7 +11,6 @@ using Managing.Domain.Scenarios;
|
||||
using Managing.Domain.Statistics;
|
||||
using Managing.Domain.Strategies;
|
||||
using Managing.Domain.Strategies.Base;
|
||||
using Managing.Domain.Trades;
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
@@ -455,19 +454,14 @@ public class DataController : ControllerBase
|
||||
// Get all strategies for the specified user
|
||||
var userStrategies = await _mediator.Send(new GetUserStrategiesCommand(agentName));
|
||||
|
||||
// Get all positions for all strategies in a single database call to avoid DbContext concurrency issues
|
||||
var strategyIdentifiers = userStrategies.Select(s => s.Identifier).ToList();
|
||||
var allPositions = await _tradingService.GetPositionsByInitiatorIdentifiersAsync(strategyIdentifiers);
|
||||
var positionsByIdentifier = allPositions.GroupBy(p => p.InitiatorIdentifier)
|
||||
.ToDictionary(g => g.Key, g => g.ToList());
|
||||
|
||||
// Get agent balance history for the last 30 days
|
||||
var startDate = DateTime.UtcNow.AddDays(-30);
|
||||
var endDate = DateTime.UtcNow;
|
||||
var agentBalanceHistory = await _agentService.GetAgentBalances(agentName, startDate, endDate);
|
||||
|
||||
// Convert to detailed view model with additional information
|
||||
var result = userStrategies.Select(strategy => MapStrategyToViewModel(strategy, positionsByIdentifier, agentBalanceHistory))
|
||||
var result = userStrategies
|
||||
.Select(strategy => MapStrategyToViewModelAsync(strategy, agentBalanceHistory))
|
||||
.ToList();
|
||||
|
||||
return Ok(result);
|
||||
@@ -511,78 +505,15 @@ public class DataController : ControllerBase
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Maps a trading bot to a strategy view model with detailed statistics using pre-fetched positions
|
||||
/// </summary>
|
||||
/// <param name="strategy">The trading bot to map</param>
|
||||
/// <param name="positionsByIdentifier">Pre-fetched positions grouped by initiator identifier</param>
|
||||
/// <param name="agentBalanceHistory">Agent balance history data</param>
|
||||
/// <returns>A view model with detailed strategy information</returns>
|
||||
private UserStrategyDetailsViewModel MapStrategyToViewModel(Bot strategy,
|
||||
Dictionary<Guid, List<Position>> positionsByIdentifier, AgentBalanceHistory agentBalanceHistory)
|
||||
{
|
||||
// Calculate ROI percentage based on PnL relative to account value
|
||||
decimal pnl = strategy.Pnl;
|
||||
|
||||
// If we had initial investment amount, we could calculate ROI like:
|
||||
decimal initialInvestment = 1000; // Example placeholder, ideally should come from the account
|
||||
decimal roi = pnl != 0 ? (pnl / initialInvestment) * 100 : 0;
|
||||
|
||||
// Calculate volume statistics
|
||||
decimal totalVolume = strategy.Volume;
|
||||
decimal volumeLast24h = strategy.Volume;
|
||||
|
||||
// Calculate win/loss statistics
|
||||
(int wins, int losses) = (strategy.TradeWins, strategy.TradeLosses);
|
||||
|
||||
int winRate = wins + losses > 0 ? (wins * 100) / (wins + losses) : 0;
|
||||
// Calculate ROI for last 24h
|
||||
decimal roiLast24h = strategy.Roi;
|
||||
|
||||
// Get positions for this strategy from pre-fetched data
|
||||
var positions = positionsByIdentifier.TryGetValue(strategy.Identifier, out var strategyPositions)
|
||||
? strategyPositions
|
||||
: new List<Position>();
|
||||
|
||||
// Convert agent balance history to wallet balances dictionary
|
||||
var walletBalances = agentBalanceHistory?.AgentBalances?
|
||||
.ToDictionary(b => b.Time, b => b.TotalValue) ?? new Dictionary<DateTime, decimal>();
|
||||
|
||||
return new UserStrategyDetailsViewModel
|
||||
{
|
||||
Name = strategy.Name,
|
||||
State = strategy.Status,
|
||||
PnL = pnl,
|
||||
ROIPercentage = roi,
|
||||
ROILast24H = roiLast24h,
|
||||
Runtime = strategy.StartupTime,
|
||||
WinRate = winRate,
|
||||
TotalVolumeTraded = totalVolume,
|
||||
VolumeLast24H = volumeLast24h,
|
||||
Wins = wins,
|
||||
Losses = losses,
|
||||
Positions = positions,
|
||||
Identifier = strategy.Identifier,
|
||||
WalletBalances = walletBalances,
|
||||
Ticker = strategy.Ticker
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Maps a trading bot to a strategy view model with detailed statistics
|
||||
/// </summary>
|
||||
/// <param name="strategy">The trading bot to map</param>
|
||||
/// <param name="agentBalanceHistory">Agent balance history data</param>
|
||||
/// <returns>A view model with detailed strategy information</returns>
|
||||
private async Task<UserStrategyDetailsViewModel> MapStrategyToViewModelAsync(Bot strategy, AgentBalanceHistory agentBalanceHistory)
|
||||
private async Task<UserStrategyDetailsViewModel> MapStrategyToViewModelAsync(Bot strategy,
|
||||
AgentBalanceHistory agentBalanceHistory)
|
||||
{
|
||||
// Calculate ROI percentage based on PnL relative to account value
|
||||
decimal pnl = strategy.Pnl;
|
||||
|
||||
// If we had initial investment amount, we could calculate ROI like:
|
||||
decimal initialInvestment = 1000; // Example placeholder, ideally should come from the account
|
||||
decimal roi = pnl != 0 ? (pnl / initialInvestment) * 100 : 0;
|
||||
|
||||
// Calculate volume statistics
|
||||
decimal totalVolume = strategy.Volume;
|
||||
decimal volumeLast24h = strategy.Volume;
|
||||
@@ -591,8 +522,6 @@ public class DataController : ControllerBase
|
||||
(int wins, int losses) = (strategy.TradeWins, strategy.TradeLosses);
|
||||
|
||||
int winRate = wins + losses > 0 ? (wins * 100) / (wins + losses) : 0;
|
||||
// Calculate ROI for last 24h
|
||||
decimal roiLast24h = strategy.Roi;
|
||||
|
||||
// Fetch positions associated with this bot
|
||||
var positions = await _tradingService.GetPositionsByInitiatorIdentifierAsync(strategy.Identifier);
|
||||
@@ -605,9 +534,8 @@ public class DataController : ControllerBase
|
||||
{
|
||||
Name = strategy.Name,
|
||||
State = strategy.Status,
|
||||
PnL = pnl,
|
||||
ROIPercentage = roi,
|
||||
ROILast24H = roiLast24h,
|
||||
PnL = strategy.Pnl,
|
||||
ROIPercentage = strategy.Roi,
|
||||
Runtime = strategy.StartupTime,
|
||||
WinRate = winRate,
|
||||
TotalVolumeTraded = totalVolume,
|
||||
|
||||
@@ -28,11 +28,6 @@ namespace Managing.Api.Models.Responses
|
||||
/// </summary>
|
||||
public decimal ROIPercentage { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Return on investment percentage in the last 24 hours
|
||||
/// </summary>
|
||||
public decimal ROILast24H { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Date and time when the strategy was started
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user