mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-09 14:30:56 +00:00
d7dbd7078a
- Updated event handler signatures to use TValueEventArgs for consistency in Mama, Mgdi, Pwma, Rma, Sma, Ssf, Super, T3, Tema, Trima, Usf, Vidya, Wma, and Atr classes. - Enhanced argument validation by specifying parameter names in exceptions for clarity. - Adjusted tests to align with new event handler signatures. - Improved code readability and maintainability by using structured records and lambda expressions.
117 lines
3.4 KiB
C#
117 lines
3.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Xunit;
|
|
using Xunit.Abstractions;
|
|
using QuanTAlib.Tests;
|
|
|
|
namespace QuanTAlib.Tests;
|
|
|
|
public class VidyaValidationTests
|
|
{
|
|
// Note: OoplesFinance VIDYA implementation diverges significantly from our reference implementation
|
|
// (Chande Momentum Oscillator based), likely due to different volatility calculation or smoothing logic.
|
|
// Therefore, we do not validate against Ooples for VIDYA.
|
|
|
|
private readonly ValidationTestData _testData;
|
|
private readonly ITestOutputHelper _output;
|
|
|
|
public VidyaValidationTests(ITestOutputHelper output)
|
|
{
|
|
_output = output;
|
|
_testData = new ValidationTestData();
|
|
}
|
|
|
|
[Fact]
|
|
public void ValidateAgainstReference()
|
|
{
|
|
// Note: Tulip's VIDYA implementation uses Standard Deviation ratio (1992 version),
|
|
// while QuanTAlib uses Chande Momentum Oscillator (1994 version).
|
|
// Therefore, we cannot validate against Tulip.
|
|
// We validate against a simple, readable reference implementation of the CMO-based VIDYA.
|
|
|
|
var period = 14;
|
|
|
|
// QuanTAlib
|
|
var vidya = new Vidya(period);
|
|
var qResults = new List<double>();
|
|
foreach (var item in _testData.Data)
|
|
{
|
|
qResults.Add(vidya.Update(item).Value);
|
|
}
|
|
|
|
// Reference Implementation
|
|
var refResults = CalculateVidyaReference(_testData.Data, period);
|
|
|
|
// Compare
|
|
ValidationHelper.VerifyData(qResults, refResults, x => x, tolerance: 1e-9);
|
|
|
|
_output.WriteLine("VIDYA validated successfully against reference implementation");
|
|
}
|
|
|
|
[Fact]
|
|
public void ValidateBatchAgainstReference()
|
|
{
|
|
var period = 14;
|
|
|
|
// QuanTAlib Batch
|
|
var qResults = Vidya.Batch(_testData.Data, period);
|
|
|
|
// Reference Implementation
|
|
var refResults = CalculateVidyaReference(_testData.Data, period);
|
|
|
|
// Compare
|
|
ValidationHelper.VerifyData(qResults, refResults, x => x, tolerance: 1e-9);
|
|
|
|
_output.WriteLine("VIDYA Batch validated successfully against reference implementation");
|
|
}
|
|
|
|
private static IReadOnlyList<double> CalculateVidyaReference(TSeries data, int period)
|
|
{
|
|
var results = new List<double>();
|
|
var prices = data.Select(x => x.Value).ToList();
|
|
double alpha = 2.0 / (period + 1);
|
|
|
|
double prevVidya = 0;
|
|
|
|
for (int i = 0; i < prices.Count; i++)
|
|
{
|
|
if (i == 0)
|
|
{
|
|
results.Add(prices[i]);
|
|
prevVidya = prices[i];
|
|
continue;
|
|
}
|
|
|
|
double sumUp = 0;
|
|
double sumDown = 0;
|
|
|
|
var changes = new List<double>();
|
|
for (int j = 1; j <= i; j++)
|
|
{
|
|
changes.Add(prices[j] - prices[j - 1]);
|
|
}
|
|
|
|
var recentChanges = changes.TakeLast(period).ToList();
|
|
|
|
sumUp = recentChanges.Where(x => x > 0).Sum();
|
|
sumDown = recentChanges.Where(x => x < 0).Select(x => -x).Sum();
|
|
|
|
double sum = sumUp + sumDown;
|
|
double vi = 0;
|
|
if (sum > 0)
|
|
{
|
|
vi = Math.Abs(sumUp - sumDown) / sum;
|
|
}
|
|
|
|
double dynamicAlpha = alpha * vi;
|
|
double currentVidya = dynamicAlpha * prices[i] + (1 - dynamicAlpha) * prevVidya;
|
|
|
|
results.Add(currentVidya);
|
|
prevVidya = currentVidya;
|
|
}
|
|
|
|
return results;
|
|
}
|
|
}
|