Enhance code quality and stability across various modules

- Updated .coderabbit.yaml to exclude additional file types from reviews, improving the focus on relevant code changes.
- Modified scanner.sh to handle test failures more gracefully, ensuring that analysis stops on test failures and improving logging.
- Improved sonarscanner.sh to ensure build and test failures are properly reported, enhancing CI reliability.
- Refined SimdExtensions.cs documentation for clarity on variance calculation methods.
- Cleaned up TSeries.Tests.cs by simplifying the test structure and ensuring proper namespace usage.
- Fixed potential issues in tseries.cs by ensuring correct handling of DateTime values.
- Enhanced CsvFeed.cs to improve error handling during CSV parsing, ensuring robustness against malformed data.
- Updated GBM.cs to correctly calculate volume in the current bar, ensuring accurate simulation.
- Adjusted index.html to use globalThis for better compatibility across environments.
- Refined quantalib.csproj to exclude unnecessary files from compilation, streamlining the build process.
- Added comprehensive tests for the Mama class to ensure correct behavior during updates and state management.
- Improved error handling in various trend classes (Kama, Dema, Ema, T3, Tema, Wma) to ensure NaN values are managed correctly.
- Removed redundant Mama.Repro.Tests.cs file and consolidated tests into Mama.Tests.cs for better organization.
- Enhanced T3 and Tema classes to maintain state integrity during updates, particularly with NaN values.
This commit is contained in:
Miha Kralj
2025-12-10 14:51:58 -05:00
parent 7a4850956b
commit b26d5d7751
27 changed files with 260 additions and 136 deletions
-16
View File
@@ -1,16 +0,0 @@
namespace QuanTAlib.Tests;
public class MamaReproTests
{
[Fact]
public void Constructor_ThrowsArgumentException_WhenSlowLimitIsZero()
{
Assert.Throws<ArgumentException>(() => new Mama(0.5, 0.0));
}
[Fact]
public void Constructor_ThrowsArgumentException_WhenSlowLimitIsNegative()
{
Assert.Throws<ArgumentException>(() => new Mama(0.5, -0.1));
}
}
+38 -1
View File
@@ -33,7 +33,8 @@ public class MamaTests
var result = mama.Update(input);
Assert.True(double.IsNaN(result.Value));
// Should return 0.0 (last valid price default) instead of NaN to avoid state corruption
Assert.Equal(0.0, result.Value);
}
[Fact]
@@ -62,4 +63,40 @@ public class MamaTests
Assert.True(eventFired);
}
[Fact]
public void Update_Series_AppendsData()
{
var mama1 = new Mama();
var mama2 = new Mama();
var data = new TSeries();
var now = DateTime.UtcNow;
for (int i = 0; i < 50; i++)
{
data.Add(new TValue(now.AddMinutes(i), 100.0 + Math.Sin(i * 0.1) * 10));
}
// Case 1: Update all at once
var result1 = mama1.Update(data);
// Case 2: Update in chunks
var chunk1 = new TSeries();
var chunk2 = new TSeries();
for (int i = 0; i < 25; i++) chunk1.Add(data[i]);
for (int i = 25; i < 50; i++) chunk2.Add(data[i]);
mama2.Update(chunk1);
var result2 = mama2.Update(chunk2);
// Verify final state is same
Assert.Equal(mama1.Last.Value, mama2.Last.Value, 6);
Assert.Equal(mama1.Fama.Value, mama2.Fama.Value, 6);
// Verify the returned series from the second chunk matches the second half of the full result
for (int i = 0; i < 25; i++)
{
Assert.Equal(result1[25 + i].Value, result2[i].Value, 6);
}
}
}
+3 -36
View File
@@ -204,8 +204,8 @@ public sealed class Mama : ITValuePublisher
else
{
// Initialization phase
_sumPr += input.Value;
double avg = _index > 0 ? _sumPr / _index : input.Value;
_sumPr += price;
double avg = _index > 0 ? _sumPr / _index : price;
_mama = avg;
_fama = avg;
@@ -230,47 +230,14 @@ public sealed class Mama : ITValuePublisher
var v = new List<double>(len);
var t = new List<long>(len);
var temp = new Mama(_fastLimit, _slowLimit);
for (int i = 0; i < len; i++)
{
var item = source[i];
var result = temp.Update(item);
var result = Update(item);
v.Add(result.Value);
t.Add(item.Time);
}
// Copy state from temp to this
_period = temp._period;
_p_period = temp._p_period;
_phase = temp._phase;
_p_phase = temp._p_phase;
_mama = temp._mama;
_p_mama = temp._p_mama;
_fama = temp._fama;
_p_fama = temp._p_fama;
_sumPr = temp._sumPr;
_p_sumPr = temp._p_sumPr;
_index = temp._index;
_i2 = temp._i2;
_p_i2 = temp._p_i2;
_q2 = temp._q2;
_p_q2 = temp._p_q2;
_re = temp._re;
_p_re = temp._p_re;
_im = temp._im;
_p_im = temp._p_im;
_lastValidPrice = temp._lastValidPrice;
_priceBuffer.CopyFrom(temp._priceBuffer);
_smoothBuffer.CopyFrom(temp._smoothBuffer);
_detrender.CopyFrom(temp._detrender);
_I1_buffer.CopyFrom(temp._I1_buffer);
_Q1_buffer.CopyFrom(temp._Q1_buffer);
Last = temp.Last;
Fama = temp.Fama;
return new TSeries(t, v);
}