Add async for saving backtests

This commit is contained in:
2025-11-05 16:58:46 +07:00
parent db6e06ad5d
commit 5afddb895e
3 changed files with 14 additions and 2 deletions

View File

@@ -8,6 +8,7 @@ namespace Managing.Application.Abstractions.Repositories;
public interface IBacktestRepository public interface IBacktestRepository
{ {
void InsertBacktestForUser(User user, Backtest result); void InsertBacktestForUser(User user, Backtest result);
Task InsertBacktestForUserAsync(User user, Backtest result);
IEnumerable<Backtest> GetBacktestsByUser(User user); IEnumerable<Backtest> GetBacktestsByUser(User user);
Task<IEnumerable<Backtest>> GetBacktestsByUserAsync(User user); Task<IEnumerable<Backtest>> GetBacktestsByUserAsync(User user);
IEnumerable<Backtest> GetBacktestsByRequestId(Guid requestId); IEnumerable<Backtest> GetBacktestsByRequestId(Guid requestId);

View File

@@ -79,6 +79,7 @@ public class BacktestTradingBotGrain : Grain, IBacktestTradingBotGrain
// Initialize wallet balance with first candle // Initialize wallet balance with first candle
tradingBot.WalletBalances.Clear(); tradingBot.WalletBalances.Clear();
tradingBot.WalletBalances.Add(candles.FirstOrDefault()!.Date, config.BotTradingBalance); tradingBot.WalletBalances.Add(candles.FirstOrDefault()!.Date, config.BotTradingBalance);
var initialBalance = config.BotTradingBalance;
var fixedCandles = new HashSet<Candle>(); var fixedCandles = new HashSet<Candle>();
// Process all candles following the exact pattern from GetBacktestingResult // Process all candles following the exact pattern from GetBacktestingResult
@@ -166,13 +167,13 @@ public class BacktestTradingBotGrain : Grain, IBacktestTradingBotGrain
Metadata = metadata, Metadata = metadata,
StartDate = candles.FirstOrDefault()!.OpenTime, StartDate = candles.FirstOrDefault()!.OpenTime,
EndDate = candles.LastOrDefault()!.OpenTime, EndDate = candles.LastOrDefault()!.OpenTime,
InitialBalance = tradingBot.WalletBalances.FirstOrDefault().Value, InitialBalance = initialBalance,
NetPnl = finalPnl - fees, NetPnl = finalPnl - fees,
}; };
if (save && user != null) if (save && user != null)
{ {
_backtestRepository.InsertBacktestForUser(user, result); await _backtestRepository.InsertBacktestForUserAsync(user, result);
} }
// Send notification if backtest meets criteria // Send notification if backtest meets criteria

View File

@@ -32,6 +32,16 @@ public class PostgreSqlBacktestRepository : IBacktestRepository
_context.SaveChanges(); _context.SaveChanges();
} }
public async Task InsertBacktestForUserAsync(User user, Backtest result)
{
ValidateBacktestData(result);
result.User = user;
var entity = PostgreSqlMappers.Map(result);
_context.Backtests.Add(entity);
await _context.SaveChangesAsync();
}
/// <summary> /// <summary>
/// Validates that all numeric fields in the backtest are of the correct type /// Validates that all numeric fields in the backtest are of the correct type
/// </summary> /// </summary>