Fix realtime volume

This commit is contained in:
2025-09-24 13:14:38 +07:00
parent d19df1938b
commit 253c448acb
2 changed files with 34 additions and 3 deletions

View File

@@ -116,15 +116,34 @@ public class PlatformSummaryGrain : Grain, IPlatformSummaryGrain, IRemindable
// Update state
_state.State.TotalAgents = totalAgents;
_state.State.TotalActiveStrategies = totalActiveStrategies;
_state.State.TotalPlatformVolume = totalVolume;
// Only update volume if it hasn't been updated by events recently
// This preserves real-time volume updates from position events
if (!_state.State.VolumeUpdatedByEvents)
{
_state.State.TotalPlatformVolume = totalVolume;
_logger.LogDebug("Updated volume from strategies: {Volume}", totalVolume);
}
else
{
_logger.LogDebug("Preserving event-updated volume: {Volume}", _state.State.TotalPlatformVolume);
}
_state.State.TotalPlatformPnL = totalPnL;
_state.State.OpenInterest = totalOpenInterest;
_state.State.TotalPositionCount = totalPositionCount;
_state.State.LastUpdated = DateTime.UtcNow;
_state.State.HasPendingChanges = false;
// Update volume breakdown by asset
UpdateVolumeBreakdown(strategies);
// Update volume breakdown by asset only if volume wasn't updated by events
if (!_state.State.VolumeUpdatedByEvents)
{
UpdateVolumeBreakdown(strategies);
}
else
{
_logger.LogDebug("Preserving event-updated volume breakdown");
}
// Update position count breakdown
UpdatePositionCountBreakdown(strategies);
@@ -309,6 +328,9 @@ public class PlatformSummaryGrain : Grain, IPlatformSummaryGrain, IRemindable
_state.State.VolumeByAsset[asset] += evt.Volume;
// Mark that volume has been updated by events
_state.State.VolumeUpdatedByEvents = true;
// Update open interest (subtract the closed position's volume)
_state.State.OpenInterest = Math.Max(0, _state.State.OpenInterest - evt.Volume);
@@ -347,6 +369,9 @@ public class PlatformSummaryGrain : Grain, IPlatformSummaryGrain, IRemindable
_state.State.VolumeByAsset[asset] += evt.Volume;
// Mark that volume has been updated by events
_state.State.VolumeUpdatedByEvents = true;
// Update open interest and position count
// Since this is called only when position is fully open on broker, we always increase counts
_state.State.TotalPositionCount++;
@@ -421,6 +446,9 @@ public class PlatformSummaryGrain : Grain, IPlatformSummaryGrain, IRemindable
_state.State.LastSnapshot = DateTime.UtcNow;
// Reset the volume updated by events flag daily to allow periodic refresh from strategies
_state.State.VolumeUpdatedByEvents = false;
await _state.WriteStateAsync();
}