fix no candle when closing position

This commit is contained in:
2025-08-15 09:13:26 +07:00
parent 8eefab4597
commit 54bf914e95

View File

@@ -1028,11 +1028,29 @@ public class TradingBotBase : ITradingBot
await ServiceScopeHelpers.WithScopedService<IExchangeService>(_scopeFactory, async exchangeService =>
{
recentCandles = Config.IsForBacktest
? new List<Candle>() { LastCandle }
? (LastCandle != null ? new List<Candle>() { LastCandle } : new List<Candle>())
: (await exchangeService.GetCandlesInflux(TradingExchanges.Evm, Config.Ticker,
DateTime.UtcNow.AddHours(-4), Config.Timeframe)).ToList();
});
// Check if we have any candles before proceeding
if (recentCandles == null || !recentCandles.Any())
{
await LogWarning($"No recent candles available for position {position.Identifier}. Using current candle data instead.");
// Fallback to current candle if available
if (currentCandle != null)
{
recentCandles = new List<Candle> { currentCandle };
}
else
{
await LogWarning($"No candle data available for position {position.Identifier}. Cannot determine stop loss/take profit hit.");
Logger.LogError("No candle data available for position {PositionId}. Cannot determine stop loss/take profit hit.", position.Identifier);
return;
}
}
var minPriceRecent = recentCandles.Min(c => c.Low);
var maxPriceRecent = recentCandles.Max(c => c.High);