Add filters and sorting for backtests

This commit is contained in:
2025-10-14 18:06:36 +07:00
parent 49b0f7b696
commit 74adad5834
21 changed files with 4028 additions and 81 deletions

View File

@@ -1,12 +1,14 @@
using System.Diagnostics;
using Exilion.TradingAtomics;
using Managing.Application.Abstractions.Repositories;
using Managing.Application.Abstractions.Shared;
using Managing.Domain.Backtests;
using Managing.Domain.Bots;
using Managing.Domain.Users;
using Managing.Infrastructure.Databases.PostgreSql.Entities;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using static Managing.Common.Enums;
namespace Managing.Infrastructure.Databases.PostgreSql;
@@ -377,8 +379,13 @@ public class PostgreSqlBacktestRepository : IBacktestRepository
}
}
public (IEnumerable<LightBacktest> Backtests, int TotalCount) GetBacktestsByUserPaginated(User user, int page,
int pageSize, string sortBy = "score", string sortOrder = "desc")
public (IEnumerable<LightBacktest> Backtests, int TotalCount) GetBacktestsByUserPaginated(
User user,
int page,
int pageSize,
BacktestSortableColumn sortBy = BacktestSortableColumn.Score,
string sortOrder = "desc",
BacktestsFilter? filter = null)
{
var stopwatch = Stopwatch.StartNew();
@@ -386,28 +393,83 @@ public class PostgreSqlBacktestRepository : IBacktestRepository
.AsNoTracking()
.Where(b => b.UserId == user.Id);
if (filter != null)
{
if (filter.ScoreMin.HasValue)
baseQuery = baseQuery.Where(b => b.Score >= filter.ScoreMin.Value);
if (filter.ScoreMax.HasValue)
baseQuery = baseQuery.Where(b => b.Score <= filter.ScoreMax.Value);
if (filter.WinrateMin.HasValue)
baseQuery = baseQuery.Where(b => b.WinRate >= filter.WinrateMin.Value);
if (filter.WinrateMax.HasValue)
baseQuery = baseQuery.Where(b => b.WinRate <= filter.WinrateMax.Value);
if (filter.MaxDrawdownMax.HasValue)
baseQuery = baseQuery.Where(b => b.MaxDrawdown <= filter.MaxDrawdownMax.Value);
if (filter.Tickers != null && filter.Tickers.Any())
{
var tickerArray = filter.Tickers.ToArray();
baseQuery = baseQuery.Where(b => tickerArray.Contains(b.Ticker));
}
if (filter.Indicators != null && filter.Indicators.Any())
{
foreach (var ind in filter.Indicators)
{
var token = "," + ind + ",";
baseQuery = baseQuery.Where(b => ("," + b.IndicatorsCsv + ",").Contains(token));
}
}
if (filter.DurationMin.HasValue)
baseQuery = baseQuery.Where(b => b.Duration >= filter.DurationMin.Value);
if (filter.DurationMax.HasValue)
baseQuery = baseQuery.Where(b => b.Duration <= filter.DurationMax.Value);
}
var afterQueryMs = stopwatch.ElapsedMilliseconds;
var totalCount = baseQuery.Count();
var afterCountMs = stopwatch.ElapsedMilliseconds;
// Apply sorting
IQueryable<BacktestEntity> sortedQuery = sortBy.ToLower() switch
IQueryable<BacktestEntity> sortedQuery = sortBy switch
{
"score" => sortOrder == "desc"
BacktestSortableColumn.Score => sortOrder == "desc"
? baseQuery.OrderByDescending(b => b.Score)
: baseQuery.OrderBy(b => b.Score),
"finalpnl" => sortOrder == "desc"
BacktestSortableColumn.FinalPnl => sortOrder == "desc"
? baseQuery.OrderByDescending(b => b.FinalPnl)
: baseQuery.OrderBy(b => b.FinalPnl),
"winrate" => sortOrder == "desc"
BacktestSortableColumn.WinRate => sortOrder == "desc"
? baseQuery.OrderByDescending(b => b.WinRate)
: baseQuery.OrderBy(b => b.WinRate),
"growthpercentage" => sortOrder == "desc"
BacktestSortableColumn.GrowthPercentage => sortOrder == "desc"
? baseQuery.OrderByDescending(b => b.GrowthPercentage)
: baseQuery.OrderBy(b => b.GrowthPercentage),
"hodlpercentage" => sortOrder == "desc"
BacktestSortableColumn.HodlPercentage => sortOrder == "desc"
? baseQuery.OrderByDescending(b => b.HodlPercentage)
: baseQuery.OrderBy(b => b.HodlPercentage),
BacktestSortableColumn.Duration => sortOrder == "desc"
? baseQuery.OrderByDescending(b => b.Duration)
: baseQuery.OrderBy(b => b.Duration),
BacktestSortableColumn.Timeframe => sortOrder == "desc"
? baseQuery.OrderByDescending(b => b.Timeframe)
: baseQuery.OrderBy(b => b.Timeframe),
BacktestSortableColumn.IndicatorsCount => sortOrder == "desc"
? baseQuery.OrderByDescending(b => b.IndicatorsCount)
: baseQuery.OrderBy(b => b.IndicatorsCount),
BacktestSortableColumn.MaxDrawdown => sortOrder == "desc"
? baseQuery.OrderByDescending(b => b.MaxDrawdown)
: baseQuery.OrderBy(b => b.MaxDrawdown),
BacktestSortableColumn.Fees => sortOrder == "desc"
? baseQuery.OrderByDescending(b => b.Fees)
: baseQuery.OrderBy(b => b.Fees),
BacktestSortableColumn.SharpeRatio => sortOrder == "desc"
? baseQuery.OrderByDescending(b => b.SharpeRatio)
: baseQuery.OrderBy(b => b.SharpeRatio),
BacktestSortableColumn.Ticker => sortOrder == "desc"
? baseQuery.OrderByDescending(b => b.Ticker)
: baseQuery.OrderBy(b => b.Ticker),
_ => sortOrder == "desc"
? baseQuery.OrderByDescending(b => b.Score)
: baseQuery.OrderBy(b => b.Score)
@@ -427,21 +489,16 @@ public class PostgreSqlBacktestRepository : IBacktestRepository
{
Id = entity.Identifier,
Config = JsonConvert.DeserializeObject<TradingBotConfig>(entity.ConfigJson),
Ticker = entity.Ticker,
FinalPnl = entity.FinalPnl,
WinRate = entity.WinRate,
GrowthPercentage = entity.GrowthPercentage,
HodlPercentage = entity.HodlPercentage,
StartDate = entity.StartDate,
EndDate = entity.EndDate,
MaxDrawdown = !string.IsNullOrEmpty(entity.StatisticsJson)
? JsonConvert.DeserializeObject<PerformanceMetrics>(entity.StatisticsJson)?.MaxDrawdown
: null,
MaxDrawdown = entity.MaxDrawdown,
Fees = entity.Fees,
SharpeRatio = !string.IsNullOrEmpty(entity.StatisticsJson)
? JsonConvert.DeserializeObject<PerformanceMetrics>(entity.StatisticsJson)?.SharpeRatio != null
? (double?)JsonConvert.DeserializeObject<PerformanceMetrics>(entity.StatisticsJson).SharpeRatio
: null
: null,
SharpeRatio = (double?)entity.SharpeRatio,
Score = entity.Score,
ScoreMessage = entity.ScoreMessage ?? string.Empty
});
@@ -450,36 +507,95 @@ public class PostgreSqlBacktestRepository : IBacktestRepository
}
public async Task<(IEnumerable<LightBacktest> Backtests, int TotalCount)> GetBacktestsByUserPaginatedAsync(
User user, int page, int pageSize, string sortBy = "score", string sortOrder = "desc")
User user,
int page,
int pageSize,
BacktestSortableColumn sortBy = BacktestSortableColumn.Score,
string sortOrder = "desc",
BacktestsFilter? filter = null)
{
var stopwatch = Stopwatch.StartNew();
var baseQuery = _context.Backtests
.AsNoTracking()
.Where(b => b.UserId == user.Id);
if (filter != null)
{
if (filter.ScoreMin.HasValue)
baseQuery = baseQuery.Where(b => b.Score >= filter.ScoreMin.Value);
if (filter.ScoreMax.HasValue)
baseQuery = baseQuery.Where(b => b.Score <= filter.ScoreMax.Value);
if (filter.WinrateMin.HasValue)
baseQuery = baseQuery.Where(b => b.WinRate >= filter.WinrateMin.Value);
if (filter.WinrateMax.HasValue)
baseQuery = baseQuery.Where(b => b.WinRate <= filter.WinrateMax.Value);
if (filter.MaxDrawdownMax.HasValue)
baseQuery = baseQuery.Where(b => b.MaxDrawdown <= filter.MaxDrawdownMax.Value);
if (filter.Tickers != null && filter.Tickers.Any())
{
var tickerArray = filter.Tickers.ToArray();
baseQuery = baseQuery.Where(b => tickerArray.Contains(b.Ticker));
}
if (filter.Indicators != null && filter.Indicators.Any())
{
foreach (var ind in filter.Indicators)
{
var token = "," + ind + ",";
baseQuery = baseQuery.Where(b => ("," + b.IndicatorsCsv + ",").Contains(token));
}
}
if (filter.DurationMin.HasValue)
baseQuery = baseQuery.Where(b => b.Duration >= filter.DurationMin.Value);
if (filter.DurationMax.HasValue)
baseQuery = baseQuery.Where(b => b.Duration <= filter.DurationMax.Value);
}
var afterQueryMs = stopwatch.ElapsedMilliseconds;
var totalCount = await baseQuery.CountAsync().ConfigureAwait(false);
var afterCountMs = stopwatch.ElapsedMilliseconds;
// Apply sorting
IQueryable<BacktestEntity> sortedQuery = sortBy.ToLower() switch
IQueryable<BacktestEntity> sortedQuery = sortBy switch
{
"score" => sortOrder == "desc"
BacktestSortableColumn.Score => sortOrder == "desc"
? baseQuery.OrderByDescending(b => b.Score)
: baseQuery.OrderBy(b => b.Score),
"finalpnl" => sortOrder == "desc"
BacktestSortableColumn.FinalPnl => sortOrder == "desc"
? baseQuery.OrderByDescending(b => b.FinalPnl)
: baseQuery.OrderBy(b => b.FinalPnl),
"winrate" => sortOrder == "desc"
BacktestSortableColumn.WinRate => sortOrder == "desc"
? baseQuery.OrderByDescending(b => b.WinRate)
: baseQuery.OrderBy(b => b.WinRate),
"growthpercentage" => sortOrder == "desc"
BacktestSortableColumn.GrowthPercentage => sortOrder == "desc"
? baseQuery.OrderByDescending(b => b.GrowthPercentage)
: baseQuery.OrderBy(b => b.GrowthPercentage),
"hodlpercentage" => sortOrder == "desc"
BacktestSortableColumn.HodlPercentage => sortOrder == "desc"
? baseQuery.OrderByDescending(b => b.HodlPercentage)
: baseQuery.OrderBy(b => b.HodlPercentage),
BacktestSortableColumn.Duration => sortOrder == "desc"
? baseQuery.OrderByDescending(b => b.Duration)
: baseQuery.OrderBy(b => b.Duration),
BacktestSortableColumn.Timeframe => sortOrder == "desc"
? baseQuery.OrderByDescending(b => b.Timeframe)
: baseQuery.OrderBy(b => b.Timeframe),
BacktestSortableColumn.IndicatorsCount => sortOrder == "desc"
? baseQuery.OrderByDescending(b => b.IndicatorsCount)
: baseQuery.OrderBy(b => b.IndicatorsCount),
BacktestSortableColumn.MaxDrawdown => sortOrder == "desc"
? baseQuery.OrderByDescending(b => b.MaxDrawdown)
: baseQuery.OrderBy(b => b.MaxDrawdown),
BacktestSortableColumn.Fees => sortOrder == "desc"
? baseQuery.OrderByDescending(b => b.Fees)
: baseQuery.OrderBy(b => b.Fees),
BacktestSortableColumn.SharpeRatio => sortOrder == "desc"
? baseQuery.OrderByDescending(b => b.SharpeRatio)
: baseQuery.OrderBy(b => b.SharpeRatio),
BacktestSortableColumn.Ticker => sortOrder == "desc"
? baseQuery.OrderByDescending(b => b.Ticker)
: baseQuery.OrderBy(b => b.Ticker),
_ => sortOrder == "desc"
? baseQuery.OrderByDescending(b => b.Score)
: baseQuery.OrderBy(b => b.Score)
@@ -506,15 +622,9 @@ public class PostgreSqlBacktestRepository : IBacktestRepository
HodlPercentage = entity.HodlPercentage,
StartDate = entity.StartDate,
EndDate = entity.EndDate,
MaxDrawdown = !string.IsNullOrEmpty(entity.StatisticsJson)
? JsonConvert.DeserializeObject<PerformanceMetrics>(entity.StatisticsJson)?.MaxDrawdown
: null,
MaxDrawdown = entity.MaxDrawdown,
Fees = entity.Fees,
SharpeRatio = !string.IsNullOrEmpty(entity.StatisticsJson)
? JsonConvert.DeserializeObject<PerformanceMetrics>(entity.StatisticsJson)?.SharpeRatio != null
? (double?)JsonConvert.DeserializeObject<PerformanceMetrics>(entity.StatisticsJson).SharpeRatio
: null
: null,
SharpeRatio = (double?)entity.SharpeRatio,
Score = entity.Score,
ScoreMessage = entity.ScoreMessage ?? string.Empty
});