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

@@ -18,6 +18,7 @@ namespace Managing.Application.Abstractions.Services
/// <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>
Task<Backtest> RunTradingBotBacktest(
TradingBotConfig config,
@@ -26,7 +27,8 @@ namespace Managing.Application.Abstractions.Services
User user = null,
bool save = false,
bool withCandles = false,
string requestId = null);
string requestId = null,
object metadata = null);
/// <summary>
/// Runs a trading bot backtest with pre-loaded candles.
@@ -37,13 +39,15 @@ namespace Managing.Application.Abstractions.Services
/// <param name="user">The user running the backtest (optional)</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>
Task<Backtest> RunTradingBotBacktest(
TradingBotConfig config,
List<Candle> candles,
User user = null,
bool withCandles = false,
string requestId = null);
string requestId = null,
object metadata = null);
// Additional methods for backtest management
bool DeleteBacktest(string id);

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

View File

@@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
#nullable enable
using System.ComponentModel.DataAnnotations;
using Exilion.TradingAtomics;
using Managing.Domain.Bots;
using Managing.Domain.Candles;
@@ -58,6 +59,7 @@ public class Backtest
[Required] public Dictionary<IndicatorType, IndicatorsResultBase> IndicatorsValues { get; set; }
[Required] public double Score { get; set; }
public string RequestId { get; set; }
public object? Metadata { get; set; }
/// <summary>
/// Creates a new TradingBotConfig based on this backtest's configuration for starting a live bot.

View File

@@ -23,5 +23,6 @@ namespace Managing.Infrastructure.Databases.MongoDb.Collections
public double Score { get; set; }
public string Identifier { get; set; }
public string RequestId { get; set; }
public string? Metadata { get; set; }
}
}

View File

@@ -150,7 +150,8 @@ public static class MongoMappers
StartDate = b.StartDate,
EndDate = b.EndDate,
Score = b.Score,
RequestId = b.RequestId
RequestId = b.RequestId,
Metadata = string.IsNullOrEmpty(b.Metadata) ? null : JsonSerializer.Deserialize<object>(b.Metadata)
};
return bTest;
@@ -178,7 +179,8 @@ public static class MongoMappers
StartDate = result.StartDate,
EndDate = result.EndDate,
Score = result.Score,
RequestId = result.RequestId
RequestId = result.RequestId,
Metadata = result.Metadata == null ? null : JsonSerializer.Serialize(result.Metadata)
};
}

View File

@@ -3290,6 +3290,7 @@ export interface Backtest {
indicatorsValues: { [key in keyof typeof IndicatorType]?: IndicatorsResultBase; };
score: number;
requestId?: string | null;
metadata?: any | null;
}
export interface TradingBotConfig {

View File

@@ -237,6 +237,7 @@ export interface Backtest {
indicatorsValues: { [key in keyof typeof IndicatorType]?: IndicatorsResultBase; };
score: number;
requestId?: string | null;
metadata?: any | null;
}
export interface TradingBotConfig {