// COMMODITY CHANNEL INDEX /// <include file='./info.xml' path='indicator/*' /> /// public static IEnumerable <CciResult> GetCci <TQuote>( this IEnumerable <TQuote> quotes, int lookbackPeriods = 20) where TQuote : IQuote { // convert quotes List <QuoteD> quotesList = quotes.ConvertToList(); // check parameter arguments ValidateCci(lookbackPeriods); // initialize List <CciResult> results = new(quotesList.Count); // roll through quotes for (int i = 0; i < quotesList.Count; i++) { QuoteD q = quotesList[i]; int index = i + 1; CciResult result = new() { Date = q.Date, Tp = (q.High + q.Low + q.Close) / 3 }; results.Add(result); if (index >= lookbackPeriods) { // average TP over lookback double avgTp = 0; for (int p = index - lookbackPeriods; p < index; p++) { CciResult d = results[p]; avgTp += (double)d.Tp; } avgTp /= lookbackPeriods; // average Deviation over lookback double avgDv = 0; for (int p = index - lookbackPeriods; p < index; p++) { CciResult d = results[p]; avgDv += Math.Abs(avgTp - (double)d.Tp); } avgDv /= lookbackPeriods; result.Cci = (avgDv == 0) ? null : (result.Tp - avgTp) / (0.015 * avgDv); } } return(results); }
public void Removed() { List <CciResult> results = quotes.GetCci(20) .RemoveWarmupPeriods() .ToList(); // assertions Assert.AreEqual(502 - 19, results.Count); CciResult last = results.LastOrDefault(); Assert.AreEqual(-52.9946, Math.Round((double)last.Cci, 4)); }
public void Standard() { List <CciResult> results = quotes.GetCci(20).ToList(); // assertions // proper quantities // should always be the same number of results as there is quotes Assert.AreEqual(502, results.Count); Assert.AreEqual(483, results.Where(x => x.Cci != null).Count()); // sample value CciResult r = results[501]; Assert.AreEqual(-52.9946, Math.Round((double)r.Cci, 4)); }
public void GetCciTest() { int lookbackPeriod = 20; IEnumerable <CciResult> results = Indicator.GetCci(history, lookbackPeriod); // assertions // proper quantities // should always be the same number of results as there is history Assert.AreEqual(502, results.Count()); Assert.AreEqual(502 - lookbackPeriod + 1, results.Where(x => x.Cci != null).Count()); // sample value CciResult result = results.Where(x => x.Date == DateTime.Parse("12/31/2018")).FirstOrDefault(); Assert.AreEqual((decimal) - 52.9946, Math.Round((decimal)result.Cci, 4)); }
public void GetCci() { int lookbackPeriod = 20; List <CciResult> results = Indicator.GetCci(history, lookbackPeriod).ToList(); // assertions // proper quantities // should always be the same number of results as there is history Assert.AreEqual(502, results.Count); Assert.AreEqual(502 - lookbackPeriod + 1, results.Where(x => x.Cci != null).Count()); // sample value CciResult r = results[501]; Assert.AreEqual(-52.9946m, Math.Round((decimal)r.Cci, 4)); }