Update genetic impl

This commit is contained in:
2025-07-10 22:07:21 +07:00
parent 2fc7a1d4bb
commit 551480d618
7 changed files with 29 additions and 13 deletions

View File

@@ -218,7 +218,8 @@ public class BacktestController : BaseController
request.EndDate, request.EndDate,
user, user,
request.Save, request.Save,
request.WithCandles); request.WithCandles,
null); // No requestId for regular backtests
await NotifyBacktesingSubscriberAsync(backtestResult); await NotifyBacktesingSubscriberAsync(backtestResult);

View File

@@ -17,6 +17,7 @@ namespace Managing.Application.Abstractions.Services
/// <param name="user">The user running the backtest (optional)</param> /// <param name="user">The user running the backtest (optional)</param>
/// <param name="save">Whether to save the backtest results</param> /// <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="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>
/// <returns>The backtest results</returns> /// <returns>The backtest results</returns>
Task<Backtest> RunTradingBotBacktest( Task<Backtest> RunTradingBotBacktest(
TradingBotConfig config, TradingBotConfig config,
@@ -24,7 +25,8 @@ namespace Managing.Application.Abstractions.Services
DateTime endDate, DateTime endDate,
User user = null, User user = null,
bool save = false, bool save = false,
bool withCandles = false); bool withCandles = false,
string requestId = null);
/// <summary> /// <summary>
/// Runs a trading bot backtest with pre-loaded candles. /// Runs a trading bot backtest with pre-loaded candles.
@@ -34,12 +36,14 @@ namespace Managing.Application.Abstractions.Services
/// <param name="candles">The candles to use for backtesting</param> /// <param name="candles">The candles to use for backtesting</param>
/// <param name="user">The user running the backtest (optional)</param> /// <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="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>
/// <returns>The backtest results</returns> /// <returns>The backtest results</returns>
Task<Backtest> RunTradingBotBacktest( Task<Backtest> RunTradingBotBacktest(
TradingBotConfig config, TradingBotConfig config,
List<Candle> candles, List<Candle> candles,
User user = null, User user = null,
bool withCandles = false); bool withCandles = false,
string requestId = null);
// Additional methods for backtest management // Additional methods for backtest management
bool DeleteBacktest(string id); bool DeleteBacktest(string id);

View File

@@ -69,6 +69,7 @@ namespace Managing.Application.Backtesting
/// <param name="user">The user running the backtest (optional)</param> /// <param name="user">The user running the backtest (optional)</param>
/// <param name="save">Whether to save the backtest results</param> /// <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="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>
/// <returns>The backtest results</returns> /// <returns>The backtest results</returns>
public async Task<Backtest> RunTradingBotBacktest( public async Task<Backtest> RunTradingBotBacktest(
TradingBotConfig config, TradingBotConfig config,
@@ -76,12 +77,13 @@ namespace Managing.Application.Backtesting
DateTime endDate, DateTime endDate,
User user = null, User user = null,
bool save = false, bool save = false,
bool withCandles = false) bool withCandles = false,
string requestId = null)
{ {
var account = await GetAccountFromConfig(config); var account = await GetAccountFromConfig(config);
var candles = GetCandles(account, config.Ticker, config.Timeframe, startDate, endDate); 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 // Set start and end dates
result.StartDate = startDate; result.StartDate = startDate;
@@ -108,9 +110,10 @@ namespace Managing.Application.Backtesting
TradingBotConfig config, TradingBotConfig config,
List<Candle> candles, List<Candle> candles,
User user = null, 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);
} }
/// <summary> /// <summary>
@@ -120,7 +123,8 @@ namespace Managing.Application.Backtesting
TradingBotConfig config, TradingBotConfig config,
List<Candle> candles, List<Candle> candles,
User user = null, User user = null,
bool withCandles = false) bool withCandles = false,
string requestId = null)
{ {
var tradingBot = _botFactory.CreateBacktestTradingBot(config); var tradingBot = _botFactory.CreateBacktestTradingBot(config);
@@ -136,7 +140,7 @@ namespace Managing.Application.Backtesting
tradingBot.User = user; tradingBot.User = user;
await tradingBot.LoadAccount(); 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) if (user != null)
{ {
@@ -178,7 +182,8 @@ namespace Managing.Application.Backtesting
ITradingBot bot, ITradingBot bot,
List<Candle> candles, List<Candle> candles,
User user = null, User user = null,
bool withCandles = false) bool withCandles = false,
string requestId = null)
{ {
if (candles == null || candles.Count == 0) if (candles == null || candles.Count == 0)
{ {
@@ -267,7 +272,8 @@ namespace Managing.Application.Backtesting
? AggregateValues(indicatorsValues, bot.IndicatorsValues) ? AggregateValues(indicatorsValues, bot.IndicatorsValues)
: new Dictionary<IndicatorType, IndicatorsResultBase>(), : new Dictionary<IndicatorType, IndicatorsResultBase>(),
Score = score, Score = score,
Id = Guid.NewGuid().ToString() Id = Guid.NewGuid().ToString(),
RequestId = requestId
}; };
// Send notification if backtest meets criteria // Send notification if backtest meets criteria

View File

@@ -293,7 +293,8 @@ public class TradingBotFitness : IFitness
_request.EndDate, _request.EndDate,
_request.User, _request.User,
false, // Don't save individual backtests false, // Don't save individual backtests
false // Don't include candles false, // Don't include candles
_request.RequestId
).Result; ).Result;
// Calculate fitness based on backtest results // Calculate fitness based on backtest results

View File

@@ -57,6 +57,7 @@ public class Backtest
[Required] public User User { get; set; } [Required] public User User { get; set; }
[Required] public Dictionary<IndicatorType, IndicatorsResultBase> IndicatorsValues { get; set; } [Required] public Dictionary<IndicatorType, IndicatorsResultBase> IndicatorsValues { get; set; }
[Required] public double Score { get; set; } [Required] public double Score { get; set; }
public string RequestId { get; set; }
/// <summary> /// <summary>
/// Creates a new TradingBotConfig based on this backtest's configuration for starting a live bot. /// Creates a new TradingBotConfig based on this backtest's configuration for starting a live bot.

View File

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

View File

@@ -149,7 +149,8 @@ public static class MongoMappers
Statistics = b.Statistics, Statistics = b.Statistics,
StartDate = b.StartDate, StartDate = b.StartDate,
EndDate = b.EndDate, EndDate = b.EndDate,
Score = b.Score Score = b.Score,
RequestId = b.RequestId
}; };
return bTest; return bTest;
@@ -177,6 +178,7 @@ public static class MongoMappers
StartDate = result.StartDate, StartDate = result.StartDate,
EndDate = result.EndDate, EndDate = result.EndDate,
Score = result.Score, Score = result.Score,
RequestId = result.RequestId
}; };
} }