mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-25 22:08:05 +00:00
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:
@@ -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()
|
||||
{
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user