mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-04 12:07:44 +00:00
060649192f
- 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
149 lines
3.8 KiB
C#
149 lines
3.8 KiB
C#
namespace QuanTAlib.Tests;
|
|
|
|
using Xunit;
|
|
|
|
public class LanczosValidationTests
|
|
{
|
|
private static TSeries MakeSeries(int count = 500)
|
|
{
|
|
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 42);
|
|
return gbm.Fetch(count, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1)).Close;
|
|
}
|
|
|
|
private readonly TSeries _data = MakeSeries();
|
|
|
|
[Fact]
|
|
public void Batch_Matches_Streaming()
|
|
{
|
|
int period = 14;
|
|
|
|
var streaming = new Lanczos(period);
|
|
var streamResults = new double[_data.Count];
|
|
for (int i = 0; i < _data.Count; i++)
|
|
{
|
|
streamResults[i] = streaming.Update(_data[i]).Value;
|
|
}
|
|
|
|
var batchResults = Lanczos.Batch(_data, period);
|
|
|
|
for (int i = 0; i < _data.Count; i++)
|
|
{
|
|
Assert.Equal(streamResults[i], batchResults[i].Value, 1e-9);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void Span_Matches_Streaming()
|
|
{
|
|
int period = 14;
|
|
|
|
var streaming = new Lanczos(period);
|
|
var streamResults = new double[_data.Count];
|
|
for (int i = 0; i < _data.Count; i++)
|
|
{
|
|
streamResults[i] = streaming.Update(_data[i]).Value;
|
|
}
|
|
|
|
var spanOutput = new double[_data.Count];
|
|
Lanczos.Batch(_data.Values, spanOutput, period);
|
|
|
|
for (int i = 0; i < _data.Count; i++)
|
|
{
|
|
Assert.Equal(streamResults[i], spanOutput[i], 1e-9);
|
|
}
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(2)]
|
|
[InlineData(7)]
|
|
[InlineData(14)]
|
|
[InlineData(50)]
|
|
public void DifferentPeriods_ProduceValidResults(int period)
|
|
{
|
|
var lanczos = new Lanczos(period);
|
|
foreach (var tv in _data)
|
|
{
|
|
var result = lanczos.Update(tv);
|
|
Assert.True(double.IsFinite(result.Value));
|
|
}
|
|
Assert.True(lanczos.IsHot);
|
|
}
|
|
|
|
[Fact]
|
|
public void ConstantInput_ConvergesToConstant()
|
|
{
|
|
var lanczos = new Lanczos(10);
|
|
for (int i = 0; i < 50; i++)
|
|
{
|
|
lanczos.Update(new TValue(DateTime.UtcNow, 42.0));
|
|
}
|
|
Assert.Equal(42.0, lanczos.Last.Value, 1e-10);
|
|
}
|
|
|
|
[Fact]
|
|
public void Calculate_ReturnsHotIndicator()
|
|
{
|
|
var (results, indicator) = Lanczos.Calculate(_data, 14);
|
|
Assert.True(indicator.IsHot);
|
|
Assert.Equal(_data.Count, results.Count);
|
|
}
|
|
|
|
[Fact]
|
|
public void BarCorrection_Consistency()
|
|
{
|
|
int period = 7;
|
|
var lanczos = new Lanczos(period);
|
|
|
|
for (int i = 0; i < 20; i++)
|
|
{
|
|
lanczos.Update(new TValue(DateTime.UtcNow, 100.0 + i), isNew: true);
|
|
}
|
|
|
|
double original = lanczos.Last.Value;
|
|
|
|
lanczos.Update(new TValue(DateTime.UtcNow, 999.0), isNew: false);
|
|
lanczos.Update(new TValue(DateTime.UtcNow, 119.0), isNew: false);
|
|
|
|
Assert.Equal(original, lanczos.Last.Value, 1e-10);
|
|
}
|
|
|
|
[Fact]
|
|
public void SubsetStability()
|
|
{
|
|
int period = 10;
|
|
var src = MakeSeries(200);
|
|
|
|
var full = new Lanczos(period);
|
|
for (int i = 0; i < src.Count; i++)
|
|
{
|
|
full.Update(src[i]);
|
|
}
|
|
|
|
var subset = new Lanczos(period);
|
|
for (int i = 0; i < src.Count; i++)
|
|
{
|
|
subset.Update(src[i]);
|
|
}
|
|
|
|
Assert.Equal(full.Last.Value, subset.Last.Value, 1e-10);
|
|
}
|
|
|
|
[Fact]
|
|
public void OddAndEvenPeriods_BothWork()
|
|
{
|
|
var oddLanczos = new Lanczos(7);
|
|
var evenLanczos = new Lanczos(8);
|
|
|
|
foreach (var tv in _data)
|
|
{
|
|
var oddResult = oddLanczos.Update(tv);
|
|
var evenResult = evenLanczos.Update(tv);
|
|
Assert.True(double.IsFinite(oddResult.Value));
|
|
Assert.True(double.IsFinite(evenResult.Value));
|
|
}
|
|
|
|
Assert.True(oddLanczos.IsHot);
|
|
Assert.True(evenLanczos.IsHot);
|
|
}
|
|
}
|