Fix all tests

This commit is contained in:
2025-11-14 04:03:00 +07:00
parent 0831cf2ca0
commit 2548e9b757
21 changed files with 253888 additions and 1948 deletions

View File

@@ -79,6 +79,19 @@ public static class TradingBox
Dictionary<string, LightSignal> previousSignal, IndicatorComboConfig config, int? loopbackPeriod,
Dictionary<IndicatorType, IndicatorsResultBase> preCalculatedIndicatorValues)
{
// Validate required parameters
if (lightScenario == null)
throw new ArgumentNullException(nameof(lightScenario), "Scenario cannot be null");
if (newCandles == null)
throw new ArgumentNullException(nameof(newCandles), "Candles cannot be null");
// Empty candles or no indicators is a valid business case - return null
if (!newCandles.Any() || lightScenario.Indicators == null || !lightScenario.Indicators.Any())
{
return null;
}
var signalOnCandles = new List<LightSignal>();
foreach (var indicator in lightScenario.Indicators)
@@ -174,8 +187,17 @@ public static class TradingBox
{
if (scenario.Indicators.Count == 1)
{
// Only one strategy, return the single signal
return signalOnCandles.Single();
// Only one strategy, return the single signal if it meets minimum confidence
var signal = signalOnCandles.Single();
// Check if signal meets minimum confidence threshold
// None confidence should always be rejected regardless of threshold
if (signal.Confidence == Confidence.None || signal.Confidence < config.MinimumConfidence)
{
return null; // Below minimum confidence threshold or None
}
return signal;
}
// Optimized: Sort only if needed, then convert to HashSet
@@ -224,9 +246,9 @@ public static class TradingBox
// Calculate confidence based on the average confidence of all signals
var averageConfidence = CalculateAverageConfidence(allDirectionalSignals);
if (finalDirection == TradeDirection.None || averageConfidence < config.MinimumConfidence)
if (finalDirection == TradeDirection.None || averageConfidence == Confidence.None || averageConfidence < config.MinimumConfidence)
{
return null; // No valid signal or below minimum confidence
return null; // No valid signal, None confidence, or below minimum confidence
}
// Create composite signal
@@ -258,8 +280,8 @@ public static class TradingBox
var confidenceValues = signals.Select(s => (int)s.Confidence).ToList();
var averageValue = confidenceValues.Average();
// Round to nearest confidence level
var roundedValue = Math.Round(averageValue);
// Floor to be conservative (round down to lower confidence)
var roundedValue = Math.Floor(averageValue);
// Ensure the value is within valid confidence enum range
roundedValue = Math.Max(0, Math.Min(3, roundedValue));
@@ -443,9 +465,17 @@ public static class TradingBox
{
var stopLoss = 0M;
var takeProfit = 0M;
// Filter candles after the position's opening trade was filled, up to the next position
var candlesBeforeNextPosition = candles.Where(c =>
c.Date >= position.Date && c.Date <= (nextPosition == null ? candles.Last().Date : nextPosition.Date))
c.Date >= position.Open.Date && c.Date <= (nextPosition == null ? candles.Last().Date : nextPosition.Open.Date))
.ToList();
// If no candles after position opened, return zeros
if (!candlesBeforeNextPosition.Any())
{
return (0, 0);
}
if (position.OriginDirection == TradeDirection.Long)
{
@@ -467,7 +497,10 @@ public static class TradingBox
private static decimal GetPercentageFromEntry(decimal entry, decimal price)
{
return Math.Abs(100 - ((100 * price) / entry));
// Calculate the percentage difference as a decimal (e.g., 0.10 for 10%)
// Always return positive value (absolute) since we use this for both SL and TP
if (entry == 0) return 0; // Avoid division by zero
return Math.Abs((price - entry) / entry);
}
public static ProfitAndLoss GetProfitAndLoss(Position position, decimal quantity, decimal price, decimal leverage)