Add backtest light

This commit is contained in:
2025-07-17 05:19:16 +07:00
parent 6a634eafaa
commit 27bed791c3
16 changed files with 181 additions and 270 deletions

View File

@@ -83,7 +83,7 @@ public class BacktestRepository : IBacktestRepository
return backtests.Select(b => MongoMappers.Map(b));
}
public (IEnumerable<Backtest> Backtests, int TotalCount) GetBacktestsByRequestIdPaginated(string requestId,
public (IEnumerable<LightBacktest> Backtests, int TotalCount) GetBacktestsByRequestIdPaginated(string requestId,
int page, int pageSize, string sortBy = "score", string sortOrder = "desc")
{
var stopwatch = Stopwatch.StartNew();
@@ -101,14 +101,12 @@ public class BacktestRepository : IBacktestRepository
.Include(b => b.WinRate)
.Include(b => b.GrowthPercentage)
.Include(b => b.HodlPercentage)
.Include(b => b.User)
.Include(b => b.Statistics)
.Include(b => b.StartDate)
.Include(b => b.EndDate)
.Include(b => b.Score)
.Include(b => b.RequestId)
.Include(b => b.Metadata)
.Include(b => b.Config);
.Include(b => b.Config)
.Include(b => b.Fees)
.Include(b => b.Statistics);
// Build sort definition
var sortDefinition = sortBy.ToLower() switch
@@ -146,7 +144,21 @@ public class BacktestRepository : IBacktestRepository
Console.WriteLine(
$"[BacktestRepo] Query: {afterQueryMs}ms, Count: {afterCountMs - afterQueryMs}ms, Projection: {afterProjectionMs - afterCountMs}ms, ToList: {afterToListMs - afterProjectionMs}ms, Total: {afterToListMs}ms");
var mappedBacktests = backtests.Select(b => MongoMappers.Map(b));
var mappedBacktests = backtests.Select(b => new LightBacktest
{
Id = b.Identifier,
Config = MongoMappers.Map(b.Config),
FinalPnl = b.FinalPnl,
WinRate = b.WinRate,
GrowthPercentage = b.GrowthPercentage,
HodlPercentage = b.HodlPercentage,
StartDate = b.StartDate,
EndDate = b.EndDate,
MaxDrawdown = b.Statistics?.MaxDrawdown,
Fees = b.Fees,
SharpeRatio = b.Statistics?.SharpeRatio != null ? (double)b.Statistics.SharpeRatio : null,
Score = b.Score
});
return (mappedBacktests, (int)totalCount);
}
@@ -198,7 +210,7 @@ public class BacktestRepository : IBacktestRepository
}
}
public (IEnumerable<Backtest> Backtests, int TotalCount) GetBacktestsByUserPaginated(User user, int page,
public (IEnumerable<LightBacktest> Backtests, int TotalCount) GetBacktestsByUserPaginated(User user, int page,
int pageSize, string sortBy = "score", string sortOrder = "desc")
{
var stopwatch = Stopwatch.StartNew();
@@ -216,14 +228,12 @@ public class BacktestRepository : IBacktestRepository
.Include(b => b.WinRate)
.Include(b => b.GrowthPercentage)
.Include(b => b.HodlPercentage)
.Include(b => b.User)
.Include(b => b.Statistics)
.Include(b => b.StartDate)
.Include(b => b.EndDate)
.Include(b => b.Score)
.Include(b => b.RequestId)
.Include(b => b.Metadata)
.Include(b => b.Config);
.Include(b => b.Config)
.Include(b => b.Fees)
.Include(b => b.Statistics);
// Build sort definition
var sortDefinition = sortBy.ToLower() switch
@@ -261,7 +271,21 @@ public class BacktestRepository : IBacktestRepository
Console.WriteLine(
$"[BacktestRepo] User Query: {afterQueryMs}ms, Count: {afterCountMs - afterQueryMs}ms, Projection: {afterProjectionMs - afterCountMs}ms, ToList: {afterToListMs - afterProjectionMs}ms, Total: {afterToListMs}ms");
var mappedBacktests = backtests.Select(b => MongoMappers.Map(b));
var mappedBacktests = backtests.Select(b => new LightBacktest
{
Id = b.Identifier,
Config = MongoMappers.Map(b.Config),
FinalPnl = b.FinalPnl,
WinRate = b.WinRate,
GrowthPercentage = b.GrowthPercentage,
HodlPercentage = b.HodlPercentage,
StartDate = b.StartDate,
EndDate = b.EndDate,
MaxDrawdown = b.Statistics?.MaxDrawdown,
Fees = b.Fees,
SharpeRatio = b.Statistics?.SharpeRatio != null ? (double)b.Statistics.SharpeRatio : null,
Score = b.Score
});
return (mappedBacktests, (int)totalCount);
}

View File

@@ -26,6 +26,8 @@ namespace Managing.Infrastructure.Databases.MongoDb.Collections
public MoneyManagementDto OptimizedMoneyManagement { get; internal set; }
public UserDto User { get; set; }
public PerformanceMetrics Statistics { get; set; }
[BsonRepresentation(BsonType.Decimal128)]
public decimal Fees { get; set; }
public double Score { get; set; }
public string Identifier { get; set; }
public string RequestId { get; set; }