Update scoring system

This commit is contained in:
2025-07-11 20:58:28 +07:00
parent 8a2b0ba323
commit 754a21da69
6 changed files with 304 additions and 95 deletions

View File

@@ -682,7 +682,13 @@ namespace Managing.Application.Tests
totalPnL: (double)backtestResult.FinalPnl,
fees: (double)backtestResult.Fees,
tradeCount: backtestResult.Positions?.Count ?? 0,
maxDrawdownRecoveryTime: backtestResult.Statistics?.MaxDrawdownRecoveryTime ?? TimeSpan.Zero
maxDrawdownRecoveryTime: backtestResult.Statistics?.MaxDrawdownRecoveryTime ?? TimeSpan.Zero,
maxDrawdown: backtestResult.Statistics?.MaxDrawdown ?? 0,
initialBalance: config.BotTradingBalance,
startDate: backtestResult.StartDate,
endDate: backtestResult.EndDate,
feesPaid: backtestResult.Fees,
timeframe: config.Timeframe
);
var scenarioResult = new ScenarioBacktestResult

View File

@@ -143,7 +143,8 @@ namespace Managing.Application.Backtesting
tradingBot.User = user;
await tradingBot.LoadAccount();
var result = await GetBacktestingResult(config, tradingBot, candles, user, withCandles, requestId, metadata);
var result =
await GetBacktestingResult(config, tradingBot, candles, user, withCandles, requestId, metadata);
if (user != null)
{
@@ -255,7 +256,12 @@ namespace Managing.Application.Backtesting
totalPnL: (double)finalPnl,
fees: (double)fees,
tradeCount: bot.Positions.Count,
maxDrawdownRecoveryTime: stats.MaxDrawdownRecoveryTime
maxDrawdownRecoveryTime: stats.MaxDrawdownRecoveryTime,
maxDrawdown: stats.MaxDrawdown,
initialBalance: config.BotTradingBalance,
startDate: candles[0].Date,
endDate: candles.Last().Date,
timeframe: config.Timeframe
);
var score = BacktestScorer.CalculateTotalScore(scoringParams);

View File

@@ -813,7 +813,7 @@ public class TradingBotFitness : IFitness
).Result;
// Calculate multi-objective fitness based on backtest results
var fitness = CalculateMultiObjectiveFitness(backtest, config);
var fitness = CalculateFitness(backtest, config);
return fitness;
}
@@ -824,38 +824,13 @@ public class TradingBotFitness : IFitness
}
}
private double CalculateMultiObjectiveFitness(Backtest backtest, TradingBotConfig config)
private double CalculateFitness(Backtest backtest, TradingBotConfig config)
{
if (backtest == null || backtest.Statistics == null)
return 0.1;
var stats = backtest.Statistics;
// Multi-objective fitness function (matching frontend)
var pnlScore = Math.Max(0, (double)stats.TotalPnL / 1000); // Normalize PnL
var winRateScore = backtest.WinRate / 100.0; // Normalize win rate
var riskRewardScore =
Math.Min(2, (double)stats.WinningTrades / Math.Max(1, Math.Abs((double)stats.LoosingTrades)));
var consistencyScore = 1 - Math.Abs((double)stats.TotalPnL - (double)backtest.FinalPnl) /
Math.Max(1, Math.Abs((double)stats.TotalPnL));
// Risk-reward ratio bonus
var riskRewardRatio = (double)(config.MoneyManagement.TakeProfit / config.MoneyManagement.StopLoss);
var riskRewardBonus = Math.Min(0.2, (riskRewardRatio - 1.1) * 0.1);
// Drawdown score (normalized to 0-1, where lower drawdown is better)
var maxDrawdownPc = Math.Abs((double)stats.MaxDrawdownPc);
var drawdownScore = Math.Max(0, 1 - (maxDrawdownPc / 50));
// Weighted combination
var fitness =
pnlScore * 0.3 +
winRateScore * 0.2 +
riskRewardScore * 0.2 +
consistencyScore * 0.1 +
riskRewardBonus * 0.1 +
drawdownScore * 0.1;
return Math.Max(0, fitness);
// Use the comprehensive backtest score directly as fitness
// The BacktestScorer already includes all important metrics with proper weighting
return backtest.Score;
}
}

View File

