Improve perf for backtests
This commit is contained in:
@@ -28,25 +28,10 @@ public class EmaTrendIndicatorBase : EmaBaseIndicatorBase
|
||||
try
|
||||
{
|
||||
var ema = candles.GetEma(Period.Value).ToList();
|
||||
var emaCandles = MapEmaToCandle(ema, candles.TakeLast(Period.Value));
|
||||
|
||||
if (ema.Count == 0)
|
||||
return null;
|
||||
|
||||
var previousCandle = emaCandles[0];
|
||||
foreach (var currentCandle in emaCandles.Skip(1))
|
||||
{
|
||||
if (currentCandle.Close > (decimal)currentCandle.Ema)
|
||||
{
|
||||
AddSignal(currentCandle, TradeDirection.Long, Confidence.None);
|
||||
}
|
||||
else
|
||||
{
|
||||
AddSignal(currentCandle, TradeDirection.Short, Confidence.None);
|
||||
}
|
||||
|
||||
previousCandle = currentCandle;
|
||||
}
|
||||
ProcessEmaTrendSignals(ema, candles);
|
||||
|
||||
return Signals.OrderBy(s => s.Date).ToList();
|
||||
}
|
||||
@@ -56,6 +41,74 @@ public class EmaTrendIndicatorBase : EmaBaseIndicatorBase
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Runs the indicator using pre-calculated EMA values for performance optimization.
|
||||
/// </summary>
|
||||
public override List<LightSignal> Run(HashSet<Candle> candles, IndicatorsResultBase preCalculatedValues)
|
||||
{
|
||||
if (candles.Count <= 2 * Period)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// Use pre-calculated EMA values if available
|
||||
List<EmaResult> ema = null;
|
||||
if (preCalculatedValues?.Ema != null && preCalculatedValues.Ema.Any())
|
||||
{
|
||||
// Filter pre-calculated EMA values to match the candles we're processing
|
||||
ema = preCalculatedValues.Ema
|
||||
.Where(e => candles.Any(c => c.Date == e.Date))
|
||||
.OrderBy(e => e.Date)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
// If no pre-calculated values or they don't match, fall back to regular calculation
|
||||
if (ema == null || !ema.Any())
|
||||
{
|
||||
return Run(candles);
|
||||
}
|
||||
|
||||
ProcessEmaTrendSignals(ema, candles);
|
||||
|
||||
return Signals.OrderBy(s => s.Date).ToList();
|
||||
}
|
||||
catch (RuleException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Processes EMA trend signals based on price position relative to EMA line.
|
||||
/// This method is shared between the regular Run() and optimized Run() methods.
|
||||
/// </summary>
|
||||
/// <param name="ema">List of EMA calculation results</param>
|
||||
/// <param name="candles">Candles to process</param>
|
||||
private void ProcessEmaTrendSignals(List<EmaResult> ema, HashSet<Candle> candles)
|
||||
{
|
||||
var emaCandles = MapEmaToCandle(ema, candles.TakeLast(Period.Value));
|
||||
|
||||
if (emaCandles.Count == 0)
|
||||
return;
|
||||
|
||||
var previousCandle = emaCandles[0];
|
||||
foreach (var currentCandle in emaCandles.Skip(1))
|
||||
{
|
||||
if (currentCandle.Close > (decimal)currentCandle.Ema)
|
||||
{
|
||||
AddSignal(currentCandle, TradeDirection.Long, Confidence.None);
|
||||
}
|
||||
else
|
||||
{
|
||||
AddSignal(currentCandle, TradeDirection.Short, Confidence.None);
|
||||
}
|
||||
|
||||
previousCandle = currentCandle;
|
||||
}
|
||||
}
|
||||
|
||||
public override IndicatorsResultBase GetIndicatorValues(HashSet<Candle> candles)
|
||||
{
|
||||
return new IndicatorsResultBase()
|
||||
|
||||
Reference in New Issue
Block a user