Example #1
0
        // ON-BALANCE VOLUME
        /// <include file='./info.xml' path='indicator/*' />
        ///
        public static IEnumerable <ObvResult> GetObv <TQuote>(
            IEnumerable <TQuote> history,
            int?smaPeriod = null)
            where TQuote : IQuote
        {
            // sort history
            List <TQuote> historyList = history.Sort();

            // check parameter arguments
            ValidateObv(history, smaPeriod);

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

            decimal?prevClose = null;
            decimal obv       = 0;

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

                if (prevClose == null || h.Close == prevClose)
                {
                    // no change to OBV
                }
                else if (h.Close > prevClose)
                {
                    obv += h.Volume;
                }
                else if (h.Close < prevClose)
                {
                    obv -= h.Volume;
                }

                ObvResult result = new ObvResult
                {
                    Date = h.Date,
                    Obv  = obv
                };
                results.Add(result);

                prevClose = h.Close;

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

                    result.ObvSma = sumSma / smaPeriod;
                }
            }

            return(results);
        }
Example #2
0
        // ON-BALANCE VOLUME
        public static IEnumerable <ObvResult> GetObv(IEnumerable <Quote> history, int?smaPeriod = null)
        {
            // clean quotes
            history = Cleaners.PrepareHistory(history);

            // check parameters
            ValidateObv(history, smaPeriod);

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

            decimal?prevClose = null;
            decimal obv       = 0;

            foreach (Quote h in history)
            {
                if (prevClose == null || h.Close == prevClose)
                {
                    // no change to OBV
                }
                else if (h.Close > prevClose)
                {
                    obv += h.Volume;
                }
                else if (h.Close < prevClose)
                {
                    obv -= h.Volume;
                }

                ObvResult result = new ObvResult
                {
                    Index = (int)h.Index,
                    Date  = h.Date,
                    Obv   = obv
                };
                results.Add(result);

                prevClose = h.Close;

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

                    result.Sma = sumSma / smaPeriod;
                }
            }

            return(results);
        }
Example #3
0
        // ON-BALANCE VOLUME
        public static IEnumerable<ObvResult> GetObv(IEnumerable<Quote> history)
        {

            // clean quotes
            history = Cleaners.PrepareHistory(history);

            // check parameters
            ValidateObv(history);

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

            decimal? prevClose = null;
            long obv = 0;

            foreach (Quote h in history)
            {
                if(prevClose == null || h.Close == prevClose)
                {
                    // no change to OBV
                }
                else if(h.Close > prevClose)
                {
                    obv += h.Volume;
                }
                else if(h.Close < prevClose)
                {
                    obv -= h.Volume;
                }

                ObvResult result = new ObvResult
                {
                    Index = (int)h.Index,
                    Date = h.Date,
                    Obv = obv
                };
                results.Add(result);

                prevClose = h.Close;
            }

            return results;
        }