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

@@ -0,0 +1,165 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Managing.Infrastructure.Databases.Migrations
{
/// <inheritdoc />
public partial class BacktestFiltersDenormalization : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<TimeSpan>(
name: "Duration",
table: "Backtests",
type: "interval",
nullable: false,
defaultValue: new TimeSpan(0, 0, 0, 0, 0));
migrationBuilder.AddColumn<int>(
name: "IndicatorsCount",
table: "Backtests",
type: "integer",
nullable: false,
defaultValue: 0);
migrationBuilder.AddColumn<string>(
name: "IndicatorsCsv",
table: "Backtests",
type: "text",
nullable: false,
defaultValue: "");
migrationBuilder.AddColumn<decimal>(
name: "MaxDrawdown",
table: "Backtests",
type: "numeric(18,8)",
nullable: false,
defaultValue: 0m);
migrationBuilder.AddColumn<TimeSpan>(
name: "MaxDrawdownRecoveryTime",
table: "Backtests",
type: "interval",
nullable: false,
defaultValue: new TimeSpan(0, 0, 0, 0, 0));
migrationBuilder.AddColumn<string>(
name: "Name",
table: "Backtests",
type: "character varying(255)",
maxLength: 255,
nullable: false,
defaultValue: "");
migrationBuilder.AddColumn<decimal>(
name: "SharpeRatio",
table: "Backtests",
type: "numeric(18,8)",
nullable: false,
defaultValue: 0m);
migrationBuilder.AddColumn<string>(
name: "Ticker",
table: "Backtests",
type: "character varying(32)",
maxLength: 32,
nullable: false,
defaultValue: "");
migrationBuilder.AddColumn<int>(
name: "Timeframe",
table: "Backtests",
type: "integer",
nullable: false,
defaultValue: 0);
migrationBuilder.CreateIndex(
name: "IX_Backtests_UserId_IndicatorsCount",
table: "Backtests",
columns: new[] { "UserId", "IndicatorsCount" });
migrationBuilder.CreateIndex(
name: "IX_Backtests_UserId_IndicatorsCsv",
table: "Backtests",
columns: new[] { "UserId", "IndicatorsCsv" });
migrationBuilder.CreateIndex(
name: "IX_Backtests_UserId_Name",
table: "Backtests",
columns: new[] { "UserId", "Name" });
migrationBuilder.CreateIndex(
name: "IX_Backtests_UserId_Ticker",
table: "Backtests",
columns: new[] { "UserId", "Ticker" });
migrationBuilder.CreateIndex(
name: "IX_Backtests_UserId_Timeframe",
table: "Backtests",
columns: new[] { "UserId", "Timeframe" });
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropIndex(
name: "IX_Backtests_UserId_IndicatorsCount",
table: "Backtests");
migrationBuilder.DropIndex(
name: "IX_Backtests_UserId_IndicatorsCsv",
table: "Backtests");
migrationBuilder.DropIndex(
name: "IX_Backtests_UserId_Name",
table: "Backtests");
migrationBuilder.DropIndex(
name: "IX_Backtests_UserId_Ticker",
table: "Backtests");
migrationBuilder.DropIndex(
name: "IX_Backtests_UserId_Timeframe",
table: "Backtests");
migrationBuilder.DropColumn(
name: "Duration",
table: "Backtests");
migrationBuilder.DropColumn(
name: "IndicatorsCount",
table: "Backtests");
migrationBuilder.DropColumn(
name: "IndicatorsCsv",
table: "Backtests");
migrationBuilder.DropColumn(
name: "MaxDrawdown",
table: "Backtests");
migrationBuilder.DropColumn(
name: "MaxDrawdownRecoveryTime",
table: "Backtests");
migrationBuilder.DropColumn(
name: "Name",
table: "Backtests");
migrationBuilder.DropColumn(
name: "SharpeRatio",
table: "Backtests");
migrationBuilder.DropColumn(
name: "Ticker",
table: "Backtests");
migrationBuilder.DropColumn(
name: "Timeframe",
table: "Backtests");
}
}
}

View File

@@ -0,0 +1,36 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Managing.Infrastructure.Databases.Migrations
{
/// <inheritdoc />
public partial class RemoveIndicatorIndexes : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropIndex(
name: "IX_Backtests_UserId_IndicatorsCount",
table: "Backtests");
migrationBuilder.DropIndex(
name: "IX_Backtests_UserId_IndicatorsCsv",
table: "Backtests");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateIndex(
name: "IX_Backtests_UserId_IndicatorsCount",
table: "Backtests",
columns: new[] { "UserId", "IndicatorsCount" });
migrationBuilder.CreateIndex(
name: "IX_Backtests_UserId_IndicatorsCsv",
table: "Backtests",
columns: new[] { "UserId", "IndicatorsCsv" });
}
}
}

View File

@@ -157,6 +157,11 @@ namespace Managing.Infrastructure.Databases.Migrations
b.Property<DateTime>("CreatedAt")
.HasColumnType("timestamp with time zone");
b.Property<TimeSpan>("Duration")
.ValueGeneratedOnAdd()
.HasColumnType("interval")
.HasDefaultValue(new TimeSpan(0, 0, 0, 0, 0));
b.Property<DateTime>("EndDate")
.HasColumnType("timestamp with time zone");
@@ -177,6 +182,23 @@ namespace Managing.Infrastructure.Databases.Migrations
.HasMaxLength(255)
.HasColumnType("character varying(255)");
b.Property<int>("IndicatorsCount")
.HasColumnType("integer");
b.Property<string>("IndicatorsCsv")
.IsRequired()
.HasColumnType("text");
b.Property<decimal>("MaxDrawdown")
.ValueGeneratedOnAdd()
.HasColumnType("decimal(18,8)")
.HasDefaultValue(0m);
b.Property<TimeSpan>("MaxDrawdownRecoveryTime")
.ValueGeneratedOnAdd()
.HasColumnType("interval")
.HasDefaultValue(new TimeSpan(0, 0, 0, 0, 0));
b.Property<string>("Metadata")
.HasColumnType("text");
@@ -184,6 +206,11 @@ namespace Managing.Infrastructure.Databases.Migrations
.IsRequired()
.HasColumnType("jsonb");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("character varying(255)");
b.Property<string>("PositionsJson")
.IsRequired()
.HasColumnType("jsonb");
@@ -200,6 +227,11 @@ namespace Managing.Infrastructure.Databases.Migrations
.HasMaxLength(1000)
.HasColumnType("text");
b.Property<decimal>("SharpeRatio")
.ValueGeneratedOnAdd()
.HasColumnType("decimal(18,8)")
.HasDefaultValue(0m);
b.Property<string>("SignalsJson")
.IsRequired()
.HasColumnType("jsonb");
@@ -210,6 +242,14 @@ namespace Managing.Infrastructure.Databases.Migrations
b.Property<string>("StatisticsJson")
.HasColumnType("jsonb");
b.Property<string>("Ticker")
.IsRequired()
.HasMaxLength(32)
.HasColumnType("character varying(32)");
b.Property<int>("Timeframe")
.HasColumnType("integer");
b.Property<DateTime>("UpdatedAt")
.HasColumnType("timestamp with time zone");
@@ -232,8 +272,14 @@ namespace Managing.Infrastructure.Databases.Migrations
b.HasIndex("RequestId", "Score");
b.HasIndex("UserId", "Name");
b.HasIndex("UserId", "Score");
b.HasIndex("UserId", "Ticker");
b.HasIndex("UserId", "Timeframe");
b.ToTable("Backtests");
});

View File

@@ -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; }

View File

@@ -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

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
});

View File

@@ -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;
}
}