Refactor MAMA and HTIT implementation for improved accuracy and performance

This commit is contained in:
Miha Kralj
2025-12-24 20:50:58 -08:00
parent 8917575994
commit 9ba89812cd
27 changed files with 1030 additions and 547 deletions
+36
View File
@@ -0,0 +1,36 @@
using System;
using Xunit;
using QuanTAlib;
namespace QuanTAlib.Tests;
public class SmaZeroDivTests
{
[Fact]
public void Sma_Update_WithIsNewFalse_OnEmptyBuffer_DoesNotThrow()
{
var sma = new Sma(10);
// Buffer is empty initially.
// Calling Update with isNew=false should not cause division by zero.
// It should return NaN or 0 or Last, but definitely not throw or return Infinity.
var result = sma.Update(new TValue(DateTime.UtcNow, 100), isNew: false);
// Since buffer count is 0, we expect NaN based on our fix.
Assert.True(double.IsNaN(result.Value), $"Expected NaN but got {result.Value}");
}
[Fact]
public void Sma_Update_WithIsNewFalse_AfterReset_DoesNotThrow()
{
var sma = new Sma(10);
sma.Update(new TValue(DateTime.UtcNow, 100));
sma.Reset();
// Buffer is empty after Reset.
var result = sma.Update(new TValue(DateTime.UtcNow, 200), isNew: false);
Assert.True(double.IsNaN(result.Value), $"Expected NaN but got {result.Value}");
}
}
+1 -1
View File
@@ -196,7 +196,7 @@ public sealed class Sma : AbstractBase
_buffer.UpdateNewest(val);
}
double result = _state.Sum / _buffer.Count;
double result = _buffer.Count > 0 ? _state.Sum / _buffer.Count : double.NaN;
Last = new TValue(input.Time, result);
PubEvent(Last);
return Last;