Add AgentBalance to UserStrategies endpoint
This commit is contained in:
@@ -461,8 +461,13 @@ public class DataController : ControllerBase
|
|||||||
var positionsByIdentifier = allPositions.GroupBy(p => p.InitiatorIdentifier)
|
var positionsByIdentifier = allPositions.GroupBy(p => p.InitiatorIdentifier)
|
||||||
.ToDictionary(g => g.Key, g => g.ToList());
|
.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
|
// Convert to detailed view model with additional information
|
||||||
var result = userStrategies.Select(strategy => MapStrategyToViewModel(strategy, positionsByIdentifier))
|
var result = userStrategies.Select(strategy => MapStrategyToViewModel(strategy, positionsByIdentifier, agentBalanceHistory))
|
||||||
.ToList();
|
.ToList();
|
||||||
|
|
||||||
return Ok(result);
|
return Ok(result);
|
||||||
@@ -495,8 +500,13 @@ public class DataController : ControllerBase
|
|||||||
return NotFound($"Strategy '{strategyName}' not found for user '{agentName}'");
|
return NotFound($"Strategy '{strategyName}' not found for user '{agentName}'");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
|
||||||
// Map the strategy to a view model using the shared method
|
// Map the strategy to a view model using the shared method
|
||||||
var result = await MapStrategyToViewModelAsync(strategy);
|
var result = await MapStrategyToViewModelAsync(strategy, agentBalanceHistory);
|
||||||
|
|
||||||
return Ok(result);
|
return Ok(result);
|
||||||
}
|
}
|
||||||
@@ -506,9 +516,10 @@ public class DataController : ControllerBase
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="strategy">The trading bot to map</param>
|
/// <param name="strategy">The trading bot to map</param>
|
||||||
/// <param name="positionsByIdentifier">Pre-fetched positions grouped by initiator identifier</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>
|
/// <returns>A view model with detailed strategy information</returns>
|
||||||
private UserStrategyDetailsViewModel MapStrategyToViewModel(Bot strategy,
|
private UserStrategyDetailsViewModel MapStrategyToViewModel(Bot strategy,
|
||||||
Dictionary<Guid, List<Position>> positionsByIdentifier)
|
Dictionary<Guid, List<Position>> positionsByIdentifier, AgentBalanceHistory agentBalanceHistory)
|
||||||
{
|
{
|
||||||
// Calculate ROI percentage based on PnL relative to account value
|
// Calculate ROI percentage based on PnL relative to account value
|
||||||
decimal pnl = strategy.Pnl;
|
decimal pnl = strategy.Pnl;
|
||||||
@@ -533,6 +544,10 @@ public class DataController : ControllerBase
|
|||||||
? strategyPositions
|
? strategyPositions
|
||||||
: new List<Position>();
|
: 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
|
return new UserStrategyDetailsViewModel
|
||||||
{
|
{
|
||||||
Name = strategy.Name,
|
Name = strategy.Name,
|
||||||
@@ -548,7 +563,7 @@ public class DataController : ControllerBase
|
|||||||
Losses = losses,
|
Losses = losses,
|
||||||
Positions = positions,
|
Positions = positions,
|
||||||
Identifier = strategy.Identifier,
|
Identifier = strategy.Identifier,
|
||||||
WalletBalances = new Dictionary<DateTime, decimal>(),
|
WalletBalances = walletBalances,
|
||||||
Ticker = strategy.Ticker
|
Ticker = strategy.Ticker
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -557,8 +572,9 @@ public class DataController : ControllerBase
|
|||||||
/// Maps a trading bot to a strategy view model with detailed statistics
|
/// Maps a trading bot to a strategy view model with detailed statistics
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="strategy">The trading bot to map</param>
|
/// <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>
|
/// <returns>A view model with detailed strategy information</returns>
|
||||||
private async Task<UserStrategyDetailsViewModel> MapStrategyToViewModelAsync(Bot strategy)
|
private async Task<UserStrategyDetailsViewModel> MapStrategyToViewModelAsync(Bot strategy, AgentBalanceHistory agentBalanceHistory)
|
||||||
{
|
{
|
||||||
// Calculate ROI percentage based on PnL relative to account value
|
// Calculate ROI percentage based on PnL relative to account value
|
||||||
decimal pnl = strategy.Pnl;
|
decimal pnl = strategy.Pnl;
|
||||||
@@ -581,6 +597,10 @@ public class DataController : ControllerBase
|
|||||||
// Fetch positions associated with this bot
|
// Fetch positions associated with this bot
|
||||||
var positions = await _tradingService.GetPositionsByInitiatorIdentifierAsync(strategy.Identifier);
|
var positions = await _tradingService.GetPositionsByInitiatorIdentifierAsync(strategy.Identifier);
|
||||||
|
|
||||||
|
// 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
|
return new UserStrategyDetailsViewModel
|
||||||
{
|
{
|
||||||
Name = strategy.Name,
|
Name = strategy.Name,
|
||||||
@@ -596,7 +616,8 @@ public class DataController : ControllerBase
|
|||||||
Losses = losses,
|
Losses = losses,
|
||||||
Positions = positions.ToList(),
|
Positions = positions.ToList(),
|
||||||
Identifier = strategy.Identifier,
|
Identifier = strategy.Identifier,
|
||||||
WalletBalances = new Dictionary<DateTime, decimal>(),
|
WalletBalances = walletBalances,
|
||||||
|
Ticker = strategy.Ticker
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user