Fix backtest consistency
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using System.Collections.Concurrent;
|
||||
using Managing.Application.Abstractions.Repositories;
|
||||
using Managing.Application.Abstractions.Repositories;
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Domain.Accounts;
|
||||
using Managing.Domain.Bots;
|
||||
@@ -432,7 +431,6 @@ public class TradingService : ITradingService
|
||||
|
||||
/// <summary>
|
||||
/// Calculates indicators values for a given scenario and candles.
|
||||
/// Uses parallel processing for independent indicator calculations to improve performance.
|
||||
/// </summary>
|
||||
/// <param name="scenario">The scenario containing indicators.</param>
|
||||
/// <param name="candles">The candles to calculate indicators for.</param>
|
||||
@@ -441,7 +439,7 @@ public class TradingService : ITradingService
|
||||
Scenario scenario,
|
||||
HashSet<Candle> candles)
|
||||
{
|
||||
// Offload CPU-bound indicator calculations to thread pool with parallel processing
|
||||
// Offload CPU-bound indicator calculations to thread pool
|
||||
return await Task.Run(() =>
|
||||
{
|
||||
var indicatorsValues = new Dictionary<IndicatorType, IndicatorsResultBase>();
|
||||
@@ -451,39 +449,19 @@ public class TradingService : ITradingService
|
||||
return indicatorsValues;
|
||||
}
|
||||
|
||||
// Use parallel processing for independent indicator calculations
|
||||
// Configure parallelism based on indicator count and system capabilities
|
||||
var maxDegreeOfParallelism = Math.Min(scenario.Indicators.Count, Environment.ProcessorCount);
|
||||
|
||||
var options = new ParallelOptions
|
||||
{
|
||||
MaxDegreeOfParallelism = maxDegreeOfParallelism,
|
||||
CancellationToken = CancellationToken.None
|
||||
};
|
||||
|
||||
// Use thread-safe concurrent dictionary for parallel writes
|
||||
var concurrentResults = new ConcurrentDictionary<IndicatorType, IndicatorsResultBase>();
|
||||
|
||||
// Parallel calculation of indicators
|
||||
Parallel.ForEach(scenario.Indicators, options, indicator =>
|
||||
// Build indicators from scenario
|
||||
foreach (var indicator in scenario.Indicators)
|
||||
{
|
||||
try
|
||||
{
|
||||
var buildedIndicator = ScenarioHelpers.BuildIndicator(ScenarioHelpers.BaseToLight(indicator));
|
||||
var result = buildedIndicator.GetIndicatorValues(candles);
|
||||
concurrentResults[indicator.Type] = result;
|
||||
indicatorsValues[indicator.Type] = buildedIndicator.GetIndicatorValues(candles);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error calculating indicator {IndicatorName}: {ErrorMessage}",
|
||||
indicator.Name, ex.Message);
|
||||
}
|
||||
});
|
||||
|
||||
// Convert to regular dictionary for return
|
||||
foreach (var kvp in concurrentResults)
|
||||
{
|
||||
indicatorsValues[kvp.Key] = kvp.Value;
|
||||
}
|
||||
|
||||
return indicatorsValues;
|
||||
|
||||
Reference in New Issue
Block a user