Add unit tests for various indicators and update project file

- Implemented unit tests for the following indicators:
  - KAMA (Kaufman Adaptive Moving Average)
  - SMA (Simple Moving Average)
  - T3 (Tillson T3 Moving Average)
  - TEMA (Triple Exponential Moving Average)
  - TRIMA (Triangular Moving Average)
  - WMA (Weighted Moving Average)

- Each test class includes tests for constructor defaults, history depth, short name, initialization, processing updates, and source type handling.

- Updated the Quantower.Tests.csproj to include all new test files in the lib directory.
This commit is contained in:
Miha Kralj
2025-12-08 11:40:21 -08:00
parent ed5e5c8209
commit c2b33a8320
15 changed files with 33 additions and 46 deletions
+3 -2
View File
@@ -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?
+3 -3
View File
@@ -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
+1 -1
View File
@@ -31,7 +31,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="**\*.Tests.cs" />
<Compile Include="**\*.Tests.cs" Exclude="**\*.Quantower.Tests.cs" />
</ItemGroup>
<ItemGroup>
+24 -39
View File
@@ -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<long>(len);
var v = new List<double>(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<len; i++)
for (int i = 0; i < len; i++)
{
t.Add(source.Times[i]);
v.Add(outputSpan[i]);
@@ -217,14 +214,14 @@ public sealed class Kama : ITValuePublisher
double fastAlpha = 2.0 / (fastPeriod + 1);
double slowAlpha = 2.0 / (slowPeriod + 1);
// We need a buffer for price history to calculate ER
// Size period + 1
int bufSize = period + 1;
Span<double> 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;
}
}
+2 -1
View File
@@ -24,7 +24,8 @@
<!-- Include mock types -->
<Compile Include="Mocks\*.cs" />
<!-- Include test files -->
<Compile Include="*.Tests.cs" />
<Compile Include="**\*.Tests.cs" />
<Compile Include="..\lib\**\*.Quantower.Tests.cs" />
<!-- Include core library types -->
<Compile Include="..\lib\core\**\*.cs" Exclude="..\lib\core\**\*.Tests.cs" />
<!-- Include trends implementations -->