Optimize backtest memory usage by implementing a rolling window for candle storage and update performance benchmarks with new test data.

This commit is contained in:
2025-11-15 13:54:39 +07:00
parent e814eb749c
commit bed25e7222
3 changed files with 15 additions and 4 deletions

View File

@@ -220,6 +220,8 @@ public class BacktestExecutor
// The signal calculation depends on rolling window state and cannot be pre-calculated effectively
// Use optimized rolling window approach - TradingBox.GetSignal only needs last 600 candles
const int RollingWindowSize = 600; // TradingBox.GetSignal only needs last 600 candles
var rollingWindowCandles = new Queue<Candle>(RollingWindowSize);
var candlesProcessed = 0;
// Signal caching optimization - reduce signal update frequency for better performance
@@ -231,8 +233,6 @@ public class BacktestExecutor
var lastWalletCheck = 0;
var lastWalletBalance = config.BotTradingBalance;
var fixedCandles = new HashSet<Candle>();
// Track memory usage during processing
var peakMemory = initialMemory;
const int memoryCheckInterval = 100; // Check memory every N candles to reduce GC.GetTotalMemory overhead
@@ -252,12 +252,19 @@ public class BacktestExecutor
// Check for cancellation (timeout or shutdown)
cancellationToken.ThrowIfCancellationRequested();
// Add to HashSet for reuse
fixedCandles.Add(candle);
// Maintain rolling window of last 600 candles to prevent exponential memory growth
rollingWindowCandles.Enqueue(candle);
if (rollingWindowCandles.Count > RollingWindowSize)
{
rollingWindowCandles.Dequeue(); // Remove oldest candle
}
tradingBot.LastCandle = candle;
// Run with optimized backtest path (minimize async calls)
var signalUpdateStart = Stopwatch.GetTimestamp();
// Convert rolling window to HashSet for TradingBot.UpdateSignals compatibility
var fixedCandles = new HashSet<Candle>(rollingWindowCandles);
await tradingBot.UpdateSignals(fixedCandles, preCalculatedIndicatorValues);
signalUpdateTotalTime += Stopwatch.GetElapsedTime(signalUpdateStart);