Ejemplo n.º 1
0
        // SIMPLE MOVING AVERAGE
        public static IEnumerable <SmaResult> GetSma(IEnumerable <Quote> history, int lookbackPeriod)
        {
            // clean quotes
            history = Cleaners.PrepareHistory(history);

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

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

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

                    result.Sma = period.Average();
                }

                results.Add(result);
            }

            return(results);
        }
Ejemplo n.º 2
0
        // SIMPLE MOVING AVERAGE
        public static IEnumerable <SmaResult> GetSma(IEnumerable <Quote> history, int lookbackPeriod)
        {
            // clean quotes
            history = Cleaners.PrepareHistory(history);

            // check parameters
            ValidateSma(history, lookbackPeriod);

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

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

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

                    // simple moving average
                    result.Sma = period
                                 .Select(x => x.Close)
                                 .Average();

                    // mean absolute deviation
                    result.Mad = period
                                 .Select(x => Math.Abs(x.Close - (decimal)result.Sma))
                                 .Average();

                    // mean squared error
                    result.Mse = period
                                 .Select(x => (x.Close - (decimal)result.Sma) * (x.Close - (decimal)result.Sma))
                                 .Average();

                    // mean absolute percent error
                    result.Mape = period
                                  .Where(x => x.Close != 0)
                                  .Select(x => Math.Abs(x.Close - (decimal)result.Sma) / x.Close)
                                  .Average();
                }

                results.Add(result);
            }

            return(results);
        }
Ejemplo n.º 3
0
        // STARC BANDS
        /// <include file='./info.xml' path='indicator/*' />
        ///
        public static IEnumerable <StarcBandsResult> GetStarcBands <TQuote>(
            IEnumerable <TQuote> history,
            int smaPeriod      = 20,
            decimal multiplier = 2,
            int atrPeriod      = 10)
            where TQuote : IQuote
        {
            // sort history
            List <TQuote> historyList = history.Sort();

            // check parameter arguments
            ValidateStarcBands(history, smaPeriod, multiplier, atrPeriod);

            // initialize
            List <StarcBandsResult> results    = new List <StarcBandsResult>(historyList.Count);
            List <SmaResult>        smaResults = GetSma(history, smaPeriod).ToList();
            List <AtrResult>        atrResults = GetAtr(history, atrPeriod).ToList();
            int lookbackPeriod = Math.Max(smaPeriod, atrPeriod);

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

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

                if (index >= lookbackPeriod)
                {
                    SmaResult s = smaResults[i];
                    AtrResult a = atrResults[i];

                    result.Centerline = s.Sma;
                    result.UpperBand  = s.Sma + multiplier * a.Atr;
                    result.LowerBand  = s.Sma - multiplier * a.Atr;
                }

                results.Add(result);
            }

            return(results);
        }
Ejemplo n.º 4
0
        // SIMPLE MOVING AVERAGE
        /// <include file='./info.xml' path='indicators/type[@name="Main"]/*' />
        ///
        public static IEnumerable <SmaResult> GetSma <TQuote>(
            IEnumerable <TQuote> history,
            int lookbackPeriod)
            where TQuote : IQuote
        {
            // sort history
            List <TQuote> historyList = history.Sort();

            // check parameter arguments
            ValidateSma(history, lookbackPeriod);

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

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

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

                if (index >= lookbackPeriod)
                {
                    decimal sumSma = 0m;
                    for (int p = index - lookbackPeriod; p < index; p++)
                    {
                        TQuote d = historyList[p];
                        sumSma += d.Close;
                    }

                    result.Sma = sumSma / lookbackPeriod;
                }

                results.Add(result);
            }

            return(results);
        }
Ejemplo n.º 5
0
        // SIMPLE MOVING AVERAGE
        public static IEnumerable <SmaResult> GetSma(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 SMA.  " +
                                              string.Format("You provided {0} periods of history when {1} is required.", qtyHistory, minHistory));
            }

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

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

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

                    result.Sma = period.Average();
                }

                results.Add(result);
            }

            return(results);
        }
