Fix agent grain calculation

This commit is contained in:
2025-09-28 11:47:47 +07:00
parent d432549d26
commit 6e07bac6ae
4 changed files with 36 additions and 132 deletions

View File

@@ -388,7 +388,6 @@ public class TradingBotBase : ITradingBot
}
});
NotificationEventType eventType = NotificationEventType.PositionUpdated;
if (!Config.IsForBacktest)
{
var brokerPosition = brokerPositions.FirstOrDefault(p => p.Ticker == Config.Ticker);
@@ -404,12 +403,13 @@ public class TradingBotBase : ITradingBot
internalPosition.Open.SetStatus(TradeStatus.Filled);
positionForSignal.Open.SetStatus(TradeStatus.Filled);
eventType = NotificationEventType.PositionOpened;
await UpdatePositionDatabase(internalPosition);
if (previousPositionStatus != PositionStatus.Filled && internalPosition.Status == PositionStatus.Filled)
{
await NotifyAgentAndPlatformGrainAsync(NotificationEventType.PositionOpened, internalPosition);
}else{
await NotifyAgentAndPlatformGrainAsync(NotificationEventType.PositionUpdated, internalPosition);
}
}
@@ -507,10 +507,6 @@ public class TradingBotBase : ITradingBot
}
await SetPositionStatus(signal.Identifier, PositionStatus.Filled);
// Notify platform summary about the executed trade
await NotifyAgentAndPlatformGrainAsync(NotificationEventType.PositionOpened,
internalPosition);
}
else
{
@@ -890,10 +886,6 @@ public class TradingBotBase : ITradingBot
async messengerService => { await messengerService.SendPosition(position); });
}
// Notify AgentGrain about position opening
await NotifyAgentAndPlatformGrainAsync(NotificationEventType.PositionOpened,
position);
Logger.LogInformation($"Position requested");
return position; // Return the created position without adding to list
}
@@ -1355,8 +1347,8 @@ public class TradingBotBase : ITradingBot
// Update position in database with all trade changes
if (!Config.IsForBacktest)
{
await ServiceScopeHelpers.WithScopedService<ITradingService>(_scopeFactory,
async tradingService => { await tradingService.UpdatePositionAsync(position); });
await UpdatePositionDatabase(position);
await NotifyAgentAndPlatformGrainAsync(NotificationEventType.PositionClosed, position);
}
// Update the last position closing time for cooldown period tracking
@@ -1474,10 +1466,14 @@ public class TradingBotBase : ITradingBot
private void UpdatePositionPnl(Guid identifier, decimal realized)
{
Positions[identifier].ProfitAndLoss = new ProfitAndLoss()
if (Positions[identifier].ProfitAndLoss == null)
{
Realized = realized
};
Positions[identifier].ProfitAndLoss = new ProfitAndLoss(){
Realized = realized
};
}else{
Positions[identifier].ProfitAndLoss.Realized = realized;
}
}
private void SetSignalStatus(string signalIdentifier, SignalStatus signalStatus)
@@ -2130,6 +2126,7 @@ public class TradingBotBase : ITradingBot
};
await agentGrain.OnPositionUpdatedAsync(positionUpdatedEvent);
// No need to notify platform grain, it will be notified when position is closed or opened only
Logger.LogDebug("Sent position updated event to both grains for position {PositionId}",
position.Identifier);