Update clamping for backtest

This commit is contained in:
2025-07-18 13:51:10 +07:00
parent bb3891c95e
commit 13aa681c2b
2 changed files with 21 additions and 23 deletions

View File

@@ -364,7 +364,7 @@ namespace Managing.Application.Backtesting
{
try
{
if (backtest.Score > 50)
if (backtest.Score > 60)
{
await _messengerService.SendBacktestNotification(backtest);
}

View File

@@ -422,7 +422,8 @@ public class GeneticService : IGeneticService
}
catch (Exception notificationEx)
{
_logger.LogWarning(notificationEx, "Failed to send genetic algorithm notification for request {RequestId}", request.RequestId);
_logger.LogWarning(notificationEx,
"Failed to send genetic algorithm notification for request {RequestId}", request.RequestId);
}
return new GeneticAlgorithmResult
@@ -531,7 +532,10 @@ public class TradingBotChromosome : ChromosomeBase
return geneIndex switch
{
0 => new Gene(GetRandomInRange((0.9, _maxTakeProfit))), // Take profit (0.9% to max TP)
1 => new Gene(GetRandomInRange(GeneticService.ParameterRanges["stopLoss"])), // Stop loss (will be constrained during initialization)
1 => new Gene(
GetRandomInRange(
GeneticService.ParameterRanges
["stopLoss"])), // Stop loss (will be constrained during initialization)
2 => new Gene(GetRandomIntInRange(GeneticService.ParameterRanges["cooldownPeriod"])), // Cooldown period
3 => new Gene(GetRandomIntInRange(GeneticService.ParameterRanges["maxLossStreak"])), // Max loss streak
_ => new Gene(0)
@@ -614,6 +618,7 @@ public class TradingBotChromosome : ChromosomeBase
{
clone._indicatorSelectionPattern = (int[])_indicatorSelectionPattern.Clone();
}
return clone;
}
@@ -700,15 +705,12 @@ public class TradingBotChromosome : ChromosomeBase
// Get take profit from chromosome (gene 0)
var takeProfit = Convert.ToDouble(genes[0].Value);
// Get stop loss from chromosome (gene 1) and enforce constraints at phenotype level
// Get stop loss from chromosome (gene 1)
var stopLoss = Convert.ToDouble(genes[1].Value);
// Enforce 1.1:1 risk-reward ratio constraint at phenotype level
var minStopLoss = Math.Max(0.2, takeProfit / 1.1); // Minimum 0.2% to cover fees
var maxStopLoss = takeProfit - 0.1; // Ensure SL is less than TP with some buffer
// Clamp stop loss to valid range (this maintains genotype-phenotype mapping)
stopLoss = Math.Max(minStopLoss, Math.Min(maxStopLoss, stopLoss));
// Only enforce minimum stop loss to cover fees, allow any risk-reward ratio above that
var minStopLoss = 0.2; // Minimum 0.2% to cover fees
stopLoss = Math.Max(minStopLoss, stopLoss);
// Get loopback period from gene 4
var loopbackPeriod = Convert.ToInt32(genes[4].Value);
@@ -833,10 +835,8 @@ public class TradingBotChromosome : ChromosomeBase
var takeProfit = GetRandomInRange((0.9, _maxTakeProfit));
ReplaceGene(0, new Gene(takeProfit));
// Generate stop loss with constraints based on take profit (gene 1)
var minStopLoss = Math.Max(0.2, takeProfit / 1.1); // Minimum 0.2% to cover fees
var maxStopLoss = takeProfit - 0.1; // Ensure SL is less than TP with some buffer
var stopLoss = GetRandomInRange((minStopLoss, maxStopLoss));
// Generate stop loss independently (gene 1) - only enforce minimum to cover fees
var stopLoss = GetRandomInRange(GeneticService.ParameterRanges["stopLoss"]);
ReplaceGene(1, new Gene(stopLoss));
// Initialize remaining genes normally
@@ -845,8 +845,6 @@ public class TradingBotChromosome : ChromosomeBase
ReplaceGene(i, GenerateGene(i));
}
}
}
/// <summary>