Beispiel #1
0
        private static IEnumerable <StdDevResult> CalcStdDev(
            List <BasicData> bdList, int lookbackPeriod, int?smaPeriod = null)
        {
            // check parameter arguments
            ValidateStdDev(bdList, lookbackPeriod, smaPeriod);

            // initialize
            List <StdDevResult> results = new List <StdDevResult>(bdList.Count);

            // roll through history
            for (int i = 0; i < bdList.Count; i++)
            {
                BasicData bd    = bdList[i];
                int       index = i + 1;

                StdDevResult result = new StdDevResult
                {
                    Date = bd.Date,
                };

                if (index >= lookbackPeriod)
                {
                    double[] periodValues = new double[lookbackPeriod];
                    decimal  sum          = 0m;
                    int      n            = 0;

                    for (int p = index - lookbackPeriod; p < index; p++)
                    {
                        BasicData d = bdList[p];
                        periodValues[n] = (double)d.Value;
                        sum            += d.Value;
                        n++;
                    }

                    decimal periodAvg = sum / lookbackPeriod;

                    result.StdDev = (decimal)Functions.StdDev(periodValues);
                    result.Mean   = periodAvg;

                    result.ZScore = (result.StdDev == 0) ? null
                        : (bd.Value - periodAvg) / result.StdDev;
                }

                results.Add(result);

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

                    result.StdDevSma = sumSma / smaPeriod;
                }
            }

            return(results);
        }
Beispiel #2
0
        // Standard Deviation
        public static IEnumerable <StdDevResult> GetStdDev(IEnumerable <Quote> history, int lookbackPeriod)
        {
            // clean quotes
            history = Cleaners.PrepareHistory(history);

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


            // roll through history and compute lookback standard deviation
            foreach (Quote h in history)
            {
                StdDevResult result = new StdDevResult
                {
                    Index = (int)h.Index,
                    Date  = h.Date
                };

                if (h.Index >= lookbackPeriod)
                {
                    IEnumerable <double> period = history
                                                  .Where(x => x.Index <= h.Index && x.Index > (h.Index - lookbackPeriod))
                                                  .Select(x => (double)x.Close);

                    result.StdDev = (decimal)Functions.StdDev(period);
                }

                results.Add(result);
            }

            return(results);
        }
Beispiel #3
0
        private static IEnumerable <StdDevResult> CalcStdDev(
            IEnumerable <BasicData> basicData, int lookbackPeriod, int?smaPeriod = null)
        {
            // clean data
            List <BasicData> basicDataList = Cleaners.PrepareBasicData(basicData).ToList();

            // validate inputs
            ValidateStdDev(basicData, lookbackPeriod, smaPeriod);

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

            // roll through history and compute lookback standard deviation
            foreach (BasicData bd in basicDataList)
            {
                StdDevResult result = new StdDevResult
                {
                    Index = (int)bd.Index,
                    Date  = bd.Date,
                };

                if (bd.Index >= lookbackPeriod)
                {
                    double[] periodValues = new double[lookbackPeriod];
                    decimal  sum          = 0m;
                    int      n            = 0;

                    for (int p = (int)bd.Index - lookbackPeriod; p < bd.Index; p++)
                    {
                        BasicData d = basicDataList[p];
                        periodValues[n] = (double)d.Value;
                        sum            += d.Value;
                        n++;
                    }

                    decimal periodAvg = sum / lookbackPeriod;

                    result.StdDev = (decimal)Functions.StdDev(periodValues);
                    result.ZScore = (bd.Value - periodAvg) / result.StdDev;
                }

                results.Add(result);

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

                    result.Sma = sumSma / smaPeriod;
                }
            }

            return(results);
        }
Beispiel #4
0
        // Standard Deviation
        public static IEnumerable <StdDevResult> GetStdDev(IEnumerable <Quote> history, int lookbackPeriod)
        {
            // clean quotes
            history = Cleaners.PrepareHistory(history);

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

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

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


            // roll through history and compute lookback standard deviation
            foreach (Quote h in history)
            {
                StdDevResult result = new StdDevResult
                {
                    Index = (int)h.Index,
                    Date  = h.Date
                };

                if (h.Index >= lookbackPeriod)
                {
                    IEnumerable <double> period = history
                                                  .Where(x => x.Index <= h.Index && x.Index > (h.Index - lookbackPeriod))
                                                  .Select(x => (double)x.Close);

                    result.StdDev = (decimal)Functions.StdDev(period);
                }

                results.Add(result);
            }

            return(results);
        }
Beispiel #5
0
        private static IEnumerable <StdDevResult> CalcStdDev(IEnumerable <BasicData> basicData, int lookbackPeriod)
        {
            // clean data
            basicData = Cleaners.PrepareBasicData(basicData);

            // validate inputs
            ValidateStdDev(basicData, lookbackPeriod);

            // initialize results
            List <StdDevResult> results   = new List <StdDevResult>();
            decimal?            prevValue = null;

            // roll through history and compute lookback standard deviation
            foreach (BasicData h in basicData)
            {
                StdDevResult result = new StdDevResult
                {
                    Index = (int)h.Index,
                    Date  = h.Date,
                };

                if (h.Index >= lookbackPeriod)
                {
                    // price based
                    double[] period = basicData
                                      .Where(x => x.Index > (h.Index - lookbackPeriod) && x.Index <= h.Index)
                                      .Select(x => (double)x.Value)
                                      .ToArray();

                    result.StdDev = (decimal)Functions.StdDev(period);
                    result.ZScore = (h.Value - (decimal)period.Average()) / result.StdDev;
                }

                results.Add(result);
                prevValue = h.Value;
            }

            return(results);
        }
Beispiel #6
0
        // STANDARD DEVIATION
        public static IEnumerable <StdDevResult> GetStdDev(IEnumerable <Quote> history, int lookbackPeriod)
        {
            // clean quotes
            history = Cleaners.PrepareHistory(history);

            // validate inputs
            ValidateStdDev(history, lookbackPeriod);

            // initialize results
            List <StdDevResult> results   = new List <StdDevResult>();
            decimal?            prevClose = null;

            // roll through history and compute lookback standard deviation
            foreach (Quote h in history)
            {
                StdDevResult result = new StdDevResult
                {
                    Index = (int)h.Index,
                    Date  = h.Date,
                };

                if (h.Index >= lookbackPeriod)
                {
                    // price based
                    IEnumerable <double> period = history
                                                  .Where(x => x.Index > (h.Index - lookbackPeriod) && x.Index <= h.Index)
                                                  .Select(x => (double)x.Close);

                    result.StdDev = (decimal)Functions.StdDev(period);
                    result.ZScore = (h.Close - (decimal)period.Average()) / result.StdDev;
                }

                results.Add(result);
                prevClose = h.Close;
            }

            return(results);
        }