Do not stop bot if position open
This commit is contained in:
@@ -398,27 +398,39 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use coordinated balance checking and swap management through AgentGrain
|
if (_tradingBot.Positions.Any(p => p.Value.IsOpen() || p.Value.Status.Equals(PositionStatus.New)))
|
||||||
try
|
|
||||||
{
|
{
|
||||||
var agentGrain = GrainFactory.GetGrain<IAgentGrain>(_state.State.User.Id);
|
_logger.LogInformation(
|
||||||
var balanceCheckResult =
|
"Bot {BotId} has open positions. Trading loop will continue managing existing positions.",
|
||||||
await agentGrain.CheckAndEnsureEthBalanceAsync(_state.State.Identifier, _tradingBot.Account.Name);
|
_state.State.Identifier);
|
||||||
|
}
|
||||||
if (!balanceCheckResult.IsSuccessful)
|
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
|
var agentGrain = GrainFactory.GetGrain<IAgentGrain>(_state.State.User.Id);
|
||||||
if (balanceCheckResult.ShouldStopBot)
|
var balanceCheckResult =
|
||||||
|
await agentGrain.CheckAndEnsureEthBalanceAsync(_state.State.Identifier,
|
||||||
|
_tradingBot.Account.Name);
|
||||||
|
|
||||||
|
if (!balanceCheckResult.IsSuccessful)
|
||||||
{
|
{
|
||||||
await StopAsync(balanceCheckResult.Message);
|
// Check if the bot should stop due to this failure
|
||||||
return;
|
if (balanceCheckResult.ShouldStopBot)
|
||||||
|
{
|
||||||
|
await StopAsync(balanceCheckResult.Message);
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
catch (Exception ex)
|
||||||
catch (Exception ex)
|
{
|
||||||
{
|
_logger.LogError(ex, "Error during coordinated balance check for bot {BotId}",
|
||||||
_logger.LogError(ex, "Error during coordinated balance check for bot {BotId}", _state.State.Identifier);
|
_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
|
||||||
@@ -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);
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user