mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-25 05:48:06 +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,159 @@
|
||||
using TradingPlatform.BusinessLayer;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class LanczosIndicatorTests
|
||||
{
|
||||
[Fact]
|
||||
public void LanczosIndicator_Constructor_SetsDefaults()
|
||||
{
|
||||
var indicator = new LanczosIndicator();
|
||||
|
||||
Assert.Equal(14, indicator.Period);
|
||||
Assert.Equal(SourceType.Close, indicator.Source);
|
||||
Assert.True(indicator.ShowColdValues);
|
||||
Assert.Equal("LANCZOS - Lanczos (Sinc) Window Moving Average", indicator.Name);
|
||||
Assert.False(indicator.SeparateWindow);
|
||||
Assert.True(indicator.OnBackGround);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LanczosIndicator_MinHistoryDepths_IsZero()
|
||||
{
|
||||
var indicator = new LanczosIndicator { Period = 14 };
|
||||
|
||||
Assert.Equal(0, LanczosIndicator.MinHistoryDepths);
|
||||
Assert.Equal(0, ((IWatchlistIndicator)indicator).MinHistoryDepths);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LanczosIndicator_ShortName_IncludesPeriodAndSource()
|
||||
{
|
||||
var indicator = new LanczosIndicator { Period = 10 };
|
||||
|
||||
Assert.Contains("LANCZOS", indicator.ShortName, StringComparison.Ordinal);
|
||||
Assert.Contains("10", indicator.ShortName, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LanczosIndicator_SourceCodeLink_IsValid()
|
||||
{
|
||||
var indicator = new LanczosIndicator();
|
||||
|
||||
Assert.Contains("github.com", indicator.SourceCodeLink, StringComparison.Ordinal);
|
||||
Assert.Contains("Lanczos.Quantower.cs", indicator.SourceCodeLink, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LanczosIndicator_Initialize_CreatesInternalLanczos()
|
||||
{
|
||||
var indicator = new LanczosIndicator { Period = 14 };
|
||||
|
||||
indicator.Initialize();
|
||||
|
||||
Assert.Single(indicator.LinesSeries);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LanczosIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
|
||||
{
|
||||
var indicator = new LanczosIndicator { Period = 5 };
|
||||
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 LanczosIndicator_ProcessUpdate_NewBar_ComputesValue()
|
||||
{
|
||||
var indicator = new LanczosIndicator { Period = 5 };
|
||||
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 LanczosIndicator_ProcessUpdate_NewTick_ProcessesWithoutError()
|
||||
{
|
||||
var indicator = new LanczosIndicator { Period = 5 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
indicator.HistoricalData.AddBar(now, 100, 105, 95, 102);
|
||||
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
double firstValue = indicator.LinesSeries[0].GetValue(0);
|
||||
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewTick));
|
||||
double secondValue = indicator.LinesSeries[0].GetValue(0);
|
||||
|
||||
Assert.True(double.IsFinite(firstValue));
|
||||
Assert.True(double.IsFinite(secondValue));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LanczosIndicator_MultipleUpdates_ProducesCorrectSequence()
|
||||
{
|
||||
var indicator = new LanczosIndicator { Period = 5 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
double[] closes = { 100, 102, 104, 103, 105, 107, 106 };
|
||||
|
||||
foreach (var close in closes)
|
||||
{
|
||||
indicator.HistoricalData.AddBar(now, close, close + 2, close - 2, close);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
now = now.AddMinutes(1);
|
||||
}
|
||||
|
||||
for (int i = 0; i < closes.Length; i++)
|
||||
{
|
||||
Assert.True(double.IsFinite(indicator.LinesSeries[0].GetValue(closes.Length - 1 - i)));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LanczosIndicator_DifferentSourceTypes_Work()
|
||||
{
|
||||
var sources = new[] { SourceType.Open, SourceType.High, SourceType.Low, SourceType.Close, SourceType.HL2, SourceType.HLC3 };
|
||||
|
||||
foreach (var source in sources)
|
||||
{
|
||||
var indicator = new LanczosIndicator { Period = 5, Source = source };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
indicator.HistoricalData.AddBar(now, 100, 110, 90, 105);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
|
||||
Assert.True(double.IsFinite(indicator.LinesSeries[0].GetValue(0)),
|
||||
$"Source {source} should produce finite value");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LanczosIndicator_Period_CanBeChanged()
|
||||
{
|
||||
var indicator = new LanczosIndicator { Period = 14 };
|
||||
Assert.Equal(14, indicator.Period);
|
||||
|
||||
indicator.Period = 20;
|
||||
Assert.Equal(20, indicator.Period);
|
||||
Assert.Equal(0, LanczosIndicator.MinHistoryDepths);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,420 @@
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class LanczosTests
|
||||
{
|
||||
private const int DefaultPeriod = 14;
|
||||
private const double Epsilon = 1e-10;
|
||||
|
||||
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();
|
||||
|
||||
// ── A) Constructor validation ──────────────────────────────────────
|
||||
|
||||
[Theory]
|
||||
[InlineData(0)]
|
||||
[InlineData(1)]
|
||||
[InlineData(-5)]
|
||||
public void Constructor_InvalidPeriod_Throws(int period)
|
||||
{
|
||||
var ex = Assert.Throws<ArgumentException>(() => new Lanczos(period));
|
||||
Assert.Equal("period", ex.ParamName);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(2)]
|
||||
[InlineData(14)]
|
||||
[InlineData(100)]
|
||||
public void Constructor_ValidPeriod_Succeeds(int period)
|
||||
{
|
||||
var lanczos = new Lanczos(period);
|
||||
Assert.Contains(period.ToString(System.Globalization.CultureInfo.InvariantCulture), lanczos.Name, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_DefaultName()
|
||||
{
|
||||
var lanczos = new Lanczos(14);
|
||||
Assert.Equal("Lanczos(14)", lanczos.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_NullSource_Throws()
|
||||
{
|
||||
Assert.Throws<NullReferenceException>(() => new Lanczos(null!, DefaultPeriod));
|
||||
}
|
||||
|
||||
// ── B) Basic calculation ───────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Update_ReturnsTValue()
|
||||
{
|
||||
var lanczos = new Lanczos(DefaultPeriod);
|
||||
var result = lanczos.Update(new TValue(DateTime.UtcNow, 100.0));
|
||||
Assert.IsType<TValue>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Last_IsAccessible()
|
||||
{
|
||||
var lanczos = new Lanczos(DefaultPeriod);
|
||||
lanczos.Update(new TValue(DateTime.UtcNow, 100.0));
|
||||
Assert.True(double.IsFinite(lanczos.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Name_IsCorrect()
|
||||
{
|
||||
var lanczos = new Lanczos(20);
|
||||
Assert.Equal("Lanczos(20)", lanczos.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_ReturnsFiniteValue()
|
||||
{
|
||||
var lanczos = new Lanczos(DefaultPeriod);
|
||||
foreach (var tv in _data)
|
||||
{
|
||||
var result = lanczos.Update(tv);
|
||||
Assert.True(double.IsFinite(result.Value));
|
||||
}
|
||||
}
|
||||
|
||||
// ── C) State + bar correction ──────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void IsNew_True_AdvancesState()
|
||||
{
|
||||
var lanczos = new Lanczos(5);
|
||||
var now = DateTime.UtcNow;
|
||||
lanczos.Update(new TValue(now, 10.0), isNew: true);
|
||||
lanczos.Update(new TValue(now.AddMinutes(1), 20.0), isNew: true);
|
||||
Assert.True(double.IsFinite(lanczos.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsNew_False_DoesNotAdvanceBuffer()
|
||||
{
|
||||
// The Lanczos sinc window has zero weight at the newest bar position
|
||||
// (since sinc(1)=0 at k=period-1), so we test that isNew=false does not
|
||||
// advance the buffer by verifying state is preserved after correction.
|
||||
var lanczos = new Lanczos(7);
|
||||
var src = MakeSeries(20);
|
||||
|
||||
for (int i = 0; i < src.Count; i++)
|
||||
{
|
||||
lanczos.Update(src[i], isNew: true);
|
||||
}
|
||||
|
||||
double original = lanczos.Last.Value;
|
||||
|
||||
// Multiple corrections should not change the final result when
|
||||
// we restore the original value
|
||||
lanczos.Update(new TValue(src[src.Count - 1].Time, 500.0), isNew: false);
|
||||
lanczos.Update(new TValue(src[src.Count - 1].Time, src[src.Count - 1].Value), isNew: false);
|
||||
|
||||
Assert.Equal(original, lanczos.Last.Value, Epsilon);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IterativeCorrections_Restore()
|
||||
{
|
||||
var lanczos = new Lanczos(14);
|
||||
var src = MakeSeries(30);
|
||||
|
||||
for (int i = 0; i < src.Count; i++)
|
||||
{
|
||||
lanczos.Update(src[i], isNew: true);
|
||||
}
|
||||
|
||||
double original = lanczos.Last.Value;
|
||||
|
||||
for (int c = 0; c < 5; c++)
|
||||
{
|
||||
lanczos.Update(new TValue(src[src.Count - 1].Time, 200.0 + c), isNew: false);
|
||||
}
|
||||
|
||||
// Restore original value
|
||||
lanczos.Update(new TValue(src[src.Count - 1].Time, src[src.Count - 1].Value), isNew: false);
|
||||
Assert.Equal(original, lanczos.Last.Value, Epsilon);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Reset_ClearsState()
|
||||
{
|
||||
var lanczos = new Lanczos(DefaultPeriod);
|
||||
foreach (var tv in _data)
|
||||
{
|
||||
lanczos.Update(tv);
|
||||
}
|
||||
|
||||
lanczos.Reset();
|
||||
Assert.False(lanczos.IsHot);
|
||||
}
|
||||
|
||||
// ── D) Warmup/convergence ──────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void IsHot_FlipsAtPeriod()
|
||||
{
|
||||
var lanczos = new Lanczos(5);
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
lanczos.Update(new TValue(DateTime.UtcNow, 100.0 + i));
|
||||
Assert.False(lanczos.IsHot);
|
||||
}
|
||||
lanczos.Update(new TValue(DateTime.UtcNow, 105.0));
|
||||
Assert.True(lanczos.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WarmupPeriod_MatchesPeriod()
|
||||
{
|
||||
var lanczos = new Lanczos(10);
|
||||
Assert.Equal(10, lanczos.WarmupPeriod);
|
||||
}
|
||||
|
||||
// ── E) Robustness ──────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void NaN_UsesLastValidValue()
|
||||
{
|
||||
var lanczos = new Lanczos(5);
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
lanczos.Update(new TValue(DateTime.UtcNow, 100.0));
|
||||
}
|
||||
|
||||
lanczos.Update(new TValue(DateTime.UtcNow, double.NaN));
|
||||
Assert.True(double.IsFinite(lanczos.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Infinity_UsesLastValidValue()
|
||||
{
|
||||
var lanczos = new Lanczos(5);
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
lanczos.Update(new TValue(DateTime.UtcNow, 100.0));
|
||||
}
|
||||
|
||||
lanczos.Update(new TValue(DateTime.UtcNow, double.PositiveInfinity));
|
||||
Assert.True(double.IsFinite(lanczos.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BatchNaN_Safe()
|
||||
{
|
||||
var lanczos = new Lanczos(5);
|
||||
var src = MakeSeries(50);
|
||||
var result = lanczos.Update(src);
|
||||
Assert.Equal(src.Count, result.Count);
|
||||
for (int i = 0; i < result.Count; i++)
|
||||
{
|
||||
Assert.True(double.IsFinite(result[i].Value));
|
||||
}
|
||||
}
|
||||
|
||||
// ── F) Consistency (4-API match) ───────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void AllModes_ProduceSameResults()
|
||||
{
|
||||
int period = 10;
|
||||
var src = MakeSeries(100);
|
||||
|
||||
// Streaming
|
||||
var streaming = new Lanczos(period);
|
||||
var streamResults = new double[src.Count];
|
||||
for (int i = 0; i < src.Count; i++)
|
||||
{
|
||||
streamResults[i] = streaming.Update(src[i]).Value;
|
||||
}
|
||||
|
||||
// Batch (TSeries)
|
||||
var batchResults = Lanczos.Batch(src, period);
|
||||
|
||||
// Span
|
||||
var spanOutput = new double[src.Count];
|
||||
Lanczos.Batch(src.Values, spanOutput, period);
|
||||
|
||||
// Event-based
|
||||
var publisher = new TSeries();
|
||||
var eventLanczos = new Lanczos(publisher, period);
|
||||
var eventResults = new double[src.Count];
|
||||
for (int i = 0; i < src.Count; i++)
|
||||
{
|
||||
publisher.Add(src[i], isNew: true);
|
||||
eventResults[i] = eventLanczos.Last.Value;
|
||||
}
|
||||
|
||||
for (int i = 0; i < src.Count; i++)
|
||||
{
|
||||
Assert.Equal(streamResults[i], batchResults[i].Value, 1e-6);
|
||||
Assert.Equal(streamResults[i], spanOutput[i], 1e-6);
|
||||
Assert.Equal(streamResults[i], eventResults[i], 1e-6);
|
||||
}
|
||||
}
|
||||
|
||||
// ── G) Span API tests ──────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_MismatchedLengths_Throws()
|
||||
{
|
||||
var src = new double[10];
|
||||
var output = new double[5];
|
||||
var ex = Assert.Throws<ArgumentException>(() => Lanczos.Batch(src, output, 5));
|
||||
Assert.Equal("output", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_PeriodTooSmall_Throws()
|
||||
{
|
||||
var src = new double[10];
|
||||
var output = new double[10];
|
||||
var ex = Assert.Throws<ArgumentException>(() => Lanczos.Batch(src, output, 1));
|
||||
Assert.Equal("period", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_EmptyInput_NoOp()
|
||||
{
|
||||
var src = ReadOnlySpan<double>.Empty;
|
||||
var output = Span<double>.Empty;
|
||||
Lanczos.Batch(src, output, 5);
|
||||
Assert.True(true);
|
||||
}
|
||||
|
||||
// ── H) Chainability ────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Pub_Fires()
|
||||
{
|
||||
var lanczos = new Lanczos(5);
|
||||
int count = 0;
|
||||
lanczos.Pub += (object? _, in TValueEventArgs e) => count++;
|
||||
lanczos.Update(new TValue(DateTime.UtcNow, 100.0));
|
||||
Assert.Equal(1, count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EventBased_Chaining()
|
||||
{
|
||||
var source = new TSeries();
|
||||
using var lanczos = new Lanczos(source, 5);
|
||||
|
||||
source.Add(new TValue(DateTime.UtcNow, 100.0), isNew: true);
|
||||
Assert.True(double.IsFinite(lanczos.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Dispose_UnsubscribesFromSource()
|
||||
{
|
||||
var source = new TSeries();
|
||||
var lanczos = new Lanczos(source, 5);
|
||||
lanczos.Dispose();
|
||||
|
||||
source.Add(new TValue(DateTime.UtcNow, 100.0), isNew: true);
|
||||
Assert.Equal(default, lanczos.Last);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Dispose_Idempotent()
|
||||
{
|
||||
var lanczos = new Lanczos(5);
|
||||
lanczos.Dispose();
|
||||
lanczos.Dispose();
|
||||
Assert.True(true);
|
||||
}
|
||||
|
||||
// ── I) Lanczos-specific: sinc properties ───────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void ConstantInput_ReturnsConstant()
|
||||
{
|
||||
var lanczos = new Lanczos(7);
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
lanczos.Update(new TValue(DateTime.UtcNow, 42.0));
|
||||
}
|
||||
Assert.Equal(42.0, lanczos.Last.Value, 1e-10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Period2_ReducesToSma()
|
||||
{
|
||||
// For period=2: k=0 -> x = -1, k=1 -> x = 1
|
||||
// sinc(-1) = sinc(1) = 0, both weights zero -> normalization makes them equal -> SMA(2)
|
||||
int period = 2;
|
||||
var lanczos = new Lanczos(period);
|
||||
var sma = new Sma(period);
|
||||
|
||||
var src = MakeSeries(50);
|
||||
for (int i = 0; i < src.Count; i++)
|
||||
{
|
||||
lanczos.Update(src[i]);
|
||||
sma.Update(src[i]);
|
||||
}
|
||||
|
||||
Assert.Equal(sma.Last.Value, lanczos.Last.Value, 1e-8);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LargerPeriod_SmoothsMore()
|
||||
{
|
||||
var src = MakeSeries(200);
|
||||
|
||||
var smallPeriod = new Lanczos(5);
|
||||
var largePeriod = new Lanczos(20);
|
||||
|
||||
double sumDiffSmall = 0;
|
||||
double sumDiffLarge = 0;
|
||||
int countSmall = 0;
|
||||
int countLarge = 0;
|
||||
|
||||
for (int i = 0; i < src.Count; i++)
|
||||
{
|
||||
double raw = src[i].Value;
|
||||
smallPeriod.Update(src[i]);
|
||||
largePeriod.Update(src[i]);
|
||||
|
||||
if (smallPeriod.IsHot)
|
||||
{
|
||||
sumDiffSmall += Math.Abs(raw - smallPeriod.Last.Value);
|
||||
countSmall++;
|
||||
}
|
||||
if (largePeriod.IsHot)
|
||||
{
|
||||
sumDiffLarge += Math.Abs(raw - largePeriod.Last.Value);
|
||||
countLarge++;
|
||||
}
|
||||
}
|
||||
|
||||
double avgDiffSmall = sumDiffSmall / countSmall;
|
||||
double avgDiffLarge = sumDiffLarge / countLarge;
|
||||
|
||||
// Larger period should smooth more (larger avg deviation from raw)
|
||||
Assert.True(avgDiffLarge > avgDiffSmall);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_ReturnsResultsAndIndicator()
|
||||
{
|
||||
var (results, indicator) = Lanczos.Calculate(_data, 14);
|
||||
Assert.Equal(_data.Count, results.Count);
|
||||
Assert.True(indicator.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Prime_SetsState()
|
||||
{
|
||||
var lanczos = new Lanczos(5);
|
||||
var src = MakeSeries(20);
|
||||
lanczos.Prime(src.Values);
|
||||
Assert.True(lanczos.IsHot);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user