@@ -1,3 +1,5 @@
using static Managing.Common.Enums;
namespace Managing.Domain.Backtests;
public class BacktestScoringParams
@@ -11,6 +13,14 @@ public class BacktestScoringParams
public double Fees { get; }
public int TradeCount { get; }
public TimeSpan MaxDrawdownRecoveryTime { get; }
// New properties for enhanced scoring
public decimal MaxDrawdown { get; }
public decimal InitialBalance { get; }
public DateTime StartDate { get; }
public DateTime EndDate { get; }
public decimal FeesPaid { get; }
public Timeframe Timeframe { get; }
public BacktestScoringParams(
double sharpeRatio,
@@ -21,7 +31,12 @@ public class BacktestScoringParams
double totalPnL,
double fees,
int tradeCount,
TimeSpan maxDrawdownRecoveryTime)
TimeSpan maxDrawdownRecoveryTime,
decimal maxDrawdown = 0,
decimal initialBalance = 0,
DateTime startDate = default,
DateTime endDate = default,
Timeframe timeframe = Timeframe.OneHour)
{
SharpeRatio = sharpeRatio;
MaxDrawdownPc = maxDrawdownPc;
@@ -32,5 +47,10 @@ public class BacktestScoringParams
Fees = fees;
TradeCount = tradeCount;
MaxDrawdownRecoveryTime = maxDrawdownRecoveryTime;
MaxDrawdown = maxDrawdown;
InitialBalance = initialBalance;
StartDate = startDate;
EndDate = endDate;
Timeframe = timeframe;
}
}

View File

