Files
QuanTAlib/lib/channels/aberr/tests/Aberr.Validation.Tests.cs
Miha Kralj 060649192f docs: remove C# Implementation Considerations sections, clean up temp scripts, reorganize test files
- Remove 'C# Implementation Considerations' sections from 34 indicator .md files
- Delete 29 temp PowerShell scripts (_fix_mojibake.ps1, _hex_scan.ps1, etc.)
- Move test files into tests/ subdirectories for consistent project structure
- Add trader-focused bullet points to indicator documentation
2026-03-12 12:34:16 -07:00

425 lines
15 KiB
C#

using Xunit.Abstractions;
namespace QuanTAlib.Tests;
/// <summary>
/// Validation tests for Aberr indicator.
/// Note: Skender.Stock.Indicators, TA-Lib, Tulip, and OoplesFinance do not provide
/// Aberr (Aberration Bands) implementation for cross-validation. These tests validate
/// against manual calculations and internal consistency across all API modes.
/// </summary>
public sealed class AberrValidationTests(ITestOutputHelper output) : IDisposable
{
private readonly ValidationTestData _testData = new();
private bool _disposed;
public void Dispose()
{
Dispose(true);
}
private void Dispose(bool disposing)
{
if (_disposed)
{
return;
}
_disposed = true;
if (disposing)
{
_testData?.Dispose();
}
}
[Fact]
public void Validate_ManualCalculation_Period3()
{
// Manual calculation verification (same-bar SMA deviation)
// Values: [100, 110, 120]
// Bar 1: SMA=100, Dev=|100-100|=0, AvgDev=0
// Bar 2: SMA=(100+110)/2=105, Dev2=|110-105|=5, AvgDev=(0+5)/2=2.5
// Bar 3: SMA=(100+110+120)/3=110, Dev3=|120-110|=10, AvgDev=(0+5+10)/3=5.0
var series = new TSeries();
var time = DateTime.UtcNow;
series.Add(new TValue(time, 100));
series.Add(new TValue(time.AddMinutes(1), 110));
series.Add(new TValue(time.AddMinutes(2), 120));
var aberr = new Aberr(3, 2.0);
var (middle, upper, lower) = aberr.Update(series);
// SMA(3) = 110
Assert.Equal(110.0, middle.Last.Value, 1e-10);
// AvgDev = (0 + 5 + 10) / 3 = 5.0
const double expectedAvgDev = 5.0;
double expectedBandWidth = 2.0 * expectedAvgDev;
Assert.Equal(110.0 + expectedBandWidth, upper.Last.Value, 1e-10);
Assert.Equal(110.0 - expectedBandWidth, lower.Last.Value, 1e-10);
output.WriteLine("Aberr manual calculation (period 3) validated successfully");
}
[Fact]
public void Validate_ManualCalculation_Period5()
{
// Manual calculation verification with period 5
// Use simple arithmetic sequence: 100, 110, 120, 130, 140
var series = new TSeries();
var time = DateTime.UtcNow;
double[] values = [100, 110, 120, 130, 140];
for (int i = 0; i < values.Length; i++)
{
series.Add(new TValue(time.AddMinutes(i), values[i]));
}
var aberr = new Aberr(5, 2.0);
var (middle, _, _) = aberr.Update(series);
// SMA(5) = (100 + 110 + 120 + 130 + 140) / 5 = 120
Assert.Equal(120.0, middle.Last.Value, 1e-10);
output.WriteLine("Aberr manual calculation (period 5) validated successfully");
}
[Fact]
public void Validate_Multiplier_Effect()
{
// Verify multiplier affects band width correctly
var series = new TSeries();
var time = DateTime.UtcNow;
for (int i = 0; i < 20; i++)
{
// Oscillating values to create deviation
double value = 100 + (i % 2 == 0 ? 10 : -10);
series.Add(new TValue(time.AddMinutes(i), value));
}
var (middle1, upper1, _) = Aberr.Batch(series, 10, 1.0);
var (middle2, upper2, _) = Aberr.Batch(series, 10, 2.0);
var (middle3, upper3, _) = Aberr.Batch(series, 10, 3.0);
// Middle should be the same regardless of multiplier
Assert.Equal(middle1.Last.Value, middle2.Last.Value, 1e-10);
Assert.Equal(middle2.Last.Value, middle3.Last.Value, 1e-10);
// Band widths should scale linearly with multiplier
double bw1 = upper1.Last.Value - middle1.Last.Value;
double bw2 = upper2.Last.Value - middle2.Last.Value;
double bw3 = upper3.Last.Value - middle3.Last.Value;
Assert.Equal(bw1 * 2.0, bw2, 1e-10);
Assert.Equal(bw1 * 3.0, bw3, 1e-10);
output.WriteLine("Aberr multiplier effect validated successfully");
}
[Fact]
public void Validate_AllModes_Consistency_Batch()
{
int[] periods = [5, 10, 20, 50, 100];
foreach (var period in periods)
{
// Batch mode using instance
var aberr = new Aberr(period, 2.0);
var (qMiddle, qUpper, qLower) = aberr.Update(_testData.Data);
// Static batch
var (sMiddle, sUpper, sLower) = Aberr.Batch(_testData.Data, period, 2.0);
// Verify match
ValidationHelper.VerifySeriesEqual(qMiddle, sMiddle);
ValidationHelper.VerifySeriesEqual(qUpper, sUpper);
ValidationHelper.VerifySeriesEqual(qLower, sLower);
}
output.WriteLine("Aberr Batch modes consistency validated successfully");
}
[Fact]
public void Validate_AllModes_Consistency_Streaming()
{
int[] periods = [5, 10, 20, 50, 100];
foreach (var period in periods)
{
// Streaming mode
var streamingAberr = new Aberr(period, 2.0);
var streamMiddle = new TSeries();
var streamUpper = new TSeries();
var streamLower = new TSeries();
foreach (var item in _testData.Data)
{
streamingAberr.Update(item);
streamMiddle.Add(streamingAberr.Last);
streamUpper.Add(streamingAberr.Upper);
streamLower.Add(streamingAberr.Lower);
}
// Batch mode for comparison
var (batchMiddle, batchUpper, batchLower) = Aberr.Batch(_testData.Data, period, 2.0);
// Verify match
ValidationHelper.VerifySeriesEqual(batchMiddle, streamMiddle);
ValidationHelper.VerifySeriesEqual(batchUpper, streamUpper);
ValidationHelper.VerifySeriesEqual(batchLower, streamLower);
}
output.WriteLine("Aberr Streaming mode consistency validated successfully");
}
[Fact]
public void Validate_AllModes_Consistency_Span()
{
int[] periods = [5, 10, 20, 50, 100];
double[] source = _testData.RawData.ToArray();
foreach (var period in periods)
{
// Span mode
int len = source.Length;
double[] spanMiddle = new double[len];
double[] spanUpper = new double[len];
double[] spanLower = new double[len];
Aberr.Batch(source.AsSpan(), spanMiddle.AsSpan(), spanUpper.AsSpan(), spanLower.AsSpan(),
period, 2.0);
// Batch mode for comparison
var (batchMiddle, batchUpper, batchLower) = Aberr.Batch(_testData.Data, period, 2.0);
// Verify match
for (int i = 0; i < len; i++)
{
Assert.Equal(batchMiddle[i].Value, spanMiddle[i], 9);
Assert.Equal(batchUpper[i].Value, spanUpper[i], 9);
Assert.Equal(batchLower[i].Value, spanLower[i], 9);
}
}
output.WriteLine("Aberr Span mode consistency validated successfully");
}
[Fact]
public void Validate_AllModes_Consistency_Eventing()
{
int[] periods = [5, 10, 20, 50];
foreach (var period in periods)
{
// Eventing mode
var pubSource = new TSeries();
var eventingInd = new Aberr(pubSource, period, 2.0);
var eventMiddle = new TSeries();
var eventUpper = new TSeries();
var eventLower = new TSeries();
foreach (var item in _testData.Data)
{
pubSource.Add(item);
eventMiddle.Add(eventingInd.Last);
eventUpper.Add(eventingInd.Upper);
eventLower.Add(eventingInd.Lower);
}
// Batch mode for comparison
var (batchMiddle, batchUpper, batchLower) = Aberr.Batch(_testData.Data, period, 2.0);
// Verify match
ValidationHelper.VerifySeriesEqual(batchMiddle, eventMiddle);
ValidationHelper.VerifySeriesEqual(batchUpper, eventUpper);
ValidationHelper.VerifySeriesEqual(batchLower, eventLower);
}
output.WriteLine("Aberr Eventing mode consistency validated successfully");
}
[Fact]
public void Validate_Calculate_ReturnsHotIndicator()
{
int[] periods = [5, 10, 20, 50, 100];
foreach (var period in periods)
{
var ((_, _, _), indicator) = Aberr.Calculate(_testData.Data, period, 2.0);
// Verify indicator is hot
Assert.True(indicator.IsHot);
Assert.Equal(period, indicator.WarmupPeriod);
// Note: Indicator state after Prime may not exactly match batch output because
// deviation calculations depend on SMA history. Prime only restores the last
// WarmupPeriod bars, so deviations are calculated differently.
// We verify the indicator is in a valid state for continued streaming.
Assert.True(double.IsFinite(indicator.Last.Value));
Assert.True(double.IsFinite(indicator.Upper.Value));
Assert.True(double.IsFinite(indicator.Lower.Value));
// Verify can continue streaming
var nextValue = new TValue(DateTime.UtcNow.AddDays(1), 100);
indicator.Update(nextValue);
Assert.True(indicator.IsHot);
}
output.WriteLine("Aberr Calculate method validated successfully");
}
[Fact]
public void Validate_LargeDataset_NoOverflow()
{
// Test with the full 5000 bar dataset
var (middle, upper, lower) = Aberr.Batch(_testData.Data, 100, 2.0);
// All outputs should be finite
ValidationHelper.VerifyAllFinite(middle, startIndex: 0);
ValidationHelper.VerifyAllFinite(upper, startIndex: 0);
ValidationHelper.VerifyAllFinite(lower, startIndex: 0);
// Upper should always be >= Middle, Middle should always be >= Lower
for (int i = 100; i < middle.Count; i++)
{
Assert.True(upper[i].Value >= middle[i].Value,
$"Upper ({upper[i].Value}) should be >= Middle ({middle[i].Value}) at index {i}");
Assert.True(middle[i].Value >= lower[i].Value,
$"Middle ({middle[i].Value}) should be >= Lower ({lower[i].Value}) at index {i}");
}
output.WriteLine("Aberr large dataset (5000 bars) validated successfully");
}
[Fact]
public void Validate_BandWidth_IsSymmetric()
{
// Verify that Upper - Middle == Middle - Lower
// This confirms the band width is applied symmetrically
var (middle, upper, lower) = Aberr.Batch(_testData.Data, 20, 2.0);
// After warmup, verify symmetry
for (int i = 20; i < _testData.Data.Count; i++)
{
double upperDiff = upper[i].Value - middle[i].Value;
double lowerDiff = middle[i].Value - lower[i].Value;
Assert.Equal(upperDiff, lowerDiff, 1e-9);
}
output.WriteLine("Aberr band width symmetry validated successfully");
}
[Fact]
public void Validate_Prime_ProducesCorrectState()
{
// Prime with history and verify state matches full calculation
int period = 20;
// Full batch calculation
var (batchMiddle, batchUpper, batchLower) = Aberr.Batch(_testData.Data, period, 2.0);
// Prime indicator with subset and continue
var primedIndicator = new Aberr(period, 2.0);
var subset = new TSeries();
for (int i = 0; i < 100; i++)
{
subset.Add(_testData.Data[i]);
}
primedIndicator.Prime(subset);
// Continue streaming from where Prime left off
for (int i = 100; i < _testData.Data.Count; i++)
{
primedIndicator.Update(_testData.Data[i]);
}
// Final values should match
Assert.Equal(batchMiddle.Last.Value, primedIndicator.Last.Value, 1e-9);
Assert.Equal(batchUpper.Last.Value, primedIndicator.Upper.Value, 1e-9);
Assert.Equal(batchLower.Last.Value, primedIndicator.Lower.Value, 1e-9);
output.WriteLine("Aberr Prime method validated successfully");
}
[Fact]
public void Validate_MiddleBand_MatchesSMA()
{
// Verify the middle band is exactly the SMA
int period = 20;
var aberr = new Aberr(period, 2.0);
var sma = new Sma(period);
var aberrResults = aberr.Update(_testData.Data);
var smaResults = sma.Update(_testData.Data);
// Middle band should match SMA exactly
for (int i = 0; i < _testData.Data.Count; i++)
{
Assert.Equal(smaResults[i].Value, aberrResults.Middle[i].Value, 1e-10);
}
output.WriteLine("Aberr middle band matches SMA validated successfully");
}
[Fact]
public void Validate_DeviationCalculation()
{
// Verify the deviation is calculated as |source - SMA|
int period = 5;
// Use predictable values
var series = new TSeries();
var time = DateTime.UtcNow;
double[] values = [100, 120, 80, 110, 90];
for (int i = 0; i < values.Length; i++)
{
series.Add(new TValue(time.AddMinutes(i), values[i]));
}
var aberr = new Aberr(period, 1.0); // multiplier = 1 for easier verification
var (middle, upper, _) = aberr.Update(series);
// SMA(5) = (100 + 120 + 80 + 110 + 90) / 5 = 100
Assert.Equal(100.0, middle.Last.Value, 1e-10);
// Band width = AvgDeviation (since multiplier = 1)
// The deviations are calculated incrementally, so we verify the final result
double bandWidth = upper.Last.Value - middle.Last.Value;
Assert.True(bandWidth >= 0, "Band width should be non-negative");
Assert.True(double.IsFinite(bandWidth), "Band width should be finite");
output.WriteLine("Aberr deviation calculation validated successfully");
}
[Fact]
public void Validate_Consistency_AcrossPeriods()
{
// Verify behavior is consistent across different periods
int[] periods = [3, 5, 10, 20, 50, 100, 200];
foreach (var period in periods)
{
var (middle, upper, lower) = Aberr.Batch(_testData.Data, period, 2.0);
// All values should be finite
for (int i = 0; i < middle.Count; i++)
{
Assert.True(double.IsFinite(middle[i].Value), $"Middle[{i}] not finite for period {period}");
Assert.True(double.IsFinite(upper[i].Value), $"Upper[{i}] not finite for period {period}");
Assert.True(double.IsFinite(lower[i].Value), $"Lower[{i}] not finite for period {period}");
}
// Upper >= Middle >= Lower (bands are symmetric around middle)
for (int i = period; i < middle.Count; i++)
{
Assert.True(upper[i].Value >= middle[i].Value);
Assert.True(middle[i].Value >= lower[i].Value);
}
}
output.WriteLine($"Aberr consistency across {periods.Length} periods validated successfully");
}
}