Update scoring system
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user