Пример #1
0
        // Full Stochastic
        /// <summary>
        /// Calculates the Full Stochastic Oscillator and fills indicators with the data needed to graph it.
        /// </summary>
        /// <param name="key">Name of the first set to put in the graph key</param>
        /// <param name="key2">Name of the second set to put in the graph key</param>
        /// <param name="data">StockPoints to calculate the Full Stochastic with.</param>
        /// <param name="indicators">The mfvvalues needed to graph the Full Stochastic.</param>
        /// <param name="period1">The number of period to calculate Fast Stochastic.</param>
        /// <param name="period2">The number of period to calculate SMA of Fast Stochastic.</param>
        /// <param name="period3">The number of period to calculate SMA of calculated SMA of Fast Stochastic.</param>
        /// <param name="offset">Where to start the graph.</param>
        public static void CalculateStochastic(out string key, out string key2, StockPoints data, Dictionary<DateTime, Dictionary<string, double>> indicators, int period1, int period2, int period3, out int offset)
        {
            key = string.Format("Full STO %K({0}:{1})", period1.ToString("00"), period2.ToString("00"));
            key2 = string.Format("Full STO %D({0})", period3.ToString("00"));

            offset = period1 - 1;

            SimpleMovingAverage ksma = new SimpleMovingAverage(period2);
            SimpleMovingAverage dsma = new SimpleMovingAverage(period3);

            // lowest among the lowest for the chosen period
            double lowestLow;

            // highest among the highest for the chosen period
            double highestHigh;

            // Lowest Low = lowest low for the look-back period
            // Highest High = highest high for the look-back period
            // fastk = (Current Close - Lowest Low)/(Highest High - Lowest Low) * 100
            double fastk;

            // fullk smoothed with X-period SMA
            double fullk;

            // X-period SMA of Full fullk
            double fulld;

            int i = 0;
            // loop through each stockpoint skipping the
            foreach (StockPoint point in data)
            {
                lowestLow = data.Skip(data.IndexOf(point) - (period1 - 1)).Take(period1).OrderBy(sp => sp.Low).FirstOrDefault().Low;
                highestHigh = data.Skip(data.IndexOf(point) - (period1 - 1)).Take(period1).OrderByDescending(sp => sp.High).FirstOrDefault().High;

                fastk = (point.Close - lowestLow) / (highestHigh - lowestLow == 0 ? 1 : highestHigh - lowestLow) * 100;
                ksma.AddValue(fastk);
                fullk = ksma.MovingAverage();

                dsma.AddValue(fullk);
                fulld = dsma.MovingAverage();

                if (i < period1 - 1)
                {
                    i++;
                }
                else
                {
                    AddValue(point.PointDateTime, key, fullk, indicators);
                    AddValue(point.PointDateTime, key2, fulld, indicators);
                }
            }
        }