Skip to content

Commit

Permalink
fix: stoch SMMA init bug (#1108)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaveSkender authored Nov 4, 2023
1 parent c4f020d commit aba673c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/s-z/Stoch/Stoch.Series.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ internal static List<StochResult> 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++)
{
Expand Down Expand Up @@ -102,7 +102,7 @@ internal static List<StochResult> 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;
Expand Down
14 changes: 14 additions & 0 deletions tests/indicators/s-z/Stc/Stc.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<StcResult> results = quotes
.GetStc(10, 23, 50)
.ToList();

Assert.AreEqual(58, results.Count);
}

[TestMethod]
public void Removed()
{
Expand Down

0 comments on commit aba673c

Please sign in to comment.