/// <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);
        }
        protected override decimal ComputeNextValue(IReadOnlyWindow <IndicatorDataPoint> window, IndicatorDataPoint input)
        {
            var stop = 0;
            var src  = input.Value;

            if (src == Src.FirstOrDefault())
            {
                return(xEma);
            }
            Src.Add(src);

            if (Src.IsReady)
            {
                //PFE = sqrt(pow(close - close[Length], 2) + 100)
                var s1    = Src.Skip(1).FirstOrDefault();
                var sL    = Src.LastOrDefault();
                var perfL = src - sL;
                PFE = Math.Sqrt((double)(perfL * perfL) + 100);

                //C2C = sum(sqrt(pow((close - close[1]), 2) + 1), Length)
                var perf1 = src - s1;
                var tosum = Math.Sqrt((double)(perf1 * perf1) + 1);
                C2C.Add(tosum);

                //xFracEff = iff(close - close[Length] > 0,  round((PFE / C2C) * 100) , round(-(PFE / C2C) * 100))
                double c2csum   = 0;
                double xFracEff = 0;
                if (C2C.IsReady)
                {
                    c2csum   = C2C.Sum();
                    xFracEff = (perfL > 0) ? Math.Round(PFE / c2csum * 100)  : Math.Round(-PFE / c2csum * 100);
                    xEma.Update(input.Time, (decimal)xFracEff);
                }
                if (xEma.IsReady)
                {
                    return(xEma);           //Math.Min(Math.Max(xEma, -100), 100);
                }
            }
            return(0);
        }