Exemplo n.º 1
0
        // WILLIAMS FRACTAL
        public static IEnumerable <FractalResult> GetFractal <TQuote>(
            IEnumerable <TQuote> history)
            where TQuote : IQuote
        {
            // sort history
            List <TQuote> historyList = history.Sort();

            // check parameter arguments
            ValidateFractal(history);

            // initialize
            List <FractalResult> results = new List <FractalResult>(historyList.Count);

            // roll through history
            for (int i = 0; i < historyList.Count; i++)
            {
                TQuote h     = historyList[i];
                int    index = i + 1;

                FractalResult r = new FractalResult()
                {
                    Date = h.Date
                };

                if (index > 2 && index <= historyList.Count - 2)
                {
                    // bearish signal
                    if (h.High > historyList[i - 2].High &&
                        h.High > historyList[i - 1].High &&
                        h.High > historyList[i + 1].High &&
                        h.High > historyList[i + 2].High)
                    {
                        r.FractalBear = h.High;
                    }

                    // bullish signal
                    if (h.Low < historyList[i - 2].Low &&
                        h.Low < historyList[i - 1].Low &&
                        h.Low < historyList[i + 1].Low &&
                        h.Low < historyList[i + 2].Low)
                    {
                        r.FractalBull = h.Low;
                    }
                }

                results.Add(r);
            }

            return(results);
        }
Exemplo n.º 2
0
        // FRACTAL CHAOS BANDS
        /// <include file='./info.xml' path='indicator/*' />
        ///
        public static IEnumerable <FcbResult> GetFcb <TQuote>(
            IEnumerable <TQuote> history,
            int windowSpan = 2)
            where TQuote : IQuote
        {
            // check parameter arguments
            ValidateFcb(history, windowSpan);

            // initialize
            List <FractalResult> fractals = GetFractal(history, windowSpan).ToList();
            int size = fractals.Count;
            List <FcbResult> results = new(size);
            decimal?         upperLine = null, lowerLine = null;

            // roll through history
            for (int i = 0; i < size; i++)
            {
                int           index = i + 1;
                FractalResult f     = fractals[i];

                FcbResult r = new()
                {
                    Date = f.Date
                };

                if (index >= 2 * windowSpan + 1)
                {
                    FractalResult fp = fractals[i - windowSpan];

                    upperLine = (fp.FractalBear != null) ? (decimal)fp.FractalBear : upperLine;
                    lowerLine = (fp.FractalBull != null) ? (decimal)fp.FractalBull : lowerLine;

                    r.UpperBand = upperLine;
                    r.LowerBand = lowerLine;
                }

                results.Add(r);
            }

            return(results);
        }
Exemplo n.º 3
0
        // WILLIAMS FRACTAL
        /// <include file='./info.xml' path='indicator/*' />
        ///
        public static IEnumerable <FractalResult> GetFractal <TQuote>(
            IEnumerable <TQuote> history,
            int windowSpan = 2)
            where TQuote : IQuote
        {
            // sort history
            List <TQuote> historyList = history.Sort();

            // check parameter arguments
            ValidateFractal(history, windowSpan);

            // initialize
            List <FractalResult> results = new List <FractalResult>(historyList.Count);

            // roll through history
            for (int i = 0; i < historyList.Count; i++)
            {
                TQuote h     = historyList[i];
                int    index = i + 1;

                FractalResult r = new FractalResult()
                {
                    Date = h.Date
                };

                if (index > windowSpan && index <= historyList.Count - windowSpan)
                {
                    bool isHigh = true;
                    bool isLow  = true;

                    for (int p = i - windowSpan; p <= i + windowSpan; p++)
                    {
                        // skip current period
                        if (p == i)
                        {
                            continue;
                        }

                        // evaluate "wings"
                        TQuote d = historyList[p];

                        if (h.High <= d.High)
                        {
                            isHigh = false;
                        }

                        if (h.Low >= d.Low)
                        {
                            isLow = false;
                        }
                    }

                    // bearish signal
                    if (isHigh)
                    {
                        r.FractalBear = h.High;
                    }

                    // bullish signal
                    if (isLow)
                    {
                        r.FractalBull = h.Low;
                    }
                }

                results.Add(r);
            }

            return(results);
        }