mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-22 20:48:04 +00:00
feat: add LPF - Ehlers Linear Predictive Filter (TASC Jan 2025)
Implements Ehlers' Linear Predictive Filter for dominant cycle detection: - Roofing filter (HP + SuperSmoother) → AGC → Griffiths adaptive predictor - DFT spectrum from predictor coefficients → Center of Gravity dominant cycle - Outputs: DominantCycle, Signal (AGC-normalized), Predict (one-bar-ahead) Files added: - lib/cycles/lpf/Lpf.cs (core implementation, sealed class) - lib/cycles/lpf/Lpf.Quantower.cs (3 LineSeries: Cycle, Signal, Predict) - lib/cycles/lpf/Lpf.md (canonical template v3 documentation) - lib/cycles/lpf/lpf.pine (PineScript v6 reference) - lib/cycles/lpf/tests/Lpf.Tests.cs (38 unit tests) - lib/cycles/lpf/tests/Lpf.Quantower.Tests.cs (22 adapter tests) Updated: index files, Python bridge (Exports.cs, _bridge.py, cycles.py)
This commit is contained in:
@@ -0,0 +1,289 @@
|
||||
using TradingPlatform.BusinessLayer;
|
||||
|
||||
namespace QuanTAlib.Quantower.Tests;
|
||||
|
||||
public class LpfIndicatorTests
|
||||
{
|
||||
[Fact]
|
||||
public void LpfIndicator_Constructor_SetsDefaults()
|
||||
{
|
||||
var indicator = new LpfIndicator();
|
||||
|
||||
Assert.Equal(18, indicator.LowerBound);
|
||||
Assert.Equal(40, indicator.UpperBound);
|
||||
Assert.Equal(40, indicator.DataLength);
|
||||
Assert.Equal(SourceType.Close, indicator.Source);
|
||||
Assert.True(indicator.ShowColdValues);
|
||||
Assert.Equal("LPF - Ehlers Linear Predictive Filter", indicator.Name);
|
||||
Assert.True(indicator.SeparateWindow);
|
||||
Assert.True(indicator.OnBackGround);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LpfIndicator_MinHistoryDepths_EqualsZero()
|
||||
{
|
||||
var indicator = new LpfIndicator();
|
||||
|
||||
Assert.Equal(0, LpfIndicator.MinHistoryDepths);
|
||||
Assert.Equal(0, ((IWatchlistIndicator)indicator).MinHistoryDepths);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LpfIndicator_ShortName_IncludesParameters()
|
||||
{
|
||||
var indicator = new LpfIndicator { LowerBound = 18, UpperBound = 40, DataLength = 40 };
|
||||
|
||||
Assert.True(indicator.ShortName.Contains("LPF", StringComparison.Ordinal));
|
||||
Assert.True(indicator.ShortName.Contains("18", StringComparison.Ordinal));
|
||||
Assert.True(indicator.ShortName.Contains("40", StringComparison.Ordinal));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LpfIndicator_Initialize_CreatesInternalLpf()
|
||||
{
|
||||
var indicator = new LpfIndicator { LowerBound = 18, UpperBound = 40, DataLength = 40 };
|
||||
|
||||
indicator.Initialize();
|
||||
|
||||
// After init, line series should exist (Cycle + Signal + Predict)
|
||||
Assert.Equal(3, indicator.LinesSeries.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LpfIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
|
||||
{
|
||||
var indicator = new LpfIndicator { LowerBound = 18, UpperBound = 40, DataLength = 40 };
|
||||
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 LpfIndicator_ProcessUpdate_NewBar_ComputesValue()
|
||||
{
|
||||
var indicator = new LpfIndicator { LowerBound = 18, UpperBound = 40, DataLength = 40 };
|
||||
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 LpfIndicator_ProcessUpdate_NewTick_ProcessesWithoutError()
|
||||
{
|
||||
var indicator = new LpfIndicator { LowerBound = 18, UpperBound = 40, DataLength = 40 };
|
||||
indicator.Initialize();
|
||||
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewTick));
|
||||
|
||||
Assert.NotNull(indicator);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LpfIndicator_MultipleUpdates_ProducesCorrectSequence()
|
||||
{
|
||||
var indicator = new LpfIndicator { LowerBound = 18, UpperBound = 40, DataLength = 40 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
double[] closes = { 100, 102, 105, 103, 107, 110, 108, 112, 115, 113 };
|
||||
|
||||
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 LpfIndicator_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 LpfIndicator { LowerBound = 18, UpperBound = 40, DataLength = 40, 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 LpfIndicator_LowerBound_CanBeChanged()
|
||||
{
|
||||
var indicator = new LpfIndicator { LowerBound = 18 };
|
||||
|
||||
Assert.Equal(18, indicator.LowerBound);
|
||||
|
||||
indicator.LowerBound = 10;
|
||||
Assert.Equal(10, indicator.LowerBound);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LpfIndicator_UpperBound_CanBeChanged()
|
||||
{
|
||||
var indicator = new LpfIndicator { UpperBound = 40 };
|
||||
|
||||
Assert.Equal(40, indicator.UpperBound);
|
||||
|
||||
indicator.UpperBound = 100;
|
||||
Assert.Equal(100, indicator.UpperBound);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LpfIndicator_DataLength_CanBeChanged()
|
||||
{
|
||||
var indicator = new LpfIndicator { DataLength = 40 };
|
||||
|
||||
Assert.Equal(40, indicator.DataLength);
|
||||
|
||||
indicator.DataLength = 60;
|
||||
Assert.Equal(60, indicator.DataLength);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LpfIndicator_Source_CanBeChanged()
|
||||
{
|
||||
var indicator = new LpfIndicator { Source = SourceType.Close };
|
||||
|
||||
Assert.Equal(SourceType.Close, indicator.Source);
|
||||
|
||||
indicator.Source = SourceType.Open;
|
||||
Assert.Equal(SourceType.Open, indicator.Source);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LpfIndicator_ShowColdValues_CanBeChanged()
|
||||
{
|
||||
var indicator = new LpfIndicator { ShowColdValues = true };
|
||||
|
||||
Assert.True(indicator.ShowColdValues);
|
||||
|
||||
indicator.ShowColdValues = false;
|
||||
Assert.False(indicator.ShowColdValues);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LpfIndicator_ShortName_UpdatesWhenParametersChange()
|
||||
{
|
||||
var indicator = new LpfIndicator { LowerBound = 18, UpperBound = 40, DataLength = 40 };
|
||||
string initialName = indicator.ShortName;
|
||||
|
||||
Assert.True(initialName.Contains("18", StringComparison.Ordinal));
|
||||
Assert.True(initialName.Contains("40", StringComparison.Ordinal));
|
||||
|
||||
indicator.LowerBound = 10;
|
||||
indicator.UpperBound = 60;
|
||||
string updatedName = indicator.ShortName;
|
||||
|
||||
Assert.True(updatedName.Contains("10", StringComparison.Ordinal));
|
||||
Assert.True(updatedName.Contains("60", StringComparison.Ordinal));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LpfIndicator_ProcessUpdate_IgnoresNonBarUpdates()
|
||||
{
|
||||
var indicator = new LpfIndicator { LowerBound = 18, UpperBound = 40, DataLength = 40 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
indicator.HistoricalData.AddBar(now, 100, 105, 95, 102);
|
||||
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewTick));
|
||||
|
||||
Assert.NotNull(indicator);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LpfIndicator_CycleSeries_HasCorrectProperties()
|
||||
{
|
||||
var indicator = new LpfIndicator();
|
||||
indicator.Initialize();
|
||||
|
||||
var lineSeries = indicator.LinesSeries[0];
|
||||
|
||||
Assert.Equal("Cycle", lineSeries.Name);
|
||||
Assert.Equal(2, lineSeries.Width);
|
||||
Assert.Equal(LineStyle.Solid, lineSeries.Style);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LpfIndicator_SignalSeries_HasCorrectProperties()
|
||||
{
|
||||
var indicator = new LpfIndicator();
|
||||
indicator.Initialize();
|
||||
|
||||
var signalSeries = indicator.LinesSeries[1];
|
||||
|
||||
Assert.Equal("Signal", signalSeries.Name);
|
||||
Assert.Equal(1, signalSeries.Width);
|
||||
Assert.Equal(LineStyle.Solid, signalSeries.Style);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LpfIndicator_PredictSeries_HasCorrectProperties()
|
||||
{
|
||||
var indicator = new LpfIndicator();
|
||||
indicator.Initialize();
|
||||
|
||||
var predictSeries = indicator.LinesSeries[2];
|
||||
|
||||
Assert.Equal("Predict", predictSeries.Name);
|
||||
Assert.Equal(1, predictSeries.Width);
|
||||
Assert.Equal(LineStyle.Dot, predictSeries.Style);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LpfIndicator_SineWave_ProducesFiniteValues()
|
||||
{
|
||||
var indicator = new LpfIndicator { LowerBound = 10, UpperBound = 50, DataLength = 50 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
const int knownPeriod = 30;
|
||||
|
||||
for (int i = 0; i < 200; i++)
|
||||
{
|
||||
double price = 100.0 + 10.0 * Math.Sin(2.0 * Math.PI * i / knownPeriod);
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), price, price + 1, price - 1, price);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
}
|
||||
|
||||
double cycleValue = indicator.LinesSeries[0].GetValue(0);
|
||||
Assert.InRange(cycleValue, 10, 50);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LpfIndicator_SourceCodeLink_IsValid()
|
||||
{
|
||||
var indicator = new LpfIndicator();
|
||||
Assert.Contains("github.com", indicator.SourceCodeLink);
|
||||
Assert.Contains("Lpf.Quantower.cs", indicator.SourceCodeLink);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,507 @@
|
||||
using Xunit;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class LpfTests
|
||||
{
|
||||
private const double Tolerance = 1e-9;
|
||||
|
||||
#region Constructor Tests
|
||||
|
||||
[Fact]
|
||||
public void Constructor_DefaultParameters_SetsProperties()
|
||||
{
|
||||
var lpf = new Lpf();
|
||||
|
||||
Assert.Equal("LPF(18,40,40)", lpf.Name);
|
||||
Assert.False(lpf.IsHot);
|
||||
Assert.Equal(18, lpf.LowerBound);
|
||||
Assert.Equal(40, lpf.UpperBound);
|
||||
Assert.Equal(40, lpf.DataLength);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_CustomParameters_SetsProperties()
|
||||
{
|
||||
var lpf = new Lpf(lowerBound: 10, upperBound: 60, dataLength: 50);
|
||||
|
||||
Assert.Equal("LPF(10,60,50)", lpf.Name);
|
||||
Assert.Equal(10, lpf.LowerBound);
|
||||
Assert.Equal(60, lpf.UpperBound);
|
||||
Assert.Equal(50, lpf.DataLength);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(7)]
|
||||
[InlineData(0)]
|
||||
[InlineData(-1)]
|
||||
public void Constructor_InvalidLowerBound_ThrowsArgumentOutOfRange(int lower)
|
||||
{
|
||||
var ex = Assert.Throws<ArgumentOutOfRangeException>(() => new Lpf(lower, 40));
|
||||
Assert.Equal("lowerBound", ex.ParamName);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(18, 18)]
|
||||
[InlineData(18, 10)]
|
||||
[InlineData(20, 20)]
|
||||
public void Constructor_UpperBoundNotGreaterThanLower_ThrowsArgumentOutOfRange(int lower, int upper)
|
||||
{
|
||||
var ex = Assert.Throws<ArgumentOutOfRangeException>(() => new Lpf(lower, upper));
|
||||
Assert.Equal("upperBound", ex.ParamName);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(3)]
|
||||
[InlineData(0)]
|
||||
[InlineData(-1)]
|
||||
public void Constructor_InvalidDataLength_ThrowsArgumentOutOfRange(int len)
|
||||
{
|
||||
var ex = Assert.Throws<ArgumentOutOfRangeException>(() => new Lpf(18, 40, len));
|
||||
Assert.Equal("dataLength", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WithNullSource_ThrowsArgumentNullException()
|
||||
{
|
||||
Assert.Throws<ArgumentNullException>(() => new Lpf(null!, 18, 40));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WithValidSource_Subscribes()
|
||||
{
|
||||
var source = new TSeries();
|
||||
var lpf = new Lpf(source, 18, 40);
|
||||
|
||||
source.Add(new TValue(DateTime.UtcNow, 100.0));
|
||||
Assert.NotEqual(default, lpf.Last);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Basic Calculation Tests
|
||||
|
||||
[Fact]
|
||||
public void Update_ReturnsValidTValue()
|
||||
{
|
||||
var lpf = new Lpf();
|
||||
var result = lpf.Update(new TValue(DateTime.UtcNow, 100.0));
|
||||
|
||||
Assert.True(double.IsFinite(result.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_AfterWarmup_IsHotTrue()
|
||||
{
|
||||
var lpf = new Lpf();
|
||||
|
||||
var gbm = new GBM(seed: 42);
|
||||
var bars = gbm.Fetch(200, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
foreach (var bar in bars)
|
||||
{
|
||||
lpf.Update(new TValue(bar.Time, bar.Close));
|
||||
}
|
||||
|
||||
Assert.True(lpf.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_DominantCycle_WithinRange()
|
||||
{
|
||||
var lpf = new Lpf(18, 40, 40);
|
||||
|
||||
var gbm = new GBM(seed: 42);
|
||||
var bars = gbm.Fetch(500, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
foreach (var bar in bars)
|
||||
{
|
||||
lpf.Update(new TValue(bar.Time, bar.Close));
|
||||
}
|
||||
|
||||
Assert.InRange(lpf.DominantCycle, 18, 40);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_Signal_WithinUnitRange()
|
||||
{
|
||||
var lpf = new Lpf();
|
||||
|
||||
var gbm = new GBM(seed: 42);
|
||||
var bars = gbm.Fetch(500, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
foreach (var bar in bars)
|
||||
{
|
||||
lpf.Update(new TValue(bar.Time, bar.Close));
|
||||
}
|
||||
|
||||
// AGC normalization should keep signal within [-1, 1]
|
||||
Assert.InRange(lpf.Signal, -1.0, 1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_InitialValue_WithinBounds()
|
||||
{
|
||||
var lpf = new Lpf(18, 40, 40);
|
||||
|
||||
var result = lpf.Update(new TValue(DateTime.UtcNow, 100.0));
|
||||
|
||||
Assert.True(result.Value >= 18 && result.Value <= 40);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_PureSine_ConvergesNearTruePeriod()
|
||||
{
|
||||
int truePeriod = 30;
|
||||
var lpf = new Lpf(lowerBound: 10, upperBound: 50, dataLength: 50);
|
||||
|
||||
// Feed a pure sine wave with known period
|
||||
for (int i = 0; i < 500; i++)
|
||||
{
|
||||
double val = Math.Sin(2.0 * Math.PI * i / truePeriod);
|
||||
lpf.Update(new TValue(DateTime.UtcNow.AddSeconds(i), val));
|
||||
}
|
||||
|
||||
// Should converge reasonably close to true period
|
||||
// Allow generous tolerance since LPF needs time to adapt
|
||||
Assert.InRange(lpf.DominantCycle, truePeriod - 10, truePeriod + 10);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Bar Correction Tests
|
||||
|
||||
[Fact]
|
||||
public void Update_IsNewTrue_AdvancesState()
|
||||
{
|
||||
var lpf = new Lpf();
|
||||
|
||||
lpf.Update(new TValue(DateTime.UtcNow, 100.0), isNew: true);
|
||||
var first = lpf.Last.Value;
|
||||
|
||||
lpf.Update(new TValue(DateTime.UtcNow.AddSeconds(1), 110.0), isNew: true);
|
||||
var second = lpf.Last.Value;
|
||||
|
||||
Assert.True(double.IsFinite(first) && double.IsFinite(second));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_IsNewFalse_ReplacesCurrentBar()
|
||||
{
|
||||
var lpf = new Lpf();
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
lpf.Update(new TValue(DateTime.UtcNow.AddSeconds(i), 100.0 + Math.Sin(i * 0.1) * 10), isNew: true);
|
||||
}
|
||||
|
||||
lpf.Update(new TValue(DateTime.UtcNow.AddSeconds(100), 110.0), isNew: true);
|
||||
var beforeCorrection = lpf.Last.Value;
|
||||
|
||||
lpf.Update(new TValue(DateTime.UtcNow.AddSeconds(100), 90.0), isNew: false);
|
||||
var afterCorrection = lpf.Last.Value;
|
||||
|
||||
Assert.True(double.IsFinite(beforeCorrection) && double.IsFinite(afterCorrection));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_MultipleCorrections_RestoresToSnapshot()
|
||||
{
|
||||
var lpf = new Lpf();
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
lpf.Update(new TValue(DateTime.UtcNow.AddSeconds(i), 100.0 + i), isNew: true);
|
||||
}
|
||||
|
||||
lpf.Update(new TValue(DateTime.UtcNow.AddSeconds(100), 150.0), isNew: true);
|
||||
var originalValue = lpf.Last.Value;
|
||||
|
||||
lpf.Update(new TValue(DateTime.UtcNow.AddSeconds(100), 160.0), isNew: false);
|
||||
lpf.Update(new TValue(DateTime.UtcNow.AddSeconds(100), 140.0), isNew: false);
|
||||
lpf.Update(new TValue(DateTime.UtcNow.AddSeconds(100), 150.0), isNew: false);
|
||||
var restoredValue = lpf.Last.Value;
|
||||
|
||||
Assert.Equal(originalValue, restoredValue, Tolerance);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Reset Tests
|
||||
|
||||
[Fact]
|
||||
public void Reset_ClearsState()
|
||||
{
|
||||
var lpf = new Lpf();
|
||||
|
||||
for (int i = 0; i < 200; i++)
|
||||
{
|
||||
lpf.Update(new TValue(DateTime.UtcNow.AddSeconds(i), 100.0 + i));
|
||||
}
|
||||
|
||||
Assert.True(lpf.IsHot);
|
||||
|
||||
lpf.Reset();
|
||||
|
||||
Assert.False(lpf.IsHot);
|
||||
Assert.Equal(default, lpf.Last);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Reset_AllowsReuse()
|
||||
{
|
||||
var lpf = new Lpf();
|
||||
|
||||
for (int i = 0; i < 200; i++)
|
||||
{
|
||||
lpf.Update(new TValue(DateTime.UtcNow.AddSeconds(i), 100.0 + Math.Sin(i * 0.1) * 10));
|
||||
}
|
||||
var firstResult = lpf.Last.Value;
|
||||
|
||||
lpf.Reset();
|
||||
|
||||
for (int i = 0; i < 200; i++)
|
||||
{
|
||||
lpf.Update(new TValue(DateTime.UtcNow.AddSeconds(i), 100.0 + Math.Sin(i * 0.1) * 10));
|
||||
}
|
||||
var secondResult = lpf.Last.Value;
|
||||
|
||||
Assert.Equal(firstResult, secondResult, Tolerance);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region NaN/Infinity Handling Tests
|
||||
|
||||
[Fact]
|
||||
public void Update_NaN_UsesLastValidValue()
|
||||
{
|
||||
var lpf = new Lpf();
|
||||
|
||||
lpf.Update(new TValue(DateTime.UtcNow, 100.0));
|
||||
lpf.Update(new TValue(DateTime.UtcNow.AddSeconds(1), double.NaN));
|
||||
|
||||
Assert.True(double.IsFinite(lpf.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_Infinity_UsesLastValidValue()
|
||||
{
|
||||
var lpf = new Lpf();
|
||||
|
||||
lpf.Update(new TValue(DateTime.UtcNow, 100.0));
|
||||
lpf.Update(new TValue(DateTime.UtcNow.AddSeconds(1), double.PositiveInfinity));
|
||||
|
||||
Assert.True(double.IsFinite(lpf.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_NegativeInfinity_UsesLastValidValue()
|
||||
{
|
||||
var lpf = new Lpf();
|
||||
|
||||
lpf.Update(new TValue(DateTime.UtcNow, 100.0));
|
||||
lpf.Update(new TValue(DateTime.UtcNow.AddSeconds(1), double.NegativeInfinity));
|
||||
|
||||
Assert.True(double.IsFinite(lpf.Last.Value));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Consistency Tests
|
||||
|
||||
[Theory]
|
||||
[InlineData(42)]
|
||||
[InlineData(123)]
|
||||
[InlineData(456)]
|
||||
public void Update_Deterministic_AcrossSeeds(int seed)
|
||||
{
|
||||
var lpf1 = new Lpf();
|
||||
var lpf2 = new Lpf();
|
||||
|
||||
var gbm = new GBM(seed: seed);
|
||||
var bars = gbm.Fetch(300, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
foreach (var bar in bars)
|
||||
{
|
||||
var input = new TValue(bar.Time, bar.Close);
|
||||
lpf1.Update(input);
|
||||
lpf2.Update(input);
|
||||
}
|
||||
|
||||
Assert.Equal(lpf1.Last.Value, lpf2.Last.Value, Tolerance);
|
||||
Assert.Equal(lpf1.DominantCycle, lpf2.DominantCycle, Tolerance);
|
||||
Assert.Equal(lpf1.Signal, lpf2.Signal, Tolerance);
|
||||
Assert.Equal(lpf1.Predict, lpf2.Predict, Tolerance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_MatchesStreaming()
|
||||
{
|
||||
var gbm = new GBM(seed: 42);
|
||||
var bars = gbm.Fetch(200, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
// TSeries from bars
|
||||
var source = new TSeries();
|
||||
foreach (var bar in bars)
|
||||
{
|
||||
source.Add(new TValue(bar.Time, bar.Close));
|
||||
}
|
||||
|
||||
// Batch
|
||||
var batchResult = Lpf.Batch(source, 18, 40, 40);
|
||||
|
||||
// Streaming
|
||||
var lpf = new Lpf(18, 40, 40);
|
||||
var streamResults = new List<double>();
|
||||
foreach (var bar in bars)
|
||||
{
|
||||
var r = lpf.Update(new TValue(bar.Time, bar.Close));
|
||||
streamResults.Add(r.Value);
|
||||
}
|
||||
|
||||
Assert.Equal(streamResults.Count, batchResult.Count);
|
||||
for (int i = 0; i < streamResults.Count; i++)
|
||||
{
|
||||
Assert.Equal(streamResults[i], batchResult[i].Value, Tolerance);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BatchSpan_MatchesStreaming()
|
||||
{
|
||||
var gbm = new GBM(seed: 42);
|
||||
var bars = gbm.Fetch(200, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
double[] input = bars.Select(b => b.Close).ToArray();
|
||||
double[] output = new double[input.Length];
|
||||
|
||||
Lpf.Batch(input, output, 18, 40, 40);
|
||||
|
||||
var lpf = new Lpf(18, 40, 40);
|
||||
for (int i = 0; i < input.Length; i++)
|
||||
{
|
||||
var r = lpf.Update(new TValue(DateTime.MinValue, input[i]));
|
||||
Assert.Equal(r.Value, output[i], Tolerance);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constant Input Tests
|
||||
|
||||
[Fact]
|
||||
public void Update_ConstantInput_NoNaNOrInf()
|
||||
{
|
||||
var lpf = new Lpf();
|
||||
|
||||
for (int i = 0; i < 200; i++)
|
||||
{
|
||||
var result = lpf.Update(new TValue(DateTime.UtcNow.AddSeconds(i), 100.0));
|
||||
Assert.True(double.IsFinite(result.Value), $"Non-finite result at bar {i}: {result.Value}");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_ZeroInput_NoNaNOrInf()
|
||||
{
|
||||
var lpf = new Lpf();
|
||||
|
||||
for (int i = 0; i < 200; i++)
|
||||
{
|
||||
var result = lpf.Update(new TValue(DateTime.UtcNow.AddSeconds(i), 0.0));
|
||||
Assert.True(double.IsFinite(result.Value), $"Non-finite result at bar {i}: {result.Value}");
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Calculate + Dispose Tests
|
||||
|
||||
[Fact]
|
||||
public void Calculate_ReturnsResultsAndIndicator()
|
||||
{
|
||||
var gbm = new GBM(seed: 42);
|
||||
var bars = gbm.Fetch(200, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
var source = new TSeries();
|
||||
foreach (var bar in bars)
|
||||
{
|
||||
source.Add(new TValue(bar.Time, bar.Close));
|
||||
}
|
||||
|
||||
var (results, indicator) = Lpf.Calculate(source, 18, 40, 40);
|
||||
|
||||
Assert.Equal(200, results.Count);
|
||||
Assert.True(indicator.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Dispose_UnsubscribesFromSource()
|
||||
{
|
||||
var source = new TSeries();
|
||||
var lpf = new Lpf(source, 18, 40, 40);
|
||||
|
||||
source.Add(new TValue(DateTime.UtcNow, 100.0));
|
||||
Assert.NotEqual(default, lpf.Last);
|
||||
|
||||
lpf.Dispose();
|
||||
|
||||
// After dispose, adding to source should not update lpf
|
||||
var lastBefore = lpf.Last;
|
||||
source.Add(new TValue(DateTime.UtcNow.AddSeconds(1), 200.0));
|
||||
Assert.Equal(lastBefore, lpf.Last);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region DominantCycle Rate Constraint Tests
|
||||
|
||||
[Fact]
|
||||
public void Update_DominantCycle_ChangeConstrainedToTwo()
|
||||
{
|
||||
var lpf = new Lpf(10, 50, 40);
|
||||
|
||||
// Feed data and track DC changes
|
||||
var gbm = new GBM(seed: 42);
|
||||
var bars = gbm.Fetch(500, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
double prevDC = 0;
|
||||
bool first = true;
|
||||
foreach (var bar in bars)
|
||||
{
|
||||
lpf.Update(new TValue(bar.Time, bar.Close));
|
||||
double dc = lpf.DominantCycle;
|
||||
if (!first)
|
||||
{
|
||||
double delta = Math.Abs(dc - prevDC);
|
||||
Assert.True(delta <= 2.0 + 1e-10, $"DC changed by {delta} > 2.0");
|
||||
}
|
||||
prevDC = dc;
|
||||
first = false;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Prime Tests
|
||||
|
||||
[Fact]
|
||||
public void Prime_SetsState()
|
||||
{
|
||||
var lpf = new Lpf();
|
||||
|
||||
double[] data = new double[200];
|
||||
for (int i = 0; i < 200; i++)
|
||||
{
|
||||
data[i] = 100.0 + Math.Sin(i * 0.1) * 10;
|
||||
}
|
||||
|
||||
lpf.Prime(data);
|
||||
|
||||
Assert.True(lpf.IsHot);
|
||||
Assert.True(double.IsFinite(lpf.Last.Value));
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
Reference in New Issue
Block a user