From 53a2dd2c054a5a3ffff55ee01efc478d5bbf9211 Mon Sep 17 00:00:00 2001 From: Miha Kralj Date: Mon, 8 Dec 2025 18:02:36 -0500 Subject: [PATCH] chore: Update various indicators to improve null handling and code readability --- .deepsource.toml | 4 +++- README.md | 2 ++ lib/trends/alma/Alma.cs | 2 +- lib/trends/hma/Hma.cs | 2 +- lib/trends/kama/Kama.cs | 4 ++-- lib/trends/t3/T3.cs | 4 ++-- quantower/IndicatorExtensions.cs | 8 ++++---- 7 files changed, 15 insertions(+), 11 deletions(-) diff --git a/.deepsource.toml b/.deepsource.toml index 4f96f656..912f993b 100644 --- a/.deepsource.toml +++ b/.deepsource.toml @@ -3,6 +3,8 @@ version = 1 [[analyzers]] name = "csharp" enabled = true + [analyzers.meta] + skip_rules = ["CS-R1131"] [[analyzers]] name = "test-coverage" @@ -14,4 +16,4 @@ enabled = true [[transformers]] name = "dotnet-format" -enabled = true \ No newline at end of file +enabled = true diff --git a/README.md b/README.md index 3c0470a5..b9e080ec 100644 --- a/README.md +++ b/README.md @@ -147,6 +147,7 @@ QuanTAlib/ ``` Each indicator follows a consistent file pattern: + - `Indicator.cs` - Core implementation - `Indicator.Tests.cs` - Unit tests - `Indicator.Validation.Tests.cs` - Cross-validation with other libraries @@ -174,6 +175,7 @@ Apache License 2.0 - See [LICENSE](LICENSE) for details. ## Contributing Contributions welcome! Each indicator should include: + 1. Core implementation with streaming support 2. Unit tests covering edge cases 3. Validation tests against reference libraries diff --git a/lib/trends/alma/Alma.cs b/lib/trends/alma/Alma.cs index 4f87bccf..923068fc 100644 --- a/lib/trends/alma/Alma.cs +++ b/lib/trends/alma/Alma.cs @@ -118,7 +118,7 @@ public sealed class Alma : ITValuePublisher public TSeries Update(TSeries source) { - if (source.Count == 0) return new TSeries(new List(), new List()); + if (source.Count == 0) return new TSeries([], []); int len = source.Count; var t = new List(len); diff --git a/lib/trends/hma/Hma.cs b/lib/trends/hma/Hma.cs index 8dab6a4d..c7e1553f 100644 --- a/lib/trends/hma/Hma.cs +++ b/lib/trends/hma/Hma.cs @@ -75,7 +75,7 @@ public sealed class Hma : ITValuePublisher public TSeries Update(TSeries source) { - if (source.Count == 0) return new TSeries([], new List()); + if (source.Count == 0) return new TSeries([], []); int len = source.Count; var t = new List(len); diff --git a/lib/trends/kama/Kama.cs b/lib/trends/kama/Kama.cs index 192513f2..fd3ed0bc 100644 --- a/lib/trends/kama/Kama.cs +++ b/lib/trends/kama/Kama.cs @@ -157,7 +157,7 @@ public sealed class Kama : ITValuePublisher if (er > 1.0) er = 1.0; double sc = er * (_fastAlpha - _slowAlpha) + _slowAlpha; - sc = sc * sc; + sc *= sc; _kama = _p_kama + sc * (val - _p_kama); } @@ -169,7 +169,7 @@ public sealed class Kama : ITValuePublisher public TSeries Update(TSeries source) { - if (source.Count == 0) return new TSeries(new List(), new List()); + if (source.Count == 0) return new TSeries([], []); int len = source.Count; var t = new List(len); diff --git a/lib/trends/t3/T3.cs b/lib/trends/t3/T3.cs index fe548422..462abf1d 100644 --- a/lib/trends/t3/T3.cs +++ b/lib/trends/t3/T3.cs @@ -172,7 +172,7 @@ public sealed class T3 : ITValuePublisher public TSeries Update(TSeries source) { - if (source.Count == 0) return new TSeries(new List(), new List()); + if (source.Count == 0) return new TSeries(); int len = source.Count; var t = new List(len); @@ -269,7 +269,7 @@ public sealed class T3 : ITValuePublisher double c4 = 1.0 + 3.0 * v + 3.0 * v2 + v3; var p = new Parameters(alpha, c1, c2, c3, c4); - State state = State.New(); + var state = State.New(); double lastValidValue = 0; CalculateCore(source, output, p, ref state, ref lastValidValue); diff --git a/quantower/IndicatorExtensions.cs b/quantower/IndicatorExtensions.cs index b25a4584..b0b92502 100644 --- a/quantower/IndicatorExtensions.cs +++ b/quantower/IndicatorExtensions.cs @@ -119,8 +119,8 @@ public static class IndicatorExtensions public static List GetSmoothCurvePoints(Indicator indicator, IChartWindowCoordinatesConverter converter, Rectangle clientRect, LineSeries series) { - if (indicator == null) throw new ArgumentNullException(nameof(indicator)); - if (converter == null) throw new ArgumentNullException(nameof(converter)); + ArgumentNullException.ThrowIfNull(indicator); + ArgumentNullException.ThrowIfNull(converter); var data = indicator.HistoricalData; if (data == null) return new List(); @@ -193,8 +193,8 @@ public static class IndicatorExtensions public static List<(Rectangle Rect, Color Color)> GetHistogramRectangles(Indicator indicator, IChartWindowCoordinatesConverter converter, Rectangle clientRect, LineSeries series) { - if (indicator == null) throw new ArgumentNullException(nameof(indicator)); - if (converter == null) throw new ArgumentNullException(nameof(converter)); + ArgumentNullException.ThrowIfNull(indicator); + ArgumentNullException.ThrowIfNull(converter); var data = indicator.HistoricalData; if (data == null) return new List<(Rectangle, Color)>();