/// <summary> /// DEMA = 2 * EMA - EMA of EMA /// </summary> /// <see cref="http://forex-indicators.net/trend-indicators/dema"/> /// <returns></returns> public override SingleDoubleSerie Calculate() { SingleDoubleSerie demaSerie = new SingleDoubleSerie(); EMA ema = new EMA(Period, false); ema.Load(OhlcList); List <double?> emaValues = ema.Calculate().Values; // assign EMA values to Close price for (int i = 0; i < OhlcList.Count; i++) { OhlcList[i].Close = emaValues[i].HasValue ? emaValues[i].Value : 0.0; } ema.Load(OhlcList.Skip(Period - 1).ToList()); // EMA(EMA(value)) List <double?> emaEmaValues = ema.Calculate().Values; for (int i = 0; i < Period - 1; i++) { emaEmaValues.Insert(0, null); } // Calculate DEMA for (int i = 0; i < OhlcList.Count; i++) { if (i >= 2 * Period - 2) { var dema = 2 * emaValues[i] - emaEmaValues[i]; demaSerie.Values.Add(dema); } else { demaSerie.Values.Add(null); } } return(demaSerie); }
public override SingleDoubleSerie Calculate() { SingleDoubleSerie zlemaSerie = new SingleDoubleSerie(); double ratio = 2.0 / (double)(Period + 1); double lag = 1 / ratio; double wt = lag - ((int)lag / 1.0) * 1.0; //DMOD( lag, 1.0D0 ) double meanOfFirstPeriod = 0.0; switch (ColumnType) { case ColumnType.AdjClose: meanOfFirstPeriod = OhlcList.Take(Period).Select(x => x.AdjClose).Sum() / Period; break; case ColumnType.Close: meanOfFirstPeriod = OhlcList.Take(Period).Select(x => x.Close).Sum() / Period; break; case ColumnType.High: meanOfFirstPeriod = OhlcList.Take(Period).Select(x => x.High).Sum() / Period; break; case ColumnType.Low: meanOfFirstPeriod = OhlcList.Take(Period).Select(x => x.Low).Sum() / Period; break; case ColumnType.Open: meanOfFirstPeriod = OhlcList.Take(Period).Select(x => x.Open).Sum() / Period; break; case ColumnType.Volume: meanOfFirstPeriod = OhlcList.Take(Period).Select(x => x.Volume).Sum() / Period; break; default: break; } for (int i = 0; i < OhlcList.Count; i++) { if (i > Period - 1) { int loc = (int)(i - lag); double zlema = 0.0; switch (ColumnType) { case ColumnType.AdjClose: zlema = ratio * (2 * OhlcList[i].AdjClose - (OhlcList[loc].AdjClose * (1 - wt) + OhlcList[loc + 1].AdjClose * wt)) + (1 - ratio) * zlemaSerie.Values[i - 1].Value; break; case ColumnType.Close: zlema = ratio * (2 * OhlcList[i].Close - (OhlcList[loc].Close * (1 - wt) + OhlcList[loc + 1].Close * wt)) + (1 - ratio) * zlemaSerie.Values[i - 1].Value; break; case ColumnType.High: zlema = ratio * (2 * OhlcList[i].High - (OhlcList[loc].High * (1 - wt) + OhlcList[loc + 1].High * wt)) + (1 - ratio) * zlemaSerie.Values[i - 1].Value; break; case ColumnType.Low: zlema = ratio * (2 * OhlcList[i].Low - (OhlcList[loc].Low * (1 - wt) + OhlcList[loc + 1].Low * wt)) + (1 - ratio) * zlemaSerie.Values[i - 1].Value; break; case ColumnType.Open: zlema = ratio * (2 * OhlcList[i].Open - (OhlcList[loc].Open * (1 - wt) + OhlcList[loc + 1].Open * wt)) + (1 - ratio) * zlemaSerie.Values[i - 1].Value; break; case ColumnType.Volume: zlema = ratio * (2 * OhlcList[i].Volume - (OhlcList[loc].Volume * (1 - wt) + OhlcList[loc + 1].Volume * wt)) + (1 - ratio) * zlemaSerie.Values[i - 1].Value; break; default: break; } zlemaSerie.Values.Add(zlema); } else if (i == Period - 1) { zlemaSerie.Values.Add(meanOfFirstPeriod); } else { zlemaSerie.Values.Add(null); } } return(zlemaSerie); }
/// <summary> /// DEMA = 2 * EMA - EMA of EMA /// </summary> /// <see cref="http://forex-indicators.net/trend-indicators/dema"/> /// <returns></returns> public override SingleDoubleSerie Calculate() { SingleDoubleSerie demaSerie = new SingleDoubleSerie(); EMA ema = new EMA(Period, false, ColumnType); ema.Load(OhlcList); List <double?> emaValues = ema.Calculate().Values; // assign EMA values to column for (int i = 0; i < OhlcList.Count; i++) { switch (ColumnType) { case ColumnType.AdjClose: OhlcList[i].AdjClose = emaValues[i] ?? 0.0; break; case ColumnType.Close: OhlcList[i].Close = emaValues[i] ?? 0.0; break; case ColumnType.High: OhlcList[i].High = emaValues[i] ?? 0.0; break; case ColumnType.Low: OhlcList[i].Low = emaValues[i] ?? 0.0; break; case ColumnType.Open: OhlcList[i].Open = emaValues[i] ?? 0.0; break; case ColumnType.Volume: OhlcList[i].Volume = emaValues[i] ?? 0.0; break; default: break; } } ema.Load(OhlcList.Skip(Period - 1).ToList()); // EMA(EMA(value)) List <double?> emaEmaValues = ema.Calculate().Values; for (int i = 0; i < Period - 1; i++) { emaEmaValues.Insert(0, null); } // Calculate DEMA for (int i = 0; i < OhlcList.Count; i++) { if (i >= 2 * Period - 2) { var dema = 2 * emaValues[i] - emaEmaValues[i]; demaSerie.Values.Add(dema); } else { demaSerie.Values.Add(null); } } return(demaSerie); }