Add metadata to backtest

This commit is contained in:
2025-07-11 17:56:03 +07:00
parent c62570e15d
commit 47ef0cf2d5
8 changed files with 49 additions and 16 deletions

View File

@@ -70,6 +70,7 @@ namespace Managing.Application.Backtesting
/// <param name="save">Whether to save the backtest results</param>
/// <param name="withCandles">Whether to include candles and indicators values in the response</param>
/// <param name="requestId">The request ID to associate with this backtest (optional)</param>
/// <param name="metadata">Additional metadata to associate with this backtest (optional)</param>
/// <returns>The backtest results</returns>
public async Task<Backtest> RunTradingBotBacktest(
TradingBotConfig config,
@@ -78,11 +79,12 @@ namespace Managing.Application.Backtesting
User user = null,
bool save = false,
bool withCandles = false,
string requestId = null)
string requestId = null,
object metadata = null)
{
var candles = GetCandles(config.Ticker, config.Timeframe, startDate, endDate);
var result = await RunBacktestWithCandles(config, candles, user, withCandles, requestId);
var result = await RunBacktestWithCandles(config, candles, user, withCandles, requestId, metadata);
// Set start and end dates
result.StartDate = startDate;
@@ -110,9 +112,10 @@ namespace Managing.Application.Backtesting
List<Candle> candles,
User user = null,
bool withCandles = false,
string requestId = null)
string requestId = null,
object metadata = null)
{
return await RunBacktestWithCandles(config, candles, user, withCandles, requestId);
return await RunBacktestWithCandles(config, candles, user, withCandles, requestId, metadata);
}
/// <summary>
@@ -123,7 +126,8 @@ namespace Managing.Application.Backtesting
List<Candle> candles,
User user = null,
bool withCandles = false,
string requestId = null)
string requestId = null,
object metadata = null)
{
var tradingBot = _botFactory.CreateBacktestTradingBot(config);
@@ -139,7 +143,7 @@ namespace Managing.Application.Backtesting
tradingBot.User = user;
await tradingBot.LoadAccount();
var result = await GetBacktestingResult(config, tradingBot, candles, user, withCandles, requestId);
var result = await GetBacktestingResult(config, tradingBot, candles, user, withCandles, requestId, metadata);
if (user != null)
{
@@ -182,7 +186,8 @@ namespace Managing.Application.Backtesting
List<Candle> candles,
User user = null,
bool withCandles = false,
string requestId = null)
string requestId = null,
object metadata = null)
{
if (candles == null || candles.Count == 0)
{
@@ -272,7 +277,8 @@ namespace Managing.Application.Backtesting
: new Dictionary<IndicatorType, IndicatorsResultBase>(),
Score = score,
Id = Guid.NewGuid().ToString(),
RequestId = requestId
RequestId = requestId,
Metadata = metadata
};
// Send notification if backtest meets criteria

View File

@@ -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