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

@@ -1024,17 +1024,35 @@ public class TradingBotBase : ITradingBot
if (currentCandle != null) if (currentCandle != null)
{ {
List<Candle> recentCandles = null; List<Candle> recentCandles = null;
await ServiceScopeHelpers.WithScopedService<IExchangeService>(_scopeFactory, async exchangeService => await ServiceScopeHelpers.WithScopedService<IExchangeService>(_scopeFactory, async exchangeService =>
{ {
recentCandles = Config.IsForBacktest recentCandles = Config.IsForBacktest
? new List<Candle>() { LastCandle } ? (LastCandle != null ? new List<Candle>() { LastCandle } : new List<Candle>())
: (await exchangeService.GetCandlesInflux(TradingExchanges.Evm, Config.Ticker, : (await exchangeService.GetCandlesInflux(TradingExchanges.Evm, Config.Ticker,
DateTime.UtcNow.AddHours(-4), Config.Timeframe)).ToList(); DateTime.UtcNow.AddHours(-4), Config.Timeframe)).ToList();
}); });
var minPriceRecent = recentCandles.Min(c => c.Low); // Check if we have any candles before proceeding
var maxPriceRecent = recentCandles.Max(c => c.High); 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);
bool wasStopLossHit = false; bool wasStopLossHit = false;
bool wasTakeProfitHit = false; bool wasTakeProfitHit = false;