Update position value calculations in AgentGrain and BotService

- Changed the calculation of USDC value in AgentGrain to use net profit and loss instead of realized profit.
- Added similar position value calculations in BotService, including error handling for position retrieval failures.
This commit is contained in:
2025-11-20 20:34:12 +07:00
parent a6adf5e458
commit 4d4e5b6d25
2 changed files with 22 additions and 2 deletions

View File

@@ -214,8 +214,8 @@ public class AgentGrain : Grain, IAgentGrain
foreach (var position in positions.Where(p => p.IsOpen()))
{
var positionUsd = position.Open.Price * position.Open.Quantity;
var realized = position.ProfitAndLoss?.Realized ?? 0;
usdcInPositionsValue += positionUsd + realized;
var net = position.ProfitAndLoss?.Net ?? 0;
usdcInPositionsValue += positionUsd + net;
}
totalBalance = usdcWalletValue + usdcInPositionsValue;

View File

@@ -416,6 +416,26 @@ namespace Managing.Application.ManageBot
}
var usdcValue = usdcBalance?.Amount ?? 0m;
// Get positions for the user and add their USDC value (similar to AgentGrain.UpdateSummary)
try
{
var positions = (await _tradingService.GetPositionByUserIdAsync(account.User.Id))
.Where(p => p.IsValidForMetrics()).ToList();
foreach (var position in positions.Where(p => p.IsOpen()))
{
var positionUsd = position.Open.Price * position.Open.Quantity;
var net = position.ProfitAndLoss?.Net ?? 0;
usdcValue += positionUsd + net;
}
}
catch (Exception ex)
{
_tradingBotLogger.LogError(ex, "Error calculating position values for available allocation for user {UserId}", account.User.Id);
// Continue with calculation even if position retrieval fails
}
var available = usdcValue - totalAllocatedForAccount;
return available < 0m ? 0m : available;
});