Update clamping for backtest
This commit is contained in:
@@ -364,7 +364,7 @@ namespace Managing.Application.Backtesting
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (backtest.Score > 50)
|
if (backtest.Score > 60)
|
||||||
{
|
{
|
||||||
await _messengerService.SendBacktestNotification(backtest);
|
await _messengerService.SendBacktestNotification(backtest);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -422,7 +422,8 @@ public class GeneticService : IGeneticService
|
|||||||
}
|
}
|
||||||
catch (Exception notificationEx)
|
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
|
return new GeneticAlgorithmResult
|
||||||
@@ -531,7 +532,10 @@ public class TradingBotChromosome : ChromosomeBase
|
|||||||
return geneIndex switch
|
return geneIndex switch
|
||||||
{
|
{
|
||||||
0 => new Gene(GetRandomInRange((0.9, _maxTakeProfit))), // Take profit (0.9% to max TP)
|
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
|
2 => new Gene(GetRandomIntInRange(GeneticService.ParameterRanges["cooldownPeriod"])), // Cooldown period
|
||||||
3 => new Gene(GetRandomIntInRange(GeneticService.ParameterRanges["maxLossStreak"])), // Max loss streak
|
3 => new Gene(GetRandomIntInRange(GeneticService.ParameterRanges["maxLossStreak"])), // Max loss streak
|
||||||
_ => new Gene(0)
|
_ => new Gene(0)
|
||||||
@@ -614,6 +618,7 @@ public class TradingBotChromosome : ChromosomeBase
|
|||||||
{
|
{
|
||||||
clone._indicatorSelectionPattern = (int[])_indicatorSelectionPattern.Clone();
|
clone._indicatorSelectionPattern = (int[])_indicatorSelectionPattern.Clone();
|
||||||
}
|
}
|
||||||
|
|
||||||
return clone;
|
return clone;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -700,15 +705,12 @@ public class TradingBotChromosome : ChromosomeBase
|
|||||||
// Get take profit from chromosome (gene 0)
|
// Get take profit from chromosome (gene 0)
|
||||||
var takeProfit = Convert.ToDouble(genes[0].Value);
|
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);
|
var stopLoss = Convert.ToDouble(genes[1].Value);
|
||||||
|
|
||||||
// Enforce 1.1:1 risk-reward ratio constraint at phenotype level
|
// Only enforce minimum stop loss to cover fees, allow any risk-reward ratio above that
|
||||||
var minStopLoss = Math.Max(0.2, takeProfit / 1.1); // Minimum 0.2% to cover fees
|
var minStopLoss = 0.2; // Minimum 0.2% to cover fees
|
||||||
var maxStopLoss = takeProfit - 0.1; // Ensure SL is less than TP with some buffer
|
stopLoss = Math.Max(minStopLoss, stopLoss);
|
||||||
|
|
||||||
// Clamp stop loss to valid range (this maintains genotype-phenotype mapping)
|
|
||||||
stopLoss = Math.Max(minStopLoss, Math.Min(maxStopLoss, stopLoss));
|
|
||||||
|
|
||||||
// Get loopback period from gene 4
|
// Get loopback period from gene 4
|
||||||
var loopbackPeriod = Convert.ToInt32(genes[4].Value);
|
var loopbackPeriod = Convert.ToInt32(genes[4].Value);
|
||||||
@@ -833,10 +835,8 @@ public class TradingBotChromosome : ChromosomeBase
|
|||||||
var takeProfit = GetRandomInRange((0.9, _maxTakeProfit));
|
var takeProfit = GetRandomInRange((0.9, _maxTakeProfit));
|
||||||
ReplaceGene(0, new Gene(takeProfit));
|
ReplaceGene(0, new Gene(takeProfit));
|
||||||
|
|
||||||
// Generate stop loss with constraints based on take profit (gene 1)
|
// Generate stop loss independently (gene 1) - only enforce minimum to cover fees
|
||||||
var minStopLoss = Math.Max(0.2, takeProfit / 1.1); // Minimum 0.2% to cover fees
|
var stopLoss = GetRandomInRange(GeneticService.ParameterRanges["stopLoss"]);
|
||||||
var maxStopLoss = takeProfit - 0.1; // Ensure SL is less than TP with some buffer
|
|
||||||
var stopLoss = GetRandomInRange((minStopLoss, maxStopLoss));
|
|
||||||
ReplaceGene(1, new Gene(stopLoss));
|
ReplaceGene(1, new Gene(stopLoss));
|
||||||
|
|
||||||
// Initialize remaining genes normally
|
// Initialize remaining genes normally
|
||||||
@@ -845,8 +845,6 @@ public class TradingBotChromosome : ChromosomeBase
|
|||||||
ReplaceGene(i, GenerateGene(i));
|
ReplaceGene(i, GenerateGene(i));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user