Skip to content

Commit

Permalink
update EMA history requirements (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaveSkender authored Jun 13, 2020
1 parent 5693a9c commit 8e31934
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
12 changes: 7 additions & 5 deletions Indicators/Ema/Ema.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Skender.Stock.Indicators
Expand Down Expand Up @@ -59,14 +60,15 @@ private static void ValidateEma(IEnumerable<Quote> 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));
}

}
Expand Down
2 changes: 1 addition & 1 deletion Indicators/Ema/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ IEnumerable<EmaResult> 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 `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
Expand Down
13 changes: 10 additions & 3 deletions IndicatorsTests/Test.Ema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
Expand Down

0 comments on commit 8e31934

Please sign in to comment.