Do not stop strategy if position open
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user