Esempio n. 1
0
        // STOCHASTIC RSI
        public static IEnumerable <StochRsiResult> GetStochRsi(IEnumerable <Quote> history, int lookbackPeriod = 14)
        {
            // clean quotes
            history = Cleaners.PrepareHistory(history);

            // check exceptions
            int qtyHistory = history.Count();
            int minHistory = 2 * lookbackPeriod;

            if (qtyHistory < minHistory)
            {
                throw new BadHistoryException("Insufficient history provided for Stochastic RSI.  " +
                                              string.Format("You provided {0} periods of history when {1} is required.", qtyHistory, minHistory));
            }

            // initialize
            List <StochRsiResult>   results    = new List <StochRsiResult>();
            IEnumerable <RsiResult> rsiResults = GetRsi(history, lookbackPeriod);


            // calculate
            foreach (Quote h in history)
            {
                StochRsiResult result = new StochRsiResult
                {
                    Index = (int)h.Index,
                    Date  = h.Date,
                };

                if (h.Index >= 2 * lookbackPeriod)
                {
                    IEnumerable <RsiResult> period = rsiResults.Where(x => x.Index <= h.Index && x.Index > (h.Index - lookbackPeriod));
                    float?rsi     = period.Where(x => x.Index == h.Index).FirstOrDefault().Rsi;
                    float?rsiHigh = period.Select(x => x.Rsi).Max();
                    float?rsiLow  = period.Select(x => x.Rsi).Min();

                    result.StochRsi = (rsi - rsiLow) / (rsiHigh - rsiLow);
                }

                results.Add(result);
            }


            // add direction
            float?lastRSI = 0;

            foreach (StochRsiResult r in results.Where(x => x.Index >= 2 * lookbackPeriod).OrderBy(d => d.Index))
            {
                if (r.Index >= lookbackPeriod)
                {
                    r.IsIncreasing = (r.StochRsi >= lastRSI) ? true : false;
                }

                lastRSI = r.StochRsi;
            }

            return(results);
        }
Esempio n. 2
0
        // STOCHASTIC RSI
        public static IEnumerable <StochRsiResult> GetStochRsi(IEnumerable <Quote> history,
                                                               int rsiPeriod, int stochPeriod, int signalPeriod, int smoothPeriod = 1)
        {
            // clean quotes
            history = Cleaners.PrepareHistory(history);

            // validate parameters
            ValidateStochRsi(history, rsiPeriod, stochPeriod, signalPeriod, smoothPeriod);

            // initialize
            List <StochRsiResult> results = new List <StochRsiResult>();

            // get RSI
            IEnumerable <RsiResult> rsiResults = GetRsi(history, rsiPeriod);

            // convert rsi to quote format
            List <Quote> rsiQuotes = rsiResults
                                     .Where(x => x.Rsi != null)
                                     .Select(x => new Quote
            {
                Index = null,
                Date  = x.Date,
                High  = (decimal)x.Rsi,
                Low   = (decimal)x.Rsi,
                Close = (decimal)x.Rsi
            })
                                     .ToList();

            // get Stochastic of RSI
            IEnumerable <StochResult> stoResults = GetStoch(rsiQuotes, stochPeriod, signalPeriod, smoothPeriod);

            // compose
            foreach (RsiResult r in rsiResults)
            {
                StochRsiResult result = new StochRsiResult
                {
                    Index = r.Index,
                    Date  = r.Date
                };

                if (r.Index >= rsiPeriod + stochPeriod)
                {
                    StochResult sto = stoResults
                                      .Where(x => x.Index == r.Index - stochPeriod)
                                      .FirstOrDefault();

                    result.StochRsi     = sto.Oscillator;
                    result.Signal       = sto.Signal;
                    result.IsIncreasing = sto.IsIncreasing;
                }

                results.Add(result);
            }

            return(results);
        }
Esempio n. 3
0
        // STOCHASTIC RSI
        public static IEnumerable <StochRsiResult> GetStochRsi <TQuote>(
            IEnumerable <TQuote> history,
            int rsiPeriod,
            int stochPeriod,
            int signalPeriod,
            int smoothPeriod = 1)
            where TQuote : IQuote
        {
            // validate parameters
            ValidateStochRsi(history, rsiPeriod, stochPeriod, signalPeriod, smoothPeriod);

            // initialize
            List <StochRsiResult> results = new List <StochRsiResult>();

            // get RSI
            List <RsiResult> rsiResults = GetRsi(history, rsiPeriod).ToList();

            // convert rsi to quote format
            List <Quote> rsiQuotes = rsiResults
                                     .Where(x => x.Rsi != null)
                                     .Select(x => new Quote
            {
                Date  = x.Date,
                High  = (decimal)x.Rsi,
                Low   = (decimal)x.Rsi,
                Close = (decimal)x.Rsi
            })
                                     .ToList();

            // get Stochastic of RSI
            List <StochResult> stoResults = GetStoch(rsiQuotes, stochPeriod, signalPeriod, smoothPeriod).ToList();

            // compose
            for (int i = 0; i < rsiResults.Count; i++)
            {
                RsiResult r     = rsiResults[i];
                int       index = i + 1;

                StochRsiResult result = new StochRsiResult
                {
                    Date = r.Date
                };

                if (index >= rsiPeriod + stochPeriod)
                {
                    StochResult sto = stoResults[index - rsiPeriod - 1];

                    result.StochRsi = sto.Oscillator;
                    result.Signal   = sto.Signal;
                }

                results.Add(result);
            }

            return(results);
        }
Esempio n. 4
0
        // STOCHASTIC RSI
        public static IEnumerable <StochRsiResult> GetStochRsi(IEnumerable <Quote> history, int lookbackPeriod = 14)
        {
            // clean quotes
            history = Cleaners.PrepareHistory(history);

            // initialize
            List <StochRsiResult>   results    = new List <StochRsiResult>();
            IEnumerable <RsiResult> rsiResults = GetRsi(history, lookbackPeriod);


            // calculate
            foreach (Quote h in history)
            {
                StochRsiResult result = new StochRsiResult
                {
                    Index = (int)h.Index,
                    Date  = h.Date,
                };

                if (h.Index >= 2 * lookbackPeriod)
                {
                    IEnumerable <RsiResult> period = rsiResults.Where(x => x.Index <= h.Index && x.Index > (h.Index - lookbackPeriod));
                    float?rsi     = period.Where(x => x.Index == h.Index).FirstOrDefault().Rsi;
                    float?rsiHigh = period.Select(x => x.Rsi).Max();
                    float?rsiLow  = period.Select(x => x.Rsi).Min();

                    result.StochRsi = (rsi - rsiLow) / (rsiHigh - rsiLow);
                }

                results.Add(result);
            }


            // add direction
            float?lastRSI = 0;

            foreach (StochRsiResult r in results.Where(x => x.Index >= 2 * lookbackPeriod).OrderBy(d => d.Index))
            {
                if (r.Index >= lookbackPeriod)
                {
                    r.IsIncreasing = (r.StochRsi >= lastRSI) ? true : false;
                }

                lastRSI = r.StochRsi;
            }

            return(results);
        }