/// <summary>
        /// Computes the average value
        /// </summary>
        /// <param name="input">The data for the calculation</param>
        /// <returns>The average value</returns>
        protected override decimal ComputeNextValue(TradeBar input)
        {
            var price = (double)(input.High + input.Low) / 2;

            _high.Add((double)input.High);
            _low.Add((double)input.Low);

            // our first data point just return identity
            if (!_high.IsReady)
            {
                _filt = price;
            }
            double n1;
            double n2;
            double n3;
            double hh;
            double ll;
            double dimen = 0;
            double alpha;

            n3 = (_high.Max() - _low.Min()) / _n;

            hh = _high.Take(_n / 2).Max();
            ll = _low.Take(_n / 2).Min();

            n1 = (hh - ll) / (_n / 2);

            if (_high.IsReady)
            {
                hh = _high.Skip(_n / 2).Take(_n / 2).Max();
                ll = _low.Skip(_n / 2).Take(_n / 2).Min();
            }

            n2 = (hh - ll) / (_n / 2);

            if (n1 > 0 && n2 > 0 && n3 > 0)
            {
                dimen = (Math.Log(n1 + n2) - Math.Log(n3)) / Math.Log(2);
            }
            ;

            alpha = Math.Exp(_w * (dimen - 1));
            if (alpha < .01)
            {
                alpha = .01;
            }
            if (alpha > 1)
            {
                alpha = 1;
            }

            _filt = alpha * price + (1 - alpha) * _filt;

            return((decimal)_filt);
        }
Exemple #2
0
        /// <summary>
        /// Computes the average value
        /// </summary>
        /// <param name="input">The data for the calculation</param>
        /// <returns>The average value</returns>
        protected override decimal ComputeNextValue(IBaseDataBar input)
        {
            var price = (input.High + input.Low) / 2;

            _high.Add((double)input.High);
            _low.Add((double)input.Low);

            // our first data point just return identity
            if (_high.Samples <= _high.Size)
            {
                return(price);
            }

            var hh = _high.Take(_n / 2).Max();
            var ll = _low.Take(_n / 2).Min();
            var n1 = (hh - ll) / (_n / 2);

            hh = _high.Skip(_n / 2).Take(_n / 2).Max();
            ll = _low.Skip(_n / 2).Take(_n / 2).Min();

            var n2 = (hh - ll) / (_n / 2);
            var n3 = (_high.Max() - _low.Min()) / _n;

            double dimen = 0;

            if (n1 + n2 > 0 && n3 > 0)
            {
                dimen = Math.Log((n1 + n2) / n3) / Math.Log(2);
            }

            var alpha = (decimal)Math.Exp(_w * (dimen - 1));

            if (alpha < .01m)
            {
                alpha = .01m;
            }
            if (alpha > 1)
            {
                alpha = 1;
            }

            return(alpha * price + (1 - alpha) * Current.Value);
        }