/// <summary>
        /// Calculate single value
        /// </summary>
        /// <param name="collection"></param>
        /// <returns></returns>
        public override MovingAverageIndicator Calculate(IIndexCollection <IPointModel> collection)
        {
            var currentPoint = collection.ElementAtOrDefault(collection.Count - 1);

            if (currentPoint == null)
            {
                return(this);
            }

            var pointPrice = currentPoint.Bar.Close;

            switch (Mode)
            {
            case MovingAverageEnum.Bid: pointPrice = currentPoint.Bid; break;

            case MovingAverageEnum.Ask: pointPrice = currentPoint.Ask; break;
            }

            var nextIndicatorPoint = new PointModel
            {
                Last      = pointPrice,
                Time      = currentPoint.Time,
                TimeFrame = currentPoint.TimeFrame,
                Bar       = new PointBarModel
                {
                    Close = pointPrice
                }
            };

            var previousIndicatorPoint = Values.ElementAtOrDefault(collection.Count - 1);

            if (previousIndicatorPoint == null)
            {
                Values.Add(nextIndicatorPoint);
            }

            Values[collection.Count - 1] = nextIndicatorPoint;

            var average = CalculationManager.LinearWeightAverage(Values.Select(o => o.Bar.Close.Value), Values.Count - 1, Interval);

            currentPoint.Series[Name]           = currentPoint.Series.TryGetValue(Name, out IPointModel seriesItem) ? seriesItem : new MovingAverageIndicator();
            currentPoint.Series[Name].Bar.Close = currentPoint.Series[Name].Last = ConversionManager.Compare(average, 0) ? nextIndicatorPoint.Bar.Close : average;
            currentPoint.Series[Name].Time      = currentPoint.Time;
            currentPoint.Series[Name].ChartData = ChartData;

            Last = Bar.Close = currentPoint.Series[Name].Bar.Close;

            return(this);
        }
示例#2
0
        /// <summary>
        /// Calculate indicator value
        /// </summary>
        /// <param name="collection"></param>
        /// <returns></returns>
        public override ScaleIndicator Calculate(IIndexCollection <IPointModel> collection)
        {
            var currentPoint = collection.ElementAtOrDefault(collection.Count - 1);

            if (currentPoint == null || currentPoint.Series == null)
            {
                return(this);
            }

            var pointValue = currentPoint.Bar.Close ?? 0.0;

            _min = _min == null ? pointValue : Math.Min(_min.Value, pointValue);
            _max = _max == null ? pointValue : Math.Max(_max.Value, pointValue);

            var nextValue = ConversionManager.Compare(_min, _max) ? 0.0 : Min + (pointValue - _min.Value) * (Max - Min) / (_max.Value - _min.Value);

            var nextIndicatorPoint = new PointModel
            {
                Time      = currentPoint.Time,
                TimeFrame = currentPoint.TimeFrame,
                Last      = nextValue,
                Bar       = new PointBarModel
                {
                    Close = nextValue
                }
            };

            var previousIndicatorPoint = Values.ElementAtOrDefault(collection.Count - 1);

            if (previousIndicatorPoint == null)
            {
                Values.Add(nextIndicatorPoint);
            }

            Values[collection.Count - 1] = nextIndicatorPoint;

            currentPoint.Series[Name]           = currentPoint.Series.TryGetValue(Name, out IPointModel seriesItem) ? seriesItem : new ScaleIndicator();
            currentPoint.Series[Name].Bar.Close = currentPoint.Series[Name].Last = CalculationManager.LinearWeightAverage(Values.Select(o => o.Bar.Close.Value), Values.Count - 1, Interval);
            currentPoint.Series[Name].Time      = currentPoint.Time;
            currentPoint.Series[Name].ChartData = ChartData;

            Last = Bar.Close = currentPoint.Series[Name].Bar.Close;

            return(this);
        }