diff --git a/.clinerules/good-indicator.md b/.clinerules/good-indicator.md index f036c888..fe7ab7ab 100644 --- a/.clinerules/good-indicator.md +++ b/.clinerules/good-indicator.md @@ -24,6 +24,7 @@ Each indicator resides in its own directory such as `lib/trends/`, `lib/indicato | **Validation** | Cross-library validation | `[Name].Validation.Tests.cs` | | **Docs** | User documentation | `[Name].md` | | **Quantower** | Quantower adapter | `[Name].Quantower.cs` | +| **Quantower Tests** | Quantower adapter tests | `[Name].Quantower.Tests.cs` | ## 3. Implementation Rules (`[Name].cs`) @@ -143,7 +144,7 @@ Template structure: ## 7. Checklist for New Indicators -* [ ] **File Structure:** Created all 4 required files? +* [ ] **File Structure:** Created all 6 required files? * [ ] **Constructor:** Validates inputs? Sets `Name`? * [ ] **Update:** Handles `isNew` correctly? Handles `NaN`? O(1)? * [ ] **Static API:** Implemented `Calculate(Span)`? @@ -151,6 +152,6 @@ Template structure: * [ ] **Validation:** Matches external libraries (Skender/TA-Lib)? * [ ] **Docs:** Markdown file created with formula and examples? * [ ] **Quantower:** Adapter created in `[Name].Quantower.cs`? -* [ ] **Quantower Tests:** Adapter tests created in `quantower/[category]/[Name]Indicator.Tests.cs`? +* [ ] **Quantower Tests:** Adapter tests created in `[Name].Quantower.Tests.cs`? * [ ] **Index:** Added to category `_index.md` with link and description? * [ ] **Performance:** No allocations in `Update`? `[SkipLocalsInit]` used? diff --git a/README.md b/README.md index 0adc4512..3c0470a5 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ -[![Lines of Code](https://sonarcloud.io/api/project_badges/measure?project=mihakralj_QuanTAlib&metric=ncloc)](https://sonarcloud.io/summary/overall?id=mihakralj_QuanTAlib) -[![Codacy grade](https://img.shields.io/codacy/grade/b1f9109222234c87bce45f1fd4c63aee?style=flat-square)](https://app.codacy.com/gh/mihakralj/QuanTAlib/dashboard) +[![Codacy grade](https://app.codacy.com/project/badge/Grade/c8be6c08f5514e95b84d37e661a6ec27)](https://app.codacy.com/gh/mihakralj/QuanTAlib/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade) + [![codecov](https://codecov.io/gh/mihakralj/QuanTAlib/branch/main/graph/badge.svg?style=flat-square&token=YNMJRGKMTJ?style=flat-square)](https://codecov.io/gh/mihakralj/QuanTAlib) [![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=mihakralj_QuanTAlib&metric=security_rating)](https://sonarcloud.io/summary/new_code?id=mihakralj_QuanTAlib) [![CodeFactor](https://www.codefactor.io/repository/github/mihakralj/quantalib/badge/main)](https://www.codefactor.io/repository/github/mihakralj/quantalib/overview/main) @@ -8,7 +8,7 @@ ![GitHub last commit](https://img.shields.io/github/last-commit/mihakralj/QuanTAlib) [![Nuget](https://img.shields.io/nuget/dt/QuanTAlib?style=flat-square)](https://www.nuget.org/packages/QuanTAlib/) [![GitHub watchers](https://img.shields.io/github/watchers/mihakralj/QuanTAlib?style=flat-square)](https://github.com/mihakralj/QuanTAlib/watchers) -[![.NET](https://img.shields.io/badge/.NET-8.0%20|%209.0%20|%2010.0-blue?style=flat-square)](https://dotnet.microsoft.com/en-us/download/dotnet) +[![.NET](https://img.shields.io/badge/.NET-8.0%20|%2010.0-blue?style=flat-square)](https://dotnet.microsoft.com/en-us/download/dotnet) # QuanTAlib - Quantitative Technical Analysis Library diff --git a/lib/QuanTAlib.Tests.csproj b/lib/QuanTAlib.Tests.csproj index 7350b73e..d7f0ce66 100644 --- a/lib/QuanTAlib.Tests.csproj +++ b/lib/QuanTAlib.Tests.csproj @@ -31,7 +31,7 @@ - + diff --git a/quantower/trends/AlmaIndicator.Tests.cs b/lib/trends/alma/Alma.Quantower.Tests.cs similarity index 100% rename from quantower/trends/AlmaIndicator.Tests.cs rename to lib/trends/alma/Alma.Quantower.Tests.cs diff --git a/quantower/trends/DemaIndicator.Tests.cs b/lib/trends/dema/Dema.Quantower.Tests.cs similarity index 100% rename from quantower/trends/DemaIndicator.Tests.cs rename to lib/trends/dema/Dema.Quantower.Tests.cs diff --git a/quantower/trends/EmaIndicator.Tests.cs b/lib/trends/ema/Ema.Quantower.Tests.cs similarity index 100% rename from quantower/trends/EmaIndicator.Tests.cs rename to lib/trends/ema/Ema.Quantower.Tests.cs diff --git a/quantower/trends/HmaIndicator.Tests.cs b/lib/trends/hma/Hma.Quantower.Tests.cs similarity index 100% rename from quantower/trends/HmaIndicator.Tests.cs rename to lib/trends/hma/Hma.Quantower.Tests.cs diff --git a/quantower/trends/KamaIndicator.Tests.cs b/lib/trends/kama/Kama.Quantower.Tests.cs similarity index 100% rename from quantower/trends/KamaIndicator.Tests.cs rename to lib/trends/kama/Kama.Quantower.Tests.cs diff --git a/lib/trends/kama/Kama.cs b/lib/trends/kama/Kama.cs index 764d43b1..192513f2 100644 --- a/lib/trends/kama/Kama.cs +++ b/lib/trends/kama/Kama.cs @@ -69,15 +69,15 @@ public sealed class Kama : ITValuePublisher // Buffer needs to hold period + 1 values to calculate Change over 'period' bars // Change = Price[0] - Price[period] _buffer = new RingBuffer(period + 1); - + _fastAlpha = 2.0 / (fastPeriod + 1); _slowAlpha = 2.0 / (slowPeriod + 1); - + Name = $"Kama({period}, {fastPeriod}, {slowPeriod})"; _kama = double.NaN; } - public Kama(ITValuePublisher source, int period = 10, int fastPeriod = 2, int slowPeriod = 30) + public Kama(ITValuePublisher source, int period = 10, int fastPeriod = 2, int slowPeriod = 30) : this(period, fastPeriod, slowPeriod) { source.Pub += (item) => Update(item); @@ -104,14 +104,11 @@ public sealed class Kama : ITValuePublisher _p_kama = _kama; _p_volatilitySum = _volatilitySum; + bool wasFull = _buffer.IsFull; double removed = _buffer.Add(val); - if (_buffer.IsFull) + if (wasFull) { - // removed is the value that fell off (Price[period+1] relative to new state?) - // No, removed is the value that was at index 0 (oldest). - // The new oldest is at index 0. - // diff_out was abs(removed - new_oldest). double diff_out = Math.Abs(removed - _buffer[0]); _lastDiffOut = diff_out; @@ -153,7 +150,7 @@ public sealed class Kama : ITValuePublisher { double change = Math.Abs(_buffer[^1] - _buffer[0]); double volatility = _volatilitySum; - + // Avoid division by zero double er = (volatility > double.Epsilon) ? change / volatility : 0.0; // Cap ER at 1.0 just in case floating point errors push it slightly over @@ -177,14 +174,14 @@ public sealed class Kama : ITValuePublisher int len = source.Count; var t = new List(len); var v = new List(len); - + // Use static Calculate for performance var outputSpan = new double[len]; - Calculate(source.Values, outputSpan, _period, - (int)(2.0/_fastAlpha - 1), (int)(2.0/_slowAlpha - 1)); // Reverse calc periods from alphas? - // Actually better to pass alphas or periods. - // The static method signature should match constructor params. - + Calculate(source.Values, outputSpan, _period, + (int)(2.0 / _fastAlpha - 1), (int)(2.0 / _slowAlpha - 1)); // Reverse calc periods from alphas? + // Actually better to pass alphas or periods. + // The static method signature should match constructor params. + // Wait, I need to pass periods to static method. // fastPeriod = 2/fastAlpha - 1. int fastPeriod = (int)Math.Round(2.0 / _fastAlpha - 1); @@ -192,7 +189,7 @@ public sealed class Kama : ITValuePublisher Calculate(source.Values, outputSpan, _period, fastPeriod, slowPeriod); - for(int i=0; i buffer = bufSize <= 256 ? stackalloc double[bufSize] : new double[bufSize]; int bufferIdx = 0; int count = 0; - + double volatilitySum = 0; double kama = 0; bool kamaInitialized = false; @@ -241,7 +238,7 @@ public sealed class Kama : ITValuePublisher // Add to buffer double removed = buffer[bufferIdx]; buffer[bufferIdx] = val; - + // Update volatility if (count >= 1) { @@ -249,9 +246,9 @@ public sealed class Kama : ITValuePublisher // prev is at bufferIdx-1 (circular) int prevIdx = (bufferIdx - 1 + bufSize) % bufSize; double diff_in = Math.Abs(val - buffer[prevIdx]); - + volatilitySum += diff_in; - + if (count == bufSize) { // diff_out = abs(removed - new_oldest) @@ -281,31 +278,18 @@ public sealed class Kama : ITValuePublisher // Wait, bufferIdx points to where we WILL write next. // So buffer[bufferIdx] is the oldest value (the one that will be overwritten next). // So Change = abs(val - buffer[bufferIdx]) - + double change = 0; - if (count == bufSize) - { - change = Math.Abs(val - buffer[bufferIdx]); - } - else - { - // If not full, oldest is at 0? - // No, we fill 0, 1, 2... - // Oldest is at 0. - // But bufferIdx wraps. - // If count < bufSize, we haven't wrapped yet (except maybe once if count==bufSize?) - // If count < bufSize, bufferIdx is the index of next write. - // Oldest is at 0. - change = Math.Abs(val - buffer[0]); - } + change = (count == bufSize) ? Math.Abs(val - buffer[bufferIdx]) : Math.Abs(val - buffer[0]); + double er = (volatilitySum > double.Epsilon) ? change / volatilitySum : 0.0; if (er > 1.0) er = 1.0; double sc = er * (fastAlpha - slowAlpha) + slowAlpha; - sc = sc * sc; + sc *= sc; - kama = kama + sc * (val - kama); + kama += sc * (val - kama); output[i] = kama; } } @@ -320,5 +304,6 @@ public sealed class Kama : ITValuePublisher _p_volatilitySum = 0; _lastDiffOut = 0; _lastValidValue = 0; + Last = default; } } diff --git a/quantower/trends/SmaIndicator.Tests.cs b/lib/trends/sma/Sma.Quantower.Tests.cs similarity index 100% rename from quantower/trends/SmaIndicator.Tests.cs rename to lib/trends/sma/Sma.Quantower.Tests.cs diff --git a/quantower/trends/T3Indicator.Tests.cs b/lib/trends/t3/T3.Quantower.Tests.cs similarity index 100% rename from quantower/trends/T3Indicator.Tests.cs rename to lib/trends/t3/T3.Quantower.Tests.cs diff --git a/quantower/trends/TemaIndicator.Tests.cs b/lib/trends/tema/Tema.Quantower.Tests.cs similarity index 100% rename from quantower/trends/TemaIndicator.Tests.cs rename to lib/trends/tema/Tema.Quantower.Tests.cs diff --git a/quantower/trends/TrimaIndicator.Tests.cs b/lib/trends/trima/Trima.Quantower.Tests.cs similarity index 100% rename from quantower/trends/TrimaIndicator.Tests.cs rename to lib/trends/trima/Trima.Quantower.Tests.cs diff --git a/quantower/trends/WmaIndicator.Tests.cs b/lib/trends/wma/Wma.Quantower.Tests.cs similarity index 100% rename from quantower/trends/WmaIndicator.Tests.cs rename to lib/trends/wma/Wma.Quantower.Tests.cs diff --git a/quantower/Quantower.Tests.csproj b/quantower/Quantower.Tests.csproj index 6689f046..cf4609af 100644 --- a/quantower/Quantower.Tests.csproj +++ b/quantower/Quantower.Tests.csproj @@ -24,7 +24,8 @@ - + +