diff --git a/Indicators/Ema/Ema.cs b/Indicators/Ema/Ema.cs index 9d4c658ee..12aa09f6b 100644 --- a/Indicators/Ema/Ema.cs +++ b/Indicators/Ema/Ema.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; namespace Skender.Stock.Indicators @@ -59,14 +60,15 @@ private static void ValidateEma(IEnumerable history, int lookbackPeriod) // check history int qtyHistory = history.Count(); - int minHistory = 2 * lookbackPeriod; + int minHistory = Math.Max(2 * lookbackPeriod, lookbackPeriod + 100); if (qtyHistory < minHistory) { throw new BadHistoryException("Insufficient history provided for EMA. " + string.Format("You provided {0} periods of history when at least {1} is required. " - + "Since this uses a smoothing technique, " - + "we recommend you use at least 250 data points prior to the intended " - + "usage date for maximum precision.", qtyHistory, minHistory)); + + "Since this uses a smoothing technique, for a lookback period of {2}, " + + "we recommend you use at least {3} data points prior to the intended " + + "usage date for maximum precision.", + qtyHistory, minHistory, lookbackPeriod, 2 * lookbackPeriod + 250)); } } diff --git a/Indicators/Ema/README.md b/Indicators/Ema/README.md index 58ecf0b7e..daaea9ad1 100644 --- a/Indicators/Ema/README.md +++ b/Indicators/Ema/README.md @@ -12,7 +12,7 @@ IEnumerable results = Indicator.GetEma(history, lookbackPeriod); | name | type | notes | -- |-- |-- -| `history` | IEnumerable\<[Quote](/GUIDE.md#Quote)\> | Historical Quotes data should be at any consistent frequency (day, hour, minute, etc). You must supply at least 2×`N` periods of `history`. Since this uses a smoothing technique, we recommend you use at least 250 data points prior to the intended usage date for maximum precision. +| `history` | IEnumerable\<[Quote](/GUIDE.md#Quote)\> | Historical Quotes data should be at any consistent frequency (day, hour, minute, etc). You must supply at least 2×`N` or `N`+100 periods of `history`, whichever is more. Since this uses a smoothing technique, we recommend you use at least 2×`N`+250 data points prior to the intended usage date for maximum precision. | `lookbackPeriod` | int | Number of periods (`N`) in the moving average. Must be greater than 0. ## Response diff --git a/IndicatorsTests/Test.Ema.cs b/IndicatorsTests/Test.Ema.cs index d71847431..0ac179798 100644 --- a/IndicatorsTests/Test.Ema.cs +++ b/IndicatorsTests/Test.Ema.cs @@ -39,10 +39,17 @@ public void BadLookback() } [TestMethod()] - [ExpectedException(typeof(BadHistoryException), "Insufficient history.")] - public void InsufficientHistory() + [ExpectedException(typeof(BadHistoryException), "Insufficient history for N+100.")] + public void InsufficientHistoryA() { - Indicator.GetEma(history.Where(x => x.Index < 30), 30); + Indicator.GetEma(history.Where(x => x.Index < 130), 30); + } + + [TestMethod()] + [ExpectedException(typeof(BadHistoryException), "Insufficient history for 2×N.")] + public void InsufficientHistoryB() + { + Indicator.GetEma(history.Where(x => x.Index < 500), 250); } }