Add platform grain

This commit is contained in:
2025-08-14 19:44:33 +07:00
parent 345d76e06f
commit 4a45d6c970
13 changed files with 2324 additions and 59 deletions

View File

@@ -0,0 +1,118 @@
using Managing.Application.Abstractions.Models;
using Orleans;
using static Managing.Common.Enums;
namespace Managing.Application.Abstractions.Grains;
/// <summary>
/// Grain interface for managing platform-wide summary metrics
/// </summary>
public interface IPlatformSummaryGrain : IGrainWithStringKey
{
/// <summary>
/// Gets the current platform summary data
/// </summary>
Task<PlatformSummaryViewModel> GetPlatformSummaryAsync();
/// <summary>
/// Forces a refresh of all platform data
/// </summary>
Task RefreshDataAsync();
/// <summary>
/// Gets the total volume traded across all strategies
/// </summary>
Task<decimal> GetTotalVolumeAsync();
/// <summary>
/// Gets the total PnL across all strategies
/// </summary>
Task<decimal> GetTotalPnLAsync();
/// <summary>
/// Gets the total open interest across all positions
/// </summary>
Task<decimal> GetTotalOpenInterest();
/// <summary>
/// Gets the total number of open positions
/// </summary>
Task<int> GetTotalPositionCountAsync();
// Event handlers for immediate updates
Task OnStrategyDeployedAsync(StrategyDeployedEvent evt);
Task OnStrategyStoppedAsync(StrategyStoppedEvent evt);
Task OnPositionOpenedAsync(PositionOpenedEvent evt);
Task OnPositionClosedAsync(PositionClosedEvent evt);
Task OnTradeExecutedAsync(TradeExecutedEvent evt);
}
/// <summary>
/// Base class for platform metrics events
/// </summary>
public abstract class PlatformMetricsEvent
{
public DateTime Timestamp { get; set; } = DateTime.UtcNow;
}
/// <summary>
/// Event fired when a new strategy is deployed
/// </summary>
public class StrategyDeployedEvent : PlatformMetricsEvent
{
public Guid StrategyId { get; set; }
public string AgentName { get; set; } = string.Empty;
public string StrategyName { get; set; } = string.Empty;
public decimal InitialVolume { get; set; }
public decimal InitialPnL { get; set; }
}
/// <summary>
/// Event fired when a strategy is stopped
/// </summary>
public class StrategyStoppedEvent : PlatformMetricsEvent
{
public Guid StrategyId { get; set; }
public string AgentName { get; set; } = string.Empty;
public string StrategyName { get; set; } = string.Empty;
}
/// <summary>
/// Event fired when a new position is opened
/// </summary>
public class PositionOpenedEvent : PlatformMetricsEvent
{
public Guid PositionId { get; set; }
public Guid StrategyId { get; set; }
public string Ticker { get; set; } = string.Empty;
public decimal Size { get; set; }
public decimal NotionalValue { get; set; }
public TradeDirection Direction { get; set; }
}
/// <summary>
/// Event fired when a position is closed
/// </summary>
public class PositionClosedEvent : PlatformMetricsEvent
{
public Guid PositionId { get; set; }
public Guid StrategyId { get; set; }
public string Ticker { get; set; } = string.Empty;
public decimal RealizedPnL { get; set; }
public decimal Volume { get; set; }
}
/// <summary>
/// Event fired when a trade is executed
/// </summary>
public class TradeExecutedEvent : PlatformMetricsEvent
{
public Guid TradeId { get; set; }
public Guid PositionId { get; set; }
public Guid StrategyId { get; set; }
public string Ticker { get; set; } = string.Empty;
public decimal Volume { get; set; }
public decimal PnL { get; set; }
public decimal Fee { get; set; }
public TradeDirection Direction { get; set; }
}

View File

