Add BacktestCount

This commit is contained in:
2025-10-01 13:01:03 +07:00
parent 3e680ab815
commit 06850b57c4
14 changed files with 1571 additions and 9 deletions

View File

@@ -27,6 +27,7 @@ namespace Managing.Application.Backtests
private readonly IKaigenService _kaigenService;
private readonly IHubContext<BacktestHub> _hubContext;
private readonly IGrainFactory _grainFactory;
private readonly IAgentService _agentService;
public Backtester(
IExchangeService exchangeService,
@@ -37,7 +38,8 @@ namespace Managing.Application.Backtests
IMessengerService messengerService,
IKaigenService kaigenService,
IHubContext<BacktestHub> hubContext,
IGrainFactory grainFactory)
IGrainFactory grainFactory,
IAgentService agentService)
{
_exchangeService = exchangeService;
_backtestRepository = backtestRepository;
@@ -48,6 +50,7 @@ namespace Managing.Application.Backtests
_kaigenService = kaigenService;
_hubContext = hubContext;
_grainFactory = grainFactory;
_agentService = agentService;
}
/// <summary>
@@ -187,14 +190,25 @@ namespace Managing.Application.Backtests
// Create Orleans grain for backtesting
var backtestGrain = _grainFactory.GetGrain<IBacktestTradingBotGrain>(Guid.NewGuid());
// Run the backtest using the Orleans grain and return LightBacktest directly
return await backtestGrain.RunBacktestAsync(cleanConfig, candles, user, save, withCandles, requestId,
// Run the backtest using the Orleans grain
var result = await backtestGrain.RunBacktestAsync(cleanConfig, candles, user, save, withCandles, requestId,
metadata);
}
private async Task<Account> GetAccountFromConfig(TradingBotConfig config)
{
return await _accountService.GetAccountByAccountName(config.AccountName, false, false);
// Increment backtest count for the user if user is provided
if (user != null)
{
try
{
await _agentService.IncrementBacktestCountAsync(user.Id);
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to increment backtest count for user {UserId}", user.Id);
// Don't throw here as the backtest was successful, just log the error
}
}
return result;
}
private async Task<HashSet<Candle>> GetCandles(Ticker ticker, Timeframe timeframe,