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

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