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,27 +398,39 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
return;
}
// Use coordinated balance checking and swap management through AgentGrain
try
if (_tradingBot.Positions.Any(p => p.Value.IsOpen() || p.Value.Status.Equals(PositionStatus.New)))
{
var agentGrain = GrainFactory.GetGrain<IAgentGrain>(_state.State.User.Id);
var balanceCheckResult =
await agentGrain.CheckAndEnsureEthBalanceAsync(_state.State.Identifier, _tradingBot.Account.Name);
if (!balanceCheckResult.IsSuccessful)
_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
try
{
// Check if the bot should stop due to this failure
if (balanceCheckResult.ShouldStopBot)
var agentGrain = GrainFactory.GetGrain<IAgentGrain>(_state.State.User.Id);
var balanceCheckResult =
await agentGrain.CheckAndEnsureEthBalanceAsync(_state.State.Identifier,
_tradingBot.Account.Name);
if (!balanceCheckResult.IsSuccessful)
{
await StopAsync(balanceCheckResult.Message);
return;
// Check if the bot should stop due to this failure
if (balanceCheckResult.ShouldStopBot)
{
await StopAsync(balanceCheckResult.Message);
return;
}
}
}
}
catch (Exception ex)
{
_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
catch (Exception ex)
{
_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
}
}
// Execute the bot's Run method
@@ -973,7 +985,7 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
_scopeFactory,
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}",
botId, hasOpenPositions);

View File

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