private void BindCore(int startIndex) { int period = this.Period; SizedQueue currentItems = new SizedQueue(period); int currentIndex = 0; foreach (var item in this.itemsSource) { object val = this.ValueBinding.GetValue(item); currentItems.EnqueueItem((double)val); if (currentIndex >= startIndex) { double currentAverage = CalculateCurrentValue(currentItems); CategoricalDataPoint point; if (this.Owner.Model.DataPointsInternal.Count > currentIndex) { point = this.Owner.Model.DataPointsInternal[currentIndex] as CategoricalDataPoint; point.Value = currentAverage; } else { point = this.GenerateDataPoint(item, -1) as CategoricalDataPoint; point.Value = currentAverage; this.Owner.Model.DataPointsInternal.Add(point); } } currentIndex++; } }
protected override void BindCore() { int period = this.Period; SizedQueue currentItems = new SizedQueue(period); int currentIndex = 0; object previousItem = null; double value; foreach (var item in this.itemsSource) { value = TrueRangeIndicatorDataSource.CalculateValue(this.HighBinding, this.LowBinding, this.CloseBinding, previousItem, item); currentItems.EnqueueItem(value); double currentValue = MovingAverageIndicatorDataSource.CalculateCurrentValue(currentItems); 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); } currentIndex++; previousItem = item; } }
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 virtual void GenerateDataPoints(SizedQueue longPeriodItems, SizedQueue shortPeriodItems, SizedQueue signalPeriodItems) { ChartSeriesModel model = this.Owner.Model; int currentIndex = 0; CategoricalDataPoint point; double signalEMA = 0d; double macd = 0d; foreach (var item in this.itemsSource) { macd = this.CalculateMacdValue(longPeriodItems, shortPeriodItems, currentIndex, item); signalPeriodItems.EnqueueItem(macd); signalEMA = CalculateSignal(this.signalPeriod, signalPeriodItems, currentIndex, signalEMA, macd); if (model.DataPointsInternal.Count > currentIndex) { point = model.DataPointsInternal[currentIndex] as CategoricalDataPoint; point.Value = macd - signalEMA; } else { point = this.GenerateDataPoint(item, -1) as CategoricalDataPoint; point.Value = macd - signalEMA; model.DataPointsInternal.Add(point); } currentIndex++; } }
protected override void BindCore() { int currentIndex = 0; StochasticSlowIndicator owner = this.Owner as StochasticSlowIndicator; ChartSeriesModel model = this.Owner.Model; ChartSeriesModel signalModel = owner.SignalModel; int currentMainPeriod = this.MainPeriod; int currentSlowingPeriod = this.SlowingPeriod; int currentSignalPeriod = this.SignalPeriod; SizedQueue highValues = new SizedQueue(currentMainPeriod); SizedQueue lowValues = new SizedQueue(currentMainPeriod); SizedQueue fastStochValues = new SizedQueue(currentSlowingPeriod); SizedQueue slowStochValues = new SizedQueue(currentSignalPeriod); foreach (var item in this.itemsSource) { double high = (double)this.HighBinding.GetValue(item); double low = (double)this.LowBinding.GetValue(item); double close = (double)this.CloseBinding.GetValue(item); double fastStochValue = CalculateMainValue(highValues, lowValues, high, low, close); fastStochValues.EnqueueItem(fastStochValue); double slowStochValue = MovingAverageIndicatorDataSource.CalculateCurrentValue(fastStochValues); slowStochValues.EnqueueItem(slowStochValue); double slowSignalValue = MovingAverageIndicatorDataSource.CalculateCurrentValue(slowStochValues); CategoricalDataPoint point, point2; if (model.DataPointsInternal.Count > currentIndex) { point = model.DataPointsInternal[currentIndex] as CategoricalDataPoint; point.Value = slowStochValue; } else { point = this.GenerateDataPoint(item, -1) as CategoricalDataPoint; point.Value = slowStochValue; model.DataPointsInternal.Add(point); } if (signalModel.DataPointsInternal.Count > currentIndex) { point2 = signalModel.DataPointsInternal[currentIndex] as CategoricalDataPoint; point2.Value = slowSignalValue; } else { point2 = this.GenerateDataPoint(item, -1) as CategoricalDataPoint; point2.Value = slowSignalValue; signalModel.DataPointsInternal.Add(point2); } currentIndex++; } }
private static double CalculateMainValue(SizedQueue highValues, SizedQueue lowValues, double high, double low, double close) { highValues.EnqueueItem(high); lowValues.EnqueueItem(low); double max = highValues.Max(); double min = lowValues.Min(); double mainValue = (close - min) / (max - min) * 100; return(mainValue); }
protected override void BindCore() { SizedQueue typicalPrices = new SizedQueue(this.Period); int currentIndex = 0; foreach (var item in this.itemsSource) { double high = (double)this.HighBinding.GetValue(item); double low = (double)this.LowBinding.GetValue(item); double close = (double)this.CloseBinding.GetValue(item); double typicalPrice = (high + low + close) / 3d; typicalPrices.EnqueueItem(typicalPrice); double currentValue = 0d; if (currentIndex > 0) { double typicalPriceMA = MovingAverageIndicatorDataSource.CalculateCurrentValue(typicalPrices); double meanDeviation = 0; foreach (var price in typicalPrices) { meanDeviation += Math.Abs(typicalPriceMA - price); } meanDeviation /= typicalPrices.Count; currentValue = (typicalPrice - typicalPriceMA) / 0.015 / meanDeviation; } 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); } currentIndex++; } }
protected override void BindCore() { int period = this.Period; SizedQueue currentItems = new SizedQueue(period); double currentAverage = 0; double prevEMA = 0; int currentIndex = 0; double multiplier = this.IsModified ? 1d / period : 2d / (1 + period); foreach (var item in this.itemsSource) { double value = (double)this.ValueBinding.GetValue(item); //// The first values are calculated as SMA if (currentIndex < period) { currentItems.EnqueueItem(value); currentAverage = MovingAverageIndicatorDataSource.CalculateCurrentValue(currentItems); } else { currentAverage = CalculateCurrentValue(multiplier, value, prevEMA); } prevEMA = currentAverage; CategoricalDataPoint point; if (this.Owner.Model.DataPointsInternal.Count > currentIndex) { point = this.Owner.Model.DataPointsInternal[currentIndex] as CategoricalDataPoint; point.Value = currentAverage; } else { point = this.GenerateDataPoint(item, -1) as CategoricalDataPoint; point.Value = currentAverage; this.Owner.Model.DataPointsInternal.Add(point); } currentIndex++; } }
protected override void BindCore() { BollingerBandsIndicator indicator = this.Owner as BollingerBandsIndicator; int period = this.Period; double deviations = this.StandardDeviations; SizedQueue currentItems = new SizedQueue(period); int currentIndex = 0; double stdDeviation = 0; foreach (var item in this.itemsSource) { object val = this.ValueBinding.GetValue(item); currentItems.EnqueueItem((double)val); double currentAverage = MovingAverageIndicatorDataSource.CalculateCurrentValue(currentItems); // TODO: Nested loop, possible performance degrade for large data stdDeviation = CalculateStandardDeviation(currentItems, currentAverage); CategoricalDataPoint point, secondPoint; if (indicator.Model.DataPointsInternal.Count > currentIndex) { point = this.Owner.Model.DataPointsInternal[currentIndex] as CategoricalDataPoint; point.Value = currentAverage + (deviations * stdDeviation); secondPoint = indicator.lowerBandModel.DataPointsInternal[currentIndex] as CategoricalDataPoint; secondPoint.Value = currentAverage - (deviations * stdDeviation); } else { point = this.GenerateDataPoint(item, -1) as CategoricalDataPoint; point.Value = currentAverage + (deviations * stdDeviation); indicator.model.DataPointsInternal.Add(point); secondPoint = this.GenerateDataPoint(item, -1) as CategoricalDataPoint; secondPoint.Value = currentAverage - (deviations * stdDeviation); indicator.lowerBandModel.DataPointsInternal.Add(secondPoint); } currentIndex++; } }
protected override void BindCore() { int period = this.Period; double currentAverage = 0; double prevKAMA = 0; int currentIndex = 0; SizedQueue currentItems = new SizedQueue(period + 1); foreach (var item in this.itemsSource) { double value = (double)this.ValueBinding.GetValue(item); currentItems.EnqueueItem(value); //// The raw value is used for the first elements if (currentIndex < period) { currentAverage = value; prevKAMA = value; } else { currentAverage = CalculateCurrentValue(currentItems, this.slowPeriod, this.fastPeriod, prevKAMA); prevKAMA = currentAverage; } CategoricalDataPoint point; if (this.Owner.Model.DataPointsInternal.Count > currentIndex) { point = this.Owner.Model.DataPointsInternal[currentIndex] as CategoricalDataPoint; point.Value = currentAverage; } else { point = this.GenerateDataPoint(item, -1) as CategoricalDataPoint; point.Value = currentAverage; this.Owner.Model.DataPointsInternal.Add(point); } currentIndex++; } }
private void BindCore(int startIndex) { int shortPeriod = this.ShortPeriod; int longPeriod = this.LongPeriod; SizedQueue currentItemsShort = new SizedQueue(shortPeriod); SizedQueue currentItemsLong = new SizedQueue(longPeriod); int currentIndex = 0; foreach (var item in this.itemsSource) { double value = (double)this.ValueBinding.GetValue(item); currentItemsShort.EnqueueItem(value); currentItemsLong.EnqueueItem(value); if (currentIndex >= startIndex) { double shortAverage = MovingAverageIndicatorDataSource.CalculateCurrentValue(currentItemsShort); double longAverage = MovingAverageIndicatorDataSource.CalculateCurrentValue(currentItemsLong); double currentValue = (shortAverage - longAverage) / longAverage * 100; 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); } } currentIndex++; } }
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; int period2 = this.Period2; int period3 = this.Period3; SizedQueue items = new SizedQueue(period); SizedQueue items2 = new SizedQueue(period2); SizedQueue items3 = new SizedQueue(period3); SizedQueue trueRangeItems = new SizedQueue(period); SizedQueue trueRangeItems2 = new SizedQueue(period2); SizedQueue trueRangeItems3 = new SizedQueue(period3); int currentIndex = 0; double trueHigh, trueLow, trueRange; double high, low, close, range; double average1, average2, average3; double trueRangeAverage1, trueRangeAverage2, trueRangeAverage3; double previousClose = 0; foreach (var item in this.itemsSource) { high = (double)this.HighBinding.GetValue(item); low = (double)this.LowBinding.GetValue(item); close = (double)this.CloseBinding.GetValue(item); if (currentIndex == 0) { previousClose = close; } trueHigh = Math.Max(high, previousClose); trueLow = Math.Min(low, previousClose); trueRange = trueHigh - trueLow; range = close - trueLow; items.EnqueueItem(range); items2.EnqueueItem(range); items3.EnqueueItem(range); trueRangeItems.EnqueueItem(trueRange); trueRangeItems2.EnqueueItem(trueRange); trueRangeItems3.EnqueueItem(trueRange); average1 = MovingAverageIndicatorDataSource.CalculateCurrentValue(items); average2 = MovingAverageIndicatorDataSource.CalculateCurrentValue(items2); average3 = MovingAverageIndicatorDataSource.CalculateCurrentValue(items3); trueRangeAverage1 = MovingAverageIndicatorDataSource.CalculateCurrentValue(trueRangeItems); trueRangeAverage2 = MovingAverageIndicatorDataSource.CalculateCurrentValue(trueRangeItems2); trueRangeAverage3 = MovingAverageIndicatorDataSource.CalculateCurrentValue(trueRangeItems3); double value = 100 * ((4 * average1 / trueRangeAverage1) + (2 * average2 / trueRangeAverage2) + (average3 / trueRangeAverage3)) / 7d; CategoricalDataPoint point; if (this.Owner.Model.DataPointsInternal.Count > currentIndex) { point = this.Owner.Model.DataPointsInternal[currentIndex] as CategoricalDataPoint; point.Value = value; } else { point = this.GenerateDataPoint(item, -1) as CategoricalDataPoint; point.Value = value; this.Owner.Model.DataPointsInternal.Add(point); } previousClose = close; 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++; } }
protected override void BindCore() { int currentIndex = 0; int period = this.Period; int momentumShift = this.MomentumPeriod; SizedQueue values = new SizedQueue(momentumShift); SizedQueue upMomentumValues = new SizedQueue(period); SizedQueue downMomentumValues = new SizedQueue(period); double oldValue, value, currentIndicatorValue; double upMomentumAverage, downMomentumAverage; double up, down; foreach (var item in this.itemsSource) { value = (double)this.ValueBinding.GetValue(item); if (currentIndex == 0) { oldValue = value; } else if (currentIndex < momentumShift) { oldValue = values.Peek(); } else { oldValue = values.DequeueItem(); } if (oldValue > value) { up = 0; down = oldValue - value; } else { up = value - oldValue; down = 0; } upMomentumValues.EnqueueItem(up); downMomentumValues.EnqueueItem(down); upMomentumAverage = MovingAverageIndicatorDataSource.CalculateCurrentValue(upMomentumValues); downMomentumAverage = MovingAverageIndicatorDataSource.CalculateCurrentValue(downMomentumValues); if (Math.Round(upMomentumAverage + downMomentumAverage, 6) == 0) { currentIndicatorValue = 100; } else { currentIndicatorValue = 100 * upMomentumAverage / (upMomentumAverage + downMomentumAverage); } values.EnqueueItem(value); CategoricalDataPoint point; if (this.Owner.Model.DataPointsInternal.Count > currentIndex) { point = this.Owner.Model.DataPointsInternal[currentIndex] as CategoricalDataPoint; point.Value = currentIndicatorValue; } else { point = this.GenerateDataPoint(item, -1) as CategoricalDataPoint; point.Value = currentIndicatorValue; this.Owner.Model.DataPointsInternal.Add(point); } currentIndex++; } }