mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-21 20:18:05 +00:00
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:
@@ -0,0 +1,128 @@
|
||||
using TradingPlatform.BusinessLayer;
|
||||
using QuanTAlib;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public sealed class TrixIndicatorTests
|
||||
{
|
||||
[Fact]
|
||||
public void TrixIndicator_Constructor_SetsDefaults()
|
||||
{
|
||||
var indicator = new TrixIndicator();
|
||||
|
||||
Assert.Equal(14, indicator.Period);
|
||||
Assert.Equal(SourceType.Close, indicator.Source);
|
||||
Assert.True(indicator.ShowColdValues);
|
||||
Assert.Equal("TRIX - Triple Exponential Average Oscillator", indicator.Name);
|
||||
Assert.True(indicator.SeparateWindow);
|
||||
Assert.True(indicator.OnBackGround);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TrixIndicator_MinHistoryDepths_EqualsZero()
|
||||
{
|
||||
var indicator = new TrixIndicator { Period = 14 };
|
||||
|
||||
Assert.Equal(0, TrixIndicator.MinHistoryDepths);
|
||||
IWatchlistIndicator watchlistIndicator = indicator;
|
||||
Assert.Equal(0, watchlistIndicator.MinHistoryDepths);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TrixIndicator_ShortName_IncludesParameters()
|
||||
{
|
||||
var indicator = new TrixIndicator { Period = 10 };
|
||||
indicator.Initialize();
|
||||
|
||||
Assert.Contains("TRIX", indicator.ShortName, StringComparison.Ordinal);
|
||||
Assert.Contains("10", indicator.ShortName, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TrixIndicator_SourceCodeLink_IsValid()
|
||||
{
|
||||
var indicator = new TrixIndicator();
|
||||
|
||||
Assert.Contains("github.com", indicator.SourceCodeLink, StringComparison.Ordinal);
|
||||
Assert.Contains("Trix.Quantower.cs", indicator.SourceCodeLink, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TrixIndicator_Initialize_CreatesInternalTrix()
|
||||
{
|
||||
var indicator = new TrixIndicator { Period = 10 };
|
||||
|
||||
indicator.Initialize();
|
||||
|
||||
Assert.Single(indicator.LinesSeries);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TrixIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
|
||||
{
|
||||
var indicator = new TrixIndicator { Period = 5 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), 100 + i, 110 + i, 90 + i, 105 + i);
|
||||
|
||||
var args = new UpdateArgs(UpdateReason.HistoricalBar);
|
||||
indicator.ProcessUpdate(args);
|
||||
}
|
||||
|
||||
double value = indicator.LinesSeries[0].GetValue(0);
|
||||
Assert.True(double.IsFinite(value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TrixIndicator_ProcessUpdate_NewBar_ComputesValue()
|
||||
{
|
||||
var indicator = new TrixIndicator { Period = 5 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), 100 + i, 110 + i, 90 + i, 105 + i);
|
||||
}
|
||||
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(20), 120, 130, 110, 125);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewBar));
|
||||
|
||||
Assert.Equal(2, indicator.LinesSeries[0].Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TrixIndicator_Parameters_CanBeChanged()
|
||||
{
|
||||
var indicator = new TrixIndicator { Period = 14 };
|
||||
|
||||
indicator.Period = 10;
|
||||
indicator.Source = SourceType.Open;
|
||||
|
||||
Assert.Equal(10, indicator.Period);
|
||||
Assert.Equal(SourceType.Open, indicator.Source);
|
||||
Assert.Equal(0, TrixIndicator.MinHistoryDepths);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TrixIndicator_ProcessUpdate_DifferentSources()
|
||||
{
|
||||
var indicator = new TrixIndicator { Period = 5, Source = SourceType.High };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), 100 + i, 110 + i, 90 + i, 105 + i);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
}
|
||||
|
||||
double value = indicator.LinesSeries[0].GetValue(0);
|
||||
Assert.True(double.IsFinite(value));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,674 @@
|
||||
using Xunit;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
// ── A) Constructor Validation ───────────────────────────────────────────────
|
||||
public sealed class TrixConstructorTests
|
||||
{
|
||||
[Fact]
|
||||
public void Constructor_ZeroPeriod_ThrowsArgumentException()
|
||||
{
|
||||
var ex = Assert.Throws<ArgumentException>(() => new Trix(0));
|
||||
Assert.Equal("period", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_NegativePeriod_ThrowsArgumentException()
|
||||
{
|
||||
var ex = Assert.Throws<ArgumentException>(() => new Trix(-5));
|
||||
Assert.Equal("period", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_DefaultPeriod_Creates()
|
||||
{
|
||||
var trix = new Trix();
|
||||
Assert.NotNull(trix);
|
||||
Assert.Equal(14, trix.Period);
|
||||
Assert.Equal("Trix(14)", trix.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_CustomPeriod_Creates()
|
||||
{
|
||||
var trix = new Trix(5);
|
||||
Assert.Equal(5, trix.Period);
|
||||
Assert.Equal("Trix(5)", trix.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WarmupPeriod_IsTriplePeriod()
|
||||
{
|
||||
var trix = new Trix(10);
|
||||
Assert.Equal(30, trix.WarmupPeriod);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_PeriodOne_IsValid()
|
||||
{
|
||||
var trix = new Trix(1);
|
||||
Assert.NotNull(trix);
|
||||
Assert.Equal(1, trix.Period);
|
||||
}
|
||||
}
|
||||
|
||||
// ── B) Basic Calculation ────────────────────────────────────────────────────
|
||||
public sealed class TrixBasicTests
|
||||
{
|
||||
[Fact]
|
||||
public void BasicCalculation_DoesNotCrash()
|
||||
{
|
||||
var trix = new Trix(10);
|
||||
Assert.Equal(0, trix.Last.Value);
|
||||
|
||||
TValue result = trix.Update(new TValue(DateTime.UtcNow, 100));
|
||||
Assert.Equal(result.Value, trix.Last.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FirstBar_OutputIsZero()
|
||||
{
|
||||
var trix = new Trix(5);
|
||||
var result = trix.Update(new TValue(DateTime.UtcNow, 100));
|
||||
// First bar: no previous EMA3 to compare against, output = 0
|
||||
Assert.Equal(0.0, result.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SecondBar_ProducesNonZeroValue()
|
||||
{
|
||||
var trix = new Trix(5);
|
||||
trix.Update(new TValue(DateTime.UtcNow, 100));
|
||||
var result = trix.Update(new TValue(DateTime.UtcNow, 110));
|
||||
// EMA3 changes vs first bar → non-zero TRIX
|
||||
Assert.NotEqual(0.0, result.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Name_Available()
|
||||
{
|
||||
var trix = new Trix(7);
|
||||
Assert.Equal("Trix(7)", trix.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Last_IsAccessible()
|
||||
{
|
||||
var trix = new Trix(5);
|
||||
trix.Update(new TValue(DateTime.UtcNow, 100));
|
||||
trix.Update(new TValue(DateTime.UtcNow, 110));
|
||||
Assert.True(double.IsFinite(trix.Last.Value));
|
||||
}
|
||||
}
|
||||
|
||||
// ── C) State + Bar Correction ───────────────────────────────────────────────
|
||||
public sealed class TrixBarCorrectionTests
|
||||
{
|
||||
[Fact]
|
||||
public void IsNew_True_AdvancesState()
|
||||
{
|
||||
var trix = new Trix(5);
|
||||
trix.Update(new TValue(DateTime.UtcNow, 100), isNew: true);
|
||||
double val1 = trix.Last.Value;
|
||||
|
||||
trix.Update(new TValue(DateTime.UtcNow, 110), isNew: true);
|
||||
double val2 = trix.Last.Value;
|
||||
|
||||
// Different values should produce different states
|
||||
Assert.NotEqual(val1, val2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsNew_False_Rollback()
|
||||
{
|
||||
var trix = new Trix(5);
|
||||
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.1, seed: 42);
|
||||
|
||||
// Feed enough bars to get past trivial state
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
var bar = gbm.Next(isNew: true);
|
||||
trix.Update(new TValue(bar.Time, bar.Close), isNew: true);
|
||||
}
|
||||
|
||||
// Feed one more bar with isNew=true and remember value
|
||||
var nextBar = gbm.Next(isNew: true);
|
||||
var originalInput = new TValue(nextBar.Time, nextBar.Close);
|
||||
var val1 = trix.Update(originalInput, isNew: true);
|
||||
|
||||
// Correct with isNew=false (different value)
|
||||
trix.Update(new TValue(nextBar.Time, nextBar.Close + 50), isNew: false);
|
||||
|
||||
// Re-apply original value with isNew=false → should match val1
|
||||
var restored = trix.Update(originalInput, isNew: false);
|
||||
|
||||
Assert.Equal(val1.Value, restored.Value, 1e-10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IterativeCorrections_RestoreToOriginalState()
|
||||
{
|
||||
var trix = new Trix(5);
|
||||
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.1);
|
||||
|
||||
// Feed 20 new values
|
||||
TValue twentiethInput = default;
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
var bar = gbm.Next(isNew: true);
|
||||
twentiethInput = new TValue(bar.Time, bar.Close);
|
||||
trix.Update(twentiethInput, isNew: true);
|
||||
}
|
||||
|
||||
double stateAfterTwenty = trix.Last.Value;
|
||||
|
||||
// Generate 9 corrections with isNew=false (different values)
|
||||
for (int i = 0; i < 9; i++)
|
||||
{
|
||||
var bar = gbm.Next(isNew: false);
|
||||
trix.Update(new TValue(bar.Time, bar.Close), isNew: false);
|
||||
}
|
||||
|
||||
// Feed the remembered 20th input again with isNew=false
|
||||
TValue finalResult = trix.Update(twentiethInput, isNew: false);
|
||||
|
||||
Assert.Equal(stateAfterTwenty, finalResult.Value, 1e-10);
|
||||
}
|
||||
}
|
||||
|
||||
// ── D) Warmup / Convergence ─────────────────────────────────────────────────
|
||||
public sealed class TrixWarmupTests
|
||||
{
|
||||
[Fact]
|
||||
public void IsHot_InitiallyFalse()
|
||||
{
|
||||
var trix = new Trix(5);
|
||||
Assert.False(trix.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsHot_BecomesTrueAfterWarmupPeriodBars()
|
||||
{
|
||||
const int period = 5;
|
||||
var trix = new Trix(period);
|
||||
int warmup = trix.WarmupPeriod; // period * 3 = 15
|
||||
|
||||
// Feed warmup-1 bars → still cold (Count < WarmupPeriod)
|
||||
for (int i = 1; i < warmup; i++)
|
||||
{
|
||||
trix.Update(new TValue(DateTime.UtcNow, i * 10));
|
||||
Assert.False(trix.IsHot, $"Should not be hot at bar {i} (need {warmup})");
|
||||
}
|
||||
|
||||
// Bar at warmup count → hot (Count == WarmupPeriod)
|
||||
trix.Update(new TValue(DateTime.UtcNow, warmup * 10));
|
||||
Assert.True(trix.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WarmupPeriod_IsTriplePeriod()
|
||||
{
|
||||
var trix = new Trix(10);
|
||||
Assert.Equal(30, trix.WarmupPeriod);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsHot_StaysTrue()
|
||||
{
|
||||
var trix = new Trix(3);
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.02, sigma: 0.1, seed: 42);
|
||||
|
||||
for (int i = 0; i < 50; i++)
|
||||
{
|
||||
var bar = gbm.Next(isNew: true);
|
||||
trix.Update(new TValue(bar.Time, bar.Close));
|
||||
}
|
||||
|
||||
Assert.True(trix.IsHot);
|
||||
}
|
||||
}
|
||||
|
||||
// ── E) Robustness (NaN / Infinity) ─────────────────────────────────────────
|
||||
public sealed class TrixRobustnessTests
|
||||
{
|
||||
[Fact]
|
||||
public void NaN_Input_UsesLastValidValue()
|
||||
{
|
||||
var trix = new Trix(5);
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.02, sigma: 0.1, seed: 42);
|
||||
var bars = gbm.Fetch(20, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
for (int i = 0; i < 15; i++)
|
||||
{
|
||||
trix.Update(new TValue(bars[i].Time, bars[i].Close));
|
||||
}
|
||||
|
||||
var result = trix.Update(new TValue(DateTime.UtcNow, double.NaN));
|
||||
Assert.True(double.IsFinite(result.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Infinity_Input_UsesLastValidValue()
|
||||
{
|
||||
var trix = new Trix(5);
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.02, sigma: 0.1, seed: 42);
|
||||
var bars = gbm.Fetch(20, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
for (int i = 0; i < 15; i++)
|
||||
{
|
||||
trix.Update(new TValue(bars[i].Time, bars[i].Close));
|
||||
}
|
||||
|
||||
var resultPos = trix.Update(new TValue(DateTime.UtcNow, double.PositiveInfinity));
|
||||
Assert.True(double.IsFinite(resultPos.Value));
|
||||
|
||||
var resultNeg = trix.Update(new TValue(DateTime.UtcNow, double.NegativeInfinity));
|
||||
Assert.True(double.IsFinite(resultNeg.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BatchNaN_DoesNotCrash()
|
||||
{
|
||||
double[] source = [100, 110, double.NaN, 130, 140, double.NaN, 160];
|
||||
double[] output = new double[source.Length];
|
||||
|
||||
Trix.Batch(source.AsSpan(), output.AsSpan(), 3);
|
||||
|
||||
for (int i = 0; i < output.Length; i++)
|
||||
{
|
||||
Assert.True(double.IsFinite(output[i]), $"Output at index {i} is not finite");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ── F) Consistency (All 4 Modes Match) ──────────────────────────────────────
|
||||
public sealed class TrixConsistencyTests
|
||||
{
|
||||
private static TSeries GenerateCloseSeries(int count, int seed = 42)
|
||||
{
|
||||
var gbm = new GBM(startPrice: 100.0, mu: 0.05, sigma: 0.2, seed: seed);
|
||||
var bars = gbm.Fetch(count, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
return bars.Close;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AllModes_ProduceSameResult()
|
||||
{
|
||||
const int period = 10;
|
||||
var series = GenerateCloseSeries(100);
|
||||
|
||||
// 1. Batch Mode
|
||||
var batchSeries = Trix.Batch(series, period);
|
||||
double expected = batchSeries.Last.Value;
|
||||
|
||||
// 2. Span Mode
|
||||
var spanInput = series.Values.ToArray();
|
||||
var spanOutput = new double[spanInput.Length];
|
||||
Trix.Batch(spanInput.AsSpan(), spanOutput.AsSpan(), period);
|
||||
double spanResult = spanOutput[^1];
|
||||
|
||||
// 3. Streaming Mode
|
||||
var streamingInd = new Trix(period);
|
||||
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 Trix(pubSource, period);
|
||||
for (int i = 0; i < series.Count; i++)
|
||||
{
|
||||
pubSource.Add(series[i]);
|
||||
}
|
||||
double eventingResult = eventingInd.Last.Value;
|
||||
|
||||
Assert.Equal(expected, spanResult, precision: 9);
|
||||
Assert.Equal(expected, streamingResult, precision: 9);
|
||||
Assert.Equal(expected, eventingResult, precision: 9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BatchVsStreaming_AllPoints()
|
||||
{
|
||||
const int period = 5;
|
||||
var series = GenerateCloseSeries(50);
|
||||
|
||||
// Batch
|
||||
var batchSeries = Trix.Batch(series, period);
|
||||
|
||||
// Streaming
|
||||
var streamingInd = new Trix(period);
|
||||
for (int i = 0; i < series.Count; i++)
|
||||
{
|
||||
streamingInd.Update(series[i]);
|
||||
Assert.Equal(batchSeries[i].Value, streamingInd.Last.Value, 1e-10);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SpanVsBatch_AllPoints()
|
||||
{
|
||||
const int period = 7;
|
||||
var series = GenerateCloseSeries(80);
|
||||
|
||||
var batchSeries = Trix.Batch(series, period);
|
||||
|
||||
var spanInput = series.Values.ToArray();
|
||||
var spanOutput = new double[spanInput.Length];
|
||||
Trix.Batch(spanInput.AsSpan(), spanOutput.AsSpan(), period);
|
||||
|
||||
for (int i = 0; i < series.Count; i++)
|
||||
{
|
||||
Assert.Equal(batchSeries[i].Value, spanOutput[i], 1e-10);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ── G) Span API Tests ───────────────────────────────────────────────────────
|
||||
public sealed class TrixSpanTests
|
||||
{
|
||||
[Fact]
|
||||
public void Batch_Span_MismatchedLengths_Throws()
|
||||
{
|
||||
double[] source = new double[10];
|
||||
double[] output = new double[5];
|
||||
|
||||
var ex = Assert.Throws<ArgumentException>(
|
||||
() => Trix.Batch(source.AsSpan(), output.AsSpan(), 3));
|
||||
Assert.Equal("output", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_ZeroPeriod_Throws()
|
||||
{
|
||||
double[] source = new double[10];
|
||||
double[] output = new double[10];
|
||||
|
||||
var ex = Assert.Throws<ArgumentException>(
|
||||
() => Trix.Batch(source.AsSpan(), output.AsSpan(), 0));
|
||||
Assert.Equal("period", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_EmptyArrays_DoesNotThrow()
|
||||
{
|
||||
double[] source = [];
|
||||
double[] output = [];
|
||||
|
||||
Trix.Batch(source.AsSpan(), output.AsSpan(), 3);
|
||||
|
||||
Assert.True(output.Length == 0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_SingleElement()
|
||||
{
|
||||
double[] source = [100.0];
|
||||
double[] output = new double[1];
|
||||
|
||||
Trix.Batch(source.AsSpan(), output.AsSpan(), 5);
|
||||
|
||||
// First element output = 0 (no previous EMA3)
|
||||
Assert.Equal(0.0, output[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_LargeData_DoesNotStackOverflow()
|
||||
{
|
||||
const int count = 10_000;
|
||||
double[] source = new double[count];
|
||||
double[] output = new double[count];
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.02, sigma: 0.1, seed: 42);
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
var bar = gbm.Next(isNew: true);
|
||||
source[i] = bar.Close;
|
||||
}
|
||||
|
||||
Trix.Batch(source.AsSpan(), output.AsSpan(), 14);
|
||||
|
||||
// Should produce finite results
|
||||
Assert.True(double.IsFinite(output[^1]));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_NaN_HandlesGracefully()
|
||||
{
|
||||
double[] source = new double[20];
|
||||
double[] output = new double[20];
|
||||
var gbm = new GBM(startPrice: 100, seed: 42);
|
||||
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
var bar = gbm.Next(isNew: true);
|
||||
source[i] = bar.Close;
|
||||
}
|
||||
|
||||
// Inject NaN at indices 5, 10, 15
|
||||
source[5] = double.NaN;
|
||||
source[10] = double.NaN;
|
||||
source[15] = double.NaN;
|
||||
|
||||
Trix.Batch(source.AsSpan(), output.AsSpan(), 3);
|
||||
|
||||
for (int i = 0; i < output.Length; i++)
|
||||
{
|
||||
Assert.True(double.IsFinite(output[i]), $"Output[{i}] is not finite");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ── H) Chainability ────────────────────────────────────────────────────────
|
||||
public sealed class TrixEventTests
|
||||
{
|
||||
[Fact]
|
||||
public void Chainability_Works()
|
||||
{
|
||||
var trix1 = new Trix(10);
|
||||
var trix2 = new Trix(trix1, 5);
|
||||
|
||||
trix1.Update(new TValue(DateTime.UtcNow, 100));
|
||||
Assert.True(double.IsFinite(trix2.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EventChaining_ProducesResults()
|
||||
{
|
||||
var source = new TSeries();
|
||||
var trix = new Trix(source, 5);
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.02, sigma: 0.1, seed: 42);
|
||||
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
var bar = gbm.Next(isNew: true);
|
||||
source.Add(bar.Time, bar.Close);
|
||||
}
|
||||
|
||||
Assert.True(double.IsFinite(trix.Last.Value));
|
||||
Assert.True(trix.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Pub_FiresOnUpdate()
|
||||
{
|
||||
var trix = new Trix(5);
|
||||
int eventCount = 0;
|
||||
|
||||
trix.Pub += HandleEvent;
|
||||
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
trix.Update(new TValue(DateTime.UtcNow, 100 + i));
|
||||
}
|
||||
|
||||
Assert.Equal(10, eventCount);
|
||||
|
||||
trix.Pub -= HandleEvent;
|
||||
|
||||
void HandleEvent(object? sender, in TValueEventArgs e)
|
||||
{
|
||||
eventCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ── Extra: Batch Tests ──────────────────────────────────────────────────────
|
||||
public sealed class TrixBatchTests
|
||||
{
|
||||
private static TSeries GenerateCloseSeries(int count, int seed = 42)
|
||||
{
|
||||
var gbm = new GBM(startPrice: 100.0, mu: 0.05, sigma: 0.2, seed: seed);
|
||||
var bars = gbm.Fetch(count, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
return bars.Close;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_TSeries_ReturnsCorrectCount()
|
||||
{
|
||||
var series = GenerateCloseSeries(50);
|
||||
var result = Trix.Batch(series, 10);
|
||||
Assert.Equal(50, result.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_TSeries_PreservesTimestamps()
|
||||
{
|
||||
var series = GenerateCloseSeries(20);
|
||||
var result = Trix.Batch(series, 5);
|
||||
|
||||
for (int i = 0; i < series.Count; i++)
|
||||
{
|
||||
Assert.Equal(series[i].Time, result[i].Time);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_ReturnsIndicatorAndResults()
|
||||
{
|
||||
var series = GenerateCloseSeries(30);
|
||||
var (results, indicator) = Trix.Calculate(series, 5);
|
||||
|
||||
Assert.NotNull(indicator);
|
||||
Assert.Equal(30, results.Count);
|
||||
Assert.Equal(5, indicator.Period);
|
||||
Assert.True(indicator.IsHot);
|
||||
}
|
||||
}
|
||||
|
||||
// ── Extra: Reset Tests ──────────────────────────────────────────────────────
|
||||
public sealed class TrixResetTests
|
||||
{
|
||||
[Fact]
|
||||
public void Reset_ClearsState()
|
||||
{
|
||||
var trix = new Trix(5);
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.02, sigma: 0.1, seed: 42);
|
||||
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
var bar = gbm.Next(isNew: true);
|
||||
trix.Update(new TValue(bar.Time, bar.Close));
|
||||
}
|
||||
|
||||
Assert.True(trix.IsHot);
|
||||
|
||||
trix.Reset();
|
||||
|
||||
Assert.False(trix.IsHot);
|
||||
Assert.Equal(0, trix.Last.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Reset_AcceptsNewValues()
|
||||
{
|
||||
var trix = new Trix(5);
|
||||
var gbm = new GBM(startPrice: 100, seed: 42);
|
||||
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
var bar = gbm.Next(isNew: true);
|
||||
trix.Update(new TValue(bar.Time, bar.Close));
|
||||
}
|
||||
|
||||
double valueBefore = trix.Last.Value;
|
||||
trix.Reset();
|
||||
|
||||
trix.Update(new TValue(DateTime.UtcNow, 50));
|
||||
Assert.Equal(0, trix.Last.Value); // First bar after reset = 0
|
||||
|
||||
trix.Update(new TValue(DateTime.UtcNow, 60));
|
||||
Assert.NotEqual(0, trix.Last.Value);
|
||||
Assert.NotEqual(valueBefore, trix.Last.Value);
|
||||
}
|
||||
}
|
||||
|
||||
// ── Extra: Prime Tests ──────────────────────────────────────────────────────
|
||||
public sealed class TrixPrimeTests
|
||||
{
|
||||
[Fact]
|
||||
public void Prime_SetsUpState()
|
||||
{
|
||||
var trix = new Trix(5);
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.02, sigma: 0.1, seed: 42);
|
||||
double[] data = new double[20];
|
||||
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
var bar = gbm.Next(isNew: true);
|
||||
data[i] = bar.Close;
|
||||
}
|
||||
|
||||
trix.Prime(data.AsSpan());
|
||||
|
||||
Assert.True(trix.IsHot);
|
||||
Assert.True(double.IsFinite(trix.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Prime_ThenUpdate_ProducesValidResults()
|
||||
{
|
||||
var trix = new Trix(5);
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.02, sigma: 0.1, seed: 42);
|
||||
double[] data = new double[20];
|
||||
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
var bar = gbm.Next(isNew: true);
|
||||
data[i] = bar.Close;
|
||||
}
|
||||
|
||||
trix.Prime(data.AsSpan());
|
||||
|
||||
// Post-prime updates should work normally
|
||||
var result = trix.Update(new TValue(DateTime.UtcNow, 110));
|
||||
Assert.True(double.IsFinite(result.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_TSeries_RestoresStreamingState()
|
||||
{
|
||||
var trix = new Trix(5);
|
||||
var series = new TSeries();
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.02, sigma: 0.1, seed: 42);
|
||||
|
||||
for (int i = 0; i < 30; i++)
|
||||
{
|
||||
var bar = gbm.Next(isNew: true);
|
||||
series.Add(bar.Time, bar.Close);
|
||||
}
|
||||
|
||||
var batchResult = trix.Update(series);
|
||||
|
||||
// After Update(TSeries), indicator should be hot with correct last value
|
||||
Assert.True(trix.IsHot);
|
||||
Assert.Equal(batchResult.Last.Value, trix.Last.Value, 1e-10);
|
||||
|
||||
// Subsequent streaming updates should work
|
||||
var nextBar = gbm.Next(isNew: true);
|
||||
var nextResult = trix.Update(new TValue(nextBar.Time, nextBar.Close));
|
||||
Assert.True(double.IsFinite(nextResult.Value));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,474 @@
|
||||
using OoplesFinance.StockIndicators;
|
||||
using OoplesFinance.StockIndicators.Models;
|
||||
using Skender.Stock.Indicators;
|
||||
using TALib;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public sealed class TrixValidationTests(ITestOutputHelper output) : IDisposable
|
||||
{
|
||||
private readonly ValidationTestData _testData = new();
|
||||
private readonly ITestOutputHelper _output = output;
|
||||
private bool _disposed;
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(disposing: true);
|
||||
}
|
||||
|
||||
private void Dispose(bool disposing)
|
||||
{
|
||||
if (_disposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_disposed = true;
|
||||
|
||||
if (disposing)
|
||||
{
|
||||
_testData?.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
// ── A) Skender Batch ─────────────────────────────────────────────────────
|
||||
[Fact]
|
||||
public void Validate_Skender_Batch()
|
||||
{
|
||||
int[] periods = [9, 14, 25];
|
||||
|
||||
foreach (var period in periods)
|
||||
{
|
||||
var trix = new global::QuanTAlib.Trix(period);
|
||||
var qResult = trix.Update(_testData.Data);
|
||||
|
||||
var sResult = _testData.SkenderQuotes.GetTrix(period).ToList();
|
||||
|
||||
ValidationHelper.VerifyData(qResult, sResult, (s) => s.Trix);
|
||||
}
|
||||
_output.WriteLine("TRIX Batch(TSeries) validated successfully against Skender");
|
||||
}
|
||||
|
||||
// ── B) Skender Streaming ─────────────────────────────────────────────────
|
||||
[Fact]
|
||||
public void Validate_Skender_Streaming()
|
||||
{
|
||||
int[] periods = [9, 14, 25];
|
||||
|
||||
foreach (var period in periods)
|
||||
{
|
||||
var trix = new global::QuanTAlib.Trix(period);
|
||||
var qResults = new List<double>();
|
||||
foreach (var item in _testData.Data)
|
||||
{
|
||||
qResults.Add(trix.Update(item).Value);
|
||||
}
|
||||
|
||||
var sResult = _testData.SkenderQuotes.GetTrix(period).ToList();
|
||||
|
||||
ValidationHelper.VerifyData(qResults, sResult, (s) => s.Trix);
|
||||
}
|
||||
_output.WriteLine("TRIX Streaming validated successfully against Skender");
|
||||
}
|
||||
|
||||
// ── C) Skender Span ──────────────────────────────────────────────────────
|
||||
[Fact]
|
||||
public void Validate_Skender_Span()
|
||||
{
|
||||
int[] periods = [9, 14, 25];
|
||||
double[] sourceData = _testData.RawData.ToArray();
|
||||
|
||||
foreach (var period in periods)
|
||||
{
|
||||
double[] qOutput = new double[sourceData.Length];
|
||||
global::QuanTAlib.Trix.Batch(sourceData.AsSpan(), qOutput.AsSpan(), period);
|
||||
|
||||
var sResult = _testData.SkenderQuotes.GetTrix(period).ToList();
|
||||
|
||||
ValidationHelper.VerifyData(qOutput, sResult, (s) => s.Trix);
|
||||
}
|
||||
_output.WriteLine("TRIX Span validated successfully against Skender");
|
||||
}
|
||||
|
||||
// ── D) TA-Lib Span ───────────────────────────────────────────────────────
|
||||
[Fact]
|
||||
public void Validate_Talib_Span()
|
||||
{
|
||||
int[] periods = [14, 20, 50, 100];
|
||||
double[] tData = _testData.RawData.ToArray();
|
||||
|
||||
foreach (var period in periods)
|
||||
{
|
||||
double[] qOutput = new double[tData.Length];
|
||||
global::QuanTAlib.Trix.Batch(tData.AsSpan(), qOutput.AsSpan(), period);
|
||||
|
||||
double[] tOutput = new double[tData.Length];
|
||||
var retCode = TALib.Functions.Trix<double>(tData, 0..^0, tOutput, out var outRange, period);
|
||||
Assert.Equal(TALib.Core.RetCode.Success, retCode);
|
||||
|
||||
int lookback = TALib.Functions.TrixLookback(period);
|
||||
|
||||
ValidationHelper.VerifyData(qOutput, tOutput, outRange, lookback);
|
||||
}
|
||||
_output.WriteLine("TRIX Span validated against TA-Lib");
|
||||
}
|
||||
|
||||
// ── E) TA-Lib Streaming ──────────────────────────────────────────────────
|
||||
[Fact]
|
||||
public void Validate_Talib_Streaming()
|
||||
{
|
||||
int[] periods = [9, 14, 25];
|
||||
double[] tData = _testData.RawData.ToArray();
|
||||
double[] tOutput = new double[tData.Length];
|
||||
|
||||
foreach (var period in periods)
|
||||
{
|
||||
var trix = new global::QuanTAlib.Trix(period);
|
||||
var qResults = new List<double>();
|
||||
foreach (var item in _testData.Data)
|
||||
{
|
||||
qResults.Add(trix.Update(item).Value);
|
||||
}
|
||||
|
||||
var retCode = TALib.Functions.Trix<double>(tData, 0..^0, tOutput, out var outRange, period);
|
||||
Assert.Equal(TALib.Core.RetCode.Success, retCode);
|
||||
|
||||
int lookback = TALib.Functions.TrixLookback(period);
|
||||
|
||||
ValidationHelper.VerifyData(qResults, tOutput, outRange, lookback);
|
||||
}
|
||||
_output.WriteLine("TRIX Streaming validated successfully against TA-Lib");
|
||||
}
|
||||
|
||||
// ── F) Tulip Batch ───────────────────────────────────────────────────────
|
||||
[Fact]
|
||||
public void Validate_Tulip_Batch()
|
||||
{
|
||||
int[] periods = [9, 14, 25];
|
||||
double[] tData = _testData.RawData.ToArray();
|
||||
|
||||
foreach (var period in periods)
|
||||
{
|
||||
var trix = new global::QuanTAlib.Trix(period);
|
||||
var qResult = trix.Update(_testData.Data);
|
||||
|
||||
var trixIndicator = Tulip.Indicators.trix;
|
||||
double[][] inputs = [tData];
|
||||
double[] options = [period];
|
||||
|
||||
int lookback = trixIndicator.Start(options);
|
||||
double[][] outputs = [new double[tData.Length - lookback]];
|
||||
|
||||
trixIndicator.Run(inputs, options, outputs);
|
||||
var tResult = outputs[0];
|
||||
|
||||
// Tulip uses non-compensated EMA; warmup compensation causes persistent diffs
|
||||
// TRIX amplifies by 100×, so small EMA diffs become noticeable in TRIX
|
||||
ValidationHelper.VerifyData(qResult, tResult, lookback, tolerance: 1e-3);
|
||||
}
|
||||
_output.WriteLine("TRIX Batch(TSeries) validated successfully against Tulip");
|
||||
}
|
||||
|
||||
// ── G) Tulip Span ────────────────────────────────────────────────────────
|
||||
[Fact]
|
||||
public void Validate_Tulip_Span()
|
||||
{
|
||||
int[] periods = [14, 20, 50, 100];
|
||||
double[] tData = _testData.RawData.ToArray();
|
||||
|
||||
foreach (var period in periods)
|
||||
{
|
||||
double[] qOutput = new double[tData.Length];
|
||||
global::QuanTAlib.Trix.Batch(tData.AsSpan(), qOutput.AsSpan(), period);
|
||||
|
||||
var trixIndicator = Tulip.Indicators.trix;
|
||||
double[][] inputs = [tData];
|
||||
double[] options = [period];
|
||||
int lookback = trixIndicator.Start(options);
|
||||
double[][] outputs = [new double[tData.Length - lookback]];
|
||||
|
||||
trixIndicator.Run(inputs, options, outputs);
|
||||
var tResult = outputs[0];
|
||||
|
||||
// Tulip uses non-compensated EMA; warmup compensation causes minor convergence diffs
|
||||
// TRIX amplifies by 100×, so EMA diffs of ~1e-6 become ~1e-4 in TRIX
|
||||
ValidationHelper.VerifyData(qOutput, tResult, lookback, tolerance: 5e-4);
|
||||
}
|
||||
_output.WriteLine("TRIX Span validated against Tulip");
|
||||
}
|
||||
|
||||
// ── H) Tulip Streaming ───────────────────────────────────────────────────
|
||||
[Fact]
|
||||
public void Validate_Tulip_Streaming()
|
||||
{
|
||||
int[] periods = [9, 14, 25];
|
||||
double[] tData = _testData.RawData.ToArray();
|
||||
|
||||
foreach (var period in periods)
|
||||
{
|
||||
var trix = new global::QuanTAlib.Trix(period);
|
||||
var qResults = new List<double>();
|
||||
foreach (var item in _testData.Data)
|
||||
{
|
||||
qResults.Add(trix.Update(item).Value);
|
||||
}
|
||||
|
||||
var trixIndicator = Tulip.Indicators.trix;
|
||||
double[][] inputs = [tData];
|
||||
double[] options = [period];
|
||||
|
||||
int lookback = trixIndicator.Start(options);
|
||||
double[][] outputs = [new double[tData.Length - lookback]];
|
||||
|
||||
trixIndicator.Run(inputs, options, outputs);
|
||||
var tResult = outputs[0];
|
||||
|
||||
// Tulip uses non-compensated EMA; warmup compensation causes persistent diffs
|
||||
// TRIX amplifies by 100×, so small EMA diffs become noticeable in TRIX
|
||||
ValidationHelper.VerifyData(qResults, tResult, lookback, tolerance: 1e-3);
|
||||
}
|
||||
_output.WriteLine("TRIX Streaming validated successfully against Tulip");
|
||||
}
|
||||
|
||||
// ── I) Self-Consistency: All Modes ────────────────────────────────────────
|
||||
[Fact]
|
||||
public void Validate_AllModes_ProduceIdenticalResults()
|
||||
{
|
||||
int[] periods = [5, 10, 20, 50];
|
||||
|
||||
foreach (var period in periods)
|
||||
{
|
||||
// 1. Batch Mode (TSeries)
|
||||
var batchTrix = new global::QuanTAlib.Trix(period);
|
||||
var batchResult = batchTrix.Update(_testData.Data);
|
||||
|
||||
// 2. Span Mode
|
||||
double[] sourceData = _testData.RawData.ToArray();
|
||||
double[] spanOutput = new double[sourceData.Length];
|
||||
global::QuanTAlib.Trix.Batch(sourceData.AsSpan(), spanOutput.AsSpan(), period);
|
||||
|
||||
// 3. Streaming Mode
|
||||
var streamingTrix = new global::QuanTAlib.Trix(period);
|
||||
var streamingResults = new List<double>();
|
||||
foreach (var item in _testData.Data)
|
||||
{
|
||||
streamingResults.Add(streamingTrix.Update(item).Value);
|
||||
}
|
||||
|
||||
// Compare all modes
|
||||
for (int i = 0; i < _testData.Data.Count; i++)
|
||||
{
|
||||
Assert.Equal(batchResult[i].Value, spanOutput[i], 1e-8);
|
||||
Assert.Equal(batchResult[i].Value, streamingResults[i], 1e-8);
|
||||
}
|
||||
}
|
||||
_output.WriteLine("All modes validated to produce identical results");
|
||||
}
|
||||
|
||||
// ── J) Self-Consistency: Convergence ──────────────────────────────────────
|
||||
[Fact]
|
||||
public void Validate_Convergence_AfterWarmup()
|
||||
{
|
||||
int[] periods = [5, 10, 20, 50];
|
||||
|
||||
foreach (var period in periods)
|
||||
{
|
||||
var trix = new global::QuanTAlib.Trix(period);
|
||||
int warmup = trix.WarmupPeriod; // period * 3
|
||||
|
||||
Assert.False(trix.IsHot);
|
||||
|
||||
for (int i = 0; i < warmup - 1; i++)
|
||||
{
|
||||
trix.Update(_testData.Data[i]);
|
||||
Assert.False(trix.IsHot);
|
||||
}
|
||||
|
||||
trix.Update(_testData.Data[warmup - 1]);
|
||||
Assert.True(trix.IsHot);
|
||||
}
|
||||
}
|
||||
|
||||
// ── K) NaN Robustness ────────────────────────────────────────────────────
|
||||
[Fact]
|
||||
public void Validate_HandlesNaN_Gracefully()
|
||||
{
|
||||
var trix = new global::QuanTAlib.Trix(10);
|
||||
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
trix.Update(_testData.Data[i]);
|
||||
}
|
||||
|
||||
var result = trix.Update(new TValue(DateTime.UtcNow, double.NaN));
|
||||
Assert.True(double.IsFinite(result.Value));
|
||||
|
||||
for (int i = 20; i < 30; i++)
|
||||
{
|
||||
var r = trix.Update(_testData.Data[i]);
|
||||
Assert.True(double.IsFinite(r.Value));
|
||||
}
|
||||
}
|
||||
|
||||
// ── L) Infinity Robustness ───────────────────────────────────────────────
|
||||
[Fact]
|
||||
public void Validate_HandlesInfinity_Gracefully()
|
||||
{
|
||||
var trix = new global::QuanTAlib.Trix(10);
|
||||
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
trix.Update(_testData.Data[i]);
|
||||
}
|
||||
|
||||
var resultPos = trix.Update(new TValue(DateTime.UtcNow, double.PositiveInfinity));
|
||||
Assert.True(double.IsFinite(resultPos.Value));
|
||||
|
||||
var resultNeg = trix.Update(new TValue(DateTime.UtcNow, double.NegativeInfinity));
|
||||
Assert.True(double.IsFinite(resultNeg.Value));
|
||||
}
|
||||
|
||||
// ── M) Zero Crossing Behavior ────────────────────────────────────────────
|
||||
[Fact]
|
||||
public void Validate_ZeroCrossing_DetectsDirectionChange()
|
||||
{
|
||||
var trix = new global::QuanTAlib.Trix(3);
|
||||
|
||||
// Feed a long sustained uptrend to ensure TRIX stabilizes positive
|
||||
for (int i = 0; i < 50; i++)
|
||||
{
|
||||
trix.Update(new TValue(DateTime.UtcNow, 100 + i * 2));
|
||||
}
|
||||
double uptrendTrix = trix.Last.Value;
|
||||
Assert.True(uptrendTrix > 0, $"Sustained uptrend should produce positive TRIX, got {uptrendTrix}");
|
||||
|
||||
// Feed a long sustained downtrend
|
||||
for (int i = 0; i < 50; i++)
|
||||
{
|
||||
trix.Update(new TValue(DateTime.UtcNow, 200 - i * 2));
|
||||
}
|
||||
double downtrendTrix = trix.Last.Value;
|
||||
Assert.True(downtrendTrix < 0, $"Sustained downtrend should produce negative TRIX, got {downtrendTrix}");
|
||||
}
|
||||
|
||||
// ── N) Flat Line ─────────────────────────────────────────────────────────
|
||||
[Fact]
|
||||
public void Validate_FlatLine_ProducesZeroTrix()
|
||||
{
|
||||
var trix = new global::QuanTAlib.Trix(10);
|
||||
|
||||
for (int i = 0; i < 200; i++)
|
||||
{
|
||||
trix.Update(new TValue(DateTime.UtcNow, 100));
|
||||
}
|
||||
|
||||
// After sufficient warmup with flat data, TRIX ≈ 0
|
||||
// Warmup compensation introduces tiny residual; 1e-4 is sufficient
|
||||
Assert.True(Math.Abs(trix.Last.Value) < 1e-4,
|
||||
$"Expected TRIX ≈ 0 for flat line, got {trix.Last.Value}");
|
||||
}
|
||||
|
||||
// ── O) Large Dataset Precision ───────────────────────────────────────────
|
||||
[Fact]
|
||||
public void Validate_LargeDataset_MaintainsPrecision()
|
||||
{
|
||||
const int period = 20;
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 42);
|
||||
var bars = gbm.Fetch(10_000, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
// Compare batch vs streaming on last 100 points of large dataset
|
||||
var batchResult = global::QuanTAlib.Trix.Batch(bars.Close, period);
|
||||
|
||||
var streamTrix = new global::QuanTAlib.Trix(period);
|
||||
for (int i = 0; i < bars.Close.Count; i++)
|
||||
{
|
||||
streamTrix.Update(bars.Close[i]);
|
||||
}
|
||||
|
||||
// Verify final values match
|
||||
Assert.Equal(batchResult.Last.Value, streamTrix.Last.Value, 1e-9);
|
||||
}
|
||||
|
||||
// ── P) Different Periods ─────────────────────────────────────────────────
|
||||
[Fact]
|
||||
public void Validate_DifferentPeriods_ProduceDifferentSensitivity()
|
||||
{
|
||||
var trix5 = new global::QuanTAlib.Trix(5);
|
||||
var trix20 = new global::QuanTAlib.Trix(20);
|
||||
var trix50 = new global::QuanTAlib.Trix(50);
|
||||
|
||||
for (int i = 0; i < _testData.Data.Count; i++)
|
||||
{
|
||||
trix5.Update(_testData.Data[i]);
|
||||
trix20.Update(_testData.Data[i]);
|
||||
trix50.Update(_testData.Data[i]);
|
||||
}
|
||||
|
||||
Assert.True(double.IsFinite(trix5.Last.Value));
|
||||
Assert.True(double.IsFinite(trix20.Last.Value));
|
||||
Assert.True(double.IsFinite(trix50.Last.Value));
|
||||
}
|
||||
|
||||
// ── Q) Batch Span NaN ────────────────────────────────────────────────────
|
||||
[Fact]
|
||||
public void Validate_BatchSpan_HandlesNaN_InMiddle()
|
||||
{
|
||||
double[] data = new double[100];
|
||||
var gbm = new GBM(startPrice: 100, seed: 42);
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
data[i] = gbm.Next().Close;
|
||||
}
|
||||
|
||||
data[50] = double.NaN;
|
||||
|
||||
double[] result = new double[100];
|
||||
global::QuanTAlib.Trix.Batch(data.AsSpan(), result.AsSpan(), 10);
|
||||
|
||||
foreach (var value in result)
|
||||
{
|
||||
Assert.True(double.IsFinite(value), $"Expected finite value, got {value}");
|
||||
}
|
||||
}
|
||||
|
||||
// ── Cross-library: OoplesFinance ──────────────────────────────────────────
|
||||
[Fact]
|
||||
public void Trix_MatchesOoples_Structural()
|
||||
{
|
||||
const int period = 14;
|
||||
var ooplesData = _testData.SkenderQuotes.Select(static q => new TickerData
|
||||
{
|
||||
Date = q.Date,
|
||||
Open = (double)q.Open,
|
||||
High = (double)q.High,
|
||||
Low = (double)q.Low,
|
||||
Close = (double)q.Close,
|
||||
Volume = (double)q.Volume
|
||||
}).ToList();
|
||||
|
||||
var stockData = new StockData(ooplesData);
|
||||
var oResult = stockData.CalculateTrix(length: period);
|
||||
var oValues = oResult.OutputValues.Values.First();
|
||||
|
||||
var trix = new global::QuanTAlib.Trix(period);
|
||||
var qValues = new List<double>();
|
||||
foreach (var item in _testData.Data)
|
||||
{
|
||||
qValues.Add(trix.Update(item).Value);
|
||||
}
|
||||
|
||||
Assert.True(oValues.Count > 0, "Ooples Trix must produce output");
|
||||
int finiteCount = 0;
|
||||
for (int i = period; i < Math.Min(oValues.Count, qValues.Count); i++)
|
||||
{
|
||||
if (double.IsFinite(oValues[i]) && double.IsFinite(qValues[i]))
|
||||
{
|
||||
finiteCount++;
|
||||
}
|
||||
}
|
||||
Assert.True(finiteCount > 100, $"Expected >100 finite Trix pairs, got {finiteCount}");
|
||||
_output.WriteLine($"Trix Ooples structural: {finiteCount} finite pairs verified.");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user