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:
@@ -214,8 +214,8 @@ public class AgentGrain : Grain, IAgentGrain
|
|||||||
foreach (var position in positions.Where(p => p.IsOpen()))
|
foreach (var position in positions.Where(p => p.IsOpen()))
|
||||||
{
|
{
|
||||||
var positionUsd = position.Open.Price * position.Open.Quantity;
|
var positionUsd = position.Open.Price * position.Open.Quantity;
|
||||||
var realized = position.ProfitAndLoss?.Realized ?? 0;
|
var net = position.ProfitAndLoss?.Net ?? 0;
|
||||||
usdcInPositionsValue += positionUsd + realized;
|
usdcInPositionsValue += positionUsd + net;
|
||||||
}
|
}
|
||||||
|
|
||||||
totalBalance = usdcWalletValue + usdcInPositionsValue;
|
totalBalance = usdcWalletValue + usdcInPositionsValue;
|
||||||
|
|||||||
@@ -416,6 +416,26 @@ namespace Managing.Application.ManageBot
|
|||||||
}
|
}
|
||||||
|
|
||||||
var usdcValue = usdcBalance?.Amount ?? 0m;
|
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;
|
var available = usdcValue - totalAllocatedForAccount;
|
||||||
return available < 0m ? 0m : available;
|
return available < 0m ? 0m : available;
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user