Example #1
0
        // RATE OF CHANGE (ROC)
        public static IEnumerable <RocResult> GetRoc(IEnumerable <Quote> history, int lookbackPeriod)
        {
            // clean quotes
            List <Quote> historyList = Cleaners.PrepareHistory(history).ToList();

            // check parameters
            ValidateRoc(history, lookbackPeriod);

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

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

                RocResult result = new RocResult
                {
                    Index = (int)h.Index,
                    Date  = h.Date
                };

                if (h.Index > lookbackPeriod)
                {
                    Quote back = historyList[(int)h.Index - lookbackPeriod - 1];
                    result.Roc = 100 * (h.Close - back.Close) / back.Close;
                }

                results.Add(result);
            }

            return(results);
        }
Example #2
0
        // RATE OF CHANGE (ROC)
        public static IEnumerable <RocResult> GetRoc(IEnumerable <Quote> history, int lookbackPeriod)
        {
            // clean quotes
            history = Cleaners.PrepareHistory(history);

            // check parameters
            ValidateRoc(history, lookbackPeriod);

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

            // roll through history
            foreach (Quote h in history)
            {
                RocResult result = new RocResult
                {
                    Index = (int)h.Index,
                    Date  = h.Date
                };

                if (h.Index > lookbackPeriod)
                {
                    Quote back = history
                                 .Where(x => x.Index == h.Index - lookbackPeriod)
                                 .FirstOrDefault();

                    result.Roc = 100 * (h.Close - back.Close) / back.Close;
                }

                results.Add(result);
            }

            return(results);
        }
Example #3
0
        // RATE OF CHANGE (ROC)
        /// <include file='./info.xml' path='indicator/*' />
        ///
        public static IEnumerable <RocResult> GetRoc <TQuote>(
            IEnumerable <TQuote> history,
            int lookbackPeriod,
            int?smaPeriod = null)
            where TQuote : IQuote
        {
            // sort history
            List <TQuote> historyList = history.Sort();

            // check parameter arguments
            ValidateRoc(history, lookbackPeriod, smaPeriod);

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

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

                RocResult result = new RocResult
                {
                    Date = h.Date
                };

                if (index > lookbackPeriod)
                {
                    TQuote back = historyList[index - lookbackPeriod - 1];

                    result.Roc = (back.Close == 0) ? null
                        : 100 * (h.Close - back.Close) / back.Close;
                }

                results.Add(result);

                // optional SMA
                if (smaPeriod != null && index >= lookbackPeriod + smaPeriod)
                {
                    decimal?sumSma = 0m;
                    for (int p = index - (int)smaPeriod; p < index; p++)
                    {
                        sumSma += results[p].Roc;
                    }

                    result.RocSma = sumSma / smaPeriod;
                }
            }

            return(results);
        }
Example #4
0
        // RATE OF CHANGE (ROC)
        public static IEnumerable <RocResult> GetRoc(
            IEnumerable <Quote> history, int lookbackPeriod, int?smaPeriod = null)
        {
            // clean quotes
            List <Quote> historyList = Cleaners.PrepareHistory(history).ToList();

            // check parameters
            ValidateRoc(history, lookbackPeriod, smaPeriod);

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

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

                RocResult result = new RocResult
                {
                    Index = (int)h.Index,
                    Date  = h.Date
                };

                if (h.Index > lookbackPeriod)
                {
                    Quote back = historyList[(int)h.Index - lookbackPeriod - 1];
                    result.Roc = 100 * (h.Close - back.Close) / back.Close;
                }

                results.Add(result);

                // optional SMA
                if (smaPeriod != null && h.Index >= lookbackPeriod + smaPeriod)
                {
                    decimal sumSma = 0m;
                    for (int p = (int)h.Index - (int)smaPeriod; p < h.Index; p++)
                    {
                        sumSma += (decimal)results[p].Roc;
                    }

                    result.Sma = sumSma / smaPeriod;
                }
            }

            return(results);
        }
