chore: Update various indicators to improve null handling and code readability

This commit is contained in:
Miha Kralj
2025-12-08 18:02:36 -05:00
parent c2b33a8320
commit 53a2dd2c05
7 changed files with 15 additions and 11 deletions
+3 -1
View File
@@ -3,6 +3,8 @@ version = 1
[[analyzers]] [[analyzers]]
name = "csharp" name = "csharp"
enabled = true enabled = true
[analyzers.meta]
skip_rules = ["CS-R1131"]
[[analyzers]] [[analyzers]]
name = "test-coverage" name = "test-coverage"
@@ -14,4 +16,4 @@ enabled = true
[[transformers]] [[transformers]]
name = "dotnet-format" name = "dotnet-format"
enabled = true enabled = true
+2
View File
@@ -147,6 +147,7 @@ QuanTAlib/
``` ```
Each indicator follows a consistent file pattern: Each indicator follows a consistent file pattern:
- `Indicator.cs` - Core implementation - `Indicator.cs` - Core implementation
- `Indicator.Tests.cs` - Unit tests - `Indicator.Tests.cs` - Unit tests
- `Indicator.Validation.Tests.cs` - Cross-validation with other libraries - `Indicator.Validation.Tests.cs` - Cross-validation with other libraries
@@ -174,6 +175,7 @@ Apache License 2.0 - See [LICENSE](LICENSE) for details.
## Contributing ## Contributing
Contributions welcome! Each indicator should include: Contributions welcome! Each indicator should include:
1. Core implementation with streaming support 1. Core implementation with streaming support
2. Unit tests covering edge cases 2. Unit tests covering edge cases
3. Validation tests against reference libraries 3. Validation tests against reference libraries
+1 -1
View File
@@ -118,7 +118,7 @@ public sealed class Alma : ITValuePublisher
public TSeries Update(TSeries source) public TSeries Update(TSeries source)
{ {
if (source.Count == 0) return new TSeries(new List<long>(), new List<double>()); if (source.Count == 0) return new TSeries([], []);
int len = source.Count; int len = source.Count;
var t = new List<long>(len); var t = new List<long>(len);
+1 -1
View File
@@ -75,7 +75,7 @@ public sealed class Hma : ITValuePublisher
public TSeries Update(TSeries source) public TSeries Update(TSeries source)
{ {
if (source.Count == 0) return new TSeries([], new List<double>()); if (source.Count == 0) return new TSeries([], []);
int len = source.Count; int len = source.Count;
var t = new List<long>(len); var t = new List<long>(len);
+2 -2
View File
@@ -157,7 +157,7 @@ public sealed class Kama : ITValuePublisher
if (er > 1.0) er = 1.0; if (er > 1.0) er = 1.0;
double sc = er * (_fastAlpha - _slowAlpha) + _slowAlpha; double sc = er * (_fastAlpha - _slowAlpha) + _slowAlpha;
sc = sc * sc; sc *= sc;
_kama = _p_kama + sc * (val - _p_kama); _kama = _p_kama + sc * (val - _p_kama);
} }
@@ -169,7 +169,7 @@ public sealed class Kama : ITValuePublisher
public TSeries Update(TSeries source) public TSeries Update(TSeries source)
{ {
if (source.Count == 0) return new TSeries(new List<long>(), new List<double>()); if (source.Count == 0) return new TSeries([], []);
int len = source.Count; int len = source.Count;
var t = new List<long>(len); var t = new List<long>(len);
+2 -2
View File
@@ -172,7 +172,7 @@ public sealed class T3 : ITValuePublisher
public TSeries Update(TSeries source) public TSeries Update(TSeries source)
{ {
if (source.Count == 0) return new TSeries(new List<long>(), new List<double>()); if (source.Count == 0) return new TSeries();
int len = source.Count; int len = source.Count;
var t = new List<long>(len); var t = new List<long>(len);
@@ -269,7 +269,7 @@ public sealed class T3 : ITValuePublisher
double c4 = 1.0 + 3.0 * v + 3.0 * v2 + v3; double c4 = 1.0 + 3.0 * v + 3.0 * v2 + v3;
var p = new Parameters(alpha, c1, c2, c3, c4); var p = new Parameters(alpha, c1, c2, c3, c4);
State state = State.New(); var state = State.New();
double lastValidValue = 0; double lastValidValue = 0;
CalculateCore(source, output, p, ref state, ref lastValidValue); CalculateCore(source, output, p, ref state, ref lastValidValue);
+4 -4
View File
@@ -119,8 +119,8 @@ public static class IndicatorExtensions
public static List<Point> GetSmoothCurvePoints(Indicator indicator, IChartWindowCoordinatesConverter converter, Rectangle clientRect, LineSeries series) public static List<Point> GetSmoothCurvePoints(Indicator indicator, IChartWindowCoordinatesConverter converter, Rectangle clientRect, LineSeries series)
{ {
if (indicator == null) throw new ArgumentNullException(nameof(indicator)); ArgumentNullException.ThrowIfNull(indicator);
if (converter == null) throw new ArgumentNullException(nameof(converter)); ArgumentNullException.ThrowIfNull(converter);
var data = indicator.HistoricalData; var data = indicator.HistoricalData;
if (data == null) return new List<Point>(); if (data == null) return new List<Point>();
@@ -193,8 +193,8 @@ public static class IndicatorExtensions
public static List<(Rectangle Rect, Color Color)> GetHistogramRectangles(Indicator indicator, IChartWindowCoordinatesConverter converter, Rectangle clientRect, LineSeries series) public static List<(Rectangle Rect, Color Color)> GetHistogramRectangles(Indicator indicator, IChartWindowCoordinatesConverter converter, Rectangle clientRect, LineSeries series)
{ {
if (indicator == null) throw new ArgumentNullException(nameof(indicator)); ArgumentNullException.ThrowIfNull(indicator);
if (converter == null) throw new ArgumentNullException(nameof(converter)); ArgumentNullException.ThrowIfNull(converter);
var data = indicator.HistoricalData; var data = indicator.HistoricalData;
if (data == null) return new List<(Rectangle, Color)>(); if (data == null) return new List<(Rectangle, Color)>();