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
This commit is contained in:
Miha Kralj
2026-03-12 12:34:16 -07:00
parent 8937b0c0fa
commit 060649192f
1149 changed files with 1780 additions and 3316 deletions
@@ -0,0 +1,192 @@
using TradingPlatform.BusinessLayer;
namespace QuanTAlib.Tests;
public class AfirmaIndicatorTests
{
[Fact]
public void AfirmaIndicator_Constructor_SetsDefaults()
{
var indicator = new AfirmaIndicator();
Assert.Equal(10, indicator.Period);
Assert.Equal(Afirma.WindowType.BlackmanHarris, indicator.Window);
Assert.Equal(SourceType.Close, indicator.Source);
Assert.True(indicator.ShowColdValues);
Assert.Equal("AFIRMA - Autoregressive FIR Moving Average", indicator.Name);
Assert.False(indicator.SeparateWindow);
Assert.True(indicator.OnBackGround);
}
[Fact]
public void AfirmaIndicator_MinHistoryDepths_EqualsZero()
{
var indicator = new AfirmaIndicator { Period = 20 };
Assert.Equal(0, AfirmaIndicator.MinHistoryDepths);
Assert.Equal(0, ((IWatchlistIndicator)indicator).MinHistoryDepths);
}
[Fact]
public void AfirmaIndicator_ShortName_IncludesParameters()
{
var indicator = new AfirmaIndicator { Period = 15 };
Assert.Contains("AFIRMA", indicator.ShortName, StringComparison.Ordinal);
Assert.Contains("15", indicator.ShortName, StringComparison.Ordinal);
}
[Fact]
public void AfirmaIndicator_Initialize_CreatesInternalAfirma()
{
var indicator = new AfirmaIndicator { Period = 10 };
// Initialize should not throw
indicator.Initialize();
// After init, line series should exist
Assert.Single(indicator.LinesSeries);
}
[Fact]
public void AfirmaIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
{
var indicator = new AfirmaIndicator { Period = 5 };
indicator.Initialize();
// Add historical data
var now = DateTime.UtcNow;
indicator.HistoricalData.AddBar(now, 100, 105, 95, 102);
// Process update
var args = new UpdateArgs(UpdateReason.HistoricalBar);
indicator.ProcessUpdate(args);
// Line series should have a value
Assert.Equal(1, indicator.LinesSeries[0].Count);
Assert.True(double.IsFinite(indicator.LinesSeries[0].GetValue(0)));
}
[Fact]
public void AfirmaIndicator_ProcessUpdate_NewBar_ComputesValue()
{
var indicator = new AfirmaIndicator { Period = 5 };
indicator.Initialize();
var now = DateTime.UtcNow;
indicator.HistoricalData.AddBar(now, 100, 105, 95, 102);
indicator.HistoricalData.AddBar(now.AddMinutes(1), 102, 108, 100, 106);
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewBar));
Assert.Equal(2, indicator.LinesSeries[0].Count);
}
[Fact]
public void AfirmaIndicator_ProcessUpdate_NewTick_ProcessesWithoutError()
{
var indicator = new AfirmaIndicator { Period = 5 };
indicator.Initialize();
var now = DateTime.UtcNow;
indicator.HistoricalData.AddBar(now, 100, 105, 95, 102);
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
double firstValue = indicator.LinesSeries[0].GetValue(0);
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewTick));
double secondValue = indicator.LinesSeries[0].GetValue(0);
Assert.True(double.IsFinite(firstValue));
Assert.True(double.IsFinite(secondValue));
}
[Fact]
public void AfirmaIndicator_MultipleUpdates_ProducesCorrectSequence()
{
var indicator = new AfirmaIndicator { Period = 5 };
indicator.Initialize();
var now = DateTime.UtcNow;
double[] closes = { 100, 102, 104, 103, 105, 107, 106, 108 };
foreach (var close in closes)
{
indicator.HistoricalData.AddBar(now, close, close + 2, close - 2, close);
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
now = now.AddMinutes(1);
}
// All values should be finite
for (int i = 0; i < closes.Length; i++)
{
Assert.True(double.IsFinite(indicator.LinesSeries[0].GetValue(closes.Length - 1 - i)));
}
}
[Fact]
public void AfirmaIndicator_DifferentSourceTypes_Work()
{
var sources = new[] { SourceType.Open, SourceType.High, SourceType.Low, SourceType.Close, SourceType.HL2, SourceType.HLC3 };
foreach (var source in sources)
{
var indicator = new AfirmaIndicator { Period = 5, Source = source };
indicator.Initialize();
var now = DateTime.UtcNow;
indicator.HistoricalData.AddBar(now, 100, 110, 90, 105);
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
Assert.True(double.IsFinite(indicator.LinesSeries[0].GetValue(0)),
$"Source {source} should produce finite value");
}
}
[Fact]
public void AfirmaIndicator_DifferentWindowTypes_Work()
{
var windows = new[]
{
Afirma.WindowType.Rectangular,
Afirma.WindowType.Hanning,
Afirma.WindowType.Hamming,
Afirma.WindowType.Blackman,
Afirma.WindowType.BlackmanHarris,
};
foreach (var window in windows)
{
var indicator = new AfirmaIndicator { Period = 5, Window = window };
indicator.Initialize();
var now = DateTime.UtcNow;
indicator.HistoricalData.AddBar(now, 100, 110, 90, 105);
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
Assert.True(double.IsFinite(indicator.LinesSeries[0].GetValue(0)),
$"Window {window} should produce finite value");
}
}
[Fact]
public void AfirmaIndicator_Period_CanBeChanged()
{
var indicator = new AfirmaIndicator { Period = 5 };
Assert.Equal(5, indicator.Period);
indicator.Period = 20;
Assert.Equal(20, indicator.Period);
}
[Fact]
public void AfirmaIndicator_Window_CanBeChanged()
{
var indicator = new AfirmaIndicator { Window = Afirma.WindowType.Hanning };
Assert.Equal(Afirma.WindowType.Hanning, indicator.Window);
indicator.Window = Afirma.WindowType.Blackman;
Assert.Equal(Afirma.WindowType.Blackman, indicator.Window);
}
}
+656
View File
@@ -0,0 +1,656 @@
namespace QuanTAlib.Tests;
public class AfirmaTests
{
[Fact]
public void Afirma_Constructor_ValidatesInput()
{
Assert.Throws<ArgumentException>(() => new Afirma(0));
Assert.Throws<ArgumentException>(() => new Afirma(-1));
var afirma = new Afirma(10);
Assert.NotNull(afirma);
}
[Fact]
public void Afirma_Constructor_AcceptsValidParameters()
{
var afirma1 = new Afirma(1);
Assert.NotNull(afirma1);
var afirma2 = new Afirma(10, Afirma.WindowType.Blackman);
Assert.NotNull(afirma2);
var afirma3 = new Afirma(5, Afirma.WindowType.Rectangular);
Assert.NotNull(afirma3);
var afirma4 = new Afirma(10, Afirma.WindowType.BlackmanHarris, leastSquares: true);
Assert.NotNull(afirma4);
}
[Fact]
public void Afirma_Calc_ReturnsValue()
{
var afirma = new Afirma(10);
Assert.Equal(0, afirma.Last.Value);
TValue result = afirma.Update(new TValue(DateTime.UtcNow, 100));
Assert.True(result.Value > 0);
Assert.Equal(result.Value, afirma.Last.Value);
}
[Fact]
public void Afirma_FirstValue_ReturnsValue()
{
var afirma = new Afirma(10);
TValue result = afirma.Update(new TValue(DateTime.UtcNow, 100));
// First value should be based on the single input
Assert.True(double.IsFinite(result.Value));
Assert.True(result.Value > 0);
}
[Fact]
public void Afirma_LeastSquares_AffectsResult()
{
// Generate trend data where LS regression should differ from raw window
var gbm = new GBM(startPrice: 100.0, mu: 0.05, sigma: 0.01, seed: 42);
var data = new List<TValue>();
for (int i = 0; i < 20; i++)
{
var bar = gbm.Next(isNew: true);
data.Add(new TValue(bar.Time, bar.Close));
}
var afirmaDefault = new Afirma(10, Afirma.WindowType.BlackmanHarris, leastSquares: false);
var afirmaLS = new Afirma(10, Afirma.WindowType.BlackmanHarris, leastSquares: true);
double lastDefault = 0;
double lastLS = 0;
foreach (var item in data)
{
lastDefault = afirmaDefault.Update(item).Value;
lastLS = afirmaLS.Update(item).Value;
}
// They should be different
Assert.NotEqual(lastDefault, lastLS, 1e-6);
Assert.True(double.IsFinite(lastLS));
}
[Fact]
public void Afirma_LeastSquares_HandlesNaN()
{
var afirma = new Afirma(10, Afirma.WindowType.BlackmanHarris, leastSquares: true);
afirma.Update(new TValue(DateTime.UtcNow, 100));
afirma.Update(new TValue(DateTime.UtcNow, 110));
// Feed NaN - should handle gracefully (typically carries forward last valid or handles via regression on existing points)
var result = afirma.Update(new TValue(DateTime.UtcNow, double.NaN));
Assert.True(double.IsFinite(result.Value));
}
[Fact]
public void Afirma_Calc_IsNew_AcceptsParameter()
{
var afirma = new Afirma(10);
afirma.Update(new TValue(DateTime.UtcNow, 100), isNew: true);
double value1 = afirma.Last.Value;
afirma.Update(new TValue(DateTime.UtcNow, 200), isNew: true);
double value2 = afirma.Last.Value;
// Values should change with new bars
Assert.NotEqual(value1, value2);
}
[Fact]
public void Afirma_Calc_IsNew_False_UpdatesValue()
{
var afirma = new Afirma(10);
afirma.Update(new TValue(DateTime.UtcNow, 100));
afirma.Update(new TValue(DateTime.UtcNow, 110), isNew: true);
double beforeUpdate = afirma.Last.Value;
afirma.Update(new TValue(DateTime.UtcNow, 120), isNew: false);
double afterUpdate = afirma.Last.Value;
// Update should change the value
Assert.NotEqual(beforeUpdate, afterUpdate);
}
[Fact]
public void Afirma_Reset_ClearsState()
{
var afirma = new Afirma(10);
afirma.Update(new TValue(DateTime.UtcNow, 100));
afirma.Update(new TValue(DateTime.UtcNow, 105));
double valueBefore = afirma.Last.Value;
afirma.Reset();
Assert.Equal(0, afirma.Last.Value);
// After reset, should accept new values
afirma.Update(new TValue(DateTime.UtcNow, 50));
Assert.NotEqual(0, afirma.Last.Value);
Assert.NotEqual(valueBefore, afirma.Last.Value);
}
[Fact]
public void Afirma_Properties_Accessible()
{
var afirma = new Afirma(10);
Assert.Equal(0, afirma.Last.Value);
Assert.False(afirma.IsHot);
afirma.Update(new TValue(DateTime.UtcNow, 100));
Assert.NotEqual(0, afirma.Last.Value);
}
[Fact]
public void Afirma_IsHot_BecomesTrueWhenBufferFull()
{
var afirma = new Afirma(5);
Assert.False(afirma.IsHot);
for (int i = 1; i <= 4; i++)
{
afirma.Update(new TValue(DateTime.UtcNow, i * 10));
Assert.False(afirma.IsHot);
}
afirma.Update(new TValue(DateTime.UtcNow, 50));
Assert.True(afirma.IsHot);
}
[Fact]
public void Afirma_IterativeCorrections_RestoreToOriginalState()
{
var afirma = new Afirma(5);
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.1);
// Feed 10 new values
TValue tenthInput = default;
for (int i = 0; i < 10; i++)
{
var bar = gbm.Next(isNew: true);
tenthInput = new TValue(bar.Time, bar.Close);
afirma.Update(tenthInput, isNew: true);
}
// Remember state after 10 values
double stateAfterTen = afirma.Last.Value;
// Generate 9 corrections with isNew=false (different values)
for (int i = 0; i < 9; i++)
{
var bar = gbm.Next(isNew: false);
afirma.Update(new TValue(bar.Time, bar.Close), isNew: false);
}
// Feed the remembered 10th input again with isNew=false
TValue finalResult = afirma.Update(tenthInput, isNew: false);
// State should match the original state after 10 values
Assert.Equal(stateAfterTen, finalResult.Value, 1e-10);
}
[Fact]
public void Afirma_BatchCalc_MatchesIterativeCalc()
{
var afirmaIterative = new Afirma(10);
var afirmaBatch = new Afirma(10);
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.1);
// Generate data
var series = new TSeries();
for (int i = 0; i < 100; i++)
{
var bar = gbm.Next(isNew: true);
series.Add(bar.Time, bar.Close);
}
Assert.True(series.Count > 0);
// Calculate iteratively
var iterativeResults = new TSeries();
foreach (var item in series)
{
iterativeResults.Add(afirmaIterative.Update(item));
}
// Calculate batch
var batchResults = afirmaBatch.Update(series);
// Compare
Assert.Equal(iterativeResults.Count, batchResults.Count);
for (int i = 0; i < iterativeResults.Count; i++)
{
Assert.Equal(iterativeResults[i].Value, batchResults[i].Value, 1e-10);
Assert.Equal(iterativeResults[i].Time, batchResults[i].Time);
}
}
[Fact]
public void Afirma_NaN_Input_UsesLastValidValue()
{
var afirma = new Afirma(10);
// Feed some valid values
afirma.Update(new TValue(DateTime.UtcNow, 100));
afirma.Update(new TValue(DateTime.UtcNow, 110));
// Feed NaN - should use last valid value
var resultAfterNaN = afirma.Update(new TValue(DateTime.UtcNow, double.NaN));
// Result should be finite (not NaN)
Assert.True(double.IsFinite(resultAfterNaN.Value));
Assert.NotEqual(0, resultAfterNaN.Value);
}
[Fact]
public void Afirma_Infinity_Input_UsesLastValidValue()
{
var afirma = new Afirma(10);
// Feed some valid values
afirma.Update(new TValue(DateTime.UtcNow, 100));
afirma.Update(new TValue(DateTime.UtcNow, 110));
// Feed positive infinity - should use last valid value
var resultAfterPosInf = afirma.Update(new TValue(DateTime.UtcNow, double.PositiveInfinity));
Assert.True(double.IsFinite(resultAfterPosInf.Value));
// Feed negative infinity - should use last valid value
var resultAfterNegInf = afirma.Update(new TValue(DateTime.UtcNow, double.NegativeInfinity));
Assert.True(double.IsFinite(resultAfterNegInf.Value));
}
[Fact]
public void Afirma_MultipleNaN_ContinuesWithLastValid()
{
var afirma = new Afirma(10);
// Feed valid values
afirma.Update(new TValue(DateTime.UtcNow, 100));
afirma.Update(new TValue(DateTime.UtcNow, 110));
afirma.Update(new TValue(DateTime.UtcNow, 120));
// Feed multiple NaN values
var r1 = afirma.Update(new TValue(DateTime.UtcNow, double.NaN));
var r2 = afirma.Update(new TValue(DateTime.UtcNow, double.NaN));
var r3 = afirma.Update(new TValue(DateTime.UtcNow, double.NaN));
// All results should be finite
Assert.True(double.IsFinite(r1.Value));
Assert.True(double.IsFinite(r2.Value));
Assert.True(double.IsFinite(r3.Value));
}
[Fact]
public void Afirma_BatchCalc_HandlesNaN()
{
var afirma = new Afirma(10);
// Create series with NaN values interspersed
var series = new TSeries();
series.Add(DateTime.UtcNow.Ticks, 100);
series.Add(DateTime.UtcNow.Ticks + 1, 110);
series.Add(DateTime.UtcNow.Ticks + 2, double.NaN);
series.Add(DateTime.UtcNow.Ticks + 3, 120);
series.Add(DateTime.UtcNow.Ticks + 4, double.PositiveInfinity);
series.Add(DateTime.UtcNow.Ticks + 5, 130);
var results = afirma.Update(series);
// All results should be finite
foreach (var result in results)
{
Assert.True(double.IsFinite(result.Value), $"Expected finite value but got {result.Value}");
}
}
[Fact]
public void Afirma_Reset_ClearsLastValidValue()
{
var afirma = new Afirma(10);
// Feed values including NaN
afirma.Update(new TValue(DateTime.UtcNow, 100));
afirma.Update(new TValue(DateTime.UtcNow, double.NaN));
// Reset
afirma.Reset();
// After reset, first valid value should establish new baseline
var result = afirma.Update(new TValue(DateTime.UtcNow, 50));
Assert.True(double.IsFinite(result.Value));
}
[Fact]
public void Afirma_StaticBatch_Works()
{
var series = new TSeries();
series.Add(DateTime.UtcNow.Ticks, 10);
series.Add(DateTime.UtcNow.Ticks + 1, 20);
series.Add(DateTime.UtcNow.Ticks + 2, 30);
series.Add(DateTime.UtcNow.Ticks + 3, 40);
series.Add(DateTime.UtcNow.Ticks + 4, 50);
var results = Afirma.Batch(series, 5);
Assert.Equal(5, results.Count);
Assert.True(double.IsFinite(results.Last.Value));
}
[Fact]
public void Afirma_Period1_ReturnsSmoothedValues()
{
var afirma = new Afirma(1);
var r1 = afirma.Update(new TValue(DateTime.UtcNow, 100));
var r2 = afirma.Update(new TValue(DateTime.UtcNow, 200));
var r3 = afirma.Update(new TValue(DateTime.UtcNow, 150));
Assert.True(double.IsFinite(r1.Value));
Assert.True(double.IsFinite(r2.Value));
Assert.True(double.IsFinite(r3.Value));
}
// ============== Span API Tests ==============
[Fact]
public void Afirma_SpanBatch_ValidatesInput()
{
double[] source = [1, 2, 3, 4, 5];
double[] output = new double[5];
double[] wrongSizeOutput = new double[3];
// Period must be >= 1
Assert.Throws<ArgumentException>(() => Afirma.Batch(source.AsSpan(), output.AsSpan(), 0));
Assert.Throws<ArgumentException>(() => Afirma.Batch(source.AsSpan(), output.AsSpan(), -1));
// Output must be same length as source
Assert.Throws<ArgumentException>(() => Afirma.Batch(source.AsSpan(), wrongSizeOutput.AsSpan(), 5));
}
[Fact]
public void Afirma_SpanBatch_MatchesTSeriesBatch()
{
var series = new TSeries();
double[] source = new double[100];
double[] output = new double[100];
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.1, seed: 42);
for (int i = 0; i < 100; i++)
{
var bar = gbm.Next(isNew: true);
source[i] = bar.Close;
series.Add(bar.Time, bar.Close);
}
// Calculate with TSeries API
var tseriesResult = Afirma.Batch(series, 10);
// Calculate with Span API
Afirma.Batch(source.AsSpan(), output.AsSpan(), 10);
// Compare results
for (int i = 0; i < 100; i++)
{
Assert.Equal(tseriesResult[i].Value, output[i], 1e-10);
}
}
[Fact]
public void Afirma_SpanBatch_CalculatesCorrectly()
{
double[] source = [10, 20, 30, 40, 50];
double[] output = new double[5];
Afirma.Batch(source.AsSpan(), output.AsSpan(), 5);
// All outputs should be finite
foreach (var val in output)
{
Assert.True(double.IsFinite(val), $"Expected finite value but got {val}");
}
}
[Fact]
public void Afirma_SpanBatch_ZeroAllocation()
{
double[] source = new double[10000];
double[] output = new double[10000];
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 42);
for (int i = 0; i < source.Length; i++)
{
source[i] = gbm.Next().Close;
}
// Warm up
Afirma.Batch(source.AsSpan(), output.AsSpan(), 10);
// This test verifies the method runs without throwing
Assert.True(double.IsFinite(output[^1]));
}
[Fact]
public void Afirma_SpanBatch_HandlesNaN()
{
double[] source = [100, 110, double.NaN, 120, 130];
double[] output = new double[5];
Afirma.Batch(source.AsSpan(), output.AsSpan(), 5);
// All outputs should be finite
foreach (var val in output)
{
Assert.True(double.IsFinite(val), $"Expected finite value but got {val}");
}
}
[Fact]
public void Afirma_AllModes_ProduceSameResult()
{
// Arrange
const int period = 10;
var window = Afirma.WindowType.BlackmanHarris;
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 123);
var bars = gbm.Fetch(1000, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
var series = bars.Close;
// 1. Batch Mode
var batchSeries = Afirma.Batch(series, period, window);
double expected = batchSeries.Last.Value;
// 2. Span Mode
var tValues = series.Values.ToArray();
var spanInput = new ReadOnlySpan<double>(tValues);
var spanOutput = new double[tValues.Length];
Afirma.Batch(spanInput, spanOutput, period, window);
double spanResult = spanOutput[^1];
// 3. Streaming Mode
var streamingInd = new Afirma(period, window);
for (int i = 0; i < series.Count; i++)
{
streamingInd.Update(series[i]);
}
double streamingResult = streamingInd.Last.Value;
// 4. Eventing Mode
var pubSource = new TSeries();
var eventingInd = new Afirma(pubSource, period, window);
for (int i = 0; i < series.Count; i++)
{
pubSource.Add(series[i]);
}
double eventingResult = eventingInd.Last.Value;
// Assert
Assert.Equal(expected, spanResult, precision: 9);
Assert.Equal(expected, streamingResult, precision: 9);
Assert.Equal(expected, eventingResult, precision: 9);
}
[Fact]
public void Afirma_Chainability_Works()
{
var source = new TSeries();
var afirma = new Afirma(source, 10);
source.Add(new TValue(DateTime.UtcNow, 100));
Assert.True(double.IsFinite(afirma.Last.Value));
}
[Fact]
public void Afirma_WarmupPeriod_IsSetCorrectly()
{
var afirma = new Afirma(21);
Assert.Equal(21, afirma.WarmupPeriod);
}
[Fact]
public void Afirma_Prime_SetsStateCorrectly()
{
var afirma = new Afirma(5);
double[] history = [10, 20, 30, 40, 50];
afirma.Prime(history);
Assert.True(afirma.IsHot);
Assert.True(double.IsFinite(afirma.Last.Value));
// Verify it continues correctly
afirma.Update(new TValue(DateTime.UtcNow, 60));
Assert.True(double.IsFinite(afirma.Last.Value));
}
[Fact]
public void Afirma_Prime_WithInsufficientHistory_IsNotHot()
{
var afirma = new Afirma(10);
double[] history = [10, 20, 30, 40, 50];
afirma.Prime(history);
Assert.False(afirma.IsHot);
Assert.True(double.IsFinite(afirma.Last.Value));
}
[Fact]
public void Afirma_Prime_HandlesNaN_InHistory()
{
var afirma = new Afirma(3);
double[] history = [10, 20, double.NaN, 40];
afirma.Prime(history);
Assert.True(afirma.IsHot);
Assert.True(double.IsFinite(afirma.Last.Value));
}
[Fact]
public void Afirma_Calculate_ReturnsCorrectResultsAndHotIndicator()
{
var series = new TSeries();
for (int i = 1; i <= 10; i++)
{
series.Add(DateTime.UtcNow, i * 10);
}
var (results, indicator) = Afirma.Calculate(series, 5);
// Check results
Assert.Equal(10, results.Count);
Assert.True(double.IsFinite(results.Last.Value));
// Check indicator state
Assert.True(indicator.IsHot);
Assert.Equal(results.Last.Value, indicator.Last.Value);
Assert.Equal(5, indicator.WarmupPeriod);
// Verify indicator continues correctly
indicator.Update(new TValue(DateTime.UtcNow, 110));
Assert.True(double.IsFinite(indicator.Last.Value));
}
[Fact]
public void Afirma_DifferentWindowTypes_Work()
{
var windows = new[]
{
Afirma.WindowType.Rectangular,
Afirma.WindowType.Hanning,
Afirma.WindowType.Hamming,
Afirma.WindowType.Blackman,
Afirma.WindowType.BlackmanHarris
};
foreach (var window in windows)
{
var afirma = new Afirma(10, window);
for (int i = 0; i < 20; i++)
{
afirma.Update(new TValue(DateTime.UtcNow, 100 + i));
}
Assert.True(double.IsFinite(afirma.Last.Value), $"Window {window} should produce finite value");
Assert.True(afirma.IsHot, $"Window {window} should become hot");
}
}
[Fact]
public void Afirma_FlatLine_ReturnsSameValue()
{
var afirma = new Afirma(10);
for (int i = 0; i < 20; i++)
{
afirma.Update(new TValue(DateTime.UtcNow, 100));
}
// With a flat line, the filtered value should be close to the input
Assert.Equal(100, afirma.Last.Value, 1e-6);
}
[Fact]
public void Afirma_Taps1_Works()
{
var afirma = new Afirma(1);
var r1 = afirma.Update(new TValue(DateTime.UtcNow, 100));
var r2 = afirma.Update(new TValue(DateTime.UtcNow, 200));
// With 1 tap, output should equal input
Assert.Equal(100, r1.Value, 1e-10);
Assert.Equal(200, r2.Value, 1e-10);
}
[Fact]
public void Afirma_Pub_EventFires()
{
var afirma = new Afirma(10);
bool eventFired = false;
afirma.Pub += (object? sender, in TValueEventArgs args) => eventFired = true;
afirma.Update(new TValue(DateTime.UtcNow, 100));
Assert.True(eventFired);
}
}
@@ -0,0 +1,369 @@
using Xunit.Abstractions;
namespace QuanTAlib.Tests;
/// <summary>
/// Validation tests for AFIRMA indicator.
/// AFIRMA is a specialized FIR filter with windowed sinc coefficients.
/// Since no external library implements this exact algorithm, validation
/// focuses on internal consistency and mathematical properties.
/// </summary>
public sealed class AfirmaValidationTests : IDisposable
{
private readonly ValidationTestData _testData;
private readonly ITestOutputHelper _output;
private bool _disposed;
public AfirmaValidationTests(ITestOutputHelper output)
{
_output = output;
_testData = new ValidationTestData();
}
public void Dispose()
{
Dispose(true);
}
private void Dispose(bool disposing)
{
if (_disposed)
{
return;
}
_disposed = true;
if (disposing)
{
_testData?.Dispose();
}
}
[Fact]
public void Validate_InternalConsistency_Batch()
{
int[] periods = { 5, 10, 20, 50 };
foreach (var period in periods)
{
// Calculate QuanTAlib AFIRMA (batch TSeries)
var afirma = new Afirma(period);
var qResult = afirma.Update(_testData.Data);
// Verify all results are finite
foreach (var val in qResult)
{
Assert.True(double.IsFinite(val.Value),
$"AFIRMA({period}) produced non-finite value");
}
// Verify count matches input
Assert.Equal(_testData.Data.Count, qResult.Count);
}
_output.WriteLine("AFIRMA Batch(TSeries) internal consistency validated");
}
[Fact]
public void Validate_InternalConsistency_Streaming()
{
int[] periods = { 5, 10, 20, 50 };
foreach (var period in periods)
{
// Calculate QuanTAlib AFIRMA (streaming)
var afirma = new Afirma(period);
var qResults = new List<double>();
foreach (var item in _testData.Data)
{
qResults.Add(afirma.Update(item).Value);
}
// Verify all results are finite
foreach (var val in qResults)
{
Assert.True(double.IsFinite(val),
$"AFIRMA({period}) streaming produced non-finite value");
}
// Verify count matches input
Assert.Equal(_testData.Data.Count, qResults.Count);
}
_output.WriteLine("AFIRMA Streaming internal consistency validated");
}
[Fact]
public void Validate_InternalConsistency_Span()
{
int[] periods = { 5, 10, 20, 50 };
// Prepare data for Span API
double[] sourceData = _testData.RawData.ToArray();
foreach (var period in periods)
{
// Calculate QuanTAlib AFIRMA (Span API)
double[] qOutput = new double[sourceData.Length];
Afirma.Batch(sourceData.AsSpan(), qOutput.AsSpan(), period);
// Verify all results are finite
foreach (var val in qOutput)
{
Assert.True(double.IsFinite(val),
$"AFIRMA({period}) span produced non-finite value");
}
}
_output.WriteLine("AFIRMA Span internal consistency validated");
}
[Fact]
public void Validate_BatchStreamingConsistency()
{
int[] periods = { 5, 10, 20 };
foreach (var period in periods)
{
// Batch calculation
var afirmaBatch = new Afirma(period);
var batchResult = afirmaBatch.Update(_testData.Data);
// Streaming calculation
var afirmaStream = new Afirma(period);
var streamResults = new List<double>();
foreach (var item in _testData.Data)
{
streamResults.Add(afirmaStream.Update(item).Value);
}
// Compare last 100 values
int compareCount = Math.Min(100, batchResult.Count);
for (int i = 0; i < compareCount; i++)
{
int idx = batchResult.Count - compareCount + i;
Assert.Equal(batchResult[idx].Value, streamResults[idx], 1e-10);
}
}
_output.WriteLine("AFIRMA Batch/Streaming consistency validated");
}
[Fact]
public void Validate_SpanBatchConsistency()
{
int[] periods = { 5, 10, 20 };
double[] sourceData = _testData.RawData.ToArray();
foreach (var period in periods)
{
// TSeries Batch
var afirma = new Afirma(period);
var tseriesResult = afirma.Update(_testData.Data);
// Span Batch
double[] spanOutput = new double[sourceData.Length];
Afirma.Batch(sourceData.AsSpan(), spanOutput.AsSpan(), period);
// Compare
for (int i = 0; i < sourceData.Length; i++)
{
Assert.Equal(tseriesResult[i].Value, spanOutput[i], 1e-10);
}
}
_output.WriteLine("AFIRMA Span/Batch consistency validated");
}
[Fact]
public void Validate_WindowTypes_Consistency()
{
var windows = new[]
{
Afirma.WindowType.Rectangular,
Afirma.WindowType.Hanning,
Afirma.WindowType.Hamming,
Afirma.WindowType.Blackman,
Afirma.WindowType.BlackmanHarris
};
const int period = 10;
foreach (var window in windows)
{
// Batch
var afirmaBatch = new Afirma(period, window);
var batchResult = afirmaBatch.Update(_testData.Data);
// Streaming
var afirmaStream = new Afirma(period, window);
foreach (var item in _testData.Data)
{
afirmaStream.Update(item);
}
// Compare last values
Assert.Equal(batchResult.Last.Value, afirmaStream.Last.Value, 1e-10);
_output.WriteLine($"Window {window}: Batch={batchResult.Last.Value:F6}, Stream={afirmaStream.Last.Value:F6}");
}
_output.WriteLine("AFIRMA Window types consistency validated");
}
[Fact]
public void Validate_FlatInput_ReturnsConstant()
{
int period = 10;
double constantValue = 100.0;
// Create flat input
var flatSeries = new TSeries();
for (int i = 0; i < 100; i++)
{
flatSeries.Add(DateTime.UtcNow.AddSeconds(i), constantValue);
}
var afirma = new Afirma(period);
var result = afirma.Update(flatSeries);
// After warmup, all values should equal the constant
for (int i = period; i < result.Count; i++)
{
Assert.Equal(constantValue, result[i].Value, 1e-9);
}
_output.WriteLine($"AFIRMA flat input returns constant: {result.Last.Value:F9}");
}
[Fact]
public void Validate_Smoothing_ReducesVariance()
{
int period = 21;
// Calculate variance of input
var rawData = _testData.RawData.ToArray();
double inputMean = rawData.Average();
double inputVariance = rawData.Average(x => Math.Pow(x - inputMean, 2));
// Calculate AFIRMA
var afirma = new Afirma(period);
var result = afirma.Update(_testData.Data);
// Calculate variance of output (after warmup)
var outputValues = result.Skip(period).Select(v => v.Value).ToList();
double outputMean = outputValues.Average();
double outputVariance = outputValues.Average(x => Math.Pow(x - outputMean, 2));
// Output variance should be less than input variance (smoothing effect)
Assert.True(outputVariance < inputVariance,
$"AFIRMA should reduce variance. Input: {inputVariance:F4}, Output: {outputVariance:F4}");
_output.WriteLine($"AFIRMA smoothing effect: Input variance={inputVariance:F4}, Output variance={outputVariance:F4}");
}
[Fact]
public void Validate_LargerPeriod_MoreSmoothing()
{
// Calculate with different periods (which implies different tap counts)
var afirma5 = new Afirma(5);
var afirma11 = new Afirma(11);
var afirma21 = new Afirma(21);
var result5 = afirma5.Update(_testData.Data);
var result11 = afirma11.Update(_testData.Data);
var result21 = afirma21.Update(_testData.Data);
// Calculate variance of each
double GetVariance(TSeries series, int skip)
{
var values = series.Skip(skip).Select(v => v.Value).ToList();
double mean = values.Average();
return values.Average(x => Math.Pow(x - mean, 2));
}
double var5 = GetVariance(result5, 5);
double var11 = GetVariance(result11, 11);
double var21 = GetVariance(result21, 21);
// Larger period should generally produce smoother output (lower variance)
// This is a statistical property, not guaranteed for all data
_output.WriteLine($"Variance by period: 5={var5:F4}, 11={var11:F4}, 21={var21:F4}");
// At minimum, all should be finite
Assert.True(double.IsFinite(var5));
Assert.True(double.IsFinite(var11));
Assert.True(double.IsFinite(var21));
}
[Fact]
public void Validate_DifferentWindows_DifferentCharacteristics()
{
int period = 10;
var rectangularResult = Afirma.Batch(_testData.Data, period, Afirma.WindowType.Rectangular);
var blackmanHarrisResult = Afirma.Batch(_testData.Data, period, Afirma.WindowType.BlackmanHarris);
// Results should be different (different window characteristics)
double rectLast = rectangularResult.Last.Value;
double bhLast = blackmanHarrisResult.Last.Value;
// They should generally not be exactly equal
// (unless input happens to be perfectly constant)
_output.WriteLine($"Rectangular: {rectLast:F6}, Blackman-Harris: {bhLast:F6}");
// Both should be finite and reasonable
Assert.True(double.IsFinite(rectLast));
Assert.True(double.IsFinite(bhLast));
}
[Fact]
public void Afirma_LeastSquares_Streaming_Matches_Batch()
{
int[] periods = { 5, 10, 20 };
foreach (var period in periods)
{
// Batch calculation with leastSquares=true
var afirmaBatch = new Afirma(period, leastSquares: true);
var batchResult = afirmaBatch.Update(_testData.Data);
// Streaming calculation with leastSquares=true
var afirmaStream = new Afirma(period, leastSquares: true);
var streamResults = new List<double>();
foreach (var item in _testData.Data)
{
streamResults.Add(afirmaStream.Update(item).Value);
}
// Compare last 100 values
int compareCount = Math.Min(100, batchResult.Count);
for (int i = 0; i < compareCount; i++)
{
int idx = batchResult.Count - compareCount + i;
Assert.Equal(batchResult[idx].Value, streamResults[idx], 1e-10);
}
}
}
[Fact]
public void Afirma_Correction_Recomputes()
{
var ind = new Afirma(20);
var t0 = DateTime.MinValue;
// Build state well past warmup
for (int i = 0; i < 50; i++)
{
ind.Update(new TValue(t0.AddSeconds(i), 100.0 + (i * 0.5)));
}
// Anchor bar
var anchorTime = t0.AddSeconds(50);
const double anchorValue = 125.0;
ind.Update(new TValue(anchorTime, anchorValue), isNew: true);
double anchorResult = ind.Last.Value;
// Correction with dramatically different value — must yield different result
ind.Update(new TValue(anchorTime, anchorValue * 10), isNew: false);
Assert.NotEqual(anchorResult, ind.Last.Value);
// Correction back to original — must exactly restore original result
ind.Update(new TValue(anchorTime, anchorValue), isNew: false);
Assert.Equal(anchorResult, ind.Last.Value, 1e-9);
}
}