Update open interest

This commit is contained in:
2025-08-14 23:53:45 +07:00
parent 580ce4d9c9
commit 2622da05e6
7 changed files with 44 additions and 34 deletions

View File

@@ -75,14 +75,8 @@ public class PlatformSummaryGrain : Grain, IPlatformSummaryGrain, IRemindable
{
_logger.LogInformation("Refreshing platform summary data");
// Get all data in parallel for better performance
var agentsTask = _agentService.GetAllAgentSummaries();
var strategiesTask = _botService.GetBotsAsync();
await Task.WhenAll(agentsTask, strategiesTask);
var agents = await agentsTask;
var strategies = await strategiesTask;
var agents = await _agentService.GetAllAgentSummaries();
var strategies = await _botService.GetBotsAsync();
// Calculate totals
var totalAgents = agents.Count();
@@ -183,19 +177,20 @@ public class PlatformSummaryGrain : Grain, IPlatformSummaryGrain, IRemindable
try
{
// Get all open positions from all accounts
var openPositions = await _tradingService.GetBrokerPositions(null);
// Get positions directly from database instead of exchange
var allPositions = await _tradingService.GetAllDatabasePositionsAsync();
var openPositions = allPositions?.Where(p => !p.IsFinished());
if (openPositions?.Any() == true)
{
var positionCount = openPositions.Count();
// Calculate open interest as the sum of position notional values
// Open interest = sum of (position size * price) for all open positions
// Calculate open interest as the sum of leveraged position notional values
// Open interest = sum of (position size * price * leverage) for all open positions
var openInterest = openPositions
.Where(p => p.Open?.Price > 0 && p.Open?.Quantity > 0)
.Sum(p => p.Open.Price * p.Open.Quantity);
.Sum(p => (p.Open.Price * p.Open.Quantity) * p.Open.Leverage);
_logger.LogDebug("Calculated position metrics: {PositionCount} positions, {OpenInterest} open interest",
_logger.LogDebug("Calculated position metrics: {PositionCount} positions, {OpenInterest} leveraged open interest",
positionCount, openInterest);
return (openInterest, positionCount);