protected double CalculateMacdValue(SizedQueue longPeriodItems, SizedQueue shortPeriodItems, int currentIndex, object item) { double value = (double)this.ValueBinding.GetValue(item); longPeriodItems.EnqueueItem(value); shortPeriodItems.EnqueueItem(value); if (currentIndex < this.LongPeriod) { this.currentLongEMA = MovingAverageIndicatorDataSource.CalculateCurrentValue(longPeriodItems); } else { this.currentLongEMA = ExponentialMovingAverageIndicatorDataSource.CalculateCurrentValue(false, this.LongPeriod, value, this.currentLongEMA); } if (currentIndex < this.ShortPeriod) { this.currentShortEMA = MovingAverageIndicatorDataSource.CalculateCurrentValue(shortPeriodItems); } else { this.currentShortEMA = ExponentialMovingAverageIndicatorDataSource.CalculateCurrentValue(false, this.ShortPeriod, value, this.currentShortEMA); } return(this.currentShortEMA - this.currentLongEMA); }
protected static double CalculateSignal(int signalPeriod, SizedQueue signalPeriodItems, int currentIndex, double signalEMA, double macd) { if (currentIndex < signalPeriod) { signalEMA = MovingAverageIndicatorDataSource.CalculateCurrentValue(signalPeriodItems); } else { signalEMA = ExponentialMovingAverageIndicatorDataSource.CalculateCurrentValue(false, signalPeriod, macd, signalEMA); } return(signalEMA); }
protected override void BindCore() { int period = this.Period; SizedQueue currentItems = new SizedQueue(period); SizedQueue emaOneItems = new SizedQueue(period); SizedQueue emaTwoItems = new SizedQueue(period); int currentIndex = 0; double emaOne, emaTwo, emaThree; double lastEmaOne = 0; double lastEmaTwo = 0; double lastEmaThree = 0; double currentValue; foreach (var item in this.itemsSource) { double value = (double)this.ValueBinding.GetValue(item); currentItems.EnqueueItem(value); if (currentIndex < period) { emaOne = MovingAverageIndicatorDataSource.CalculateCurrentValue(currentItems); emaOneItems.EnqueueItem(emaOne); emaTwo = MovingAverageIndicatorDataSource.CalculateCurrentValue(emaOneItems); emaTwoItems.EnqueueItem(emaTwo); emaThree = MovingAverageIndicatorDataSource.CalculateCurrentValue(emaTwoItems); } else { emaOne = ExponentialMovingAverageIndicatorDataSource.CalculateCurrentValue(false, period, value, lastEmaOne); emaOneItems.EnqueueItem(emaOne); emaTwo = ExponentialMovingAverageIndicatorDataSource.CalculateCurrentValue(false, period, emaOne, lastEmaTwo); emaTwoItems.EnqueueItem(emaTwo); emaThree = ExponentialMovingAverageIndicatorDataSource.CalculateCurrentValue(false, period, emaTwo, lastEmaThree); } if (currentIndex == 0) { currentValue = 0; } else { currentValue = 100 * (emaThree - lastEmaThree) / emaThree; } CategoricalDataPoint point; if (this.Owner.Model.DataPointsInternal.Count > currentIndex) { point = this.Owner.Model.DataPointsInternal[currentIndex] as CategoricalDataPoint; point.Value = currentValue; } else { point = this.GenerateDataPoint(item, -1) as CategoricalDataPoint; point.Value = currentValue; this.Owner.Model.DataPointsInternal.Add(point); } lastEmaOne = emaOne; lastEmaTwo = emaTwo; lastEmaThree = emaThree; currentIndex++; } }
protected override void BindCore() { int period = this.Period; SizedQueue losses = new SizedQueue(period); SizedQueue gains = new SizedQueue(period); double prevValue = 0; double currentValue = 0; double lossesAverage = 0; double gainsAverage = 0; int currentIndex = 0; foreach (var item in this.itemsSource) { double value = (double)this.ValueBinding.GetValue(item); double difference = 0d; if (currentIndex > 0) { difference = Math.Abs(value - prevValue); } double gain = 0; double loss = 0; if (value > prevValue) { gain = difference; loss = 0d; } else { gain = 0d; loss = difference; } gains.EnqueueItem(gain); losses.EnqueueItem(loss); if (currentIndex < period) { lossesAverage = MovingAverageIndicatorDataSource.CalculateCurrentValue(losses); gainsAverage = MovingAverageIndicatorDataSource.CalculateCurrentValue(gains); } else { gainsAverage = ExponentialMovingAverageIndicatorDataSource.CalculateCurrentValue(false, period, gain, gainsAverage); lossesAverage = ExponentialMovingAverageIndicatorDataSource.CalculateCurrentValue(false, period, loss, lossesAverage); currentValue = 100 - (100 / (1 + (gainsAverage / lossesAverage))); } CategoricalDataPoint point; if (this.Owner.Model.DataPointsInternal.Count > currentIndex) { point = this.Owner.Model.DataPointsInternal[currentIndex] as CategoricalDataPoint; point.Value = currentValue; } else { point = this.GenerateDataPoint(item, -1) as CategoricalDataPoint; point.Value = currentValue; this.Owner.Model.DataPointsInternal.Add(point); } prevValue = value; currentIndex++; } }