feat(rsi, alma, bilateral, blma, butter, ema, htit, kama, lsma, pwma, rma, trima, vidya, wma): enhance calculations with NaN handling and edge case management; improve performance and stability across multiple classes

This commit is contained in:
Miha Kralj
2025-12-25 22:16:07 -08:00
parent 0d077c24d8
commit 86e2934f1b
17 changed files with 203 additions and 71 deletions
+15
View File
@@ -590,6 +590,21 @@ public class EmaTests
Assert.Equal(verifyEma.Last.Value, indicator.Last.Value, 1e-10);
}
[Fact]
public void Ema_Batch_AllNaNs_ReturnsNaN()
{
double[] source = [double.NaN, double.NaN, double.NaN];
double[] output = new double[3];
Ema.Batch(source.AsSpan(), output.AsSpan(), 5);
// Should be all NaNs, not 0s
foreach (var val in output)
{
Assert.True(double.IsNaN(val), $"Expected NaN but got {val}");
}
}
[Fact]
public void Ema_AllModes_ProduceSameResult()
{
+8
View File
@@ -382,6 +382,7 @@ public sealed class Ema : AbstractBase
var state = State.New();
double lastValid = 0;
bool foundValid = false;
// Find first valid value to seed lastValid
for (int k = 0; k < source.Length; k++)
@@ -389,10 +390,17 @@ public sealed class Ema : AbstractBase
if (double.IsFinite(source[k]))
{
lastValid = source[k];
foundValid = true;
break;
}
}
if (!foundValid)
{
output.Fill(double.NaN);
return;
}
CalculateCore(source, output, alpha, ref state, ref lastValid);
}