fix no candle when closing position
This commit is contained in:
@@ -1024,17 +1024,35 @@ public class TradingBotBase : ITradingBot
|
||||
|
||||
if (currentCandle != null)
|
||||
{
|
||||
List<Candle> recentCandles = null;
|
||||
await ServiceScopeHelpers.WithScopedService<IExchangeService>(_scopeFactory, async exchangeService =>
|
||||
{
|
||||
recentCandles = Config.IsForBacktest
|
||||
? new List<Candle>() { LastCandle }
|
||||
: (await exchangeService.GetCandlesInflux(TradingExchanges.Evm, Config.Ticker,
|
||||
DateTime.UtcNow.AddHours(-4), Config.Timeframe)).ToList();
|
||||
});
|
||||
List<Candle> recentCandles = null;
|
||||
await ServiceScopeHelpers.WithScopedService<IExchangeService>(_scopeFactory, async exchangeService =>
|
||||
{
|
||||
recentCandles = Config.IsForBacktest
|
||||
? (LastCandle != null ? new List<Candle>() { LastCandle } : new List<Candle>())
|
||||
: (await exchangeService.GetCandlesInflux(TradingExchanges.Evm, Config.Ticker,
|
||||
DateTime.UtcNow.AddHours(-4), Config.Timeframe)).ToList();
|
||||
});
|
||||
|
||||
var minPriceRecent = recentCandles.Min(c => c.Low);
|
||||
var maxPriceRecent = recentCandles.Max(c => c.High);
|
||||
// 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);
|
||||
|
||||
bool wasStopLossHit = false;
|
||||
bool wasTakeProfitHit = false;
|
||||
|
||||
Reference in New Issue
Block a user