Ejemplo n.º 6
0
        // SIMPLE MOVING AVERAGE
        /// <include file='./info.xml' path='indicator/*' />
        ///
        public static IEnumerable <SmaResult> GetSma <TQuote>(
            IEnumerable <TQuote> history,
            int lookbackPeriod,
            bool extended = false)
            where TQuote : IQuote
        {
            // sort history
            List <TQuote> historyList = history.Sort();

            // check parameter arguments
            ValidateSma(history, lookbackPeriod);

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

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

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

                if (index >= lookbackPeriod)
                {
                    decimal sumSma = 0m;
                    for (int p = index - lookbackPeriod; p < index; p++)
                    {
                        TQuote d = historyList[p];
                        sumSma += d.Close;
                    }

                    result.Sma = sumSma / lookbackPeriod;

                    // add optional extended values
                    if (extended)
                    {
                        decimal sumMad  = 0m;
                        decimal sumMse  = 0m;
                        decimal?sumMape = 0m;

                        for (int p = index - lookbackPeriod; p < index; p++)
                        {
                            TQuote d = historyList[p];
                            sumMad += Math.Abs(d.Close - (decimal)result.Sma);
                            sumMse += (d.Close - (decimal)result.Sma) * (d.Close - (decimal)result.Sma);

                            sumMape += (d.Close == 0) ? null
                                : Math.Abs(d.Close - (decimal)result.Sma) / d.Close;
                        }

                        // mean absolute deviation
                        result.Mad = sumMad / lookbackPeriod;

                        // mean squared error
                        result.Mse = sumMse / lookbackPeriod;

                        // mean absolute percent error
                        result.Mape = sumMape / lookbackPeriod;
                    }
                }

                results.Add(result);
            }

            return(results);
        }
Ejemplo n.º 7
0
        // SIMPLE MOVING AVERAGE
        public static IEnumerable <SmaResult> GetSma(
            IEnumerable <Quote> history, int lookbackPeriod, bool extended = false)
        {
            // clean quotes
            Cleaners.PrepareHistory(history);

            // check parameters
            ValidateSma(history, lookbackPeriod);

            // initialize
            List <Quote>     historyList = history.ToList();
            List <SmaResult> results     = new List <SmaResult>();

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

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

                if (h.Index >= lookbackPeriod)
                {
                    List <Quote> period = historyList
                                          .Where(x => x.Index > (h.Index - lookbackPeriod) && x.Index <= h.Index)
                                          .ToList();

                    // simple moving average
                    result.Sma = period
                                 .Select(x => x.Close)
                                 .Average();

                    // add optional extended values
                    if (extended)
                    {
                        // mean absolute deviation
                        result.Mad = period
                                     .Select(x => Math.Abs(x.Close - (decimal)result.Sma))
                                     .Average();

                        // mean squared error
                        result.Mse = period
                                     .Select(x => (x.Close - (decimal)result.Sma) * (x.Close - (decimal)result.Sma))
                                     .Average();

                        // mean absolute percent error
                        result.Mape = period
                                      .Where(x => x.Close != 0)
                                      .Select(x => Math.Abs(x.Close - (decimal)result.Sma) / x.Close)
                                      .Average();
                    }
                }

                results.Add(result);
            }

            return(results);
        }
Ejemplo n.º 8
0
        // SIMPLE MOVING AVERAGE
        public static IEnumerable <SmaResult> GetSma(
            IEnumerable <Quote> history, int lookbackPeriod, bool extended = false)
        {
            // clean quotes
            List <Quote> historyList = Cleaners.PrepareHistory(history).ToList();

            // check parameters
            ValidateSma(history, lookbackPeriod);

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

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

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

                if (h.Index >= lookbackPeriod)
                {
                    decimal sumSma = 0m;
                    for (int p = (int)h.Index - lookbackPeriod; p < h.Index; p++)
                    {
                        Quote d = historyList[p];
                        sumSma += d.Close;
                    }

                    result.Sma = sumSma / lookbackPeriod;

                    // add optional extended values
                    if (extended)
                    {
                        decimal sumMad  = 0m;
                        decimal sumMse  = 0m;
                        decimal sumMape = 0m;

                        for (int p = (int)h.Index - lookbackPeriod; p < h.Index; p++)
                        {
                            Quote d = historyList[p];
                            sumMad  += Math.Abs(d.Close - (decimal)result.Sma);
                            sumMse  += (d.Close - (decimal)result.Sma) * (d.Close - (decimal)result.Sma);
                            sumMape += Math.Abs(d.Close - (decimal)result.Sma) / d.Close;
                        }

                        // mean absolute deviation
                        result.Mad = sumMad / lookbackPeriod;

                        // mean squared error
                        result.Mse = sumMse / lookbackPeriod;

                        // mean absolute percent error
                        result.Mape = sumMape / lookbackPeriod;
                    }
                }

                results.Add(result);
            }

            return(results);
        }