// WEIGHTED MOVING AVERAGE public static IEnumerable <WmaResult> GetWma <TQuote>( IEnumerable <TQuote> history, int lookbackPeriod) where TQuote : IQuote { // clean quotes List <TQuote> historyList = history.Sort(); // check parameters ValidateWma(history, lookbackPeriod); // initialize List <WmaResult> results = new List <WmaResult>(); decimal divisor = (lookbackPeriod * (lookbackPeriod + 1)) / 2m; // roll through history for (int i = 0; i < historyList.Count; i++) { TQuote h = historyList[i]; int index = i + 1; WmaResult result = new WmaResult { Date = h.Date }; if (index >= lookbackPeriod) { decimal wma = 0; for (int p = index - lookbackPeriod; p < index; p++) { TQuote d = historyList[p]; wma += d.Close * (lookbackPeriod - (decimal)(index - p - 1)) / divisor; } result.Wma = wma; } results.Add(result); } return(results); }
// WEIGHTED MOVING AVERAGE public static IEnumerable <WmaResult> GetWma(IEnumerable <Quote> history, int lookbackPeriod) { // clean quotes List <Quote> historyList = Cleaners.PrepareHistory(history).ToList(); // check parameters ValidateWma(history, lookbackPeriod); // initialize List <WmaResult> results = new List <WmaResult>(); decimal divisor = (lookbackPeriod * (lookbackPeriod + 1)) / 2m; // roll through history for (int i = 0; i < historyList.Count; i++) { Quote h = historyList[i]; WmaResult result = new WmaResult { Index = (int)h.Index, Date = h.Date }; if (h.Index >= lookbackPeriod) { decimal wma = 0; for (int p = (int)h.Index - lookbackPeriod; p < h.Index; p++) { Quote d = historyList[p]; wma += d.Close * (lookbackPeriod - (decimal)(h.Index - d.Index)) / divisor; } result.Wma = wma; } results.Add(result); } return(results); }
// WEIGHTED MOVING AVERAGE public static IEnumerable <WmaResult> GetWma(IEnumerable <Quote> history, int lookbackPeriod) { // clean quotes Cleaners.PrepareHistory(history); // check parameters ValidateWma(history, lookbackPeriod); // initialize List <Quote> historyList = history.ToList(); List <WmaResult> results = new List <WmaResult>(); decimal divisor = (lookbackPeriod * (lookbackPeriod + 1)) / 2m; // roll through history for (int i = 0; i < historyList.Count; i++) { Quote h = historyList[i]; WmaResult result = new WmaResult { Index = (int)h.Index, Date = h.Date }; if (h.Index >= lookbackPeriod) { List <Quote> period = historyList .Where(x => x.Index > (h.Index - lookbackPeriod) && x.Index <= h.Index) .ToList(); result.Wma = period .Select(x => x.Close * (lookbackPeriod - (h.Index - x.Index)) / divisor) .Sum(); } results.Add(result); } return(results); }
// HULL MOVING AVERAGE /// <include file='./info.xml' path='indicator/*' /> /// public static IEnumerable <HmaResult> GetHma <TQuote>( IEnumerable <TQuote> history, int lookbackPeriod) where TQuote : IQuote { // sort history List <TQuote> historyList = history.Sort(); // check parameter arguments ValidateHma(history, lookbackPeriod); // initialize List <Quote> synthHistory = new List <Quote>(); List <WmaResult> wmaN1 = GetWma(history, lookbackPeriod).ToList(); List <WmaResult> wmaN2 = GetWma(history, lookbackPeriod / 2).ToList(); // roll through history, to get interim synthetic history for (int i = 0; i < historyList.Count; i++) { TQuote h = historyList[i]; Quote sh = new Quote { Date = h.Date }; WmaResult w1 = wmaN1[i]; WmaResult w2 = wmaN2[i]; if (w1.Wma != null && w2.Wma != null) { sh.Close = (decimal)(w2.Wma * 2m - w1.Wma); synthHistory.Add(sh); } } // add back truncated null results int sqN = (int)Math.Sqrt(lookbackPeriod); int shiftQty = lookbackPeriod - 1; List <HmaResult> results = historyList .Take(shiftQty) .Select(x => new HmaResult { Date = x.Date }) .ToList(); // calculate final HMA = WMA with period SQRT(n) List <HmaResult> hmaResults = GetWma(synthHistory, sqN) .Select(x => new HmaResult { Date = x.Date, Hma = x.Wma }) .ToList(); // add WMA to results results.AddRange(hmaResults); results = results.OrderBy(x => x.Date).ToList(); return(results); }
// HULL MOVING AVERAGE public static IEnumerable <HmaResult> GetHma(IEnumerable <Quote> history, int lookbackPeriod) { // clean quotes List <Quote> historyList = Cleaners.PrepareHistory(history).ToList(); // check parameters ValidateHma(history, lookbackPeriod); // initialize List <Quote> synthHistory = new List <Quote>(); List <WmaResult> wmaN1 = GetWma(history, lookbackPeriod).ToList(); List <WmaResult> wmaN2 = GetWma(history, lookbackPeriod / 2).ToList(); // create interim synthetic history // roll through history for (int i = 0; i < historyList.Count; i++) { Quote h = historyList[i]; Quote sh = new Quote { Date = h.Date }; WmaResult w1 = wmaN1[i]; WmaResult w2 = wmaN2[i]; if (w1.Wma != null && w2.Wma != null) { sh.Close = (decimal)(w2.Wma * 2m - w1.Wma); synthHistory.Add(sh); } } // initialize results, add back truncated null results int sqN = (int)Math.Sqrt(lookbackPeriod); int shiftQty = lookbackPeriod - 1; List <HmaResult> results = historyList .Select(x => new HmaResult { Index = (int)x.Index, Date = x.Date }) .Where(x => x.Index <= shiftQty) .ToList(); // calculate final HMA = WMA with period SQRT(n) List <HmaResult> hmaResults = GetWma(synthHistory, sqN) .Select(x => new HmaResult { Index = x.Index + shiftQty, Date = x.Date, Hma = x.Wma }) .ToList(); // add WMA to results results.AddRange(hmaResults); results = results.OrderBy(x => x.Index).ToList(); return(results); }