mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-15 17:18:05 +00:00
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:
@@ -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?
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
[](https://sonarcloud.io/summary/overall?id=mihakralj_QuanTAlib)
|
||||
[](https://app.codacy.com/gh/mihakralj/QuanTAlib/dashboard)
|
||||
[](https://app.codacy.com/gh/mihakralj/QuanTAlib/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
|
||||
|
||||
[](https://codecov.io/gh/mihakralj/QuanTAlib)
|
||||
[](https://sonarcloud.io/summary/new_code?id=mihakralj_QuanTAlib)
|
||||
[](https://www.codefactor.io/repository/github/mihakralj/quantalib/overview/main)
|
||||
@@ -8,7 +8,7 @@
|
||||

|
||||
[](https://www.nuget.org/packages/QuanTAlib/)
|
||||
[](https://github.com/mihakralj/QuanTAlib/watchers)
|
||||
[](https://dotnet.microsoft.com/en-us/download/dotnet)
|
||||
[](https://dotnet.microsoft.com/en-us/download/dotnet)
|
||||
|
||||
# QuanTAlib - Quantitative Technical Analysis Library
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="**\*.Tests.cs" />
|
||||
<Compile Include="**\*.Tests.cs" Exclude="**\*.Quantower.Tests.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
+24
-39
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 -->
|
||||
|
||||
Reference in New Issue
Block a user