mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-26 06:18:05 +00:00
Enhance validation tests for various indicators with external library comparisons
- Added detailed comments explaining the validation limitations for MMA and ZLEMA due to differences in algorithm implementations. - Implemented validation tests for True Range against TALib and Tulip, ensuring directional agreement. - Updated Ulcer Index validation to clarify differences in algorithmic approaches between QuanTAlib and Skender. - Enhanced Ease of Movement tests to verify directional agreement with Tulip's EMV, noting differences in volume scaling. - Expanded Klinger Volume Oscillator tests to validate against Skender and Tulip, focusing on directional agreement across multiple period configurations. - Improved Negative Volume Index tests to compare percentage changes with Tulip, addressing differences in starting values. - Updated Positive Volume Index tests to validate against Tulip, emphasizing percentage change comparisons. - Enhanced Williams Accumulation/Distribution tests to verify directional agreement with Tulip, highlighting formula differences.
This commit is contained in:
@@ -1,29 +1,46 @@
|
||||
using Skender.Stock.Indicators;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Validation tests for ROC (Rate of Change) against Tulip MOM (Momentum).
|
||||
/// Tulip's MOM calculates absolute change: current - past
|
||||
/// Validation tests for ROC (Rate of Change) against external libraries.
|
||||
/// ROC computes absolute change: current - past (same as momentum).
|
||||
///
|
||||
/// Tulip's MOM calculates absolute change: current - past.
|
||||
/// Skender's GetRoc returns RocResult with .Momentum (absolute change).
|
||||
/// </summary>
|
||||
public class RocValidationTests
|
||||
public sealed class RocValidationTests(ITestOutputHelper output) : IDisposable
|
||||
{
|
||||
private readonly GBM _gbm = new(sigma: 0.5, mu: 0.05, seed: 60200);
|
||||
private readonly ValidationTestData _testData = new();
|
||||
private readonly ITestOutputHelper _output = output;
|
||||
private bool _disposed;
|
||||
|
||||
private const int TestPeriod = 9;
|
||||
private const int DataPoints = 500;
|
||||
private const double TulipTolerance = 1e-9;
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(disposing: true);
|
||||
}
|
||||
|
||||
private void Dispose(bool disposing)
|
||||
{
|
||||
if (_disposed) { return; }
|
||||
_disposed = true;
|
||||
if (disposing) { _testData?.Dispose(); }
|
||||
}
|
||||
|
||||
#region Tulip MOM Validation
|
||||
|
||||
[Fact]
|
||||
public void Roc_MatchesTulipMom_Batch()
|
||||
{
|
||||
var bars = _gbm.Fetch(DataPoints, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
var source = bars.Close;
|
||||
double[] tulipInput = source.Values.ToArray();
|
||||
double[] tulipInput = _testData.RawData.ToArray();
|
||||
|
||||
// Get QuanTAlib ROC result
|
||||
var quantResult = Roc.Batch(source, TestPeriod);
|
||||
var quantResult = Roc.Batch(_testData.Data, TestPeriod);
|
||||
|
||||
// Calculate Tulip MOM (momentum = current - past)
|
||||
var momIndicator = Tulip.Indicators.mom;
|
||||
@@ -35,29 +52,23 @@ public class RocValidationTests
|
||||
momIndicator.Run(inputs, options, outputs);
|
||||
var tulipResult = outputs[0];
|
||||
|
||||
// Compare (accounting for Tulip's offset due to lookback)
|
||||
for (int i = 0; i < tulipResult.Length; i++)
|
||||
{
|
||||
int qIdx = i + lookback;
|
||||
Assert.Equal(tulipResult[i], quantResult[qIdx].Value, TulipTolerance);
|
||||
}
|
||||
ValidationHelper.VerifyData(quantResult, tulipResult, lookback);
|
||||
|
||||
_output.WriteLine("ROC Batch validated successfully against Tulip MOM");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Roc_MatchesTulipMom_Streaming()
|
||||
{
|
||||
var bars = _gbm.Fetch(DataPoints, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
var source = bars.Close;
|
||||
double[] tulipInput = source.Values.ToArray();
|
||||
double[] tulipInput = _testData.RawData.ToArray();
|
||||
|
||||
// Get QuanTAlib ROC result via streaming
|
||||
var roc = new Roc(TestPeriod);
|
||||
var streamingResults = new List<double>();
|
||||
|
||||
for (int i = 0; i < source.Count; i++)
|
||||
foreach (var item in _testData.Data)
|
||||
{
|
||||
var tv = roc.Update(new TValue(source[i].Time, source[i].Value), true);
|
||||
streamingResults.Add(tv.Value);
|
||||
streamingResults.Add(roc.Update(item).Value);
|
||||
}
|
||||
|
||||
// Calculate Tulip MOM
|
||||
@@ -70,24 +81,19 @@ public class RocValidationTests
|
||||
momIndicator.Run(inputs, options, outputs);
|
||||
var tulipResult = outputs[0];
|
||||
|
||||
// Compare after warmup
|
||||
for (int i = 0; i < tulipResult.Length; i++)
|
||||
{
|
||||
int qIdx = i + lookback;
|
||||
Assert.Equal(tulipResult[i], streamingResults[qIdx], TulipTolerance);
|
||||
}
|
||||
ValidationHelper.VerifyData(streamingResults, tulipResult, lookback);
|
||||
|
||||
_output.WriteLine("ROC Streaming validated successfully against Tulip MOM");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Roc_MatchesTulipMom_Span()
|
||||
{
|
||||
var bars = _gbm.Fetch(DataPoints, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
var source = bars.Close;
|
||||
double[] tulipInput = source.Values.ToArray();
|
||||
double[] tulipInput = _testData.RawData.ToArray();
|
||||
|
||||
// Get QuanTAlib ROC result via span
|
||||
var quantOutput = new double[DataPoints];
|
||||
Roc.Batch(source.Values, quantOutput, TestPeriod);
|
||||
var quantOutput = new double[tulipInput.Length];
|
||||
Roc.Batch(new ReadOnlySpan<double>(tulipInput), quantOutput, TestPeriod);
|
||||
|
||||
// Calculate Tulip MOM
|
||||
var momIndicator = Tulip.Indicators.mom;
|
||||
@@ -99,12 +105,9 @@ public class RocValidationTests
|
||||
momIndicator.Run(inputs, options, outputs);
|
||||
var tulipResult = outputs[0];
|
||||
|
||||
// Compare after warmup
|
||||
for (int i = 0; i < tulipResult.Length; i++)
|
||||
{
|
||||
int qIdx = i + lookback;
|
||||
Assert.Equal(tulipResult[i], quantOutput[qIdx], TulipTolerance);
|
||||
}
|
||||
ValidationHelper.VerifyData(quantOutput, tulipResult, lookback);
|
||||
|
||||
_output.WriteLine("ROC Span validated successfully against Tulip MOM");
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -119,11 +122,9 @@ public class RocValidationTests
|
||||
[InlineData(50)]
|
||||
public void Roc_MatchesTulipMom_DifferentPeriods(int period)
|
||||
{
|
||||
var bars = _gbm.Fetch(DataPoints, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
var source = bars.Close;
|
||||
double[] tulipInput = source.Values.ToArray();
|
||||
double[] tulipInput = _testData.RawData.ToArray();
|
||||
|
||||
var quantResult = Roc.Batch(source, period);
|
||||
var quantResult = Roc.Batch(_testData.Data, period);
|
||||
|
||||
// Calculate Tulip MOM
|
||||
var momIndicator = Tulip.Indicators.mom;
|
||||
@@ -135,11 +136,68 @@ public class RocValidationTests
|
||||
momIndicator.Run(inputs, options, outputs);
|
||||
var tulipResult = outputs[0];
|
||||
|
||||
for (int i = 0; i < tulipResult.Length; i++)
|
||||
ValidationHelper.VerifyData(quantResult, tulipResult, lookback);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Skender Validation
|
||||
|
||||
[Fact]
|
||||
public void Roc_MatchesSkender_Batch()
|
||||
{
|
||||
// QuanTAlib ROC
|
||||
var qResult = Roc.Batch(_testData.Data, TestPeriod);
|
||||
|
||||
// Skender GetRoc returns RocResult with .Momentum (absolute change)
|
||||
var sResult = _testData.SkenderQuotes.GetRoc(TestPeriod).ToList();
|
||||
|
||||
// Compare last 100 records
|
||||
ValidationHelper.VerifyData(qResult, sResult, (s) => s.Momentum);
|
||||
|
||||
_output.WriteLine("ROC Batch validated successfully against Skender (GetRoc.Momentum)");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Roc_MatchesSkender_Streaming()
|
||||
{
|
||||
// QuanTAlib ROC (streaming)
|
||||
var roc = new Roc(TestPeriod);
|
||||
var qResults = new List<double>();
|
||||
foreach (var item in _testData.Data)
|
||||
{
|
||||
int qIdx = i + lookback;
|
||||
Assert.Equal(tulipResult[i], quantResult[qIdx].Value, TulipTolerance);
|
||||
qResults.Add(roc.Update(item).Value);
|
||||
}
|
||||
|
||||
// Skender GetRoc
|
||||
var sResult = _testData.SkenderQuotes.GetRoc(TestPeriod).ToList();
|
||||
|
||||
int count = qResults.Count;
|
||||
int start = Math.Max(0, count - ValidationHelper.DefaultVerificationCount);
|
||||
|
||||
for (int i = start; i < count; i++)
|
||||
{
|
||||
if (sResult[i].Momentum is null) { continue; }
|
||||
Assert.True(
|
||||
Math.Abs(qResults[i] - sResult[i].Momentum!.Value) <= ValidationHelper.SkenderTolerance,
|
||||
$"Mismatch at index {i}: QuanTAlib={qResults[i]:G17}, Skender={sResult[i].Momentum:G17}");
|
||||
}
|
||||
|
||||
_output.WriteLine("ROC Streaming validated successfully against Skender (GetRoc.Momentum)");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(1)]
|
||||
[InlineData(5)]
|
||||
[InlineData(20)]
|
||||
[InlineData(50)]
|
||||
public void Roc_MatchesSkender_DifferentPeriods(int period)
|
||||
{
|
||||
var qResult = Roc.Batch(_testData.Data, period);
|
||||
|
||||
var sResult = _testData.SkenderQuotes.GetRoc(period).ToList();
|
||||
|
||||
ValidationHelper.VerifyData(qResult, sResult, (s) => s.Momentum);
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -185,11 +243,9 @@ public class RocValidationTests
|
||||
[Fact]
|
||||
public void Roc_Period1_MatchesTulipMom()
|
||||
{
|
||||
var bars = _gbm.Fetch(DataPoints, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
var source = bars.Close;
|
||||
double[] tulipInput = source.Values.ToArray();
|
||||
double[] tulipInput = _testData.RawData.ToArray();
|
||||
|
||||
var quantResult = Roc.Batch(source, 1);
|
||||
var quantResult = Roc.Batch(_testData.Data, 1);
|
||||
|
||||
// Calculate Tulip MOM with period 1
|
||||
var momIndicator = Tulip.Indicators.mom;
|
||||
@@ -201,12 +257,32 @@ public class RocValidationTests
|
||||
momIndicator.Run(inputs, options, outputs);
|
||||
var tulipResult = outputs[0];
|
||||
|
||||
// Period 1 is single-bar change
|
||||
for (int i = 0; i < tulipResult.Length; i++)
|
||||
ValidationHelper.VerifyData(quantResult, tulipResult, lookback);
|
||||
|
||||
_output.WriteLine("ROC Period=1 validated against Tulip MOM");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_MatchesStreaming_IdenticalResults()
|
||||
{
|
||||
// Batch
|
||||
var batchResult = Roc.Batch(_testData.Data, TestPeriod);
|
||||
|
||||
// Streaming
|
||||
var roc = new Roc(TestPeriod);
|
||||
var streamingResults = new List<double>();
|
||||
foreach (var item in _testData.Data)
|
||||
{
|
||||
int qIdx = i + lookback;
|
||||
Assert.Equal(tulipResult[i], quantResult[qIdx].Value, TulipTolerance);
|
||||
streamingResults.Add(roc.Update(item).Value);
|
||||
}
|
||||
|
||||
int count = _testData.Data.Count;
|
||||
int start = Math.Max(0, count - ValidationHelper.DefaultVerificationCount);
|
||||
for (int i = start; i < count; i++)
|
||||
{
|
||||
Assert.Equal(batchResult[i].Value, streamingResults[i], ValidationHelper.DefaultTolerance);
|
||||
}
|
||||
_output.WriteLine("ROC Batch vs Streaming consistency validated");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
Reference in New Issue
Block a user