@@ -0,0 +1,68 @@
using static Managing.Common.Enums;
namespace Managing.Application.Abstractions.Grains;
/// <summary>
/// State model for Platform Summary Grain
/// </summary>
public class PlatformSummaryGrainState
{
public DateTime LastUpdated { get; set; }
public DateTime LastSnapshot { get; set; }
public bool HasPendingChanges { get; set; }
// Current metrics
public int TotalAgents { get; set; }
public int TotalActiveStrategies { get; set; }
public decimal TotalPlatformPnL { get; set; }
public decimal TotalPlatformVolume { get; set; }
public decimal TotalOpenInterest { get; set; }
public int TotalPositionCount { get; set; }
// 24-hour ago values (for comparison)
public int TotalAgents24hAgo { get; set; }
public int TotalActiveStrategies24hAgo { get; set; }
public decimal TotalPlatformPnL24hAgo { get; set; }
public decimal TotalPlatformVolume24hAgo { get; set; }
public decimal TotalOpenInterest24hAgo { get; set; }
public int TotalPositionCount24hAgo { get; set; }
// Historical snapshots
public List<HourlySnapshot> HourlySnapshots { get; set; } = new();
public List<DailySnapshot> DailySnapshots { get; set; } = new();
// Volume breakdown by asset
public Dictionary<string, decimal> VolumeByAsset { get; set; } = new();
// Position count breakdown
public Dictionary<string, int> PositionCountByAsset { get; set; } = new();
public Dictionary<TradeDirection, int> PositionCountByDirection { get; set; } = new();
}
/// <summary>
/// Hourly snapshot of platform metrics
/// </summary>
public class HourlySnapshot
{
public DateTime Timestamp { get; set; }
public int TotalAgents { get; set; }
public int TotalStrategies { get; set; }
public decimal TotalVolume { get; set; }
public decimal TotalPnL { get; set; }
public decimal TotalOpenInterest { get; set; }
public int TotalPositionCount { get; set; }
}
/// <summary>
/// Daily snapshot of platform metrics
/// </summary>
public class DailySnapshot
{
public DateTime Date { get; set; }
public int TotalAgents { get; set; }
public int TotalStrategies { get; set; }
public decimal TotalVolume { get; set; }
public decimal TotalPnL { get; set; }
public decimal TotalOpenInterest { get; set; }
public int TotalPositionCount { get; set; }
}

View File

@@ -0,0 +1,102 @@
using static Managing.Common.Enums;
namespace Managing.Application.Abstractions.Models;
/// <summary>
/// Platform-wide statistics without individual agent details
/// </summary>
public class PlatformSummaryViewModel
{
/// <summary>
/// Total number of agents on the platform
/// </summary>
public int TotalAgents { get; set; }
/// <summary>
/// Total number of active strategies across all agents
/// </summary>
public int TotalActiveStrategies { get; set; }
/// <summary>
/// Total platform-wide profit and loss in USD
/// </summary>
public decimal TotalPlatformPnL { get; set; }
/// <summary>
/// Total volume traded across all agents in USD
/// </summary>
public decimal TotalPlatformVolume { get; set; }
/// <summary>
/// Total volume traded across all agents in the last 24 hours in USD
/// </summary>
public decimal TotalPlatformVolumeLast24h { get; set; }
/// <summary>
/// Total open interest across all positions in USD
/// </summary>
public decimal TotalOpenInterest { get; set; }
/// <summary>
/// Total number of open positions across all strategies
/// </summary>
public int TotalPositionCount { get; set; }
// 24-hour changes
/// <summary>
/// Change in agent count over the last 24 hours
/// </summary>
public int AgentsChange24h { get; set; }
/// <summary>
/// Change in strategy count over the last 24 hours
/// </summary>
public int StrategiesChange24h { get; set; }
/// <summary>
/// Change in PnL over the last 24 hours
/// </summary>
public decimal PnLChange24h { get; set; }
/// <summary>
/// Change in volume over the last 24 hours
/// </summary>
public decimal VolumeChange24h { get; set; }
/// <summary>
/// Change in open interest over the last 24 hours
/// </summary>
public decimal OpenInterestChange24h { get; set; }
/// <summary>
/// Change in position count over the last 24 hours
/// </summary>
public int PositionCountChange24h { get; set; }
// Breakdowns
/// <summary>
/// Volume breakdown by asset/ticker
/// </summary>
public Dictionary<string, decimal> VolumeByAsset { get; set; } = new();
/// <summary>
/// Position count breakdown by asset/ticker
/// </summary>
public Dictionary<string, int> PositionCountByAsset { get; set; } = new();
/// <summary>
/// Position count breakdown by direction (Long/Short)
/// </summary>
public Dictionary<TradeDirection, int> PositionCountByDirection { get; set; } = new();
// Metadata
/// <summary>
/// When the data was last updated
/// </summary>
public DateTime LastUpdated { get; set; }
/// <summary>
/// When the last 24-hour snapshot was taken
/// </summary>
public DateTime Last24HourSnapshot { get; set; }
}