Add filters and sorting for backtests
This commit is contained in:
@@ -36,6 +36,28 @@ public class BacktestEntity
|
||||
[Column(TypeName = "jsonb")]
|
||||
public string ConfigJson { get; set; } = string.Empty;
|
||||
|
||||
// Denormalized bot/backtest name (e.g., "MyBundleTest #3") for sorting/filtering
|
||||
[Required]
|
||||
[MaxLength(255)]
|
||||
public string Name { get; set; } = string.Empty;
|
||||
|
||||
// Denormalized ticker string for fast filtering/sorting
|
||||
[Required]
|
||||
[MaxLength(32)]
|
||||
public string Ticker { get; set; } = string.Empty;
|
||||
|
||||
// Stored timeframe as enum numeric value for direct sorting/filtering
|
||||
[Required]
|
||||
public int Timeframe { get; set; }
|
||||
|
||||
// Comma-separated indicator types for filtering, e.g., "EMA_CROSS,MACD_CROSS"
|
||||
[Column(TypeName = "text")]
|
||||
public string IndicatorsCsv { get; set; } = string.Empty;
|
||||
|
||||
// Number of indicators used in the scenario for sorting/filtering
|
||||
[Required]
|
||||
public int IndicatorsCount { get; set; }
|
||||
|
||||
[Required]
|
||||
[Column(TypeName = "jsonb")]
|
||||
public string PositionsJson { get; set; } = string.Empty;
|
||||
@@ -50,6 +72,10 @@ public class BacktestEntity
|
||||
[Required]
|
||||
public DateTime EndDate { get; set; }
|
||||
|
||||
// Precomputed for filtering: EndDate - StartDate
|
||||
[Required]
|
||||
public TimeSpan Duration { get; set; }
|
||||
|
||||
[Required]
|
||||
[Column(TypeName = "jsonb")]
|
||||
public string MoneyManagementJson { get; set; } = string.Empty;
|
||||
@@ -63,6 +89,19 @@ public class BacktestEntity
|
||||
[Column(TypeName = "jsonb")]
|
||||
public string? StatisticsJson { get; set; }
|
||||
|
||||
// Extracted metrics for efficient querying/indexing
|
||||
[Required]
|
||||
[Column(TypeName = "decimal(18,8)")]
|
||||
public decimal SharpeRatio { get; set; }
|
||||
|
||||
[Required]
|
||||
[Column(TypeName = "decimal(18,8)")]
|
||||
public decimal MaxDrawdown { get; set; }
|
||||
|
||||
// PostgreSQL maps TimeSpan to interval
|
||||
[Required]
|
||||
public TimeSpan MaxDrawdownRecoveryTime { get; set; }
|
||||
|
||||
[Required]
|
||||
[Column(TypeName = "decimal(18,8)")]
|
||||
public decimal Fees { get; set; }
|
||||
|
||||
@@ -156,10 +156,19 @@ public class ManagingDbContext : DbContext
|
||||
entity.Property(e => e.HodlPercentage).HasColumnType("decimal(18,8)");
|
||||
entity.Property(e => e.Fees).HasColumnType("decimal(18,8)");
|
||||
entity.Property(e => e.ConfigJson).HasColumnType("jsonb");
|
||||
entity.Property(e => e.Name).IsRequired().HasMaxLength(255);
|
||||
entity.Property(e => e.Ticker).HasMaxLength(32);
|
||||
entity.Property(e => e.Timeframe).IsRequired();
|
||||
entity.Property(e => e.IndicatorsCsv).HasColumnType("text");
|
||||
entity.Property(e => e.IndicatorsCount).IsRequired();
|
||||
entity.Property(e => e.PositionsJson).HasColumnType("jsonb");
|
||||
entity.Property(e => e.SignalsJson).HasColumnType("jsonb");
|
||||
entity.Property(e => e.MoneyManagementJson).HasColumnType("jsonb");
|
||||
entity.Property(e => e.StatisticsJson).HasColumnType("jsonb");
|
||||
entity.Property(e => e.SharpeRatio).HasColumnType("decimal(18,8)").HasDefaultValue(0m);
|
||||
entity.Property(e => e.MaxDrawdown).HasColumnType("decimal(18,8)").HasDefaultValue(0m);
|
||||
entity.Property(e => e.MaxDrawdownRecoveryTime).HasDefaultValue(TimeSpan.Zero);
|
||||
entity.Property(e => e.Duration).HasDefaultValue(TimeSpan.Zero);
|
||||
entity.Property(e => e.ScoreMessage).HasMaxLength(1000);
|
||||
entity.Property(e => e.Metadata).HasColumnType("text");
|
||||
|
||||
@@ -177,7 +186,10 @@ public class ManagingDbContext : DbContext
|
||||
|
||||
// Composite indexes for efficient pagination and filtering
|
||||
entity.HasIndex(e => new { e.UserId, e.Score });
|
||||
entity.HasIndex(e => new { e.UserId, e.Name });
|
||||
entity.HasIndex(e => new { e.RequestId, e.Score });
|
||||
entity.HasIndex(e => new { e.UserId, e.Ticker });
|
||||
entity.HasIndex(e => new { e.UserId, e.Timeframe });
|
||||
});
|
||||
|
||||
// Configure BundleBacktestRequest entity
|
||||
|
||||
@@ -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
|
||||
});
|
||||
|
||||
@@ -332,13 +332,22 @@ public static class PostgreSqlMappers
|
||||
GrowthPercentage = backtest.GrowthPercentage,
|
||||
HodlPercentage = backtest.HodlPercentage,
|
||||
ConfigJson = JsonConvert.SerializeObject(backtest.Config),
|
||||
Name = backtest.Config?.Name ?? string.Empty,
|
||||
Ticker = backtest.Config?.Ticker.ToString() ?? string.Empty,
|
||||
Timeframe = (int)backtest.Config.Timeframe,
|
||||
IndicatorsCsv = string.Join(',', backtest.Config.Scenario.Indicators.Select(i => i.Type.ToString())),
|
||||
IndicatorsCount = backtest.Config.Scenario.Indicators.Count,
|
||||
PositionsJson = JsonConvert.SerializeObject(backtest.Positions.Values.ToList()),
|
||||
SignalsJson = JsonConvert.SerializeObject(backtest.Signals.Values.ToList()),
|
||||
StartDate = backtest.StartDate,
|
||||
EndDate = backtest.EndDate,
|
||||
Duration = backtest.EndDate - backtest.StartDate,
|
||||
MoneyManagementJson = JsonConvert.SerializeObject(backtest.Config?.MoneyManagement),
|
||||
UserId = backtest.User?.Id ?? 0,
|
||||
StatisticsJson = backtest.Statistics != null ? JsonConvert.SerializeObject(backtest.Statistics) : null,
|
||||
SharpeRatio = backtest.Statistics?.SharpeRatio ?? 0m,
|
||||
MaxDrawdown = backtest.Statistics?.MaxDrawdown ?? 0m,
|
||||
MaxDrawdownRecoveryTime = backtest.Statistics?.MaxDrawdownRecoveryTime ?? TimeSpan.Zero,
|
||||
Fees = backtest.Fees,
|
||||
Score = backtest.Score,
|
||||
ScoreMessage = backtest.ScoreMessage ?? string.Empty,
|
||||
@@ -976,4 +985,13 @@ public static class PostgreSqlMappers
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private static int? ExtractBundleIndex(string name)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(name)) return null;
|
||||
var hashIndex = name.LastIndexOf('#');
|
||||
if (hashIndex < 0 || hashIndex + 1 >= name.Length) return null;
|
||||
var numberPart = name.Substring(hashIndex + 1).Trim();
|
||||
return int.TryParse(numberPart, out var n) ? n : (int?)null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user