Do not stop strategy if position open

This commit is contained in:
2025-10-01 12:31:53 +07:00
parent 5953b96a38
commit 3e680ab815
4 changed files with 103 additions and 1 deletions

View File

@@ -835,4 +835,34 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
return Task.FromResult(false);
}
}
/// <summary>
/// Checks if the bot has any open positions
/// Returns true if there are open positions, false otherwise
/// </summary>
public Task<bool> HasOpenPositionsAsync()
{
try
{
if (_tradingBot == null)
{
// For non-running bots, check grain state positions
var hasOpenPositions = _state.State.Positions?.Values.Any(p => !p.IsFinished()) ?? false;
_logger.LogDebug("Bot {GrainId} has open positions: {HasOpenPositions} (from grain state)",
this.GetPrimaryKey(), hasOpenPositions);
return Task.FromResult(hasOpenPositions);
}
// For running bots, check live positions
var hasLiveOpenPositions = _tradingBot.Positions?.Values.Any(p => !p.IsFinished()) ?? false;
_logger.LogDebug("Bot {GrainId} has open positions: {HasOpenPositions} (from live data)",
this.GetPrimaryKey(), hasLiveOpenPositions);
return Task.FromResult(hasLiveOpenPositions);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error checking open positions for LiveTradingBotGrain {GrainId}", this.GetPrimaryKey());
return Task.FromResult(false); // Default to false on error to avoid blocking autoswap
}
}
}