diff --git a/Tests/test_eventing.cs b/Tests/test_eventing.cs index 90a71ac8..f51e50b0 100644 --- a/Tests/test_eventing.cs +++ b/Tests/test_eventing.cs @@ -17,39 +17,39 @@ public class EventingTests var input = new TSeries(); int p = 10; - // Create a list of indicator pairs (direct calculation and event-based) - var indicators = new List<(AbstractBase Direct, AbstractBase EventBased)> + // Create a list of indicator pairs (direct calculation and event-based) with names + var indicators = new List<(string Name, AbstractBase Direct, AbstractBase EventBased)> { - (new Afirma(p,p,Afirma.WindowType.BlackmanHarris), new Afirma(input, p,p,Afirma.WindowType.BlackmanHarris)), - (new Alma(p), new Alma(input, p)), - (new Convolution([1,2,3,2,1]), new Convolution(input, [1,2,3,2,1])), - (new Dema(p), new Dema(input, p)), - (new Dsma(p), new Dsma(input, p)), - (new Dwma(p), new Dwma(input, p)), - (new Ema(p), new Ema(input, p)), - (new Epma(p), new Epma(input, p)), - (new Frama(p), new Frama(input, p)), - (new Fwma(p), new Fwma(input, p)), - (new Gma(p), new Gma(input, p)), - (new Hma(p), new Hma(input, p)), - (new Htit(), new Htit(input)), - (new Hwma(p), new Hwma(input, p)), - (new Jma(p), new Jma(input, p)), - (new Kama(p), new Kama(input, p)), - (new Ltma(gamma: 0.2), new Ltma(input, gamma: 0.2)), - (new Maaf(p), new Maaf(input, p)), - (new Mama(p), new Mama(input, p)), - (new Mgdi(p), new Mgdi(input, p)), - (new Mma(p), new Mma(input, p)), - (new Qema(k1: 0.2, k2: 0.2, k3: 0.2, k4: 0.2), new Qema(input, k1: 0.2, k2: 0.2, k3: 0.2, k4: 0.2)), - (new Rema(p), new Rema(input, p)), - (new Rma(p), new Rma(input, p)), - (new Sma(p), new Sma(input, p)), - (new Wma(p), new Wma(input, p)), - (new Rma(p), new Rma(input, p)), - (new Tema(p), new Tema(input, p)), - (new Kama(2, 30, 6), new Kama(input, 2, 30, 6)), - (new Zlema(p), new Zlema(input, p)) + ("Afirma", new Afirma(p,p,Afirma.WindowType.BlackmanHarris), new Afirma(input, p,p,Afirma.WindowType.BlackmanHarris)), + ("Alma", new Alma(p), new Alma(input, p)), + ("Convolution", new Convolution(new double[] {1,2,3,2,1}), new Convolution(input, new double[] {1,2,3,2,1})), + ("Dema", new Dema(p), new Dema(input, p)), + ("Dsma", new Dsma(p), new Dsma(input, p)), + ("Dwma", new Dwma(p), new Dwma(input, p)), + ("Ema", new Ema(p), new Ema(input, p)), + ("Epma", new Epma(p), new Epma(input, p)), + ("Frama", new Frama(p), new Frama(input, p)), + ("Fwma", new Fwma(p), new Fwma(input, p)), + ("Gma", new Gma(p), new Gma(input, p)), + ("Hma", new Hma(p), new Hma(input, p)), + ("Htit", new Htit(), new Htit(input)), + ("Hwma", new Hwma(p), new Hwma(input, p)), + ("Jma", new Jma(p), new Jma(input, p)), + ("Kama", new Kama(p), new Kama(input, p)), + ("Ltma", new Ltma(gamma: 0.2), new Ltma(input, gamma: 0.2)), + ("Maaf", new Maaf(p), new Maaf(input, p)), + ("Mama", new Mama(p), new Mama(input, p)), + ("Mgdi", new Mgdi(p, kFactor: 0.6), new Mgdi(input, p, kFactor: 0.6)), + ("Mma", new Mma(p), new Mma(input, p)), + ("Qema", new Qema(k1: 0.2, k2: 0.2, k3: 0.2, k4: 0.2), new Qema(input, k1: 0.2, k2: 0.2, k3: 0.2, k4: 0.2)), + ("Rema", new Rema(p), new Rema(input, p)), + ("Rma", new Rma(p), new Rma(input, p)), + ("Sma", new Sma(p), new Sma(input, p)), + ("Wma", new Wma(p), new Wma(input, p)), + ("Rma", new Rma(p), new Rma(input, p)), + ("Tema", new Tema(p), new Tema(input, p)), + ("Kama", new Kama(2, 30, 6), new Kama(input, 2, 30, 6)), + ("Zlema", new Zlema(p), new Zlema(input, p)) }; // Generate 200 random values and feed them to both direct and event-based indicators @@ -59,23 +59,26 @@ public class EventingTests input.Add(randomValue); // Calculate direct indicators - foreach (var (direct, _) in indicators) + foreach (var (_, direct, _) in indicators) { direct.Calc(randomValue); } } // Compare the results of direct and event-based calculations - foreach (var (direct, eventBased) in indicators) + for (int i = 0; i < indicators.Count; i++) { - Assert.Equal(direct.Value, eventBased.Value, 9); + var (name, direct, eventBased) = indicators[i]; + bool areEqual = (double.IsNaN(direct.Value) && double.IsNaN(eventBased.Value)) || + Math.Abs(direct.Value - eventBased.Value) < 1e-9; + Assert.True(areEqual, $"Indicator {name} failed: Expected {direct.Value}, Actual {eventBased.Value}"); } } -private static double GetRandomDouble(RandomNumberGenerator rng) -{ - byte[] bytes = new byte[8]; - rng.GetBytes(bytes); - return (double)BitConverter.ToUInt64(bytes, 0) / ulong.MaxValue; -} -} + private static double GetRandomDouble(RandomNumberGenerator rng) + { + byte[] bytes = new byte[8]; + rng.GetBytes(bytes); + return (double)BitConverter.ToUInt64(bytes, 0) / ulong.MaxValue; + } +} \ No newline at end of file diff --git a/Tests/test_iTValue.cs b/Tests/test_iTValue.cs index 6c0a17e1..887c78cf 100644 --- a/Tests/test_iTValue.cs +++ b/Tests/test_iTValue.cs @@ -3,8 +3,6 @@ using System.Reflection; using System.Diagnostics.CodeAnalysis; using System.Security.Cryptography; -#pragma warning disable S1944, S2053, S2222, S2259, S2583, S2589, S3329, S3655, S3900, S3949, S3966, S4158, S4347, S5773, S6781 - namespace QuanTAlib; public class IndicatorTests diff --git a/Tests/test_talib.cs b/Tests/test_talib.cs index f88a2a36..fa8d07c8 100644 --- a/Tests/test_talib.cs +++ b/Tests/test_talib.cs @@ -3,8 +3,6 @@ using TALib; using System.Diagnostics.CodeAnalysis; using System.Security.Cryptography; -#pragma warning disable S1944, S2053, S2222, S2259, S2583, S2589, S3329, S3655, S3900, S3949, S3966, S4158, S4347, S5773, S6781 - namespace QuanTAlib; public class TAlibTests @@ -123,7 +121,7 @@ public class TAlibTests { QL.Add(ma.Calc(new TValue(item.Time, item.Close))); } Core.Wma(data, 0, QL.Length - 1, TALIB, out int outBegIdx, out _, period); Assert.Equal(QL.Length, TALIB.Count()); - for (int i = QL.Length - 1; i > period * 10; i--) + for (int i = QL.Length - 1; i > 1000; i--) { Assert.InRange(TALIB[i - outBegIdx] - QL[i].Value, -range, range); } diff --git a/lib/averages/Htit.cs b/lib/averages/Htit.cs index cc1a2f2b..487a89f5 100644 --- a/lib/averages/Htit.cs +++ b/lib/averages/Htit.cs @@ -1,5 +1,5 @@ //not working yet -//TODO consistency test +//TODO fails consistency test namespace QuanTAlib; diff --git a/lib/averages/Jma.cs b/lib/averages/Jma.cs index 00b77de8..8a71cd01 100644 --- a/lib/averages/Jma.cs +++ b/lib/averages/Jma.cs @@ -1,5 +1,5 @@ namespace QuanTAlib; -//TODO consistency test +//TODO fails consistency test public class Jma : AbstractBase { public readonly int Period; diff --git a/lib/averages/Maaf.cs b/lib/averages/Maaf.cs index 261adf52..bc730ade 100644 --- a/lib/averages/Maaf.cs +++ b/lib/averages/Maaf.cs @@ -1,4 +1,4 @@ -//TODO: consistency test +//TODO: fails consistency test namespace QuanTAlib; diff --git a/lib/averages/Mgdi.cs b/lib/averages/Mgdi.cs index f137821d..27d8dd4a 100644 --- a/lib/averages/Mgdi.cs +++ b/lib/averages/Mgdi.cs @@ -22,7 +22,7 @@ public class Mgdi : AbstractBase Init(); } - public Mgdi(object source, int period, double kFactor = 1.0) : this(period, kFactor) + public Mgdi(object source, int period, double kFactor = 0.6) : this(period, kFactor) { var pubEvent = source.GetType().GetEvent("Pub"); pubEvent?.AddEventHandler(source, new ValueSignal(Sub)); @@ -58,8 +58,9 @@ public class Mgdi : AbstractBase } else { + double ratio = _prevMd != 0 ? value / _prevMd : 1; double md = _prevMd + ((value - _prevMd) / - (_kFactor * _period * Math.Pow(value / _prevMd, 4))); + (_kFactor * _period * Math.Pow(ratio, 4))); _prevMd = md; }