diff --git a/src/Managing.Api/Controllers/BacktestController.cs b/src/Managing.Api/Controllers/BacktestController.cs index 64d15b7..0dbdb73 100644 --- a/src/Managing.Api/Controllers/BacktestController.cs +++ b/src/Managing.Api/Controllers/BacktestController.cs @@ -218,7 +218,8 @@ public class BacktestController : BaseController request.EndDate, user, request.Save, - request.WithCandles); + request.WithCandles, + null); // No requestId for regular backtests await NotifyBacktesingSubscriberAsync(backtestResult); diff --git a/src/Managing.Application.Abstractions/Services/IBacktester.cs b/src/Managing.Application.Abstractions/Services/IBacktester.cs index 069dcf5..d0399f0 100644 --- a/src/Managing.Application.Abstractions/Services/IBacktester.cs +++ b/src/Managing.Application.Abstractions/Services/IBacktester.cs @@ -17,6 +17,7 @@ namespace Managing.Application.Abstractions.Services /// The user running the backtest (optional) /// Whether to save the backtest results /// Whether to include candles and indicators values in the response + /// The request ID to associate with this backtest (optional) /// The backtest results Task RunTradingBotBacktest( TradingBotConfig config, @@ -24,7 +25,8 @@ namespace Managing.Application.Abstractions.Services DateTime endDate, User user = null, bool save = false, - bool withCandles = false); + bool withCandles = false, + string requestId = null); /// /// Runs a trading bot backtest with pre-loaded candles. @@ -34,12 +36,14 @@ namespace Managing.Application.Abstractions.Services /// The candles to use for backtesting /// The user running the backtest (optional) /// Whether to include candles and indicators values in the response + /// The request ID to associate with this backtest (optional) /// The backtest results Task RunTradingBotBacktest( TradingBotConfig config, List candles, User user = null, - bool withCandles = false); + bool withCandles = false, + string requestId = null); // Additional methods for backtest management bool DeleteBacktest(string id); diff --git a/src/Managing.Application/Backtesting/Backtester.cs b/src/Managing.Application/Backtesting/Backtester.cs index be9ab8a..8a0f2fd 100644 --- a/src/Managing.Application/Backtesting/Backtester.cs +++ b/src/Managing.Application/Backtesting/Backtester.cs @@ -69,6 +69,7 @@ namespace Managing.Application.Backtesting /// The user running the backtest (optional) /// Whether to save the backtest results /// Whether to include candles and indicators values in the response + /// The request ID to associate with this backtest (optional) /// The backtest results public async Task RunTradingBotBacktest( TradingBotConfig config, @@ -76,12 +77,13 @@ namespace Managing.Application.Backtesting DateTime endDate, User user = null, bool save = false, - bool withCandles = false) + bool withCandles = false, + string requestId = null) { var account = await GetAccountFromConfig(config); var candles = GetCandles(account, config.Ticker, config.Timeframe, startDate, endDate); - var result = await RunBacktestWithCandles(config, candles, user, withCandles); + var result = await RunBacktestWithCandles(config, candles, user, withCandles, requestId); // Set start and end dates result.StartDate = startDate; @@ -108,9 +110,10 @@ namespace Managing.Application.Backtesting TradingBotConfig config, List candles, User user = null, - bool withCandles = false) + bool withCandles = false, + string requestId = null) { - return await RunBacktestWithCandles(config, candles, user, withCandles); + return await RunBacktestWithCandles(config, candles, user, withCandles, requestId); } /// @@ -120,7 +123,8 @@ namespace Managing.Application.Backtesting TradingBotConfig config, List candles, User user = null, - bool withCandles = false) + bool withCandles = false, + string requestId = null) { var tradingBot = _botFactory.CreateBacktestTradingBot(config); @@ -136,7 +140,7 @@ namespace Managing.Application.Backtesting tradingBot.User = user; await tradingBot.LoadAccount(); - var result = await GetBacktestingResult(config, tradingBot, candles, user, withCandles); + var result = await GetBacktestingResult(config, tradingBot, candles, user, withCandles, requestId); if (user != null) { @@ -178,7 +182,8 @@ namespace Managing.Application.Backtesting ITradingBot bot, List candles, User user = null, - bool withCandles = false) + bool withCandles = false, + string requestId = null) { if (candles == null || candles.Count == 0) { @@ -267,7 +272,8 @@ namespace Managing.Application.Backtesting ? AggregateValues(indicatorsValues, bot.IndicatorsValues) : new Dictionary(), Score = score, - Id = Guid.NewGuid().ToString() + Id = Guid.NewGuid().ToString(), + RequestId = requestId }; // Send notification if backtest meets criteria diff --git a/src/Managing.Application/GeneticService.cs b/src/Managing.Application/GeneticService.cs index e3a2b7e..986dff6 100644 --- a/src/Managing.Application/GeneticService.cs +++ b/src/Managing.Application/GeneticService.cs @@ -293,7 +293,8 @@ public class TradingBotFitness : IFitness _request.EndDate, _request.User, false, // Don't save individual backtests - false // Don't include candles + false, // Don't include candles + _request.RequestId ).Result; // Calculate fitness based on backtest results diff --git a/src/Managing.Domain/Backtests/Backtest.cs b/src/Managing.Domain/Backtests/Backtest.cs index 80bac87..cd270e8 100644 --- a/src/Managing.Domain/Backtests/Backtest.cs +++ b/src/Managing.Domain/Backtests/Backtest.cs @@ -57,6 +57,7 @@ public class Backtest [Required] public User User { get; set; } [Required] public Dictionary IndicatorsValues { get; set; } [Required] public double Score { get; set; } + public string RequestId { get; set; } /// /// Creates a new TradingBotConfig based on this backtest's configuration for starting a live bot. diff --git a/src/Managing.Infrastructure.Database/MongoDb/Collections/BacktestDto.cs b/src/Managing.Infrastructure.Database/MongoDb/Collections/BacktestDto.cs index 50be1ec..6237a6d 100644 --- a/src/Managing.Infrastructure.Database/MongoDb/Collections/BacktestDto.cs +++ b/src/Managing.Infrastructure.Database/MongoDb/Collections/BacktestDto.cs @@ -22,5 +22,6 @@ namespace Managing.Infrastructure.Databases.MongoDb.Collections public PerformanceMetrics Statistics { get; set; } public double Score { get; set; } public string Identifier { get; set; } + public string RequestId { get; set; } } } \ No newline at end of file diff --git a/src/Managing.Infrastructure.Database/MongoDb/MongoMappers.cs b/src/Managing.Infrastructure.Database/MongoDb/MongoMappers.cs index 7cda81d..a54395d 100644 --- a/src/Managing.Infrastructure.Database/MongoDb/MongoMappers.cs +++ b/src/Managing.Infrastructure.Database/MongoDb/MongoMappers.cs @@ -149,7 +149,8 @@ public static class MongoMappers Statistics = b.Statistics, StartDate = b.StartDate, EndDate = b.EndDate, - Score = b.Score + Score = b.Score, + RequestId = b.RequestId }; return bTest; @@ -177,6 +178,7 @@ public static class MongoMappers StartDate = result.StartDate, EndDate = result.EndDate, Score = result.Score, + RequestId = result.RequestId }; }