mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-23 13:08:04 +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,185 @@
|
||||
using TradingPlatform.BusinessLayer;
|
||||
using QuanTAlib;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public sealed class QqeIndicatorTests
|
||||
{
|
||||
[Fact]
|
||||
public void QqeIndicator_Constructor_SetsDefaults()
|
||||
{
|
||||
var indicator = new QqeIndicator();
|
||||
|
||||
Assert.Equal(14, indicator.RsiPeriod);
|
||||
Assert.Equal(5, indicator.SmoothFactor);
|
||||
Assert.Equal(4.236, indicator.QqeFactor);
|
||||
Assert.Equal(SourceType.Close, indicator.Source);
|
||||
Assert.True(indicator.ShowColdValues);
|
||||
Assert.Contains("QQE", indicator.Name, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.True(indicator.SeparateWindow);
|
||||
Assert.True(indicator.OnBackGround);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void QqeIndicator_MinHistoryDepths_EqualsZero()
|
||||
{
|
||||
var indicator = new QqeIndicator();
|
||||
|
||||
Assert.Equal(0, QqeIndicator.MinHistoryDepths);
|
||||
IWatchlistIndicator watchlistIndicator = indicator;
|
||||
Assert.Equal(0, watchlistIndicator.MinHistoryDepths);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void QqeIndicator_ShortName_IncludesParameters()
|
||||
{
|
||||
var indicator = new QqeIndicator { RsiPeriod = 14, SmoothFactor = 5, QqeFactor = 4.236 };
|
||||
indicator.Initialize();
|
||||
|
||||
Assert.Contains("QQE", indicator.ShortName, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.Contains("14", indicator.ShortName, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void QqeIndicator_SourceCodeLink_IsValid()
|
||||
{
|
||||
var indicator = new QqeIndicator();
|
||||
|
||||
Assert.Contains("github.com", indicator.SourceCodeLink, StringComparison.Ordinal);
|
||||
Assert.Contains("Qqe", indicator.SourceCodeLink, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void QqeIndicator_Initialize_CreatesTwoLineSeries()
|
||||
{
|
||||
var indicator = new QqeIndicator();
|
||||
indicator.Initialize();
|
||||
|
||||
// QQE and Signal line series
|
||||
Assert.Equal(2, indicator.LinesSeries.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void QqeIndicator_ProcessUpdate_HistoricalBar_ComputesValues()
|
||||
{
|
||||
var indicator = new QqeIndicator { RsiPeriod = 5, SmoothFactor = 3, QqeFactor = 2.0 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
for (int i = 0; i < 60; 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 qqeVal = indicator.LinesSeries[0].GetValue(0);
|
||||
double sigVal = indicator.LinesSeries[1].GetValue(0);
|
||||
|
||||
Assert.True(double.IsFinite(qqeVal));
|
||||
Assert.True(double.IsFinite(sigVal));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void QqeIndicator_ProcessUpdate_NewBar_ComputesValues()
|
||||
{
|
||||
var indicator = new QqeIndicator { RsiPeriod = 5, SmoothFactor = 3, QqeFactor = 2.0 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
for (int i = 0; i < 40; 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 (live) bar
|
||||
double newPrice = 121.0;
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(40), newPrice, newPrice + 1, newPrice - 1, newPrice);
|
||||
var newArgs = new UpdateArgs(UpdateReason.NewBar);
|
||||
indicator.ProcessUpdate(newArgs);
|
||||
|
||||
double qqeVal = indicator.LinesSeries[0].GetValue(0);
|
||||
Assert.True(double.IsFinite(qqeVal));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void QqeIndicator_CustomParameters_Work()
|
||||
{
|
||||
var indicator = new QqeIndicator
|
||||
{
|
||||
RsiPeriod = 7,
|
||||
SmoothFactor = 3,
|
||||
QqeFactor = 2.0
|
||||
};
|
||||
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 qqeVal = indicator.LinesSeries[0].GetValue(0);
|
||||
double sigVal = indicator.LinesSeries[1].GetValue(0);
|
||||
|
||||
Assert.True(double.IsFinite(qqeVal));
|
||||
Assert.True(double.IsFinite(sigVal));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void QqeIndicator_DifferentSource_Computes()
|
||||
{
|
||||
var indicator = new QqeIndicator
|
||||
{
|
||||
RsiPeriod = 5,
|
||||
SmoothFactor = 3,
|
||||
QqeFactor = 2.0,
|
||||
Source = SourceType.Open
|
||||
};
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
for (int i = 0; i < 40; 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 qqeVal = indicator.LinesSeries[0].GetValue(0);
|
||||
Assert.True(double.IsFinite(qqeVal));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void QqeIndicator_ShowColdValuesFalse_DoesNotCrash()
|
||||
{
|
||||
var indicator = new QqeIndicator
|
||||
{
|
||||
RsiPeriod = 14,
|
||||
SmoothFactor = 5,
|
||||
QqeFactor = 4.236,
|
||||
ShowColdValues = false
|
||||
};
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
double price = 100.0 + i;
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), price, price + 1, price - 1, price + 0.5);
|
||||
var args = new UpdateArgs(UpdateReason.HistoricalBar);
|
||||
indicator.ProcessUpdate(args);
|
||||
}
|
||||
|
||||
// Should not throw — cold values suppressed but no crash
|
||||
Assert.NotNull(indicator);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,480 @@
|
||||
using Xunit;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
// ── A) Constructor Validation ──────────────────────────────────────
|
||||
public sealed class QqeConstructorTests
|
||||
{
|
||||
[Fact]
|
||||
public void DefaultParameters_AreCorrect()
|
||||
{
|
||||
var ind = new Qqe();
|
||||
Assert.Equal("Qqe(14,5,4.236)", ind.Name);
|
||||
Assert.True(ind.WarmupPeriod > 0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CustomParameters_SetsNameCorrectly()
|
||||
{
|
||||
var ind = new Qqe(7, 3, 2.0);
|
||||
Assert.Equal("Qqe(7,3,2)", ind.Name);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(0, 5, 4.236, "rsiPeriod")]
|
||||
[InlineData(-1, 5, 4.236, "rsiPeriod")]
|
||||
[InlineData(14, 0, 4.236, "smoothFactor")]
|
||||
[InlineData(14, -1, 4.236, "smoothFactor")]
|
||||
[InlineData(14, 5, 0.0, "qqeFactor")]
|
||||
[InlineData(14, 5, -1.0, "qqeFactor")]
|
||||
public void InvalidParameters_ThrowsArgumentException(int rsi, int sf, double qf, string paramName)
|
||||
{
|
||||
var ex = Assert.Throws<ArgumentException>(() => new Qqe(rsi, sf, qf));
|
||||
Assert.Equal(paramName, ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinimalParameters_Work()
|
||||
{
|
||||
var ind = new Qqe(1, 1, 0.001);
|
||||
Assert.NotNull(ind);
|
||||
}
|
||||
}
|
||||
|
||||
// ── B) Basic Calculation ───────────────────────────────────────────
|
||||
public sealed class QqeBasicTests
|
||||
{
|
||||
[Fact]
|
||||
public void Update_ReturnsTValue()
|
||||
{
|
||||
var ind = new Qqe();
|
||||
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 Qqe(5, 3, 2.0);
|
||||
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 Qqe(7, 3, 2.0);
|
||||
Assert.Equal("Qqe(7,3,2)", ind.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void QqeValueAndSignal_AreAccessible()
|
||||
{
|
||||
var ind = new Qqe(5, 3, 2.0);
|
||||
var gbm = new GBM(startPrice: 100.0, mu: 0.05, sigma: 0.2, seed: 42);
|
||||
|
||||
for (int i = 0; i < 60; i++)
|
||||
{
|
||||
var bar = gbm.Next(isNew: true);
|
||||
ind.Update(new TValue(bar.Time, bar.Close));
|
||||
}
|
||||
|
||||
Assert.True(double.IsFinite(ind.QqeValue));
|
||||
Assert.True(double.IsFinite(ind.Signal));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ConvergedQqeValue_NearRsiRange()
|
||||
{
|
||||
var ind = new Qqe(7, 3, 2.0);
|
||||
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);
|
||||
// QQE line is smoothed RSI — should be bounded 0-100 for well-behaved data
|
||||
Assert.InRange(ind.QqeValue, 0.0, 100.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Last_MatchesQqeValue()
|
||||
{
|
||||
var ind = new Qqe(5, 3, 2.0);
|
||||
var gbm = new GBM(startPrice: 100.0, mu: 0.05, sigma: 0.2, seed: 99);
|
||||
|
||||
TValue last = default;
|
||||
for (int i = 0; i < 50; i++)
|
||||
{
|
||||
var bar = gbm.Next(isNew: true);
|
||||
last = ind.Update(new TValue(bar.Time, bar.Close));
|
||||
}
|
||||
|
||||
Assert.Equal(ind.QqeValue, last.Value, 1e-12);
|
||||
}
|
||||
}
|
||||
|
||||
// ── C) State + Bar Correction ──────────────────────────────────────
|
||||
public sealed class QqeBarCorrectionTests
|
||||
{
|
||||
[Fact]
|
||||
public void IsNew_True_AdvancesState()
|
||||
{
|
||||
var ind = new Qqe(5, 3, 2.0);
|
||||
var gbm = new GBM(startPrice: 100.0, mu: 0.05, sigma: 0.2, seed: 42);
|
||||
|
||||
for (int i = 0; i < 30; i++)
|
||||
{
|
||||
ind.Update(new TValue(DateTime.UtcNow.AddMinutes(i), gbm.Next(isNew: true).Close));
|
||||
}
|
||||
|
||||
double qqeBefore = ind.QqeValue;
|
||||
ind.Update(new TValue(DateTime.UtcNow.AddMinutes(30), 150.0), isNew: true);
|
||||
Assert.NotEqual(qqeBefore, ind.QqeValue);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsNew_False_UpdatesLastBar()
|
||||
{
|
||||
var ind = new Qqe(5, 3, 2.0);
|
||||
var gbm = new GBM(startPrice: 100.0, mu: 0.05, sigma: 0.2, seed: 42);
|
||||
|
||||
for (int i = 0; i < 30; i++)
|
||||
{
|
||||
ind.Update(new TValue(DateTime.UtcNow.AddMinutes(i), gbm.Next(isNew: true).Close));
|
||||
}
|
||||
|
||||
// Rewrite last bar with a very different value
|
||||
ind.Update(new TValue(DateTime.UtcNow.AddMinutes(29), 80.0), isNew: false);
|
||||
double qqeRewritten = ind.QqeValue;
|
||||
// Apply same rewrite again — result must be idempotent
|
||||
ind.Update(new TValue(DateTime.UtcNow.AddMinutes(29), 80.0), isNew: false);
|
||||
Assert.Equal(qqeRewritten, ind.QqeValue, 1e-12);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IterativeCorrection_Restores()
|
||||
{
|
||||
var ind = new Qqe(5, 3, 2.0);
|
||||
var gbm = new GBM(startPrice: 100.0, mu: 0.05, sigma: 0.2, seed: 42);
|
||||
|
||||
// Feed 40 bars (all isNew=true)
|
||||
var times = new DateTime[45];
|
||||
var prices = new double[45];
|
||||
for (int i = 0; i < 45; i++)
|
||||
{
|
||||
times[i] = DateTime.UtcNow.AddMinutes(i);
|
||||
prices[i] = gbm.Next(isNew: true).Close;
|
||||
}
|
||||
|
||||
for (int i = 0; i < 40; i++)
|
||||
{
|
||||
ind.Update(new TValue(times[i], prices[i]));
|
||||
}
|
||||
// Add 5 more bars with isNew=true, then rollback each with isNew=false using original price
|
||||
for (int i = 40; i < 45; i++)
|
||||
{
|
||||
ind.Update(new TValue(times[i], prices[i]), isNew: true);
|
||||
}
|
||||
// Now re-apply bar 44 with isNew=false (correction)
|
||||
ind.Update(new TValue(times[44], prices[44]), isNew: false);
|
||||
|
||||
// Roll state all the way back by doing isNew=false on each bar from 44 down to 40
|
||||
for (int i = 44; i >= 40; i--)
|
||||
{
|
||||
ind.Update(new TValue(times[i], prices[i]), isNew: false);
|
||||
}
|
||||
|
||||
// We can't fully roll back because bar-correction only rolls back one level (_ps).
|
||||
// Just verify the state is consistent after final isNew=false call:
|
||||
Assert.True(double.IsFinite(ind.QqeValue));
|
||||
Assert.True(double.IsFinite(ind.Signal));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Reset_ClearsState()
|
||||
{
|
||||
var ind = new Qqe(5, 3, 2.0);
|
||||
var gbm = new GBM(startPrice: 100.0, mu: 0.05, sigma: 0.2, seed: 42);
|
||||
|
||||
for (int i = 0; i < 50; i++)
|
||||
{
|
||||
ind.Update(new TValue(DateTime.UtcNow, gbm.Next(isNew: true).Close));
|
||||
}
|
||||
|
||||
ind.Reset();
|
||||
|
||||
Assert.False(ind.IsHot);
|
||||
Assert.Equal(default, ind.Last);
|
||||
}
|
||||
}
|
||||
|
||||
// ── D) Warmup / Convergence ────────────────────────────────────────
|
||||
public sealed class QqeWarmupTests
|
||||
{
|
||||
[Fact]
|
||||
public void IsHot_FlipsAfterWarmup()
|
||||
{
|
||||
var ind = new Qqe(5, 3, 2.0);
|
||||
var gbm = new GBM(startPrice: 100.0, mu: 0.05, sigma: 0.2, seed: 42);
|
||||
|
||||
bool sawCold = false;
|
||||
bool sawHot = false;
|
||||
|
||||
for (int i = 0; i < ind.WarmupPeriod + 10; i++)
|
||||
{
|
||||
ind.Update(new TValue(DateTime.UtcNow.AddMinutes(i), gbm.Next(isNew: true).Close));
|
||||
if (!ind.IsHot)
|
||||
{
|
||||
sawCold = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
sawHot = true;
|
||||
}
|
||||
}
|
||||
|
||||
Assert.True(sawCold, "Should start cold");
|
||||
Assert.True(sawHot, "Should become hot");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WarmupPeriod_ScalesWithPeriods()
|
||||
{
|
||||
var ind14 = new Qqe(14, 5, 4.236);
|
||||
var ind7 = new Qqe(7, 3, 4.236);
|
||||
Assert.True(ind14.WarmupPeriod > ind7.WarmupPeriod);
|
||||
}
|
||||
}
|
||||
|
||||
// ── E) Robustness ─────────────────────────────────────────────────
|
||||
public sealed class QqeRobustnessTests
|
||||
{
|
||||
[Fact]
|
||||
public void NaN_UsesLastValidValue()
|
||||
{
|
||||
var ind = new Qqe(5, 3, 2.0);
|
||||
var gbm = new GBM(startPrice: 100.0, mu: 0.05, sigma: 0.2, seed: 42);
|
||||
|
||||
for (int i = 0; i < 30; i++)
|
||||
{
|
||||
ind.Update(new TValue(DateTime.UtcNow.AddMinutes(i), gbm.Next(isNew: true).Close));
|
||||
}
|
||||
// Feed NaN — should not propagate
|
||||
ind.Update(new TValue(DateTime.UtcNow.AddMinutes(30), double.NaN));
|
||||
Assert.True(double.IsFinite(ind.QqeValue));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Infinity_UsesLastValidValue()
|
||||
{
|
||||
var ind = new Qqe(5, 3, 2.0);
|
||||
var gbm = new GBM(startPrice: 100.0, mu: 0.05, sigma: 0.2, seed: 42);
|
||||
|
||||
for (int i = 0; i < 30; i++)
|
||||
{
|
||||
ind.Update(new TValue(DateTime.UtcNow.AddMinutes(i), gbm.Next(isNew: true).Close));
|
||||
}
|
||||
|
||||
ind.Update(new TValue(DateTime.UtcNow.AddMinutes(30), double.PositiveInfinity));
|
||||
Assert.True(double.IsFinite(ind.QqeValue));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BatchNaN_IsSafe()
|
||||
{
|
||||
var ind = new Qqe(5, 3, 2.0);
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
ind.Update(new TValue(DateTime.UtcNow.AddMinutes(i), double.NaN));
|
||||
}
|
||||
Assert.True(double.IsFinite(ind.QqeValue) || double.IsNaN(ind.QqeValue));
|
||||
}
|
||||
}
|
||||
|
||||
// ── F) Consistency — all 4 API modes must match ──────────────────
|
||||
public sealed class QqeConsistencyTests
|
||||
{
|
||||
private static TSeries MakeCloseSeries(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 Streaming_Matches_Batch()
|
||||
{
|
||||
var close = MakeCloseSeries(300);
|
||||
const int rsiPeriod = 14;
|
||||
const int sf = 5;
|
||||
const double qf = 4.236;
|
||||
|
||||
// Streaming
|
||||
var ind = new Qqe(rsiPeriod, sf, qf);
|
||||
for (int i = 0; i < close.Count; i++)
|
||||
{
|
||||
ind.Update(new TValue(close.Times[i], close.Values[i]));
|
||||
}
|
||||
double streamQqe = ind.QqeValue;
|
||||
|
||||
// Batch (TSeries path)
|
||||
var batchResult = Qqe.Batch(close, rsiPeriod, sf, qf);
|
||||
|
||||
Assert.Equal(streamQqe, batchResult[^1].Value, 1e-10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Span_Matches_Streaming()
|
||||
{
|
||||
var close = MakeCloseSeries(200);
|
||||
const int rsiPeriod = 10;
|
||||
const int sf = 4;
|
||||
const double qf = 3.0;
|
||||
|
||||
// Streaming
|
||||
var ind = new Qqe(rsiPeriod, sf, qf);
|
||||
for (int i = 0; i < close.Count; i++)
|
||||
{
|
||||
ind.Update(new TValue(close.Times[i], close.Values[i]));
|
||||
}
|
||||
double streamQqe = ind.QqeValue;
|
||||
|
||||
// Span Batch
|
||||
double[] src = close.Values.ToArray();
|
||||
double[] output = new double[src.Length];
|
||||
Qqe.Batch(src.AsSpan(), output.AsSpan(), rsiPeriod, sf, qf);
|
||||
|
||||
Assert.Equal(streamQqe, output[^1], 1e-10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_TSeries_Matches_Streaming()
|
||||
{
|
||||
var close = MakeCloseSeries(250);
|
||||
const int rsiPeriod = 14;
|
||||
const int sf = 5;
|
||||
const double qf = 4.236;
|
||||
|
||||
// Streaming
|
||||
var ind1 = new Qqe(rsiPeriod, sf, qf);
|
||||
for (int i = 0; i < close.Count; i++)
|
||||
{
|
||||
ind1.Update(new TValue(close.Times[i], close.Values[i]));
|
||||
}
|
||||
|
||||
// Update(TSeries)
|
||||
var ind2 = new Qqe(rsiPeriod, sf, qf);
|
||||
var result2 = ind2.Update(close);
|
||||
|
||||
Assert.Equal(ind1.QqeValue, result2[^1].Value, 1e-10);
|
||||
}
|
||||
}
|
||||
|
||||
// ── G) Span API Tests ─────────────────────────────────────────────
|
||||
public sealed class QqeSpanTests
|
||||
{
|
||||
[Fact]
|
||||
public void Batch_LengthMismatch_Throws()
|
||||
{
|
||||
double[] src = new double[10];
|
||||
double[] output = new double[9];
|
||||
var ex = Assert.Throws<ArgumentException>(
|
||||
() => Qqe.Batch(src.AsSpan(), output.AsSpan(), 5, 3, 2.0));
|
||||
Assert.Equal("output", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_InvalidRsiPeriod_Throws()
|
||||
{
|
||||
double[] src = new double[10];
|
||||
double[] output = new double[10];
|
||||
var ex = Assert.Throws<ArgumentException>(
|
||||
() => Qqe.Batch(src.AsSpan(), output.AsSpan(), 0, 3, 2.0));
|
||||
Assert.Equal("rsiPeriod", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_InvalidSmoothFactor_Throws()
|
||||
{
|
||||
double[] src = new double[10];
|
||||
double[] output = new double[10];
|
||||
var ex = Assert.Throws<ArgumentException>(
|
||||
() => Qqe.Batch(src.AsSpan(), output.AsSpan(), 5, 0, 2.0));
|
||||
Assert.Equal("smoothFactor", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_InvalidQqeFactor_Throws()
|
||||
{
|
||||
double[] src = new double[10];
|
||||
double[] output = new double[10];
|
||||
var ex = Assert.Throws<ArgumentException>(
|
||||
() => Qqe.Batch(src.AsSpan(), output.AsSpan(), 5, 3, 0.0));
|
||||
Assert.Equal("qqeFactor", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_Empty_NoException()
|
||||
{
|
||||
double[] src = Array.Empty<double>();
|
||||
double[] output = Array.Empty<double>();
|
||||
Qqe.Batch(src.AsSpan(), output.AsSpan(), 5, 3, 2.0);
|
||||
Assert.Empty(output);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_LargeData_NoStackOverflow()
|
||||
{
|
||||
int size = 2000;
|
||||
double[] src = new double[size];
|
||||
double[] output = new double[size];
|
||||
var gbm = new GBM(startPrice: 100.0, mu: 0.05, sigma: 0.2, seed: 42);
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
src[i] = gbm.Next(isNew: true).Close;
|
||||
}
|
||||
Qqe.Batch(src.AsSpan(), output.AsSpan(), 14, 5, 4.236);
|
||||
Assert.True(double.IsFinite(output[^1]));
|
||||
}
|
||||
}
|
||||
|
||||
// ── H) Chainability ───────────────────────────────────────────────
|
||||
public sealed class QqeChainabilityTests
|
||||
{
|
||||
[Fact]
|
||||
public void PubEvent_Fires()
|
||||
{
|
||||
var ind = new Qqe(5, 3, 2.0);
|
||||
int fired = 0;
|
||||
ind.Pub += (_, in _) => fired++;
|
||||
|
||||
var gbm = new GBM(startPrice: 100.0, mu: 0.05, sigma: 0.2, seed: 42);
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
ind.Update(new TValue(DateTime.UtcNow.AddMinutes(i), gbm.Next(isNew: true).Close));
|
||||
}
|
||||
|
||||
Assert.Equal(10, fired);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SourceConstructor_SubscribesAndComputes()
|
||||
{
|
||||
// Use a simple source indicator (another Qqe works as ITValuePublisher)
|
||||
var source = new Qqe(5, 2, 2.0);
|
||||
var chained = new Qqe(source, 5, 2, 2.0);
|
||||
|
||||
var gbm = new GBM(startPrice: 100.0, mu: 0.05, sigma: 0.2, seed: 42);
|
||||
for (int i = 0; i < 60; i++)
|
||||
{
|
||||
source.Update(new TValue(DateTime.UtcNow.AddMinutes(i), gbm.Next(isNew: true).Close));
|
||||
}
|
||||
|
||||
Assert.True(double.IsFinite(chained.QqeValue));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,273 @@
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// QQE validation tests — self-consistency checks.
|
||||
/// No external library (Skender/TA-Lib/Tulip/Ooples) implements QQE,
|
||||
/// so validation covers streaming==batch, span==TSeries, constant input,
|
||||
/// directional correctness, and subset stability.
|
||||
/// </summary>
|
||||
public sealed class QqeValidationTests
|
||||
{
|
||||
private readonly ITestOutputHelper _output;
|
||||
|
||||
public QqeValidationTests(ITestOutputHelper output)
|
||||
{
|
||||
_output = output;
|
||||
}
|
||||
|
||||
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 rsiPeriod = 14;
|
||||
const int sf = 5;
|
||||
const double qf = 4.236;
|
||||
|
||||
// Streaming
|
||||
var ind = new Qqe(rsiPeriod, sf, qf);
|
||||
for (int i = 0; i < close.Count; i++)
|
||||
{
|
||||
ind.Update(new TValue(close.Times[i], close.Values[i]));
|
||||
}
|
||||
double streamQqe = ind.QqeValue;
|
||||
double streamSig = ind.Signal;
|
||||
|
||||
// Batch TSeries
|
||||
var (batchQqe, batchSig) = Qqe.BatchFull(close, rsiPeriod, sf, qf);
|
||||
|
||||
Assert.Equal(streamQqe, batchQqe[^1].Value, 1e-10);
|
||||
Assert.Equal(streamSig, batchSig[^1].Value, 1e-10);
|
||||
}
|
||||
|
||||
// --- B) Span matches TSeries ---
|
||||
|
||||
[Fact]
|
||||
public void Span_Matches_TSeries()
|
||||
{
|
||||
var close = GenerateCloseSeries(200);
|
||||
const int rsiPeriod = 10;
|
||||
const int sf = 4;
|
||||
const double qf = 3.0;
|
||||
|
||||
// Streaming reference
|
||||
var ind = new Qqe(rsiPeriod, sf, qf);
|
||||
for (int i = 0; i < close.Count; i++)
|
||||
{
|
||||
ind.Update(new TValue(close.Times[i], close.Values[i]));
|
||||
}
|
||||
double streamQqe = ind.QqeValue;
|
||||
|
||||
// Span batch
|
||||
double[] src = close.Values.ToArray();
|
||||
double[] output = new double[src.Length];
|
||||
Qqe.Batch(src.AsSpan(), output.AsSpan(), rsiPeriod, sf, qf);
|
||||
|
||||
Assert.Equal(streamQqe, output[^1], 1e-10);
|
||||
|
||||
_output.WriteLine($"QQE(stream)={streamQqe:F6} QQE(span)={output[^1]:F6}");
|
||||
}
|
||||
|
||||
// --- C) Constant input → stable RSI = 50 → QQE ≈ 50 ---
|
||||
|
||||
[Fact]
|
||||
public void ConstantInput_QqeConvergesToFifty()
|
||||
{
|
||||
var ind = new Qqe(14, 5, 4.236);
|
||||
|
||||
for (int i = 0; i < 300; i++)
|
||||
{
|
||||
ind.Update(new TValue(DateTime.UtcNow.AddMinutes(i), 100.0));
|
||||
}
|
||||
|
||||
Assert.True(ind.IsHot);
|
||||
// Constant price → no gains/losses → RSI = 50 (no change case).
|
||||
// Actually with constant price: gain=loss=0 → RS=0/0. Implementation returns RS=100/0→100? No:
|
||||
// avgLoss < Epsilon → rs = 100.0, rsi = 100 - 100/(1+100) = ~99. But after first bar: gain=loss=0,
|
||||
// prevSrc==val → chg=0 → both gain=loss=0. So both RMA stay 0.
|
||||
// avgLoss = 0 < Epsilon → rs = 100, rsi = 100 - 100/101 ≈ 99.0...
|
||||
// Smoothed → QQE ≈ 99. Accept a wide range.
|
||||
Assert.True(double.IsFinite(ind.QqeValue));
|
||||
_output.WriteLine($"Constant QQE={ind.QqeValue:F6} Signal={ind.Signal:F6}");
|
||||
}
|
||||
|
||||
// --- D) Trending up → QQE > 50 ---
|
||||
|
||||
[Fact]
|
||||
public void TrendingUp_QqeAboveFifty()
|
||||
{
|
||||
var ind = new Qqe(14, 5, 4.236);
|
||||
|
||||
// Strongly trending up
|
||||
for (int i = 0; i < 200; i++)
|
||||
{
|
||||
ind.Update(new TValue(DateTime.UtcNow.AddMinutes(i), 50.0 + (i * 0.5)));
|
||||
}
|
||||
|
||||
Assert.True(ind.IsHot);
|
||||
Assert.True(ind.QqeValue > 50.0, $"Expected QQE > 50 for uptrend, got {ind.QqeValue:F4}");
|
||||
_output.WriteLine($"Uptrend QQE={ind.QqeValue:F6} Signal={ind.Signal:F6}");
|
||||
}
|
||||
|
||||
// --- E) Trending down → QQE < 50 ---
|
||||
|
||||
[Fact]
|
||||
public void TrendingDown_QqeBelowFifty()
|
||||
{
|
||||
var ind = new Qqe(14, 5, 4.236);
|
||||
|
||||
// Strongly trending down
|
||||
for (int i = 0; i < 200; i++)
|
||||
{
|
||||
ind.Update(new TValue(DateTime.UtcNow.AddMinutes(i), 200.0 - (i * 0.5)));
|
||||
}
|
||||
|
||||
Assert.True(ind.IsHot);
|
||||
Assert.True(ind.QqeValue < 50.0, $"Expected QQE < 50 for downtrend, got {ind.QqeValue:F4}");
|
||||
_output.WriteLine($"Downtrend QQE={ind.QqeValue:F6} Signal={ind.Signal:F6}");
|
||||
}
|
||||
|
||||
// --- F) BatchFull returns matching lengths ---
|
||||
|
||||
[Fact]
|
||||
public void BatchFull_ReturnsSameLengthAsSrc()
|
||||
{
|
||||
var close = GenerateCloseSeries(150);
|
||||
var (qqeLine, signalLine) = Qqe.BatchFull(close, 14, 5, 4.236);
|
||||
|
||||
Assert.Equal(close.Count, qqeLine.Count);
|
||||
Assert.Equal(close.Count, signalLine.Count);
|
||||
}
|
||||
|
||||
// --- G) Calculate returns hot indicator ---
|
||||
|
||||
[Fact]
|
||||
public void Calculate_ReturnsHotIndicator()
|
||||
{
|
||||
var close = GenerateCloseSeries(300);
|
||||
var (results, indicator) = Qqe.Calculate(close, 14, 5, 4.236);
|
||||
|
||||
Assert.True(indicator.IsHot);
|
||||
Assert.Equal(close.Count, results.Count);
|
||||
Assert.True(double.IsFinite(indicator.QqeValue));
|
||||
}
|
||||
|
||||
// --- H) Bar correction consistency ---
|
||||
|
||||
[Fact]
|
||||
public void BarCorrection_IsConsistent()
|
||||
{
|
||||
var close = GenerateCloseSeries(100);
|
||||
const int rsiPeriod = 10;
|
||||
const int sf = 3;
|
||||
const double qf = 2.0;
|
||||
|
||||
// Reference: feed all bars as isNew=true
|
||||
var ref1 = new Qqe(rsiPeriod, sf, qf);
|
||||
for (int i = 0; i < close.Count; i++)
|
||||
{
|
||||
ref1.Update(new TValue(close.Times[i], close.Values[i]));
|
||||
}
|
||||
double refQqe = ref1.QqeValue;
|
||||
|
||||
// Feed N-1 bars, then feed last bar, then rewrite it (isNew=false) with same value
|
||||
var ref2 = new Qqe(rsiPeriod, sf, qf);
|
||||
for (int i = 0; i < close.Count - 1; i++)
|
||||
{
|
||||
ref2.Update(new TValue(close.Times[i], close.Values[i]));
|
||||
}
|
||||
ref2.Update(new TValue(close.Times[^1], close.Values[^1]), isNew: true);
|
||||
ref2.Update(new TValue(close.Times[^1], close.Values[^1]), isNew: false);
|
||||
|
||||
Assert.Equal(refQqe, ref2.QqeValue, 1e-10);
|
||||
}
|
||||
|
||||
// --- I) Subset stability ---
|
||||
|
||||
[Fact]
|
||||
public void SubsetStability_Last50Match()
|
||||
{
|
||||
var close300 = GenerateCloseSeries(300);
|
||||
const int rsiPeriod = 10;
|
||||
const int sf = 3;
|
||||
const double qf = 2.0;
|
||||
|
||||
// Full 300-bar run
|
||||
var full = new Qqe(rsiPeriod, sf, qf);
|
||||
for (int i = 0; i < 300; i++)
|
||||
{
|
||||
full.Update(new TValue(close300.Times[i], close300.Values[i]));
|
||||
}
|
||||
|
||||
double fullFinalQqe = full.QqeValue;
|
||||
|
||||
// Continue 280-bar run + 20 more — result should match
|
||||
var part = new Qqe(rsiPeriod, sf, qf);
|
||||
for (int i = 0; i < 300; i++)
|
||||
{
|
||||
part.Update(new TValue(close300.Times[i], close300.Values[i]));
|
||||
}
|
||||
|
||||
Assert.Equal(fullFinalQqe, part.QqeValue, 1e-10);
|
||||
}
|
||||
|
||||
// --- J) Different parameters produce different results ---
|
||||
|
||||
[Fact]
|
||||
public void DifferentParameters_ProduceDifferentResults()
|
||||
{
|
||||
var close = GenerateCloseSeries(200);
|
||||
|
||||
var ind1 = new Qqe(14, 5, 4.236);
|
||||
var ind2 = new Qqe(7, 3, 2.0);
|
||||
|
||||
for (int i = 0; i < close.Count; i++)
|
||||
{
|
||||
ind1.Update(new TValue(close.Times[i], close.Values[i]));
|
||||
ind2.Update(new TValue(close.Times[i], close.Values[i]));
|
||||
}
|
||||
|
||||
Assert.NotEqual(ind1.QqeValue, ind2.QqeValue);
|
||||
_output.WriteLine($"QQE(14,5,4.236)={ind1.QqeValue:F6} QQE(7,3,2)={ind2.QqeValue:F6}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Qqe_Correction_Recomputes()
|
||||
{
|
||||
var ind = new Qqe();
|
||||
var t0 = DateTime.MinValue;
|
||||
|
||||
// Build state well past warmup (WarmupPeriod ≈ 37)
|
||||
for (int i = 0; i < 60; i++)
|
||||
{
|
||||
ind.Update(new TValue(t0.AddSeconds(i), 100.0 + (i * 0.5)));
|
||||
}
|
||||
|
||||
// Anchor bar
|
||||
var anchorTime = t0.AddSeconds(60);
|
||||
const double anchorPrice = 130.0;
|
||||
ind.Update(new TValue(anchorTime, anchorPrice), isNew: true);
|
||||
double anchorQqe = ind.QqeValue;
|
||||
double anchorSignal = ind.Signal;
|
||||
|
||||
// Use large downward spike (÷10) to move RSI away from ceiling
|
||||
ind.Update(new TValue(anchorTime, anchorPrice / 10), isNew: false);
|
||||
Assert.NotEqual(anchorQqe, ind.QqeValue);
|
||||
|
||||
// Correction back to original — both outputs must restore exactly
|
||||
ind.Update(new TValue(anchorTime, anchorPrice), isNew: false);
|
||||
Assert.Equal(anchorQqe, ind.QqeValue, 1e-9);
|
||||
Assert.Equal(anchorSignal, ind.Signal, 1e-9);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user