Add metadata to backtest
This commit is contained in:
@@ -299,7 +299,7 @@ public class GeneticService : IGeneticService
|
||||
_logger.LogInformation("Starting fresh genetic algorithm for request {RequestId}", request.RequestId);
|
||||
}
|
||||
|
||||
// Create fitness function
|
||||
// Create fitness function first
|
||||
var fitness = new TradingBotFitness(_backtester, request);
|
||||
|
||||
// Create genetic algorithm with better configuration
|
||||
@@ -315,6 +315,9 @@ public class GeneticService : IGeneticService
|
||||
CrossoverProbability = 0.7f // Fixed crossover rate as in frontend
|
||||
};
|
||||
|
||||
// Set the genetic algorithm reference in the fitness function
|
||||
fitness.SetGeneticAlgorithm(ga);
|
||||
|
||||
// Custom termination condition that checks for cancellation
|
||||
var originalTermination = ga.Termination;
|
||||
ga.Termination = new GenerationNumberTermination(request.Generations);
|
||||
@@ -768,6 +771,7 @@ public class TradingBotFitness : IFitness
|
||||
{
|
||||
private readonly IBacktester _backtester;
|
||||
private readonly GeneticRequest _request;
|
||||
private GeneticAlgorithm _geneticAlgorithm;
|
||||
|
||||
public TradingBotFitness(IBacktester backtester, GeneticRequest request)
|
||||
{
|
||||
@@ -775,6 +779,11 @@ public class TradingBotFitness : IFitness
|
||||
_request = request;
|
||||
}
|
||||
|
||||
public void SetGeneticAlgorithm(GeneticAlgorithm geneticAlgorithm)
|
||||
{
|
||||
_geneticAlgorithm = geneticAlgorithm;
|
||||
}
|
||||
|
||||
public double Evaluate(IChromosome chromosome)
|
||||
{
|
||||
try
|
||||
@@ -785,15 +794,22 @@ public class TradingBotFitness : IFitness
|
||||
|
||||
var config = tradingBotChromosome.GetTradingBotConfig(_request);
|
||||
|
||||
// Get current generation number (default to 0 if not available)
|
||||
var currentGeneration = _geneticAlgorithm?.GenerationsNumber ?? 0;
|
||||
|
||||
// Run backtest
|
||||
var backtest = _backtester.RunTradingBotBacktest(
|
||||
config,
|
||||
_request.StartDate,
|
||||
_request.EndDate,
|
||||
_request.User,
|
||||
true, // Don't save individual backtests
|
||||
true,
|
||||
false, // Don't include candles
|
||||
_request.RequestId
|
||||
_request.RequestId,
|
||||
new
|
||||
{
|
||||
generation = currentGeneration
|
||||
}
|
||||
).Result;
|
||||
|
||||
// Calculate multi-objective fitness based on backtest results
|
||||
|
||||
Reference in New Issue
Block a user