From 3d413e84514d12c1681ea8782cb43fe81fa93b2e Mon Sep 17 00:00:00 2001 From: Dave Skender <8432125+DaveSkender@users.noreply.github.com> Date: Sat, 4 Nov 2023 04:18:54 -0400 Subject: [PATCH] fix #1107 --- src/s-z/Stoch/Stoch.Series.cs | 4 ++-- tests/indicators/s-z/Stc/Stc.Tests.cs | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/s-z/Stoch/Stoch.Series.cs b/src/s-z/Stoch/Stoch.Series.cs index debcb40b4..3f0aaa22e 100644 --- a/src/s-z/Stoch/Stoch.Series.cs +++ b/src/s-z/Stoch/Stoch.Series.cs @@ -73,7 +73,7 @@ internal static List CalcStoch( // signal (%D) and %J int signalIndex = lookbackPeriods + smoothPeriods + signalPeriods - 2; - double? s = results[lookbackPeriods - 1].Oscillator; + double? s = null; for (int i = lookbackPeriods - 1; i < length; i++) { @@ -102,7 +102,7 @@ internal static List CalcStoch( // SMMA case else if (i >= lookbackPeriods - 1 && movingAverageType is MaType.SMMA) { - s ??= results[i].Oscillator; // reset if null + s ??= results[i].Oscillator; // set initial or reset if null s = ((s * (signalPeriods - 1)) + results[i].Oscillator) / signalPeriods; r.Signal = s; diff --git a/tests/indicators/s-z/Stc/Stc.Tests.cs b/tests/indicators/s-z/Stc/Stc.Tests.cs index 80c73566e..23f9066fb 100644 --- a/tests/indicators/s-z/Stc/Stc.Tests.cs +++ b/tests/indicators/s-z/Stc/Stc.Tests.cs @@ -118,6 +118,20 @@ public void NoQuotes() Assert.AreEqual(1, r1.Count); } + [TestMethod] + public void Issue1107() + { + // stochastic SMMA variant initialization bug + + RandomGbm quotes = new(58); + + List results = quotes + .GetStc(10, 23, 50) + .ToList(); + + Assert.AreEqual(58, results.Count); + } + [TestMethod] public void Removed() {