Refactor signal generation in TradingService to process indicators individually, improving clarity and performance. Add new API types for refining indicators, including request and response structures.

This commit is contained in:
2025-12-28 20:59:11 +07:00
parent 4f3ec31501
commit 8a7addafd7
4 changed files with 110 additions and 32 deletions

View File

@@ -580,7 +580,7 @@ public class TradingService : ITradingService
}
/// <summary>
/// Generates signals for a date range using a rolling window approach.
/// Generates signals for a date range by running each indicator individually and collecting all signals.
/// </summary>
private List<LightSignal> GenerateSignalsForDateRange(
List<Candle> candles,
@@ -588,47 +588,47 @@ public class TradingService : ITradingService
Dictionary<IndicatorType, IndicatorsResultBase> preCalculatedIndicatorValues)
{
var allSignals = new List<LightSignal>();
var previousSignals = new Dictionary<string, LightSignal>();
var lightScenario = LightScenario.FromScenario(scenario);
// Use rolling window approach similar to backtests
const int RollingWindowSize = 600;
var rollingWindowCandles = new List<Candle>(RollingWindowSize);
foreach (var candle in candles)
// Process each indicator individually to get all signals from each
foreach (var lightIndicator in lightScenario.Indicators)
{
// Maintain rolling window
if (rollingWindowCandles.Count >= RollingWindowSize)
{
rollingWindowCandles.RemoveAt(0);
}
rollingWindowCandles.Add(candle);
// Build the indicator instance
var indicatorInstance = lightIndicator.ToInterface();
// Only process if we have enough candles for indicators
if (rollingWindowCandles.Count < 2)
// Use pre-calculated indicator values if available
List<LightSignal> indicatorSignals;
if (preCalculatedIndicatorValues != null && preCalculatedIndicatorValues.ContainsKey(lightIndicator.Type))
{
continue;
// Use pre-calculated values to avoid recalculating indicators
indicatorSignals = indicatorInstance.Run(candles, preCalculatedIndicatorValues[lightIndicator.Type]);
}
else
{
// Normal path: calculate indicators on the fly
indicatorSignals = indicatorInstance.Run(candles);
}
// Generate signal for current position
var signal = TradingBox.GetSignal(
rollingWindowCandles,
lightScenario,
previousSignals,
scenario.LookbackPeriod,
preCalculatedIndicatorValues);
if (signal != null)
// Add all signals from this indicator to the collection
if (indicatorSignals != null && indicatorSignals.Count > 0)
{
// Check if this is a new signal (not already in our collection)
if (!previousSignals.ContainsKey(signal.Identifier))
{
allSignals.Add(signal);
previousSignals[signal.Identifier] = signal;
}
// Filter signals to only include those within the candle date range
var firstCandleDate = candles.First().Date;
var lastCandleDate = candles.Last().Date;
var filteredSignals = indicatorSignals
.Where(s => s.Date >= firstCandleDate && s.Date <= lastCandleDate)
.ToList();
allSignals.AddRange(filteredSignals);
}
}
return allSignals.OrderBy(s => s.Date).ToList();
// Remove duplicates based on identifier and sort by date
return allSignals
.GroupBy(s => s.Identifier)
.Select(g => g.First())
.OrderBy(s => s.Date)
.ToList();
}
}