Improve perf for backtests
This commit is contained in:
@@ -29,38 +29,13 @@ public class SuperTrendIndicatorBase : IndicatorBase
|
||||
|
||||
try
|
||||
{
|
||||
var superTrend = candles.GetSuperTrend(Period.Value, Multiplier.Value).Where(s => s.SuperTrend.HasValue);
|
||||
var superTrendCandle = MapSuperTrendToCandle(superTrend, candles.TakeLast(MinimumHistory));
|
||||
|
||||
if (superTrendCandle.Count == 0)
|
||||
var superTrend = candles.GetSuperTrend(Period.Value, Multiplier.Value)
|
||||
.Where(s => s.SuperTrend.HasValue)
|
||||
.ToList();
|
||||
if (superTrend.Count == 0)
|
||||
return null;
|
||||
|
||||
var previousCandle = superTrendCandle[0];
|
||||
foreach (var currentCandle in superTrendCandle.Skip(1))
|
||||
{
|
||||
// // Short
|
||||
// if (currentCandle.Close < previousCandle.SuperTrend && previousCandle.Close > previousCandle.SuperTrend)
|
||||
// {
|
||||
// AddSignal(currentCandle, TradeDirection.Short, Confidence.Medium);
|
||||
// }
|
||||
//
|
||||
// // Long
|
||||
// if (currentCandle.Close > previousCandle.SuperTrend && previousCandle.Close < previousCandle.SuperTrend)
|
||||
// {
|
||||
// AddSignal(currentCandle, TradeDirection.Long, Confidence.Medium);
|
||||
// }
|
||||
|
||||
if (currentCandle.SuperTrend < currentCandle.Close)
|
||||
{
|
||||
AddSignal(currentCandle, TradeDirection.Long, Confidence.Medium);
|
||||
}
|
||||
else if (currentCandle.SuperTrend > currentCandle.Close)
|
||||
{
|
||||
AddSignal(currentCandle, TradeDirection.Short, Confidence.Medium);
|
||||
}
|
||||
|
||||
previousCandle = currentCandle;
|
||||
}
|
||||
ProcessSuperTrendSignals(superTrend, candles);
|
||||
|
||||
return Signals.Where(s => s.Confidence != Confidence.None).OrderBy(s => s.Date).ToList();
|
||||
}
|
||||
@@ -70,6 +45,74 @@ public class SuperTrendIndicatorBase : IndicatorBase
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Runs the indicator using pre-calculated SuperTrend values for performance optimization.
|
||||
/// </summary>
|
||||
public override List<LightSignal> Run(HashSet<Candle> candles, IndicatorsResultBase preCalculatedValues)
|
||||
{
|
||||
if (candles.Count <= MinimumHistory)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// Use pre-calculated SuperTrend values if available
|
||||
List<SuperTrendResult> superTrend = null;
|
||||
if (preCalculatedValues?.SuperTrend != null && preCalculatedValues.SuperTrend.Any())
|
||||
{
|
||||
// Filter pre-calculated SuperTrend values to match the candles we're processing
|
||||
superTrend = preCalculatedValues.SuperTrend
|
||||
.Where(s => s.SuperTrend.HasValue && candles.Any(c => c.Date == s.Date))
|
||||
.OrderBy(s => s.Date)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
// If no pre-calculated values or they don't match, fall back to regular calculation
|
||||
if (superTrend == null || !superTrend.Any())
|
||||
{
|
||||
return Run(candles);
|
||||
}
|
||||
|
||||
ProcessSuperTrendSignals(superTrend, candles);
|
||||
|
||||
return Signals.Where(s => s.Confidence != Confidence.None).OrderBy(s => s.Date).ToList();
|
||||
}
|
||||
catch (RuleException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Processes SuperTrend signals based on price position relative to SuperTrend line.
|
||||
/// This method is shared between the regular Run() and optimized Run() methods.
|
||||
/// </summary>
|
||||
/// <param name="superTrend">List of SuperTrend calculation results</param>
|
||||
/// <param name="candles">Candles to process</param>
|
||||
private void ProcessSuperTrendSignals(List<SuperTrendResult> superTrend, HashSet<Candle> candles)
|
||||
{
|
||||
var superTrendCandle = MapSuperTrendToCandle(superTrend, candles.TakeLast(MinimumHistory));
|
||||
|
||||
if (superTrendCandle.Count == 0)
|
||||
return;
|
||||
|
||||
var previousCandle = superTrendCandle[0];
|
||||
foreach (var currentCandle in superTrendCandle.Skip(1))
|
||||
{
|
||||
if (currentCandle.SuperTrend < currentCandle.Close)
|
||||
{
|
||||
AddSignal(currentCandle, TradeDirection.Long, Confidence.Medium);
|
||||
}
|
||||
else if (currentCandle.SuperTrend > currentCandle.Close)
|
||||
{
|
||||
AddSignal(currentCandle, TradeDirection.Short, Confidence.Medium);
|
||||
}
|
||||
|
||||
previousCandle = currentCandle;
|
||||
}
|
||||
}
|
||||
|
||||
public override IndicatorsResultBase GetIndicatorValues(HashSet<Candle> candles)
|
||||
{
|
||||
return new IndicatorsResultBase()
|
||||
|
||||
Reference in New Issue
Block a user