Esempio n. 1
0
        // DONCHIAN CHANNEL
        public static IEnumerable <DonchianResult> GetDonchian <TQuote>(
            IEnumerable <TQuote> history,
            int lookbackPeriod = 20)
            where TQuote : IQuote
        {
            // sort history
            List <TQuote> historyList = history.Sort();

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

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

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

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

                if (i >= lookbackPeriod)
                {
                    decimal highHigh = 0;
                    decimal lowLow   = decimal.MaxValue;

                    // high/low over prior periods
                    for (int p = i - lookbackPeriod; p < i; p++)
                    {
                        TQuote d = historyList[p];

                        if (d.High > highHigh)
                        {
                            highHigh = d.High;
                        }

                        if (d.Low < lowLow)
                        {
                            lowLow = d.Low;
                        }
                    }

                    result.UpperBand  = highHigh;
                    result.LowerBand  = lowLow;
                    result.Centerline = (result.UpperBand + result.LowerBand) / 2;
                    result.Width      = (result.Centerline == 0) ? null
                        : (result.UpperBand - result.LowerBand) / result.Centerline;
                }

                results.Add(result);
            }

            return(results);
        }
Esempio n. 2
0
        // DONCHIAN CHANNEL
        public static IEnumerable <DonchianResult> GetDonchian(
            IEnumerable <Quote> history, int lookbackPeriod = 20)
        {
            // clean quotes
            List <Quote> historyList = Cleaners.PrepareHistory(history).ToList();

            // validate parameters
            ValidateDonchian(history, lookbackPeriod);

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

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

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

                if (h.Index >= lookbackPeriod)
                {
                    decimal highHigh = 0;
                    decimal lowLow   = decimal.MaxValue;

                    for (int p = (int)h.Index - lookbackPeriod; p < h.Index; p++)
                    {
                        Quote d = historyList[p];

                        if (d.High > highHigh)
                        {
                            highHigh = d.High;
                        }

                        if (d.Low < lowLow)
                        {
                            lowLow = d.Low;
                        }
                    }

                    result.UpperBand  = highHigh;
                    result.LowerBand  = lowLow;
                    result.Centerline = (result.UpperBand + result.LowerBand) / 2;
                    result.Width      = (result.Centerline == 0) ? null : (result.UpperBand - result.LowerBand) / result.Centerline;
                }

                results.Add(result);
            }

            return(results);
        }
Esempio n. 3
0
        // DONCHIAN CHANNEL
        public static IEnumerable <DonchianResult> GetDonchian(
            IEnumerable <Quote> history, int lookbackPeriod = 20)
        {
            // clean quotes
            history = Cleaners.PrepareHistory(history);

            // validate parameters
            ValidateDonchian(history, lookbackPeriod);

            // initialize
            List <DonchianResult> results = new List <DonchianResult>();
            decimal?prevWidth             = null;

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

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

                    result.UpperBand  = period.Select(h => h.High).Max();
                    result.LowerBand  = period.Select(l => l.Low).Min();
                    result.Centerline = (result.UpperBand + result.LowerBand) / 2;
                    result.Width      = (result.Centerline == 0) ? null : (result.UpperBand - result.LowerBand) / result.Centerline;

                    if (prevWidth != null)
                    {
                        if (result.Width == prevWidth)
                        {
                            result.IsDiverging = null;
                        }
                        else
                        {
                            result.IsDiverging = (result.Width > prevWidth);
                        }
                    }

                    // for next iteration
                    prevWidth = result.Width;
                }

                results.Add(result);
            }

            return(results);
        }
Esempio n. 4
0
        // DONCHIAN CHANNEL
        public static IEnumerable <DonchianResult> GetDonchian(
            IEnumerable <Quote> history, int lookbackPeriod = 20)
        {
            // clean quotes
            Cleaners.PrepareHistory(history);

            // validate parameters
            ValidateDonchian(history, lookbackPeriod);

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

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

                DonchianResult result = new DonchianResult
                {
                    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();

                    result.UpperBand  = period.Select(h => h.High).Max();
                    result.LowerBand  = period.Select(l => l.Low).Min();
                    result.Centerline = (result.UpperBand + result.LowerBand) / 2;
                    result.Width      = (result.Centerline == 0) ? null : (result.UpperBand - result.LowerBand) / result.Centerline;
                }

                results.Add(result);
            }

            return(results);
        }