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;
            }
        }
Exemplo n.º 2
0
        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 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++;
            }
        }
Exemplo n.º 4
0
 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);
 }
        private static double CalculateCurrentValue(SizedQueue currentItems)
        {
            int    itemsCount           = currentItems.Count;
            double sum                  = 0d;
            double currentSimpleAverage = MovingAverageIndicatorDataSource.CalculateCurrentValue(currentItems);

            for (int i = 0; i < itemsCount; i++)
            {
                sum += (itemsCount - ((2 * i) + 1)) / 2d * currentItems[i];
            }
            return(currentSimpleAverage + ((6 * sum) / itemsCount / (itemsCount + 1)));
        }
Exemplo n.º 6
0
        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++;
            }
        }
Exemplo n.º 7
0
        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++;
            }
        }
Exemplo n.º 8
0
        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++;
            }
        }
        private void BindCore(int startIndex)
        {
            int    period         = this.Period;
            int    currentIndex   = 0;
            double currentAverage = 0d;

            SizedQueue currentItems = new SizedQueue(period);

            foreach (var item in this.itemsSource)
            {
                double value = (double)this.ValueBinding.GetValue(item);
                currentItems.EnqueueItem(value);

                if (currentIndex >= startIndex)
                {
                    if (currentIndex < period - 1)
                    {
                        currentAverage = MovingAverageIndicatorDataSource.CalculateCurrentValue(currentItems);
                    }
                    else
                    {
                        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++;
            }
        }
Exemplo n.º 10
0
        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++;
            }
        }
Exemplo n.º 11
0
        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++;
            }
        }
Exemplo n.º 12
0
        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++;
            }
        }
Exemplo n.º 13
0
        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++;
            }
        }