/// <summary> /// Gets the value. /// </summary> /// <returns></returns> protected override double AveragePeriods() { if (Periods.Count < Size) { return(0.0); } var period = Periods.Last(); if (PreviousEMA == 0.0) { PreviousEMA = base.AveragePeriods(); } PreviousEMA = IndicatorUtils.EMA(period.Close, PreviousEMA, Multiplier); return(PreviousEMA); }
/// <summary> /// Gets the value. /// </summary> /// <returns></returns> public override IEnumerable <double> Update(Period period, bool normalize = true) { var adx = 0.0; var plusDI = 0.0; var minusDI = 0.0; if (PreviousPeriod != null) { var upMove = period.High - PreviousPeriod.High; var downMove = period.Low - PreviousPeriod.Low; var plusDM = upMove > downMove && upMove > 0.0 ? upMove : 0.0; var minusDM = downMove > upMove && downMove > 0.0 ? downMove : 0.0; var atr = Atr.Update(period).First(); var plusDMOverATR = atr != 0.0 ? plusDM / atr : 0.0; var minusDMOverATR = atr != 0.0 ? minusDM / atr : 0.0; PreviousEMAPlusDI = IndicatorUtils.EMA(plusDMOverATR, PreviousEMAPlusDI, Multiplier); PreviousEMAMinusDI = IndicatorUtils.EMA(minusDMOverATR, PreviousEMAMinusDI, Multiplier); plusDI = 100 * PreviousEMAPlusDI; minusDI = 100 * PreviousEMAMinusDI; var val = (plusDI + minusDI) != 0.0 ? Math.Abs((plusDI - minusDI) / (plusDI + minusDI)) : 0.0; PreviousEmaADX = IndicatorUtils.EMA(val, PreviousEmaADX, Multiplier); adx = 100 * PreviousEmaADX; } PreviousPeriod = period; Value = new double[3] { adx, plusDI, minusDI }; return(normalize ? Value.Select(v => Normalizers.DMI.Normalize(v)) : Value); }