Do not stop bot if position open

This commit is contained in:
2025-10-11 00:32:02 +07:00
parent d71d47f644
commit 117d45fb50
2 changed files with 32 additions and 20 deletions

View File

@@ -398,12 +398,22 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
return; return;
} }
if (_tradingBot.Positions.Any(p => p.Value.IsOpen() || p.Value.Status.Equals(PositionStatus.New)))
{
_logger.LogInformation(
"Bot {BotId} has open positions. Trading loop will continue managing existing positions.",
_state.State.Identifier);
}
else
{
// If no open positions, ensure ETH balance is sufficient for new positions
// Use coordinated balance checking and swap management through AgentGrain // Use coordinated balance checking and swap management through AgentGrain
try try
{ {
var agentGrain = GrainFactory.GetGrain<IAgentGrain>(_state.State.User.Id); var agentGrain = GrainFactory.GetGrain<IAgentGrain>(_state.State.User.Id);
var balanceCheckResult = var balanceCheckResult =
await agentGrain.CheckAndEnsureEthBalanceAsync(_state.State.Identifier, _tradingBot.Account.Name); await agentGrain.CheckAndEnsureEthBalanceAsync(_state.State.Identifier,
_tradingBot.Account.Name);
if (!balanceCheckResult.IsSuccessful) if (!balanceCheckResult.IsSuccessful)
{ {
@@ -417,9 +427,11 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "Error during coordinated balance check for bot {BotId}", _state.State.Identifier); _logger.LogError(ex, "Error during coordinated balance check for bot {BotId}",
_state.State.Identifier);
// Continue execution to avoid stopping the bot due to coordination errors // Continue execution to avoid stopping the bot due to coordination errors
} }
}
// Execute the bot's Run method // Execute the bot's Run method
await _tradingBot.Run(); await _tradingBot.Run();
@@ -973,7 +985,7 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
_scopeFactory, _scopeFactory,
async tradingService => await tradingService.GetPositionsByInitiatorIdentifierAsync(botId)); async tradingService => await tradingService.GetPositionsByInitiatorIdentifierAsync(botId));
var hasOpenPositions = positions?.Any(p => p.IsOpen()) ?? false; var hasOpenPositions = positions?.Any(p => p.IsOpen() || p.Status.Equals(PositionStatus.New)) ?? false;
_logger.LogDebug("Bot {GrainId} has open positions in database: {HasOpenPositions}", _logger.LogDebug("Bot {GrainId} has open positions in database: {HasOpenPositions}",
botId, hasOpenPositions); botId, hasOpenPositions);

View File

@@ -121,7 +121,7 @@ namespace Managing.Application.Trading.Handlers
request.Date, request.Date,
TradeStatus.Requested); TradeStatus.Requested);
position.Status = IsOpenTradeHandled(position.Open.Status, account.Exchange) position.Status = IsOpenTradeHandled(position.Open.Status)
? position.Status ? position.Status
: PositionStatus.Rejected; : PositionStatus.Rejected;
@@ -133,10 +133,10 @@ namespace Managing.Application.Trading.Handlers
return position; return position;
} }
private static bool IsOpenTradeHandled(TradeStatus tradeStatus, TradingExchanges exchange) private static bool IsOpenTradeHandled(TradeStatus tradeStatus)
{ {
return tradeStatus == TradeStatus.Filled return tradeStatus == TradeStatus.Filled
|| (exchange == TradingExchanges.Evm && tradeStatus == TradeStatus.Requested); || tradeStatus == TradeStatus.Requested;
} }
} }
} }