Update open interest
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -250,6 +250,11 @@ public class TradingService : ITradingService
|
||||
return await _exchangeService.GetBrokerPositions(account);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Position>> GetAllDatabasePositionsAsync()
|
||||
{
|
||||
return await _tradingRepository.GetAllPositionsAsync();
|
||||
}
|
||||
|
||||
private async Task ManageTrader(TraderFollowup a, List<Ticker> tickers)
|
||||
{
|
||||
var shortAddress = a.Account.Address.Substring(0, 6);
|
||||
|
||||
Reference in New Issue
Block a user