mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-21 03:58:04 +00:00
feat: add RSIH (Ehlers Hann-Windowed RSI) indicator
This commit is contained in:
@@ -0,0 +1,152 @@
|
||||
using TradingPlatform.BusinessLayer;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class RsihIndicatorTests
|
||||
{
|
||||
[Fact]
|
||||
public void RsihIndicator_Constructor_SetsDefaults()
|
||||
{
|
||||
var indicator = new RsihIndicator();
|
||||
|
||||
Assert.Equal(14, indicator.Period);
|
||||
Assert.Equal(SourceType.Close, indicator.Source);
|
||||
Assert.True(indicator.ShowColdValues);
|
||||
Assert.Equal("RSIH - Ehlers Hann-Windowed RSI", indicator.Name);
|
||||
Assert.True(indicator.SeparateWindow);
|
||||
Assert.True(indicator.OnBackGround);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RsihIndicator_MinHistoryDepths_EqualsZero()
|
||||
{
|
||||
var indicator = new RsihIndicator();
|
||||
|
||||
Assert.Equal(0, RsihIndicator.MinHistoryDepths);
|
||||
Assert.Equal(0, ((IWatchlistIndicator)indicator).MinHistoryDepths);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RsihIndicator_ShortName_IncludesPeriodAndSource()
|
||||
{
|
||||
var indicator = new RsihIndicator { Period = 20 };
|
||||
|
||||
Assert.Contains("RSIH", indicator.ShortName, StringComparison.Ordinal);
|
||||
Assert.Contains("20", indicator.ShortName, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RsihIndicator_SourceCodeLink_IsValid()
|
||||
{
|
||||
var indicator = new RsihIndicator();
|
||||
|
||||
Assert.Contains("github.com", indicator.SourceCodeLink, StringComparison.Ordinal);
|
||||
Assert.Contains("Rsih.Quantower.cs", indicator.SourceCodeLink, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RsihIndicator_Initialize_CreatesInternalIndicator()
|
||||
{
|
||||
var indicator = new RsihIndicator { Period = 14 };
|
||||
|
||||
indicator.Initialize();
|
||||
|
||||
Assert.Single(indicator.LinesSeries);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RsihIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
|
||||
{
|
||||
var indicator = new RsihIndicator { Period = 3 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
indicator.HistoricalData.AddBar(now, 100, 105, 95, 102);
|
||||
|
||||
var args = new UpdateArgs(UpdateReason.HistoricalBar);
|
||||
indicator.ProcessUpdate(args);
|
||||
|
||||
Assert.Equal(1, indicator.LinesSeries[0].Count);
|
||||
Assert.True(double.IsFinite(indicator.LinesSeries[0].GetValue(0)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RsihIndicator_ProcessUpdate_NewBar_ComputesValue()
|
||||
{
|
||||
var indicator = new RsihIndicator { Period = 3 };
|
||||
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 RsihIndicator_InternalIndicator_HandlesBarCorrection()
|
||||
{
|
||||
var ma = new Rsih(3);
|
||||
double[] prices = [100, 102, 99, 103, 97, 104, 98, 105, 97, 106];
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
for (int i = 0; i < prices.Length; i++)
|
||||
{
|
||||
ma.Update(new TValue(now.AddMinutes(i).Ticks, prices[i]), isNew: true);
|
||||
}
|
||||
|
||||
double beforeCorrection = ma.Last.Value;
|
||||
|
||||
ma.Update(new TValue(now.AddMinutes(9).Ticks, 100), isNew: false);
|
||||
double afterCorrection = ma.Last.Value;
|
||||
|
||||
Assert.NotEqual(beforeCorrection, afterCorrection);
|
||||
Assert.True(double.IsFinite(afterCorrection));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RsihIndicator_DifferentSourceTypes()
|
||||
{
|
||||
foreach (SourceType sourceType in new[] { SourceType.Close, SourceType.Open, SourceType.High, SourceType.Low })
|
||||
{
|
||||
var indicator = new RsihIndicator();
|
||||
indicator.Source = sourceType;
|
||||
Assert.Equal(sourceType, indicator.Source);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RsihIndicator_MultipleHistoricalBars()
|
||||
{
|
||||
var indicator = new RsihIndicator { Period = 5 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), 100 + i, 105 + i, 95 + i, 102 + i);
|
||||
indicator.ProcessUpdate(new UpdateArgs(i == 0 ? UpdateReason.HistoricalBar : UpdateReason.NewBar));
|
||||
}
|
||||
|
||||
Assert.Equal(20, indicator.LinesSeries[0].Count);
|
||||
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
Assert.True(double.IsFinite(indicator.LinesSeries[0].GetValue(i)));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RsihIndicator_PeriodChange_UpdatesConfig()
|
||||
{
|
||||
var indicator = new RsihIndicator();
|
||||
indicator.Period = 25;
|
||||
Assert.Equal(25, indicator.Period);
|
||||
|
||||
indicator.Period = 50;
|
||||
Assert.Equal(50, indicator.Period);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,479 @@
|
||||
namespace QuanTAlib;
|
||||
|
||||
public class RsihTests
|
||||
{
|
||||
private const int DefaultPeriod = 14;
|
||||
private const double Tolerance = 1e-12;
|
||||
|
||||
private static TSeries MakeSeries(int count = 500)
|
||||
{
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.5, seed: 42);
|
||||
var bars = gbm.Fetch(count, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
return bars.Close;
|
||||
}
|
||||
|
||||
// ========== A) Constructor Validation ==========
|
||||
|
||||
[Fact]
|
||||
public void Constructor_ZeroPeriod_ThrowsArgumentOutOfRangeException()
|
||||
{
|
||||
var ex = Assert.Throws<ArgumentOutOfRangeException>(() => new Rsih(0));
|
||||
Assert.Equal("period", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_NegativePeriod_ThrowsArgumentOutOfRangeException()
|
||||
{
|
||||
var ex = Assert.Throws<ArgumentOutOfRangeException>(() => new Rsih(-5));
|
||||
Assert.Equal("period", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_ValidPeriod_SetsNameAndWarmup()
|
||||
{
|
||||
var indicator = new Rsih(14);
|
||||
Assert.Equal("Rsih(14)", indicator.Name);
|
||||
Assert.Equal(15, indicator.WarmupPeriod);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_PeriodOne_IsValid()
|
||||
{
|
||||
var indicator = new Rsih(1);
|
||||
Assert.Equal("Rsih(1)", indicator.Name);
|
||||
Assert.Equal(2, indicator.WarmupPeriod);
|
||||
}
|
||||
|
||||
// ========== B) Basic Calculation ==========
|
||||
|
||||
[Fact]
|
||||
public void Update_ReturnsTValue_WithValidProperties()
|
||||
{
|
||||
var indicator = new Rsih(DefaultPeriod);
|
||||
var input = new TValue(DateTime.UtcNow, 100.0);
|
||||
TValue result = indicator.Update(input);
|
||||
|
||||
Assert.Equal(input.Time, result.Time);
|
||||
Assert.True(double.IsFinite(result.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_AfterWarmup_IsHotBecomesTrue()
|
||||
{
|
||||
var indicator = new Rsih(DefaultPeriod);
|
||||
Assert.False(indicator.IsHot);
|
||||
|
||||
for (int i = 0; i < 500; i++)
|
||||
{
|
||||
indicator.Update(new TValue(DateTime.UtcNow.AddSeconds(i), 100.0 + i * 0.1));
|
||||
}
|
||||
|
||||
Assert.True(indicator.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_LastProperty_MatchesReturnValue()
|
||||
{
|
||||
var indicator = new Rsih(DefaultPeriod);
|
||||
var input = new TValue(DateTime.UtcNow, 42.0);
|
||||
TValue result = indicator.Update(input);
|
||||
|
||||
Assert.Equal(result.Value, indicator.Last.Value, Tolerance);
|
||||
}
|
||||
|
||||
// ========== C) State + Bar Correction ==========
|
||||
|
||||
[Fact]
|
||||
public void IsNew_True_AdvancesState()
|
||||
{
|
||||
var indicator = new Rsih(10);
|
||||
|
||||
for (int i = 0; i < 15; i++)
|
||||
{
|
||||
indicator.Update(new TValue(DateTime.UtcNow.AddSeconds(i), 100.0 + i * 0.5), isNew: true);
|
||||
}
|
||||
|
||||
TValue r1 = indicator.Update(new TValue(DateTime.UtcNow.AddSeconds(30), 120.0), isNew: true);
|
||||
TValue r2 = indicator.Update(new TValue(DateTime.UtcNow.AddSeconds(31), 80.0), isNew: true);
|
||||
|
||||
Assert.NotEqual(r1.Value, r2.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsNew_False_RewritesCurrentBar()
|
||||
{
|
||||
var indicator = new Rsih(10);
|
||||
|
||||
double[] prices = [100, 102, 99, 103, 97, 104, 98, 105, 97, 106,
|
||||
101, 103, 98, 104, 96, 105, 99, 107, 98, 108,
|
||||
100, 102, 99, 103, 97, 104, 98, 105, 97, 106,
|
||||
101, 103, 98, 104, 96, 105, 99, 107, 98, 108,
|
||||
100, 102, 99, 103, 97, 104, 98, 105, 97, 106];
|
||||
|
||||
for (int i = 0; i < prices.Length; i++)
|
||||
{
|
||||
indicator.Update(new TValue(DateTime.UtcNow.AddSeconds(i), prices[i]));
|
||||
}
|
||||
|
||||
indicator.Update(new TValue(DateTime.UtcNow.AddSeconds(50), 110.0), isNew: true);
|
||||
double afterNew = indicator.Last.Value;
|
||||
|
||||
indicator.Update(new TValue(DateTime.UtcNow.AddSeconds(50), 90.0), isNew: false);
|
||||
double afterCorrection = indicator.Last.Value;
|
||||
|
||||
Assert.NotEqual(afterNew, afterCorrection);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IterativeCorrections_RestoreState()
|
||||
{
|
||||
var indicator = new Rsih(10);
|
||||
TSeries data = MakeSeries();
|
||||
|
||||
for (int i = 0; i < 50; i++)
|
||||
{
|
||||
indicator.Update(data[i], isNew: true);
|
||||
}
|
||||
|
||||
indicator.Update(data[50], isNew: true);
|
||||
|
||||
for (int j = 0; j < 5; j++)
|
||||
{
|
||||
indicator.Update(data[50], isNew: false);
|
||||
}
|
||||
|
||||
double afterCorrections = indicator.Last.Value;
|
||||
|
||||
var fresh = new Rsih(10);
|
||||
for (int i = 0; i <= 50; i++)
|
||||
{
|
||||
fresh.Update(data[i], isNew: true);
|
||||
}
|
||||
|
||||
Assert.Equal(fresh.Last.Value, afterCorrections, Tolerance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Reset_ClearsState()
|
||||
{
|
||||
var indicator = new Rsih(DefaultPeriod);
|
||||
|
||||
for (int i = 0; i < 50; i++)
|
||||
{
|
||||
indicator.Update(new TValue(DateTime.UtcNow.AddSeconds(i), 100.0 + i));
|
||||
}
|
||||
|
||||
Assert.True(indicator.IsHot);
|
||||
|
||||
indicator.Reset();
|
||||
|
||||
Assert.False(indicator.IsHot);
|
||||
Assert.Equal(default, indicator.Last);
|
||||
}
|
||||
|
||||
// ========== D) Warmup/Convergence ==========
|
||||
|
||||
[Fact]
|
||||
public void IsHot_FlipsAtCorrectTime()
|
||||
{
|
||||
var indicator = new Rsih(10);
|
||||
int hotAt = -1;
|
||||
|
||||
for (int i = 0; i < 200; i++)
|
||||
{
|
||||
indicator.Update(new TValue(DateTime.UtcNow.AddSeconds(i), 100.0 + i));
|
||||
if (indicator.IsHot && hotAt < 0)
|
||||
{
|
||||
hotAt = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Assert.InRange(hotAt, 1, 200);
|
||||
}
|
||||
|
||||
// ========== E) Robustness ==========
|
||||
|
||||
[Fact]
|
||||
public void NaN_Input_UsesLastValidValue()
|
||||
{
|
||||
var indicator = new Rsih(10);
|
||||
|
||||
for (int i = 0; i < 30; i++)
|
||||
{
|
||||
indicator.Update(new TValue(DateTime.UtcNow.AddSeconds(i), 100.0));
|
||||
}
|
||||
|
||||
TValue nanResult = indicator.Update(new TValue(DateTime.UtcNow.AddSeconds(30), double.NaN));
|
||||
|
||||
Assert.True(double.IsFinite(nanResult.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Infinity_Input_UsesLastValidValue()
|
||||
{
|
||||
var indicator = new Rsih(10);
|
||||
|
||||
for (int i = 0; i < 30; i++)
|
||||
{
|
||||
indicator.Update(new TValue(DateTime.UtcNow.AddSeconds(i), 100.0));
|
||||
}
|
||||
|
||||
TValue infResult = indicator.Update(new TValue(DateTime.UtcNow.AddSeconds(30), double.PositiveInfinity));
|
||||
Assert.True(double.IsFinite(infResult.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BatchNaN_DoesNotPropagate()
|
||||
{
|
||||
int period = 10;
|
||||
double[] source = new double[100];
|
||||
double[] output = new double[100];
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
source[i] = 100.0 + i * 0.5;
|
||||
}
|
||||
|
||||
source[50] = double.NaN;
|
||||
source[51] = double.NaN;
|
||||
|
||||
Rsih.Batch(source, output, period);
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
Assert.True(double.IsFinite(output[i]), $"Output[{i}] is not finite");
|
||||
}
|
||||
}
|
||||
|
||||
// ========== F) Consistency (4 API modes) ==========
|
||||
|
||||
[Fact]
|
||||
public void AllModes_ProduceSameResult()
|
||||
{
|
||||
int period = 10;
|
||||
TSeries data = MakeSeries();
|
||||
|
||||
// 1. Batch (TSeries)
|
||||
TSeries batchResults = Rsih.Batch(data, period);
|
||||
double expected = batchResults.Last.Value;
|
||||
|
||||
// 2. Span batch
|
||||
var tValues = data.Values.ToArray();
|
||||
var spanOutput = new double[tValues.Length];
|
||||
Rsih.Batch(new ReadOnlySpan<double>(tValues), spanOutput, period);
|
||||
double spanResult = spanOutput[^1];
|
||||
|
||||
// 3. Streaming
|
||||
var streaming = new Rsih(period);
|
||||
for (int i = 0; i < data.Count; i++)
|
||||
{
|
||||
streaming.Update(data[i]);
|
||||
}
|
||||
double streamingResult = streaming.Last.Value;
|
||||
|
||||
// 4. Eventing
|
||||
var pubSource = new TSeries();
|
||||
var eventBased = new Rsih(pubSource, period);
|
||||
for (int i = 0; i < data.Count; i++)
|
||||
{
|
||||
pubSource.Add(data[i]);
|
||||
}
|
||||
double eventingResult = eventBased.Last.Value;
|
||||
|
||||
Assert.Equal(expected, spanResult, precision: 9);
|
||||
Assert.Equal(expected, streamingResult, precision: 9);
|
||||
Assert.Equal(expected, eventingResult, precision: 9);
|
||||
}
|
||||
|
||||
// ========== G) Span API Tests ==========
|
||||
|
||||
[Fact]
|
||||
public void SpanBatch_MismatchedLengths_ThrowsArgumentException()
|
||||
{
|
||||
double[] source = new double[10];
|
||||
double[] output = new double[5];
|
||||
|
||||
var ex = Assert.Throws<ArgumentException>(() => Rsih.Batch(source, output, 5));
|
||||
Assert.Equal("output", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SpanBatch_PeriodZero_ThrowsArgumentOutOfRangeException()
|
||||
{
|
||||
double[] source = new double[10];
|
||||
double[] output = new double[10];
|
||||
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => Rsih.Batch(source, output, 0));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SpanBatch_EmptyInput_ProducesEmptyOutput()
|
||||
{
|
||||
double[] source = Array.Empty<double>();
|
||||
double[] output = Array.Empty<double>();
|
||||
var ex = Record.Exception(() => Rsih.Batch(source, output, 10));
|
||||
Assert.Null(ex);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SpanBatch_LargeData_DoesNotStackOverflow()
|
||||
{
|
||||
int size = 5000;
|
||||
double[] source = new double[size];
|
||||
double[] output = new double[size];
|
||||
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
source[i] = 100.0 + i * 0.1;
|
||||
}
|
||||
|
||||
Rsih.Batch(source, output, 20);
|
||||
|
||||
Assert.True(double.IsFinite(output[size - 1]));
|
||||
}
|
||||
|
||||
// ========== H) Chainability ==========
|
||||
|
||||
[Fact]
|
||||
public void Pub_EventFires_OnUpdate()
|
||||
{
|
||||
var indicator = new Rsih(DefaultPeriod);
|
||||
int eventCount = 0;
|
||||
|
||||
indicator.Pub += (object? sender, in TValueEventArgs args) => eventCount++;
|
||||
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
indicator.Update(new TValue(DateTime.UtcNow.AddSeconds(i), 100.0 + i));
|
||||
}
|
||||
|
||||
Assert.Equal(10, eventCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EventBased_Chaining_Works()
|
||||
{
|
||||
var source = new TSeries();
|
||||
var indicator = new Rsih(source, 5);
|
||||
|
||||
source.Add(new TValue(DateTime.UtcNow, 100));
|
||||
source.Add(new TValue(DateTime.UtcNow, 110));
|
||||
source.Add(new TValue(DateTime.UtcNow, 120));
|
||||
|
||||
Assert.True(double.IsFinite(indicator.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_ReturnsHotIndicator()
|
||||
{
|
||||
TSeries data = MakeSeries();
|
||||
(TSeries results, Rsih indicator) = Rsih.Calculate(data, DefaultPeriod);
|
||||
|
||||
Assert.Equal(data.Count, results.Count);
|
||||
Assert.True(indicator.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StaticCalculate_MatchesInstance()
|
||||
{
|
||||
const int period = 10;
|
||||
int count = 100;
|
||||
var source = new TSeries();
|
||||
var indicator = new Rsih(period);
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
source.Add(new TValue(DateTime.UtcNow.AddMinutes(i), i + 10));
|
||||
indicator.Update(source.Last);
|
||||
}
|
||||
|
||||
var staticResult = Rsih.Batch(source, period);
|
||||
|
||||
Assert.Equal(source.Count, staticResult.Count);
|
||||
Assert.Equal(indicator.Last.Value, staticResult.Last.Value, 8);
|
||||
}
|
||||
|
||||
// ========== RSIH-specific: Oscillator behavior ==========
|
||||
|
||||
[Fact]
|
||||
public void ConstantInput_OutputConvergesToZero()
|
||||
{
|
||||
var indicator = new Rsih(10);
|
||||
double lastResult = double.NaN;
|
||||
|
||||
for (int i = 0; i < 300; i++)
|
||||
{
|
||||
TValue r = indicator.Update(new TValue(DateTime.UtcNow.AddSeconds(i), 100.0));
|
||||
lastResult = r.Value;
|
||||
}
|
||||
|
||||
// Constant input → all diffs = 0 → CU = CD = 0 → RSIH = 0
|
||||
Assert.Equal(0.0, lastResult, 1e-10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TrendingInput_ProducesNonZero()
|
||||
{
|
||||
var indicator = new Rsih(10);
|
||||
double lastResult = 0.0;
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
TValue r = indicator.Update(new TValue(DateTime.UtcNow.AddSeconds(i), 100.0 + i * 2.0));
|
||||
lastResult = r.Value;
|
||||
}
|
||||
|
||||
// Strong uptrend should produce positive RSIH close to +1
|
||||
Assert.True(lastResult > 0.0);
|
||||
Assert.True(double.IsFinite(lastResult));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OutputIsBounded()
|
||||
{
|
||||
var indicator = new Rsih(10);
|
||||
TSeries data = MakeSeries(500);
|
||||
|
||||
for (int i = 0; i < data.Count; i++)
|
||||
{
|
||||
TValue r = indicator.Update(data[i]);
|
||||
Assert.InRange(r.Value, -1.0, 1.0);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UpTrend_Positive_DownTrend_Negative()
|
||||
{
|
||||
var up = new Rsih(10);
|
||||
var down = new Rsih(10);
|
||||
|
||||
for (int i = 0; i < 50; i++)
|
||||
{
|
||||
up.Update(new TValue(DateTime.UtcNow.AddSeconds(i), 100.0 + i));
|
||||
down.Update(new TValue(DateTime.UtcNow.AddSeconds(i), 200.0 - i));
|
||||
}
|
||||
|
||||
Assert.True(up.Last.Value > 0, "Ascending should produce positive RSIH");
|
||||
Assert.True(down.Last.Value < 0, "Descending should produce negative RSIH");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RsihProducesFiniteValues_OnGBMData()
|
||||
{
|
||||
var indicator = new Rsih(10);
|
||||
TSeries data = MakeSeries(200);
|
||||
|
||||
int nonFiniteCount = 0;
|
||||
for (int i = 0; i < data.Count; i++)
|
||||
{
|
||||
TValue r = indicator.Update(data[i]);
|
||||
if (!double.IsFinite(r.Value))
|
||||
{
|
||||
nonFiniteCount++;
|
||||
}
|
||||
}
|
||||
|
||||
Assert.Equal(0, nonFiniteCount);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,178 @@
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public sealed class RsihValidationTests : IDisposable
|
||||
{
|
||||
private readonly ITestOutputHelper _output;
|
||||
private readonly ValidationTestData _testData;
|
||||
private const int DefaultPeriod = 14;
|
||||
|
||||
public RsihValidationTests(ITestOutputHelper output)
|
||||
{
|
||||
_output = output;
|
||||
_testData = new ValidationTestData(10000);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_testData.Dispose();
|
||||
}
|
||||
|
||||
// ========== Self-consistency Validation ==========
|
||||
|
||||
[Fact]
|
||||
public void Rsih_BatchStreaming_Match()
|
||||
{
|
||||
// Streaming
|
||||
var streaming = new Rsih(DefaultPeriod);
|
||||
var streamResults = new List<double>(_testData.Data.Count);
|
||||
for (int i = 0; i < _testData.Data.Count; i++)
|
||||
{
|
||||
TValue r = streaming.Update(_testData.Data[i], isNew: true);
|
||||
streamResults.Add(r.Value);
|
||||
}
|
||||
|
||||
// Batch
|
||||
TSeries batchResults = Rsih.Batch(_testData.Data, DefaultPeriod);
|
||||
|
||||
int mismatchCount = 0;
|
||||
double maxDiff = 0;
|
||||
for (int i = 0; i < streamResults.Count; i++)
|
||||
{
|
||||
double diff = Math.Abs(streamResults[i] - batchResults[i].Value);
|
||||
if (diff > 1e-10)
|
||||
{
|
||||
mismatchCount++;
|
||||
maxDiff = Math.Max(maxDiff, diff);
|
||||
}
|
||||
}
|
||||
|
||||
_output.WriteLine($"Rsih({DefaultPeriod}) Batch vs Streaming: {mismatchCount} mismatches, max diff = {maxDiff:E3}");
|
||||
Assert.Equal(0, mismatchCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rsih_SpanBatch_MatchesStreaming()
|
||||
{
|
||||
// Streaming
|
||||
var streaming = new Rsih(DefaultPeriod);
|
||||
var streamResults = new List<double>(_testData.Data.Count);
|
||||
for (int i = 0; i < _testData.Data.Count; i++)
|
||||
{
|
||||
TValue r = streaming.Update(_testData.Data[i], isNew: true);
|
||||
streamResults.Add(r.Value);
|
||||
}
|
||||
|
||||
// Span batch
|
||||
double[] output = new double[_testData.Data.Count];
|
||||
Rsih.Batch(_testData.Data.Values, output, DefaultPeriod);
|
||||
|
||||
int mismatchCount = 0;
|
||||
double maxDiff = 0;
|
||||
for (int i = 0; i < streamResults.Count; i++)
|
||||
{
|
||||
double diff = Math.Abs(streamResults[i] - output[i]);
|
||||
if (diff > 1e-10)
|
||||
{
|
||||
mismatchCount++;
|
||||
maxDiff = Math.Max(maxDiff, diff);
|
||||
}
|
||||
}
|
||||
|
||||
_output.WriteLine($"Rsih({DefaultPeriod}) Span vs Streaming: {mismatchCount} mismatches, max diff = {maxDiff:E3}");
|
||||
Assert.Equal(0, mismatchCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rsih_DifferentPeriods_ProduceDifferentResults()
|
||||
{
|
||||
TSeries result10 = Rsih.Batch(_testData.Data, 10);
|
||||
TSeries result20 = Rsih.Batch(_testData.Data, 20);
|
||||
|
||||
int lastIdx = _testData.Data.Count - 1;
|
||||
_output.WriteLine($"Rsih(10) last = {result10[lastIdx].Value:F6}");
|
||||
_output.WriteLine($"Rsih(20) last = {result20[lastIdx].Value:F6}");
|
||||
|
||||
Assert.NotEqual(result10[lastIdx].Value, result20[lastIdx].Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rsih_ConstantInput_ConvergesToZero()
|
||||
{
|
||||
var indicator = new Rsih(10);
|
||||
double constantVal = 100.0;
|
||||
|
||||
double lastResult = double.NaN;
|
||||
for (int i = 0; i < 1000; i++)
|
||||
{
|
||||
TValue r = indicator.Update(new TValue(DateTime.UtcNow.AddSeconds(i), constantVal));
|
||||
lastResult = r.Value;
|
||||
}
|
||||
|
||||
_output.WriteLine($"Rsih(10) constant input result after 1000 bars: {lastResult:E6}");
|
||||
Assert.True(Math.Abs(lastResult) < 1e-6, $"Expected near-zero for constant input, got {lastResult}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rsih_Calculate_ReturnsHotIndicator()
|
||||
{
|
||||
(TSeries results, Rsih indicator) = Rsih.Calculate(_testData.Data, DefaultPeriod);
|
||||
|
||||
Assert.Equal(_testData.Data.Count, results.Count);
|
||||
Assert.True(indicator.IsHot);
|
||||
|
||||
// Verify the indicator can continue streaming
|
||||
TValue next = indicator.Update(new TValue(DateTime.UtcNow, 100.0), isNew: true);
|
||||
Assert.True(double.IsFinite(next.Value));
|
||||
|
||||
_output.WriteLine($"Rsih({DefaultPeriod}) Calculate: {results.Count} bars, last = {results[results.Count - 1].Value:F6}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rsih_BarCorrection_ProducesConsistentResults()
|
||||
{
|
||||
// Build reference: 100 bars then bar 101
|
||||
var reference = new Rsih(DefaultPeriod);
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
reference.Update(_testData.Data[i], isNew: true);
|
||||
}
|
||||
reference.Update(new TValue(DateTime.UtcNow, 50.0), isNew: true);
|
||||
double referenceVal = reference.Last.Value;
|
||||
|
||||
// Build test: 100 bars, wrong bar 101, then correct bar 101
|
||||
var test = new Rsih(DefaultPeriod);
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
test.Update(_testData.Data[i], isNew: true);
|
||||
}
|
||||
test.Update(new TValue(DateTime.UtcNow, 999.0), isNew: true); // wrong
|
||||
test.Update(new TValue(DateTime.UtcNow, 50.0), isNew: false); // correct
|
||||
double testVal = test.Last.Value;
|
||||
|
||||
_output.WriteLine($"Reference: {referenceVal:F10}, Corrected: {testVal:F10}");
|
||||
Assert.Equal(referenceVal, testVal, 1e-10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rsih_SubsetValidation_StableBehavior()
|
||||
{
|
||||
using var subset = _testData.CreateSubset(200);
|
||||
|
||||
TSeries results = Rsih.Batch(subset.Data, DefaultPeriod);
|
||||
|
||||
int nanCount = 0;
|
||||
for (int i = 0; i < results.Count; i++)
|
||||
{
|
||||
if (!double.IsFinite(results[i].Value))
|
||||
{
|
||||
nanCount++;
|
||||
}
|
||||
}
|
||||
|
||||
_output.WriteLine($"Rsih({DefaultPeriod}) on 200-bar subset: {nanCount} non-finite values");
|
||||
Assert.Equal(0, nanCount);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user