@@ -1,25 +1,33 @@
using Managing.Domain.Backtests;
using static Managing.Common.Enums;
public class BacktestScorer
{
// Updated weights without ProfitEfficiency
// Updated weights with more balanced distribution
private static readonly Dictionary<string, double> Weights = new Dictionary<string, double>
{
{ "GrowthPercentage", 0.28 },
{ "SharpeRatio", 0.18 },
{ "MaxDrawdownPc", 0.15 },
{ "GrowthPercentage", 0.25 },
{ "SharpeRatio", 0.15 },
{ "MaxDrawdownUsd", 0.12 },
{ "HodlComparison", 0.05 },
{ "WinRate", 0.18 },
{ "ProfitabilityBonus", 0.11 },
{ "TradeCount", 0.03 },
{ "RecoveryTime", 0.02 }
{ "WinRate", 0.15 },
{ "ProfitabilityBonus", 0.08 },
{ "TradeCount", 0.05 },
{ "RecoveryTime", 0.02 },
{ "TestDuration", 0.03 },
{ "FeesImpact", 0.02 }
};
public static double CalculateTotalScore(BacktestScoringParams p)
{
try
{
// Early exit for no positions
if (p.TradeCount == 0)
{
return 0;
}
var baseScore = CalculateBaseScore(p);
var finalScore = ApplyProfitabilityRules(baseScore, p);
@@ -37,12 +45,14 @@ public class BacktestScorer
{
{ "GrowthPercentage", CalculateGrowthScore(p.GrowthPercentage) },
{ "SharpeRatio", CalculateSharpeScore(p.SharpeRatio) },
{ "MaxDrawdownPc", CalculateDrawdownScore(p.MaxDrawdownPc) },
{ "MaxDrawdownUsd", CalculateDrawdownUsdScore(p.MaxDrawdown, p.InitialBalance) },
{ "HodlComparison", CalculateHodlComparisonScore(p.GrowthPercentage, p.HodlPercentage) },
{ "WinRate", CalculateWinRateScore(p.WinRate, p.TradeCount) },
{ "ProfitabilityBonus", CalculateProfitabilityBonus(p.GrowthPercentage) },
{ "TradeCount", CalculateTradeCountScore(p.TradeCount) },
{ "RecoveryTime", CalculateRecoveryScore(p.MaxDrawdownRecoveryTime) }
{ "RecoveryTime", CalculateRecoveryScore(p.MaxDrawdownRecoveryTime, p.Timeframe) },
{ "TestDuration", CalculateTestDurationScore(p.StartDate, p.EndDate, p.Timeframe) },
{ "FeesImpact", CalculateFeesImpactScore(p.FeesPaid, p.InitialBalance, (decimal)p.TotalPnL) }
};
return componentScores.Sum(kvp => kvp.Value * Weights[kvp.Key]);
@@ -50,58 +60,78 @@ public class BacktestScorer
private static double ApplyProfitabilityRules(double baseScore, BacktestScoringParams p)
{
// 1. Negative PnL Penalty (Core Rule)
var penaltyMultiplier = 1.0;
// 1. Negative PnL Penalty (Dynamic)
if (p.GrowthPercentage < 0)
{
baseScore = Math.Min(baseScore, 70) * GetNegativePnLMultiplier(p.GrowthPercentage);
var negativePenalty = Math.Abs(p.GrowthPercentage) * 0.1; // 10% penalty per 1% loss
penaltyMultiplier *= Math.Max(0.1, 1 - negativePenalty);
}
// 2. Absolute PnL Validation (Additional Recommendation)
// 2. Absolute PnL Validation (Dynamic)
if (p.TotalPnL <= 0)
{
baseScore = Math.Min(baseScore, 50);
penaltyMultiplier *= 0.3; // 70% penalty for negative absolute PnL
}
// 3. Win Rate Validation (Additional Recommendation)
// 3. Win Rate Validation (Dynamic)
if (p.WinRate < 0.3 && p.TradeCount > 10)
{
baseScore = Math.Min(baseScore, 60);
var winRatePenalty = (0.3 - p.WinRate) * 0.5; // 50% penalty per 10% below 30%
penaltyMultiplier *= Math.Max(0.2, 1 - winRatePenalty);
}
// 4. Minimum Profit Threshold (Additional Recommendation)
// 4. Minimum Profit Threshold (Dynamic)
if (p.GrowthPercentage < 2 && p.TradeCount > 5)
{
baseScore = Math.Min(baseScore, 80);
var profitPenalty = (2 - p.GrowthPercentage) * 0.1; // 10% penalty per 1% below 2%
penaltyMultiplier *= Math.Max(0.5, 1 - profitPenalty);
}
return baseScore;
// 5. Drawdown Penalty (Dynamic)
if (p.MaxDrawdownPc > 20)
{
var drawdownPenalty = (p.MaxDrawdownPc - 20) * 0.02; // 2% penalty per 1% above 20%
penaltyMultiplier *= Math.Max(0.3, 1 - drawdownPenalty);
}
// 6. Test Duration Penalty (Dynamic)
var testDurationDays = (p.EndDate - p.StartDate).TotalDays;
if (testDurationDays < 30)
{
var durationPenalty = (30 - testDurationDays) * 0.02; // 2% penalty per day below 30
penaltyMultiplier *= Math.Max(0.5, 1 - durationPenalty);
}
return baseScore * penaltyMultiplier;
}
private static double CalculateGrowthScore(double growthPercentage)
{
// More aggressive penalty for negative growth
// More aggressive scoring - harder to reach 100
if (growthPercentage < 0)
{
return Math.Max(0, 40 + (growthPercentage * 2)); // -10% → 20, -20% → 0
return Math.Max(0, 20 + (growthPercentage * 1.5)); // -10% → 5, -20% → 0
}
// Require minimum 5% growth for full score
// Require minimum 10% growth for full score (increased from 5%)
return growthPercentage switch
{
< 5 => growthPercentage * 15, // 2% → 30, 4% → 60
< 5 => growthPercentage * 8, // 2% → 16, 4% → 32
< 10 => 40 + (growthPercentage - 5) * 12, // 5% → 40, 7% → 64, 9% → 88
_ => 100
};
}
// Existing multiplier calculation
private static double GetNegativePnLMultiplier(double growthPercentage)
{
return growthPercentage switch
{
> -5 => 0.8,
> -10 => 0.6,
> -20 => 0.4,
_ => 0.2
> -5 => 0.6,
> -10 => 0.4,
> -20 => 0.2,
_ => 0.1
};
}
@@ -109,42 +139,49 @@ public class BacktestScorer
{
return growthPercentage switch
{
> 0 => 100 * (1 - 1 / (1 + growthPercentage / 50)), // Diminishing returns
> 0 => 50 * (1 - 1 / (1 + growthPercentage / 30)), // Reduced max bonus to 50
_ => 0
};
}
private static bool IsInactiveStrategy(BacktestScoringParams p)
{
// Detect strategies with no economic value
return (p.GrowthPercentage <= p.HodlPercentage &&
p.TotalPnL <= 0) ||
p.TradeCount < 3;
}
private static double CalculateSharpeScore(double sharpeRatio)
{
return sharpeRatio switch
{
< 0 => 0,
> 3 => 100,
_ => (sharpeRatio / 3) * 100
> 4 => 100, // Increased threshold from 3 to 4
_ => (sharpeRatio / 4) * 100
};
}
private static double CalculateDrawdownScore(double maxDrawdownPc)
private static double CalculateDrawdownUsdScore(decimal maxDrawdown, decimal initialBalance)
{
return maxDrawdownPc switch
if (initialBalance <= 0) return 0;
var drawdownPercentage = (double)(maxDrawdown / initialBalance * 100);
return drawdownPercentage switch
{
> 90 => 0,
_ => 100 - Math.Pow(maxDrawdownPc / 90 * 100, 2) / 100
> 30 => 0, // 30% drawdown in USD = 0 score
_ => 100 - Math.Pow(drawdownPercentage / 30 * 100, 1.5) / 100
};
}
private static double CalculateWinRateScore(double winRate, int tradeCount)
{
// Base win rate score
var baseScore = winRate * 100;
var significanceFactor = Math.Min(1, tradeCount / 100.0);
// Significance factor - more aggressive
var significanceFactor = Math.Min(1, (tradeCount - 5) / 50.0); // Start at 5 trades, full significance at 55 trades
// Additional penalty for very few trades
if (tradeCount < 10)
{
significanceFactor *= 0.5; // 50% penalty for less than 10 trades
}
return baseScore * significanceFactor;
}
@@ -153,31 +190,87 @@ public class BacktestScorer
var difference = strategyGrowth - hodlGrowth;
return difference switch
{
> 0 => 100 - (100 / (1 + difference / 5)),
_ => Math.Max(0, 30 + difference * 3)
> 0 => 80 - (80 / (1 + difference / 3)), // Reduced max to 80
_ => Math.Max(0, 20 + difference * 2) // Reduced base score
};
}
private static double CalculateTradeCountScore(int tradeCount)
{
return Math.Min(100, Math.Max(0, (tradeCount - 10) * 0.5));
}
private static double CalculateRecoveryScore(TimeSpan recoveryTime)
{
var days = recoveryTime.TotalDays;
return days switch
return tradeCount switch
{
< 0 => 100,
> 365 => 0,
_ => 100 - (days / 365 * 100)
< 5 => 0,
< 10 => (tradeCount - 5) * 10, // 5-10 trades: 0-50 points
< 50 => 50 + (tradeCount - 10) * 1.25, // 10-50 trades: 50-100 points
_ => 100
};
}
private static double CalculateRiskAdjustedGrowthScore(double growth, double drawdown)
private static double CalculateRecoveryScore(TimeSpan recoveryTime, Timeframe timeframe)
{
if (drawdown == 0) return 100;
var ratio = growth / drawdown;
return Math.Min(ratio * 10, 100);
var days = recoveryTime.TotalDays;
// Adjust recovery expectations based on timeframe
var maxRecoveryDays = timeframe switch
{
Timeframe.FiveMinutes => 3.0, // 1 week for 5m
Timeframe.FifteenMinutes => 5.0, // 2 weeks for 15m
Timeframe.ThirtyMinutes => 10.0, // 3 weeks for 30m
Timeframe.OneHour => 15.0, // 1 month for 1h
Timeframe.FourHour => 30.0, // 2 months for 4h
Timeframe.OneDay => 90.0, // 6 months for 1d
_ => 30.0 // Default to 1 month
};
if (days < 0) return 100;
if (days > maxRecoveryDays) return 0;
return 100 - (days / maxRecoveryDays * 100);
}
private static double CalculateTestDurationScore(DateTime startDate, DateTime endDate, Timeframe timeframe)
{
var durationDays = (endDate - startDate).TotalDays;
// Adjust minimum test duration based on timeframe
var minTestDays = timeframe switch
{
Timeframe.FiveMinutes => 14.0, // 3 days for 5m
Timeframe.FifteenMinutes => 28.0, // 1 week for 15m
Timeframe.ThirtyMinutes => 56.0, // 2 weeks for 30m
Timeframe.OneHour => 84.0, // 3 weeks for 1h
Timeframe.FourHour => 120.0, // 1 month for 4h
Timeframe.OneDay => 90.0, // 3 months for 1d
_ => 21.0 // Default to 3 weeks
};
var optimalTestDays = minTestDays * 3; // Optimal is 3x minimum
if (durationDays < minTestDays) return 0;
if (durationDays < optimalTestDays) return (durationDays / optimalTestDays) * 100;
return 100;
}
private static double CalculateFeesImpactScore(decimal feesPaid, decimal initialBalance, decimal totalPnL)
{
if (initialBalance <= 0) return 0;
var feesPercentage = (double)(feesPaid / initialBalance * 100);
var pnlPercentage = (double)(totalPnL / initialBalance * 100);
// If fees are higher than PnL, heavy penalty
if (feesPaid > totalPnL && totalPnL > 0)
{
return 0;
}
// Fee efficiency score
var feeEfficiency = feesPercentage switch
{
> 5 => 0, // More than 5% fees = 0
> 2 => 50 - (feesPercentage - 2) * 16.67, // 2-5%: 50-0 points
_ => 100 - feesPercentage * 25 // 0-2%: 100-50 points
};
return feeEfficiency;
}
}