Example #5
0
        private static List <PmoResult> CalcPmoRocEma <TQuote>(
            IEnumerable <TQuote> history,
            int timePeriod)
            where TQuote : IQuote
        {
            // initialize
            decimal          smoothingMultiplier = 2m / timePeriod;
            decimal?         lastRocEma          = null;
            List <RocResult> roc     = GetRoc(history, 1).ToList();
            List <PmoResult> results = new List <PmoResult>();

            int startIndex = timePeriod + 1;

            for (int i = 0; i < roc.Count; i++)
            {
                RocResult r     = roc[i];
                int       index = i + 1;

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

                if (index > startIndex)
                {
                    result.RocEma = r.Roc * smoothingMultiplier + lastRocEma * (1 - smoothingMultiplier);
                }
                else if (index == startIndex)
                {
                    decimal sumRoc = 0;
                    for (int p = index - timePeriod; p < index; p++)
                    {
                        RocResult d = roc[p];
                        sumRoc += (decimal)d.Roc;
                    }
                    result.RocEma = sumRoc / timePeriod;
                }

                lastRocEma     = result.RocEma;
                result.RocEma *= 10;
                results.Add(result);
            }

            return(results);
        }
Example #6
0
        // RATE OF CHANGE (PMO)
        public static IEnumerable <PmoResult> GetPmo(
            IEnumerable <Quote> history,
            int timePeriod      = 35,
            int smoothingPeriod = 20,
            int signalPeriod    = 10)
        {
            // clean quotes
            Cleaners.PrepareHistory(history);

            // check parameters
            ValidatePmo(history, timePeriod, smoothingPeriod, signalPeriod);

            // initialize
            List <PmoResult> results = new List <PmoResult>();
            List <RocResult> roc     = GetRoc(history, 1).ToList();

            int     startIndex          = 0;
            decimal smoothingMultiplier = 2m / timePeriod;
            decimal smoothingConstant   = 2m / smoothingPeriod;
            decimal signalConstant      = 2m / (signalPeriod + 1);
            decimal?lastRocEma          = null;
            decimal?lastPmo             = null;
            decimal?lastSignal          = null;

            // get ROC EMA variant
            startIndex = timePeriod + 1;

            for (int i = 0; i < roc.Count; i++)
            {
                RocResult r = roc[i];

                PmoResult result = new PmoResult
                {
                    Index = r.Index,
                    Date  = r.Date
                };

                if (r.Index > startIndex)
                {
                    result.RocEma = r.Roc * smoothingMultiplier + lastRocEma * (1 - smoothingMultiplier);
                }
                else if (r.Index == startIndex)
                {
                    result.RocEma = roc
                                    .Where(x => x.Index > r.Index - timePeriod && x.Index <= r.Index)
                                    .ToList()
                                    .Select(x => x.Roc)
                                    .Average();
                }

                lastRocEma     = result.RocEma;
                result.RocEma *= 10;
                results.Add(result);
            }

            // calculate PMO
            startIndex = timePeriod + smoothingPeriod;

            for (int i = startIndex - 1; i < results.Count; i++)
            {
                PmoResult p = results[i];

                if (p.Index > startIndex)
                {
                    p.Pmo = (p.RocEma - lastPmo) * smoothingConstant + lastPmo;
                }
                else if (p.Index == startIndex)
                {
                    p.Pmo = results
                            .Where(x => x.Index > p.Index - smoothingPeriod && x.Index <= p.Index)
                            .ToList()
                            .Select(x => x.RocEma)
                            .Average();
                }

                lastPmo = p.Pmo;
            }

            // add Signal
            startIndex = timePeriod + smoothingPeriod + signalPeriod - 1;

            for (int i = startIndex - 1; i < results.Count; i++)
            {
                PmoResult p = results[i];

                if (p.Index > startIndex)
                {
                    p.Signal = (p.Pmo - lastSignal) * signalConstant + lastSignal;
                }
                else if (p.Index == startIndex)
                {
                    p.Signal = results
                               .Where(x => x.Index > p.Index - signalPeriod && x.Index <= p.Index)
                               .ToList()
                               .Select(x => x.Pmo)
                               .Average();
                }

                lastSignal = p.Signal;
            }

            return(results);
        }