// COMMODITY CHANNEL INDEX public static IEnumerable <CciResult> GetCci <TQuote>( IEnumerable <TQuote> history, int lookbackPeriod = 20) where TQuote : IQuote { // clean quotes List <TQuote> historyList = history.Sort(); // validate parameters ValidateCci(history, lookbackPeriod); // initialize List <CciResult> results = new List <CciResult>(); // roll through history for (int i = 0; i < historyList.Count; i++) { TQuote h = historyList[i]; int index = i + 1; CciResult result = new CciResult { Date = h.Date, Tp = (h.High + h.Low + h.Close) / 3 }; results.Add(result); if (index >= lookbackPeriod) { // average TP over lookback decimal avgTp = 0; for (int p = index - lookbackPeriod; p < index; p++) { CciResult d = results[p]; avgTp += (decimal)d.Tp; } avgTp /= lookbackPeriod; // average Deviation over lookback decimal avgDv = 0; for (int p = index - lookbackPeriod; p < index; p++) { CciResult d = results[p]; avgDv += Math.Abs(avgTp - (decimal)d.Tp); } avgDv /= lookbackPeriod; result.Cci = (avgDv == 0) ? null : (result.Tp - avgTp) / ((decimal)0.015 * avgDv); } } return(results); }
// COMMODITY CHANNEL INDEX public static IEnumerable <CciResult> GetCci(IEnumerable <Quote> history, int lookbackPeriod = 20) { // clean quotes List <Quote> historyList = Cleaners.PrepareHistory(history).ToList(); // validate parameters ValidateCci(history, lookbackPeriod); // initialize List <CciResult> results = new List <CciResult>(); // roll through history for (int i = 0; i < historyList.Count; i++) { Quote h = historyList[i]; CciResult result = new CciResult { Index = (int)h.Index, Date = h.Date, Tp = (h.High + h.Low + h.Close) / 3 }; results.Add(result); if (h.Index >= lookbackPeriod) { // average TP over lookback decimal avgTp = 0; for (int p = (int)h.Index - lookbackPeriod; p < h.Index; p++) { CciResult d = results[p]; avgTp += (decimal)d.Tp; } avgTp /= lookbackPeriod; // average Deviation over lookback decimal avgDv = 0; for (int p = (int)h.Index - lookbackPeriod; p < h.Index; p++) { CciResult d = results[p]; avgDv += Math.Abs(avgTp - (decimal)d.Tp); } avgDv /= lookbackPeriod; result.Cci = (result.Tp - avgTp) / ((decimal)0.015 * avgDv); } } return(results); }
// COMMODITY CHANNEL INDEX public static IEnumerable <CciResult> GetCci(IEnumerable <Quote> history, int lookbackPeriod = 20) { // clean quotes history = Cleaners.PrepareHistory(history); // check exceptions int qtyHistory = history.Count(); int minHistory = lookbackPeriod + 1; if (qtyHistory < minHistory) { throw new BadHistoryException("Insufficient history provided for Commodity Channel Index. " + string.Format("You provided {0} periods of history when {1} is required.", qtyHistory, minHistory)); } // initialize List <CciResult> results = new List <CciResult>(); // roll through history to get Typical Price foreach (Quote h in history) { CciResult result = new CciResult { Index = (int)h.Index, Date = h.Date, Tp = (h.High + h.Low + h.Close) / 3 }; results.Add(result); } // roll through interim results to calculate CCI foreach (CciResult result in results.Where(x => x.Index >= lookbackPeriod)) { IEnumerable <CciResult> period = results.Where(x => x.Index <= result.Index && x.Index > (result.Index - lookbackPeriod)); decimal smaTp = (decimal)period.Select(x => x.Tp).Average(); decimal meanDv = 0; foreach (CciResult p in period) { meanDv += Math.Abs(smaTp - (decimal)p.Tp); } meanDv /= lookbackPeriod; result.Cci = (result.Tp - smaTp) / ((decimal)0.015 * meanDv); } return(results); }
// COMMODITY CHANNEL INDEX public static IEnumerable <CciResult> GetCci(IEnumerable <Quote> history, int lookbackPeriod = 20) { // clean quotes Cleaners.PrepareHistory(history); // validate parameters ValidateCci(history, lookbackPeriod); // initialize List <CciResult> results = new List <CciResult>(); // roll through history to get Typical Price foreach (Quote h in history) { CciResult result = new CciResult { Index = (int)h.Index, Date = h.Date, Tp = (h.High + h.Low + h.Close) / 3 }; results.Add(result); } // roll through interim results to calculate CCI foreach (CciResult result in results.Where(x => x.Index >= lookbackPeriod)) { List <CciResult> period = results .Where(x => x.Index > (result.Index - lookbackPeriod) && x.Index <= result.Index) .ToList(); decimal smaTp = (decimal)period.Select(x => x.Tp).Average(); decimal meanDv = 0; foreach (CciResult p in period) { meanDv += Math.Abs(smaTp - (decimal)p.Tp); } meanDv /= lookbackPeriod; result.Cci = (result.Tp - smaTp) / ((decimal)0.015 * meanDv); } return(results); }