// CHAIKIN MONEY FLOW /// <include file='./info.xml' path='indicator/*' /> /// public static IEnumerable <CmfResult> GetCmf <TQuote>( IEnumerable <TQuote> history, int lookbackPeriod = 20) where TQuote : IQuote { // sort history List <TQuote> historyList = history.Sort(); // check parameter arguments ValidateCmf(history, lookbackPeriod); // initialize List <CmfResult> results = new List <CmfResult>(historyList.Count); List <AdlResult> adlResults = GetAdl(history).ToList(); // roll through history for (int i = 0; i < adlResults.Count; i++) { AdlResult r = adlResults[i]; int index = i + 1; CmfResult result = new CmfResult { Date = r.Date, MoneyFlowMultiplier = r.MoneyFlowMultiplier, MoneyFlowVolume = r.MoneyFlowVolume }; if (index >= lookbackPeriod) { decimal sumMfv = 0; decimal sumVol = 0; for (int p = index - lookbackPeriod; p < index; p++) { TQuote h = historyList[p]; sumVol += h.Volume; AdlResult d = adlResults[p]; sumMfv += d.MoneyFlowVolume; } decimal avgMfv = sumMfv / lookbackPeriod; decimal avgVol = sumVol / lookbackPeriod; if (avgVol != 0) { result.Cmf = avgMfv / avgVol; } } results.Add(result); } return(results); }
// CHAIKIN MONEY FLOW public static IEnumerable <CmfResult> GetCmf(IEnumerable <Quote> history, int lookbackPeriod = 20) { // clean quotes List <Quote> historyList = Cleaners.PrepareHistory(history).ToList(); // check parameters ValidateCmf(history, lookbackPeriod); // initialize List <CmfResult> results = new List <CmfResult>(); List <AdlResult> adlResults = GetAdl(history).ToList(); // roll through history for (int i = 0; i < adlResults.Count; i++) { AdlResult r = adlResults[i]; CmfResult result = new CmfResult { Index = r.Index, Date = r.Date, MoneyFlowMultiplier = r.MoneyFlowMultiplier, MoneyFlowVolume = r.MoneyFlowVolume }; if (r.Index >= lookbackPeriod) { decimal sumMfv = 0; decimal sumVol = 0; for (int p = r.Index - lookbackPeriod; p < r.Index; p++) { Quote h = historyList[p]; sumVol += h.Volume; AdlResult d = adlResults[p]; sumMfv += d.MoneyFlowVolume; } decimal avgMfv = sumMfv / lookbackPeriod; decimal avgVol = sumVol / lookbackPeriod; if (avgVol != 0) { result.Cmf = avgMfv / avgVol; } } results.Add(result); } return(results); }
// SIMPLE MOVING AVERAGE public static IEnumerable <CmfResult> GetCmf(IEnumerable <Quote> history, int lookbackPeriod = 20) { // clean quotes Cleaners.PrepareHistory(history); // check parameters ValidateCmf(history, lookbackPeriod); // initialize List <CmfResult> results = new List <CmfResult>(); IEnumerable <AdlResult> adlResults = GetAdl(history); // roll through history foreach (AdlResult r in adlResults) { CmfResult result = new CmfResult { Index = r.Index, Date = r.Date, MoneyFlowMultiplier = r.MoneyFlowMultiplier, MoneyFlowVolume = r.MoneyFlowVolume }; if (r.Index >= lookbackPeriod) { List <AdlResult> period = adlResults .Where(x => x.Index <= r.Index && x.Index > (r.Index - lookbackPeriod)) .ToList(); // simple moving average result.Cmf = period .Select(x => x.MoneyFlowVolume) .Average(); } results.Add(result); } return(results); }