Fix position count
This commit is contained in:
@@ -132,7 +132,8 @@ public class PlatformSummaryGrain : Grain, IPlatformSummaryGrain, IRemindable
|
|||||||
? _state.State.DailySnapshots.OrderByDescending(s => s.Date).First().TotalVolume
|
? _state.State.DailySnapshots.OrderByDescending(s => s.Date).First().TotalVolume
|
||||||
: 0m;
|
: 0m;
|
||||||
|
|
||||||
_logger.LogInformation("Calculating cumulative volume from last snapshot date: {LastSnapshotDate}, Base volume: {CumulativeVolume}",
|
_logger.LogInformation(
|
||||||
|
"Calculating cumulative volume from last snapshot date: {LastSnapshotDate}, Base volume: {CumulativeVolume}",
|
||||||
lastSnapshotDate, cumulativeVolume);
|
lastSnapshotDate, cumulativeVolume);
|
||||||
|
|
||||||
// Calculate all metrics from positions in a single loop
|
// Calculate all metrics from positions in a single loop
|
||||||
@@ -159,7 +160,8 @@ public class PlatformSummaryGrain : Grain, IPlatformSummaryGrain, IRemindable
|
|||||||
if (position.Date.Date > lastSnapshotDate)
|
if (position.Date.Date > lastSnapshotDate)
|
||||||
{
|
{
|
||||||
newVolume += positionVolume;
|
newVolume += positionVolume;
|
||||||
_logger.LogDebug("Position {PositionId} created after last snapshot ({PositionDate} > {LastSnapshotDate}), adding volume: {Volume}",
|
_logger.LogDebug(
|
||||||
|
"Position {PositionId} created after last snapshot ({PositionDate} > {LastSnapshotDate}), adding volume: {Volume}",
|
||||||
position.Identifier, position.Date.Date, lastSnapshotDate, positionVolume);
|
position.Identifier, position.Date.Date, lastSnapshotDate, positionVolume);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -172,6 +174,7 @@ public class PlatformSummaryGrain : Grain, IPlatformSummaryGrain, IRemindable
|
|||||||
{
|
{
|
||||||
_state.State.VolumeByAsset[ticker] = 0;
|
_state.State.VolumeByAsset[ticker] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
_state.State.VolumeByAsset[ticker] += positionVolume;
|
_state.State.VolumeByAsset[ticker] += positionVolume;
|
||||||
|
|
||||||
// Position count breakdown by asset - update state directly
|
// Position count breakdown by asset - update state directly
|
||||||
@@ -179,6 +182,7 @@ public class PlatformSummaryGrain : Grain, IPlatformSummaryGrain, IRemindable
|
|||||||
{
|
{
|
||||||
_state.State.PositionCountByAsset[ticker] = 0;
|
_state.State.PositionCountByAsset[ticker] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
_state.State.PositionCountByAsset[ticker]++;
|
_state.State.PositionCountByAsset[ticker]++;
|
||||||
|
|
||||||
// Calculate fees and PnL for all positions
|
// Calculate fees and PnL for all positions
|
||||||
@@ -205,14 +209,15 @@ public class PlatformSummaryGrain : Grain, IPlatformSummaryGrain, IRemindable
|
|||||||
|
|
||||||
// CUMULATIVE volume: baseline + new volume since last snapshot
|
// CUMULATIVE volume: baseline + new volume since last snapshot
|
||||||
var updatedCumulativeVolume = cumulativeVolume + newVolume;
|
var updatedCumulativeVolume = cumulativeVolume + newVolume;
|
||||||
|
|
||||||
_logger.LogInformation("Volume calculation: Base={BaseVolume}, New={NewVolume}, Total={TotalVolume}",
|
_logger.LogInformation("Volume calculation: Base={BaseVolume}, New={NewVolume}, Total={TotalVolume}",
|
||||||
cumulativeVolume, newVolume, updatedCumulativeVolume);
|
cumulativeVolume, newVolume, updatedCumulativeVolume);
|
||||||
|
|
||||||
// Ensure volume never decreases
|
// Ensure volume never decreases
|
||||||
if (updatedCumulativeVolume < _state.State.TotalPlatformVolume)
|
if (updatedCumulativeVolume < _state.State.TotalPlatformVolume)
|
||||||
{
|
{
|
||||||
_logger.LogWarning("Calculated cumulative volume ({Calculated}) is less than current volume ({Current}). Keeping current value.",
|
_logger.LogWarning(
|
||||||
|
"Calculated cumulative volume ({Calculated}) is less than current volume ({Current}). Keeping current value.",
|
||||||
updatedCumulativeVolume, _state.State.TotalPlatformVolume);
|
updatedCumulativeVolume, _state.State.TotalPlatformVolume);
|
||||||
updatedCumulativeVolume = _state.State.TotalPlatformVolume;
|
updatedCumulativeVolume = _state.State.TotalPlatformVolume;
|
||||||
}
|
}
|
||||||
@@ -235,7 +240,8 @@ public class PlatformSummaryGrain : Grain, IPlatformSummaryGrain, IRemindable
|
|||||||
await RefreshAgentCountAsync();
|
await RefreshAgentCountAsync();
|
||||||
await _state.WriteStateAsync();
|
await _state.WriteStateAsync();
|
||||||
|
|
||||||
_logger.LogInformation("Platform summary data refreshed successfully - Cumulative volume: {Volume}", updatedCumulativeVolume);
|
_logger.LogInformation("Platform summary data refreshed successfully - Cumulative volume: {Volume}",
|
||||||
|
updatedCumulativeVolume);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -449,7 +455,9 @@ public class PlatformSummaryGrain : Grain, IPlatformSummaryGrain, IRemindable
|
|||||||
// Calculate and add missing snapshots
|
// Calculate and add missing snapshots
|
||||||
foreach (var missingDate in missingDates)
|
foreach (var missingDate in missingDates)
|
||||||
{
|
{
|
||||||
var snapshot = await CalculateDailySnapshotFromPositionsAsync(positions.ToList(), missingDate);
|
var snapshot =
|
||||||
|
await CalculateDailySnapshotFromPositionsAsync(positions.Where(p => p.IsValidForMetrics()).ToList(),
|
||||||
|
missingDate);
|
||||||
_state.State.DailySnapshots.Add(snapshot);
|
_state.State.DailySnapshots.Add(snapshot);
|
||||||
|
|
||||||
_logger.LogInformation(
|
_logger.LogInformation(
|
||||||
|
|||||||
Reference in New Issue
Block a user