Add cancellation token support to backtest execution and update progress handling

This commit is contained in:
2025-11-13 18:05:55 +07:00
parent 17d904c445
commit 1f7d914625
4 changed files with 16 additions and 15 deletions

View File

@@ -125,6 +125,7 @@ public class BacktestExecutor
/// <param name="bundleRequestId">Optional bundle request ID to update with backtest result</param>
/// <param name="metadata">Additional metadata</param>
/// <param name="progressCallback">Optional callback for progress updates (0-100)</param>
/// <param name="cancellationToken">Cancellation token to stop execution</param>
/// <returns>The lightweight backtest result</returns>
public async Task<LightBacktest> ExecuteAsync(
TradingBotConfig config,
@@ -135,7 +136,8 @@ public class BacktestExecutor
string requestId = null,
Guid? bundleRequestId = null,
object metadata = null,
Func<int, Task> progressCallback = null)
Func<int, Task> progressCallback = null,
CancellationToken cancellationToken = default)
{
if (candles == null || candles.Count == 0)
{
@@ -247,6 +249,9 @@ public class BacktestExecutor
// Process all candles with optimized rolling window approach
foreach (var candle in orderedCandles)
{
// Check for cancellation (timeout or shutdown)
cancellationToken.ThrowIfCancellationRequested();
// Add to HashSet for reuse
fixedCandles.Add(candle);
tradingBot.LastCandle = candle;