Add test for executor
This commit is contained in:
@@ -350,9 +350,10 @@ public class GeneticService : IGeneticService
|
||||
|
||||
// Load candles once at the beginning to avoid repeated database queries
|
||||
// This significantly reduces database connections during genetic algorithm execution
|
||||
_logger.LogInformation("Loading candles for genetic algorithm {RequestId}: {Ticker} on {Timeframe} from {StartDate} to {EndDate}",
|
||||
_logger.LogInformation(
|
||||
"Loading candles for genetic algorithm {RequestId}: {Ticker} on {Timeframe} from {StartDate} to {EndDate}",
|
||||
request.RequestId, request.Ticker, request.Timeframe, request.StartDate, request.EndDate);
|
||||
|
||||
|
||||
HashSet<Candle> candles;
|
||||
try
|
||||
{
|
||||
@@ -366,13 +367,13 @@ public class GeneticService : IGeneticService
|
||||
request.EndDate
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
if (candles == null || candles.Count == 0)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"No candles found for {request.Ticker} on {request.Timeframe} from {request.StartDate} to {request.EndDate}");
|
||||
}
|
||||
|
||||
|
||||
_logger.LogInformation("Loaded {CandleCount} candles for genetic algorithm {RequestId}",
|
||||
candles.Count, request.RequestId);
|
||||
}
|
||||
@@ -472,8 +473,9 @@ public class GeneticService : IGeneticService
|
||||
{
|
||||
// Reload the request from the database in the new scope
|
||||
// Use the user from the original request to get the request by ID
|
||||
var dbRequest = geneticService.GetGeneticRequestByIdForUser(request.User, request.RequestId);
|
||||
|
||||
var dbRequest =
|
||||
geneticService.GetGeneticRequestByIdForUser(request.User, request.RequestId);
|
||||
|
||||
if (dbRequest != null)
|
||||
{
|
||||
// Update the loaded request with current generation data
|
||||
@@ -482,13 +484,14 @@ public class GeneticService : IGeneticService
|
||||
dbRequest.BestChromosome = bestChromosomeJson;
|
||||
dbRequest.BestIndividual = bestIndividual;
|
||||
dbRequest.ProgressInfo = progressInfo;
|
||||
|
||||
|
||||
// Save the update
|
||||
await geneticService.UpdateGeneticRequestAsync(dbRequest);
|
||||
}
|
||||
});
|
||||
|
||||
_logger.LogDebug("Updated genetic request {RequestId} at generation {Generation} with fitness {Fitness}",
|
||||
_logger.LogDebug(
|
||||
"Updated genetic request {RequestId} at generation {Generation} with fitness {Fitness}",
|
||||
request.RequestId, generationCount, bestFitness);
|
||||
|
||||
// Check for cancellation
|
||||
@@ -540,7 +543,7 @@ public class GeneticService : IGeneticService
|
||||
request.BestIndividual = bestChromosome?.ToString() ?? "unknown";
|
||||
request.CurrentGeneration = ga.GenerationsNumber;
|
||||
request.BestFitnessSoFar = bestFitness;
|
||||
|
||||
|
||||
// Update BestChromosome if not already set
|
||||
if (bestChromosome != null && string.IsNullOrEmpty(request.BestChromosome))
|
||||
{
|
||||
@@ -553,7 +556,7 @@ public class GeneticService : IGeneticService
|
||||
}).ToArray();
|
||||
request.BestChromosome = JsonSerializer.Serialize(geneValues);
|
||||
}
|
||||
|
||||
|
||||
request.ProgressInfo = JsonSerializer.Serialize(new
|
||||
{
|
||||
generation = ga.GenerationsNumber,
|
||||
@@ -564,8 +567,9 @@ public class GeneticService : IGeneticService
|
||||
});
|
||||
|
||||
await UpdateGeneticRequestAsync(request);
|
||||
|
||||
_logger.LogInformation("Final update completed for genetic request {RequestId}. Generation: {Generation}, Best Fitness: {Fitness}",
|
||||
|
||||
_logger.LogInformation(
|
||||
"Final update completed for genetic request {RequestId}. Generation: {Generation}, Best Fitness: {Fitness}",
|
||||
request.RequestId, ga.GenerationsNumber, bestFitness);
|
||||
|
||||
// Send notification about the completed genetic algorithm
|
||||
@@ -893,7 +897,7 @@ public class TradingBotChromosome : ChromosomeBase
|
||||
return new TradingBotConfig
|
||||
{
|
||||
Name = $"Genetic_{request.RequestId}",
|
||||
AccountName = "Oda-embedded",
|
||||
AccountName = request.User.Accounts.FirstOrDefault().Name,
|
||||
Ticker = request.Ticker,
|
||||
Timeframe = request.Timeframe,
|
||||
BotTradingBalance = request.Balance,
|
||||
@@ -1009,7 +1013,7 @@ public class TradingBotFitness : IFitness
|
||||
private static readonly SemaphoreSlim _dbSemaphore = new SemaphoreSlim(4, 4); // Limit concurrent DB operations
|
||||
|
||||
public TradingBotFitness(
|
||||
IServiceScopeFactory serviceScopeFactory,
|
||||
IServiceScopeFactory serviceScopeFactory,
|
||||
GeneticRequest request,
|
||||
HashSet<Candle> candles,
|
||||
ILogger<GeneticService> logger)
|
||||
@@ -1051,7 +1055,9 @@ public class TradingBotFitness : IFitness
|
||||
save: false, // Don't save backtest results for genetic algorithm
|
||||
withCandles: false,
|
||||
requestId: _request.RequestId,
|
||||
metadata: new GeneticBacktestMetadata(_geneticAlgorithm?.GenerationsNumber ?? 0, _request.RequestId)
|
||||
bundleRequestId: null, // Genetic algorithm doesn't use bundle requests
|
||||
metadata: new GeneticBacktestMetadata(_geneticAlgorithm?.GenerationsNumber ?? 0,
|
||||
_request.RequestId)
|
||||
)
|
||||
).GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user