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,171 @@
using TradingPlatform.BusinessLayer;
using QuanTAlib;
namespace QuanTAlib.Tests;
public sealed class StochrsiIndicatorTests
{
[Fact]
public void StochrsiIndicator_Constructor_SetsDefaults()
{
var indicator = new StochrsiIndicator();
Assert.Equal(14, indicator.RsiLength);
Assert.Equal(14, indicator.StochLength);
Assert.Equal(3, indicator.KSmooth);
Assert.Equal(3, indicator.DSmooth);
Assert.Equal(SourceType.Close, indicator.Source);
Assert.True(indicator.ShowColdValues);
Assert.Contains("STOCHRSI", indicator.Name, StringComparison.OrdinalIgnoreCase);
Assert.True(indicator.SeparateWindow);
Assert.True(indicator.OnBackGround);
}
[Fact]
public void StochrsiIndicator_MinHistoryDepths_EqualsZero()
{
var indicator = new StochrsiIndicator();
Assert.Equal(0, StochrsiIndicator.MinHistoryDepths);
IWatchlistIndicator watchlistIndicator = indicator;
Assert.Equal(0, watchlistIndicator.MinHistoryDepths);
}
[Fact]
public void StochrsiIndicator_ShortName_IncludesParameters()
{
var indicator = new StochrsiIndicator { RsiLength = 14, StochLength = 14, KSmooth = 3, DSmooth = 3 };
indicator.Initialize();
Assert.Contains("StochRSI", indicator.ShortName, StringComparison.Ordinal);
Assert.Contains("14", indicator.ShortName, StringComparison.Ordinal);
}
[Fact]
public void StochrsiIndicator_SourceCodeLink_IsValid()
{
var indicator = new StochrsiIndicator();
Assert.Contains("github.com", indicator.SourceCodeLink, StringComparison.Ordinal);
Assert.Contains("Stochrsi", indicator.SourceCodeLink, StringComparison.Ordinal);
}
[Fact]
public void StochrsiIndicator_Initialize_CreatesLineSeries()
{
var indicator = new StochrsiIndicator();
indicator.Initialize();
// K and D line series
Assert.Equal(2, indicator.LinesSeries.Count);
}
[Fact]
public void StochrsiIndicator_ProcessUpdate_HistoricalBar_ComputesValues()
{
var indicator = new StochrsiIndicator { RsiLength = 5, StochLength = 5, KSmooth = 3, DSmooth = 3 };
indicator.Initialize();
var now = DateTime.UtcNow;
for (int i = 0; i < 30; i++)
{
double price = 100.0 + (i * 0.5);
indicator.HistoricalData.AddBar(now.AddMinutes(i), price, price + 1, price - 1, price + 0.5);
var args = new UpdateArgs(UpdateReason.HistoricalBar);
indicator.ProcessUpdate(args);
}
double k = indicator.LinesSeries[0].GetValue(0);
double d = indicator.LinesSeries[1].GetValue(0);
Assert.True(double.IsFinite(k));
Assert.True(double.IsFinite(d));
}
[Fact]
public void StochrsiIndicator_ProcessUpdate_NewBar_ComputesValues()
{
var indicator = new StochrsiIndicator { RsiLength = 5, StochLength = 5, KSmooth = 3, DSmooth = 3 };
indicator.Initialize();
var now = DateTime.UtcNow;
for (int i = 0; i < 20; i++)
{
double price = 100.0 + (i * 0.5);
indicator.HistoricalData.AddBar(now.AddMinutes(i), price, price + 1, price - 1, price + 0.5);
var args = new UpdateArgs(UpdateReason.HistoricalBar);
indicator.ProcessUpdate(args);
}
// Simulate a new bar
indicator.HistoricalData.AddBar(now.AddMinutes(20), 110, 120, 100, 115);
var newArgs = new UpdateArgs(UpdateReason.NewBar);
indicator.ProcessUpdate(newArgs);
double k = indicator.LinesSeries[0].GetValue(0);
double d = indicator.LinesSeries[1].GetValue(0);
Assert.True(double.IsFinite(k));
Assert.True(double.IsFinite(d));
}
[Fact]
public void StochrsiIndicator_DifferentSource_Works()
{
var indicator = new StochrsiIndicator
{
RsiLength = 5,
StochLength = 5,
KSmooth = 3,
DSmooth = 3,
Source = SourceType.Open,
};
indicator.Initialize();
var now = DateTime.UtcNow;
for (int i = 0; i < 30; i++)
{
double price = 100.0 + (i * 0.3);
indicator.HistoricalData.AddBar(now.AddMinutes(i), price, price + 2, price - 2, price + 1);
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
}
double k = indicator.LinesSeries[0].GetValue(0);
Assert.True(double.IsFinite(k));
}
[Fact]
public void StochrsiIndicator_CustomParameters_Work()
{
var indicator = new StochrsiIndicator
{
RsiLength = 7,
StochLength = 10,
KSmooth = 2,
DSmooth = 5,
};
indicator.Initialize();
var now = DateTime.UtcNow;
for (int i = 0; i < 40; i++)
{
double price = 100.0 + (i * 0.4);
indicator.HistoricalData.AddBar(now.AddMinutes(i), price, price + 1, price - 1, price + 0.5);
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
}
double k = indicator.LinesSeries[0].GetValue(0);
double d = indicator.LinesSeries[1].GetValue(0);
Assert.True(double.IsFinite(k));
Assert.True(double.IsFinite(d));
}
[Fact]
public void StochrsiIndicator_ShowColdValues_Default_True()
{
var indicator = new StochrsiIndicator();
Assert.True(indicator.ShowColdValues);
}
}
@@ -0,0 +1,768 @@
using Xunit;
namespace QuanTAlib.Tests;
// ── A) Constructor Validation ──────────────────────────────────────
public sealed class StochrsiConstructorTests
{
[Fact]
public void DefaultParameters_AreCorrect()
{
var ind = new Stochrsi();
Assert.Equal("StochRsi(14,14,3,3)", ind.Name);
// WarmupPeriod = rsi.WarmupPeriod(15) + stochLength(14)-1 + kSmooth(3)-1 + dSmooth(3)-1 = 32
Assert.Equal(32, ind.WarmupPeriod);
}
[Fact]
public void CustomParameters_SetsNameCorrectly()
{
var ind = new Stochrsi(7, 10, 2, 5);
Assert.Equal("StochRsi(7,10,2,5)", ind.Name);
}
[Theory]
[InlineData(0, 14, 3, 3, "rsiLength")]
[InlineData(-1, 14, 3, 3, "rsiLength")]
[InlineData(14, 0, 3, 3, "stochLength")]
[InlineData(14, -1, 3, 3, "stochLength")]
[InlineData(14, 14, 0, 3, "kSmooth")]
[InlineData(14, 14, -1, 3, "kSmooth")]
[InlineData(14, 14, 3, 0, "dSmooth")]
[InlineData(14, 14, 3, -1, "dSmooth")]
public void InvalidParameters_ThrowsArgumentException(int rsi, int stoch, int k, int d, string paramName)
{
var ex = Assert.Throws<ArgumentException>(() => new Stochrsi(rsi, stoch, k, d));
Assert.Equal(paramName, ex.ParamName);
}
[Fact]
public void MinimalParameters_Work()
{
var ind = new Stochrsi(1, 1, 1, 1);
Assert.Equal("StochRsi(1,1,1,1)", ind.Name);
}
[Fact]
public void Constructor_PeriodOne_IsValid()
{
var ind = new Stochrsi(1, 1, 1, 1);
Assert.NotNull(ind);
}
}
// ── B) Basic Calculation ───────────────────────────────────────────
public sealed class StochrsiBasicTests
{
[Fact]
public void Update_ReturnsTValue()
{
var ind = new Stochrsi();
TValue result = ind.Update(new TValue(DateTime.UtcNow, 100));
Assert.True(double.IsFinite(result.Value) || double.IsNaN(result.Value));
}
[Fact]
public void Last_IsAccessible()
{
var ind = new Stochrsi(5, 5, 2, 2);
ind.Update(new TValue(DateTime.UtcNow, 100));
ind.Update(new TValue(DateTime.UtcNow, 110));
Assert.IsType<TValue>(ind.Last);
}
[Fact]
public void Name_Available()
{
var ind = new Stochrsi(7, 10, 2, 5);
Assert.Equal("StochRsi(7,10,2,5)", ind.Name);
}
[Fact]
public void KAndD_AreAccessible()
{
var ind = new Stochrsi(3, 3, 1, 1);
var gbm = new GBM(startPrice: 100.0, mu: 0.05, sigma: 0.2, seed: 42);
for (int i = 0; i < 30; i++)
{
var bar = gbm.Next(isNew: true);
ind.Update(new TValue(bar.Time, bar.Close));
}
Assert.True(double.IsFinite(ind.K));
Assert.True(double.IsFinite(ind.D));
}
[Fact]
public void ConvergedValues_InRange0to100()
{
var ind = new Stochrsi(7, 7, 3, 3);
var gbm = new GBM(startPrice: 100.0, mu: 0.05, sigma: 0.2, seed: 42);
for (int i = 0; i < 100; i++)
{
var bar = gbm.Next(isNew: true);
ind.Update(new TValue(bar.Time, bar.Close));
}
Assert.True(ind.IsHot);
Assert.InRange(ind.K, -0.01, 100.01);
Assert.InRange(ind.D, -0.01, 100.01);
}
}
// ── C) State + Bar Correction ──────────────────────────────────────
public sealed class StochrsiBarCorrectionTests
{
[Fact]
public void IsNew_True_AdvancesState()
{
var ind = new Stochrsi(5, 5, 2, 2);
ind.Update(new TValue(DateTime.UtcNow, 100), isNew: true);
double val1 = ind.Last.Value;
ind.Update(new TValue(DateTime.UtcNow, 150), isNew: true);
double val2 = ind.Last.Value;
Assert.NotEqual(val1, val2);
}
[Fact]
public void IsNew_False_Rollback()
{
var ind = new Stochrsi(5, 5, 2, 2);
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 < 30; i++)
{
var bar = gbm.Next(isNew: true);
ind.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 = ind.Update(originalInput, isNew: true);
// Correct with isNew=false (different value)
ind.Update(new TValue(nextBar.Time, nextBar.Close + 50), isNew: false);
// Re-apply original value with isNew=false → should match val1
var restored = ind.Update(originalInput, isNew: false);
Assert.Equal(val1.Value, restored.Value, 1e-10);
}
[Fact]
public void IterativeCorrections_RestoreToOriginalState()
{
var ind = new Stochrsi(5, 5, 2, 2);
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.1);
// Feed 30 new values
TValue thirtiethInput = default;
for (int i = 0; i < 30; i++)
{
var bar = gbm.Next(isNew: true);
thirtiethInput = new TValue(bar.Time, bar.Close);
ind.Update(thirtiethInput, isNew: true);
}
double stateAfterThirty = ind.Last.Value;
// Generate 9 corrections with isNew=false (different values)
for (int i = 0; i < 9; i++)
{
var bar = gbm.Next(isNew: false);
ind.Update(new TValue(bar.Time, bar.Close), isNew: false);
}
// Feed the remembered 30th input again with isNew=false
TValue finalResult = ind.Update(thirtiethInput, isNew: false);
Assert.Equal(stateAfterThirty, finalResult.Value, 1e-10);
}
}
// ── D) Warmup / Convergence ────────────────────────────────────────
public sealed class StochrsiWarmupTests
{
[Fact]
public void IsHot_InitiallyFalse()
{
var ind = new Stochrsi();
Assert.False(ind.IsHot);
}
[Fact]
public void IsHot_BecomesTrueAfterSufficientBars()
{
var ind = new Stochrsi(3, 3, 1, 1);
var gbm = new GBM(startPrice: 100.0, mu: 0.05, sigma: 0.2, seed: 42);
// Feed bars until hot
bool becameHot = false;
for (int i = 0; i < 100; i++)
{
var bar = gbm.Next(isNew: true);
ind.Update(new TValue(bar.Time, bar.Close));
if (ind.IsHot)
{
becameHot = true;
break;
}
}
Assert.True(becameHot);
}
[Fact]
public void IsHot_StaysTrue()
{
var ind = new Stochrsi(3, 3, 1, 1);
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);
ind.Update(new TValue(bar.Time, bar.Close));
}
Assert.True(ind.IsHot);
// Feed more bars, should stay hot
for (int i = 0; i < 20; i++)
{
var bar = gbm.Next(isNew: true);
ind.Update(new TValue(bar.Time, bar.Close));
Assert.True(ind.IsHot);
}
}
[Fact]
public void WarmupPeriod_ScalesWithParameters()
{
// Default: rsiLength=14 → rsi.WarmupPeriod=15
// warm = 15 + 14-1 + 3-1 + 3-1 = 32
var ind1 = new Stochrsi(14, 14, 3, 3);
Assert.Equal(32, ind1.WarmupPeriod);
// Custom: rsiLength=7 → rsi.WarmupPeriod=8
// warm = 8 + 10-1 + 2-1 + 5-1 = 22
var ind2 = new Stochrsi(7, 10, 2, 5);
Assert.Equal(22, ind2.WarmupPeriod);
}
}
// ── E) Robustness (NaN / Infinity) ─────────────────────────────────
public sealed class StochrsiRobustnessTests
{
[Fact]
public void NaN_Input_UsesLastValidValue()
{
var ind = new Stochrsi(5, 5, 2, 2);
var gbm = new GBM(startPrice: 100, mu: 0.02, sigma: 0.1, seed: 42);
var bars = gbm.Fetch(30, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
for (int i = 0; i < 25; i++)
{
ind.Update(new TValue(bars[i].Time, bars[i].Close));
}
var result = ind.Update(new TValue(DateTime.UtcNow, double.NaN));
Assert.True(double.IsFinite(result.Value));
}
[Fact]
public void Infinity_Input_UsesLastValidValue()
{
var ind = new Stochrsi(5, 5, 2, 2);
var gbm = new GBM(startPrice: 100, mu: 0.02, sigma: 0.1, seed: 42);
var bars = gbm.Fetch(30, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
for (int i = 0; i < 25; i++)
{
ind.Update(new TValue(bars[i].Time, bars[i].Close));
}
var resultPos = ind.Update(new TValue(DateTime.UtcNow, double.PositiveInfinity));
Assert.True(double.IsFinite(resultPos.Value));
var resultNeg = ind.Update(new TValue(DateTime.UtcNow, double.NegativeInfinity));
Assert.True(double.IsFinite(resultNeg.Value));
}
[Fact]
public void BatchNaN_DoesNotCrash()
{
double[] source = [100, 110, 120, 130, 140, double.NaN, 160, 170, 180, 190];
double[] output = new double[source.Length];
Stochrsi.Batch(source.AsSpan(), output.AsSpan(), 3, 3, 1, 1);
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 StochrsiConsistencyTests
{
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 rsiLen = 7;
const int stochLen = 7;
const int kSm = 3;
const int dSm = 3;
var series = GenerateCloseSeries(100);
// 1. Batch Mode (TSeries)
var batchSeries = Stochrsi.Batch(series, rsiLen, stochLen, kSm, dSm);
double expected = batchSeries.Last.Value;
// 2. Span Mode
var spanInput = series.Values.ToArray();
var spanOutput = new double[spanInput.Length];
Stochrsi.Batch(spanInput.AsSpan(), spanOutput.AsSpan(), rsiLen, stochLen, kSm, dSm);
double spanResult = spanOutput[^1];
// 3. Streaming Mode
var streamingInd = new Stochrsi(rsiLen, stochLen, kSm, dSm);
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 Stochrsi(pubSource, rsiLen, stochLen, kSm, dSm);
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 rsiLen = 5;
const int stochLen = 5;
const int kSm = 2;
const int dSm = 2;
var series = GenerateCloseSeries(50);
// Batch
var batchSeries = Stochrsi.Batch(series, rsiLen, stochLen, kSm, dSm);
// Streaming
var streamingInd = new Stochrsi(rsiLen, stochLen, kSm, dSm);
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 rsiLen = 7;
const int stochLen = 7;
const int kSm = 3;
const int dSm = 3;
var series = GenerateCloseSeries(80);
var batchSeries = Stochrsi.Batch(series, rsiLen, stochLen, kSm, dSm);
var spanInput = series.Values.ToArray();
var spanOutput = new double[spanInput.Length];
Stochrsi.Batch(spanInput.AsSpan(), spanOutput.AsSpan(), rsiLen, stochLen, kSm, dSm);
for (int i = 0; i < series.Count; i++)
{
Assert.Equal(batchSeries[i].Value, spanOutput[i], 1e-10);
}
}
}
// ── G) Span API Tests ──────────────────────────────────────────────
public sealed class StochrsiSpanTests
{
[Fact]
public void Batch_Span_MismatchedLengths_Throws()
{
double[] source = new double[10];
double[] output = new double[5];
var ex = Assert.Throws<ArgumentException>(
() => Stochrsi.Batch(source.AsSpan(), output.AsSpan(), 3, 3, 1, 1));
Assert.Equal("output", ex.ParamName);
}
[Fact]
public void Batch_Span_ZeroRsiLength_Throws()
{
double[] source = new double[10];
double[] output = new double[10];
var ex = Assert.Throws<ArgumentException>(
() => Stochrsi.Batch(source.AsSpan(), output.AsSpan(), 0, 3, 1, 1));
Assert.Equal("rsiLength", ex.ParamName);
}
[Fact]
public void Batch_Span_ZeroStochLength_Throws()
{
double[] source = new double[10];
double[] output = new double[10];
var ex = Assert.Throws<ArgumentException>(
() => Stochrsi.Batch(source.AsSpan(), output.AsSpan(), 3, 0, 1, 1));
Assert.Equal("stochLength", ex.ParamName);
}
[Fact]
public void Batch_Span_ZeroKSmooth_Throws()
{
double[] source = new double[10];
double[] output = new double[10];
var ex = Assert.Throws<ArgumentException>(
() => Stochrsi.Batch(source.AsSpan(), output.AsSpan(), 3, 3, 0, 1));
Assert.Equal("kSmooth", ex.ParamName);
}
[Fact]
public void Batch_Span_ZeroDSmooth_Throws()
{
double[] source = new double[10];
double[] output = new double[10];
var ex = Assert.Throws<ArgumentException>(
() => Stochrsi.Batch(source.AsSpan(), output.AsSpan(), 3, 3, 1, 0));
Assert.Equal("dSmooth", ex.ParamName);
}
[Fact]
public void Batch_Span_EmptyArrays_DoesNotThrow()
{
double[] source = [];
double[] output = [];
Stochrsi.Batch(source.AsSpan(), output.AsSpan(), 3, 3, 1, 1);
Assert.Empty(output);
}
[Fact]
public void Batch_Span_SingleElement()
{
double[] source = [100.0];
double[] output = new double[1];
Stochrsi.Batch(source.AsSpan(), output.AsSpan(), 5, 5, 1, 1);
Assert.True(double.IsFinite(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;
}
Stochrsi.Batch(source.AsSpan(), output.AsSpan(), 14, 14, 3, 3);
Assert.True(double.IsFinite(output[^1]));
}
[Fact]
public void Batch_Span_NaN_HandlesGracefully()
{
double[] source = new double[30];
double[] output = new double[30];
var gbm = new GBM(startPrice: 100, seed: 42);
for (int i = 0; i < 30; i++)
{
var bar = gbm.Next(isNew: true);
source[i] = bar.Close;
}
// Inject NaN at indices 5, 15, 25
source[5] = double.NaN;
source[15] = double.NaN;
source[25] = double.NaN;
Stochrsi.Batch(source.AsSpan(), output.AsSpan(), 3, 3, 1, 1);
for (int i = 0; i < output.Length; i++)
{
Assert.True(double.IsFinite(output[i]), $"Output[{i}] is not finite");
}
}
}
// ── H) Chainability ────────────────────────────────────────────────
public sealed class StochrsiEventTests
{
[Fact]
public void Chainability_Works()
{
var stochrsi = new Stochrsi(5, 5, 2, 2);
// Chain another AbstractBase indicator from StochRSI output
var ema = new Ema(stochrsi, 3);
var gbm = new GBM(startPrice: 100.0, mu: 0.05, sigma: 0.2, seed: 42);
for (int i = 0; i < 30; i++)
{
var bar = gbm.Next(isNew: true);
stochrsi.Update(new TValue(bar.Time, bar.Close));
}
Assert.True(double.IsFinite(ema.Last.Value));
}
[Fact]
public void EventChaining_ProducesResults()
{
var source = new TSeries();
var ind = new Stochrsi(source, 5, 5, 2, 2);
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);
source.Add(bar.Time, bar.Close);
}
Assert.True(double.IsFinite(ind.Last.Value));
Assert.True(ind.IsHot);
}
[Fact]
public void Pub_FiresOnUpdate()
{
var ind = new Stochrsi(5, 5, 2, 2);
int eventCount = 0;
ind.Pub += HandleEvent;
for (int i = 0; i < 10; i++)
{
ind.Update(new TValue(DateTime.UtcNow, 100 + i));
}
Assert.Equal(10, eventCount);
ind.Pub -= HandleEvent;
void HandleEvent(object? sender, in TValueEventArgs e)
{
eventCount++;
}
}
}
// ── Extra: Batch Tests ─────────────────────────────────────────────
public sealed class StochrsiBatchTests
{
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 = Stochrsi.Batch(series, 5, 5, 2, 2);
Assert.Equal(50, result.Count);
}
[Fact]
public void Batch_TSeries_PreservesTimestamps()
{
var series = GenerateCloseSeries(30);
var result = Stochrsi.Batch(series, 5, 5, 2, 2);
for (int i = 0; i < series.Count; i++)
{
Assert.Equal(series[i].Time, result[i].Time);
}
}
[Fact]
public void Calculate_ReturnsIndicatorAndResults()
{
var series = GenerateCloseSeries(50);
var (results, indicator) = Stochrsi.Calculate(series, 5, 5, 2, 2);
Assert.NotNull(indicator);
Assert.Equal(50, results.Count);
Assert.True(indicator.IsHot);
}
[Fact]
public void UpdateKD_ReturnsBothKAndDSeries()
{
var series = GenerateCloseSeries(50);
var ind = new Stochrsi(5, 5, 2, 2);
var (kSeries, dSeries) = ind.UpdateKD(series);
Assert.Equal(50, kSeries.Count);
Assert.Equal(50, dSeries.Count);
// After warmup, values should be in 0-100 range
Assert.True(double.IsFinite(kSeries.Last.Value));
Assert.True(double.IsFinite(dSeries.Last.Value));
}
[Fact]
public void UpdateKD_EmptySeries_ReturnsEmpty()
{
var ind = new Stochrsi(5, 5, 2, 2);
var (kSeries, dSeries) = ind.UpdateKD(new TSeries());
Assert.Empty(kSeries);
Assert.Empty(dSeries);
}
}
// ── Extra: Reset Tests ─────────────────────────────────────────────
public sealed class StochrsiResetTests
{
[Fact]
public void Reset_ClearsState()
{
var ind = new Stochrsi(5, 5, 2, 2);
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);
ind.Update(new TValue(bar.Time, bar.Close));
}
Assert.True(ind.IsHot);
ind.Reset();
Assert.False(ind.IsHot);
Assert.Equal(0, ind.Last.Value);
}
[Fact]
public void Reset_AcceptsNewValues()
{
var ind = new Stochrsi(5, 5, 2, 2);
var gbm = new GBM(startPrice: 100, seed: 42);
for (int i = 0; i < 30; i++)
{
var bar = gbm.Next(isNew: true);
ind.Update(new TValue(bar.Time, bar.Close));
}
ind.Reset();
// After reset, should accept new values without error
var result = ind.Update(new TValue(DateTime.UtcNow, 50));
Assert.True(double.IsFinite(result.Value));
}
}
// ── Extra: Prime Tests ─────────────────────────────────────────────
public sealed class StochrsiPrimeTests
{
[Fact]
public void Prime_SetsUpState()
{
var ind = new Stochrsi(5, 5, 2, 2);
var gbm = new GBM(startPrice: 100, mu: 0.02, sigma: 0.1, seed: 42);
double[] data = new double[30];
for (int i = 0; i < 30; i++)
{
var bar = gbm.Next(isNew: true);
data[i] = bar.Close;
}
ind.Prime(data.AsSpan());
Assert.True(ind.IsHot);
Assert.True(double.IsFinite(ind.Last.Value));
}
[Fact]
public void Prime_ThenUpdate_ProducesValidResults()
{
var ind = new Stochrsi(5, 5, 2, 2);
var gbm = new GBM(startPrice: 100, mu: 0.02, sigma: 0.1, seed: 42);
double[] data = new double[30];
for (int i = 0; i < 30; i++)
{
var bar = gbm.Next(isNew: true);
data[i] = bar.Close;
}
ind.Prime(data.AsSpan());
// Post-prime updates should work normally
var result = ind.Update(new TValue(DateTime.UtcNow, 110));
Assert.True(double.IsFinite(result.Value));
}
[Fact]
public void Update_TSeries_RestoresStreamingState()
{
var ind = new Stochrsi(5, 5, 2, 2);
var series = new TSeries();
var gbm = new GBM(startPrice: 100, mu: 0.02, sigma: 0.1, seed: 42);
for (int i = 0; i < 40; i++)
{
var bar = gbm.Next(isNew: true);
series.Add(bar.Time, bar.Close);
}
var batchResult = ind.Update(series);
// After Update(TSeries), indicator should be hot with correct last value
Assert.True(ind.IsHot);
Assert.Equal(batchResult.Last.Value, ind.Last.Value, 1e-10);
// Subsequent streaming updates should work
var nextBar = gbm.Next(isNew: true);
var nextResult = ind.Update(new TValue(nextBar.Time, nextBar.Close));
Assert.True(double.IsFinite(nextResult.Value));
}
}
@@ -0,0 +1,437 @@
using OoplesFinance.StockIndicators;
using OoplesFinance.StockIndicators.Models;
using Skender.Stock.Indicators;
using TALib;
using Xunit;
using Xunit.Abstractions;
namespace QuanTAlib.Tests;
/// <summary>
/// StochRSI validation tests.
/// Cross-validates against Skender.Stock.Indicators.GetStochRsi,
/// TALib.NETCore StochRsi, OoplesFinance, and self-consistency checks.
/// </summary>
public sealed class StochrsiValidationTests : IDisposable
{
private readonly ValidationTestData _data = new();
private readonly ITestOutputHelper _output;
private bool _disposed;
public StochrsiValidationTests(ITestOutputHelper output)
{
_output = output;
}
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (!_disposed && disposing)
{
_data.Dispose();
_disposed = true;
}
}
private static TSeries GenerateCloseSeries(int count, int seed = 42)
{
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.15, seed: seed);
var bars = gbm.Fetch(count, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
return bars.Close;
}
// --- A) Streaming vs Batch self-consistency ---
[Fact]
public void Streaming_Matches_Batch()
{
var close = GenerateCloseSeries(300);
const int rsiLen = 14;
const int stochLen = 14;
const int kSmooth = 3;
const int dSmooth = 3;
// Streaming
var ind = new Stochrsi(rsiLen, stochLen, kSmooth, dSmooth);
for (int i = 0; i < close.Count; i++)
{
ind.Update(new TValue(close.Times[i], close.Values[i]));
}
double streamK = ind.K;
// Batch
var batchResult = Stochrsi.Batch(close, rsiLen, stochLen, kSmooth, dSmooth);
Assert.Equal(streamK, batchResult[^1].Value, 1e-10);
}
// --- B) Span matches TSeries ---
[Fact]
public void Span_Matches_TSeries()
{
var close = GenerateCloseSeries(200);
const int rsiLen = 14;
const int stochLen = 14;
const int kSmooth = 3;
const int dSmooth = 3;
var tsResult = Stochrsi.Batch(close, rsiLen, stochLen, kSmooth, dSmooth);
double[] closeArr = close.Values.ToArray();
var spanOut = new double[close.Count];
Stochrsi.Batch(closeArr.AsSpan(), spanOut.AsSpan(), rsiLen, stochLen, kSmooth, dSmooth);
for (int i = 0; i < close.Count; i++)
{
Assert.Equal(tsResult.Values[i], spanOut[i], 12);
}
}
// --- C) Cross-validation with Skender ---
[Fact]
public void Skender_Batch_Validates()
{
// Skender GetStochRsi(rsiPeriod, stochPeriod, signalPeriod, smaPeriods)
// signalPeriod = dSmooth, smaPeriods = kSmooth
const int rsiLen = 14;
const int stochLen = 14;
const int kSmooth = 3;
const int dSmooth = 3;
var qKD = new Stochrsi(rsiLen, stochLen, kSmooth, dSmooth).UpdateKD(_data.Data);
var skResults = _data.SkenderQuotes.GetStochRsi(rsiLen, stochLen, dSmooth, kSmooth).ToList();
// Skip warmup — compare converged values
int warmup = rsiLen + stochLen + kSmooth + dSmooth;
int totalCompared = 0;
int mismatches = 0;
for (int i = warmup; i < _data.Data.Count; i++)
{
double? skK = skResults[i].StochRsi;
double? skD = skResults[i].Signal;
if (skK.HasValue && skD.HasValue)
{
totalCompared++;
double errK = Math.Abs(qKD.K.Values[i] - skK.Value);
double errD = Math.Abs(qKD.D.Values[i] - skD.Value);
if (errK > 1e-6 || errD > 1e-6)
{
mismatches++;
}
}
}
Assert.True(totalCompared > 0, "No Skender results to compare");
double mismatchRate = (double)mismatches / totalCompared;
_output.WriteLine($"Skender batch: {totalCompared} compared, {mismatches} mismatches ({mismatchRate:P2})");
Assert.True(mismatchRate < 0.05, $"Mismatch rate {mismatchRate:P2} exceeds 5% threshold ({mismatches}/{totalCompared})");
}
[Fact]
public void Skender_Streaming_Validates()
{
const int rsiLen = 14;
const int stochLen = 14;
const int kSmooth = 3;
const int dSmooth = 3;
var ind = new Stochrsi(rsiLen, stochLen, kSmooth, dSmooth);
var qK = new List<double>();
var qD = new List<double>();
for (int i = 0; i < _data.Data.Count; i++)
{
ind.Update(new TValue(_data.Data.Times[i], _data.Data.Values[i]));
qK.Add(ind.K);
qD.Add(ind.D);
}
var skResults = _data.SkenderQuotes.GetStochRsi(rsiLen, stochLen, dSmooth, kSmooth).ToList();
int warmup = rsiLen + stochLen + kSmooth + dSmooth;
int totalCompared = 0;
int mismatches = 0;
for (int i = warmup; i < _data.Data.Count; i++)
{
double? skK = skResults[i].StochRsi;
double? skD = skResults[i].Signal;
if (skK.HasValue && skD.HasValue)
{
totalCompared++;
double errK = Math.Abs(qK[i] - skK.Value);
double errD = Math.Abs(qD[i] - skD.Value);
if (errK > 1e-6 || errD > 1e-6)
{
mismatches++;
}
}
}
Assert.True(totalCompared > 0, "No Skender results to compare");
double mismatchRate = (double)mismatches / totalCompared;
_output.WriteLine($"Skender streaming: {totalCompared} compared, {mismatches} mismatches ({mismatchRate:P2})");
Assert.True(mismatchRate < 0.05, $"Mismatch rate {mismatchRate:P2} exceeds 5% ({mismatches}/{totalCompared})");
}
// --- D) Cross-validation with TALib ---
[Fact]
public void TALib_StochRsi_Validates()
{
// TALib StochRsi: timePeriod=rsiLen, fastK_Period=stochLen, fastD_Period=dSmooth
// TALib does NOT smooth K (equivalent to kSmooth=1)
const int rsiLen = 14;
const int stochLen = 14;
const int dSmooth = 3;
double[] closeData = _data.RawData.ToArray();
double[] taK = new double[closeData.Length];
double[] taD = new double[closeData.Length];
var retCode = TALib.Functions.StochRsi(closeData.AsSpan(), 0..^0,
taK, taD, out var outRange, rsiLen, stochLen, dSmooth);
Assert.Equal(TALib.Core.RetCode.Success, retCode);
var (offset, length) = outRange.GetOffsetAndLength(taK.Length);
// Our indicator with kSmooth=1 to match TALib (no K smoothing)
var ind = new Stochrsi(rsiLen, stochLen, kSmooth: 1, dSmooth);
var qK = new List<double>();
var qD = new List<double>();
for (int i = 0; i < _data.Data.Count; i++)
{
ind.Update(new TValue(_data.Data.Times[i], _data.Data.Values[i]));
qK.Add(ind.K);
qD.Add(ind.D);
}
int matched = 0;
int mismatches = 0;
for (int j = 0; j < length; j++)
{
int qi = j + offset;
matched++;
double errK = Math.Abs(qK[qi] - taK[j]);
double errD = Math.Abs(qD[qi] - taD[j]);
if (errK > 1e-6 || errD > 1e-6)
{
mismatches++;
}
}
Assert.True(matched > 0, "No TALib results to compare");
double mismatchRate = (double)mismatches / matched;
_output.WriteLine($"TALib: {matched} compared, {mismatches} mismatches ({mismatchRate:P2})");
Assert.True(mismatchRate < 0.05, $"TALib mismatch rate {mismatchRate:P2} exceeds 5% ({mismatches}/{matched})");
}
// --- E) Cross-validation with Ooples ---
// Ooples CalculateStochasticRelativeStrengthIndex uses a fundamentally different
// algorithm (EMA-based smoothing, different RSI seeding). Not directly comparable
// to TradingView/Skender convention. Validated via Skender and TALib instead.
[Fact]
public void Ooples_StochRsi_Produces_Output()
{
var ooplesData = _data.SkenderQuotes.Select(q => new TickerData
{
Date = q.Date,
Close = (double)q.Close,
High = (double)q.High,
Low = (double)q.Low,
Open = (double)q.Open,
Volume = (double)q.Volume,
}).ToList();
var stockData = new StockData(ooplesData);
var oResult = stockData.CalculateStochasticRelativeStrengthIndex();
var oValues = oResult.OutputValues.Values.First();
// Verify Ooples produces output (smoke test — algorithms differ)
Assert.True(oValues.Count > 0, "Ooples should produce StochRSI output");
int finiteCount = 0;
for (int i = 50; i < oValues.Count; i++)
{
if (double.IsFinite(oValues[i]))
{
finiteCount++;
}
}
_output.WriteLine($"Ooples StochRSI: {oValues.Count} values, {finiteCount} finite after warmup");
Assert.True(finiteCount > 0, "Ooples should produce finite StochRSI values");
}
// --- F) Determinism ---
[Fact]
public void Deterministic_Across_Runs()
{
var close = GenerateCloseSeries(200, seed: 99);
const int rsiLen = 14;
const int stochLen = 14;
const int kSmooth = 3;
const int dSmooth = 3;
var r1 = Stochrsi.Batch(close, rsiLen, stochLen, kSmooth, dSmooth);
var r2 = Stochrsi.Batch(close, rsiLen, stochLen, kSmooth, dSmooth);
for (int i = 0; i < close.Count; i++)
{
Assert.Equal(r1.Values[i], r2.Values[i], 15);
}
}
// --- G) Different parameters produce different results ---
[Fact]
public void Different_Periods_Produce_Different_Results()
{
var close = GenerateCloseSeries(200);
var r1 = Stochrsi.Batch(close, rsiLength: 7, stochLength: 7, kSmooth: 3, dSmooth: 3);
var r2 = Stochrsi.Batch(close, rsiLength: 21, stochLength: 21, kSmooth: 3, dSmooth: 3);
bool anyDifferent = false;
for (int i = 50; i < 200; i++)
{
if (Math.Abs(r1.Values[i] - r2.Values[i]) > 0.01)
{
anyDifferent = true;
break;
}
}
Assert.True(anyDifferent);
}
// --- H) Calculate returns hot indicator ---
[Fact]
public void Calculate_Returns_Hot_Indicator()
{
var close = GenerateCloseSeries(200);
const int rsiLen = 14;
const int stochLen = 14;
const int kSmooth = 3;
const int dSmooth = 3;
var (results, indicator) = Stochrsi.Calculate(close, rsiLen, stochLen, kSmooth, dSmooth);
Assert.Equal(200, results.Count);
Assert.True(indicator.IsHot);
Assert.True(double.IsFinite(indicator.K));
Assert.True(double.IsFinite(indicator.D));
}
// --- I) Range validation (values should be 0-100) ---
[Fact]
public void Values_Within_0_100_Range()
{
var close = GenerateCloseSeries(500);
const int rsiLen = 14;
const int stochLen = 14;
const int kSmooth = 3;
const int dSmooth = 3;
var kd = new Stochrsi(rsiLen, stochLen, kSmooth, dSmooth).UpdateKD(close);
int warmup = rsiLen + stochLen + kSmooth + dSmooth;
for (int i = warmup; i < close.Count; i++)
{
double k = kd.K.Values[i];
double d = kd.D.Values[i];
Assert.True(k >= -0.01 && k <= 100.01,
$"K value {k} out of range at index {i}");
Assert.True(d >= -0.01 && d <= 100.01,
$"D value {d} out of range at index {i}");
}
}
// --- J) Skender span validation ---
[Fact]
public void Skender_Span_Validates()
{
const int rsiLen = 14;
const int stochLen = 14;
const int kSmooth = 3;
const int dSmooth = 3;
double[] closeData = _data.RawData.ToArray();
var spanOut = new double[closeData.Length];
Stochrsi.Batch(closeData.AsSpan(), spanOut.AsSpan(), rsiLen, stochLen, kSmooth, dSmooth);
var skResults = _data.SkenderQuotes.GetStochRsi(rsiLen, stochLen, dSmooth, kSmooth).ToList();
int warmup = rsiLen + stochLen + kSmooth + dSmooth;
int totalCompared = 0;
int mismatches = 0;
for (int i = warmup; i < closeData.Length; i++)
{
double? skK = skResults[i].StochRsi;
if (skK.HasValue)
{
totalCompared++;
double err = Math.Abs(spanOut[i] - skK.Value);
if (err > 1e-6)
{
mismatches++;
}
}
}
Assert.True(totalCompared > 0, "No Skender results to compare");
double mismatchRate = (double)mismatches / totalCompared;
_output.WriteLine($"Skender span: {totalCompared} compared, {mismatches} mismatches ({mismatchRate:P2})");
Assert.True(mismatchRate < 0.05, $"Skender span mismatch rate {mismatchRate:P2} exceeds 5% ({mismatches}/{totalCompared})");
}
[Fact]
public void Stochrsi_Correction_Recomputes()
{
var ind = new Stochrsi();
var t0 = DateTime.MinValue;
// Build state well past warmup (WarmupPeriod ≈ 31)
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 anchorPrice = 125.0;
ind.Update(new TValue(anchorTime, anchorPrice), isNew: true);
double anchorK = ind.K;
double anchorD = ind.D;
// Use large downward spike (÷10) to move StochRSI away from ceiling
ind.Update(new TValue(anchorTime, anchorPrice / 10), isNew: false);
Assert.NotEqual(anchorK, ind.K);
// Correction back to original — both outputs must restore exactly
ind.Update(new TValue(anchorTime, anchorPrice), isNew: false);
Assert.Equal(anchorK, ind.K, 1e-9);
Assert.Equal(anchorD, ind.D, 1e-9);
}
}