// 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); } } }
//cci public static void CalculateCCI(out string key, StockPoints data, Dictionary<DateTime, Dictionary<string, double>> indicators, int period, out int offset, out int min, out int max) { key = string.Format("CCI({0})", period.ToString("00")); offset = period - 1; min = int.MaxValue; max = int.MinValue; // cci = (Typical Price - 20-period SMA of TP) / (.015 x Mean Deviation) double cci; // Typical Price (TP) = (High + Low + Close)/3 double typicalPrice; double constant = 0.015; SimpleMovingAverage sma = new SimpleMovingAverage(period); int i = 0; foreach (StockPoint point in data) { typicalPrice = (point.High + point.Close + point.Low) / 3D; sma.AddValue(typicalPrice); if (i < period - 1) { i++; } else { cci = (typicalPrice - sma.MovingAverage())/ (constant * sma.MeanDeviation()); AddValue(point.PointDateTime, key, cci, indicators); if (min > cci) min = (int)Math.Floor(cci); if (max < cci) max = (int)Math.Ceiling(cci); } } }
// SMA /// <summary> /// Calculates the Simple Moving Average and fills indicators with the data needed to graph it. /// </summary> /// <param name="key">Name of the set to put in the graph key</param> /// <param name="data">StockPoints to calculate the SMA with.</param> /// <param name="indicators">The mfvvalues needed to graph the SMA.</param> /// <param name="period">The number of days to average.</param> /// <param name="offset">Where to start the graph.</param> public static void CalculateSma(out string key, StockPoints data, Dictionary<DateTime, Dictionary<string, double>> indicators, int period, out int offset) { key = "SMA(" + period.ToString("00") + ")"; SimpleMovingAverage sma = new SimpleMovingAverage(period); offset = period - 1; foreach (StockPoint point in data) { sma.AddValue(point.Close); if (sma.Period == sma.ValueCount) { AddValue(point.PointDateTime, key, sma.MovingAverage(), indicators); } } }
// calculate Bollinger Bands public static void CalculateBollinger(out string key, out string key2, out string key3, StockPoints data, Dictionary<DateTime, Dictionary<string, double>> indicators, int period, int multiplier, out int offset) { key = string.Format("BB-Upper-({0}:{1})", period.ToString("00"), multiplier.ToString("00")); key2 = string.Format("BB-Mid-({0}:{1})", period.ToString("00"), multiplier.ToString("00")); key3 = string.Format("BB-Lower-({0}:{1})", period.ToString("00"), multiplier.ToString("00")); offset = period - 1; double upperBand; double midBand; double lowerBand; SimpleMovingAverage sma = new SimpleMovingAverage(period); SimpleMovingAverage smaPrice = new SimpleMovingAverage(period); int i = 0; foreach (StockPoint point in data) { sma.AddValue(point.Close); if (i < period - 1) { i++; } else { double smaValue = sma.MovingAverage(); double stdMultiplied = sma.StandardDeviation() * multiplier; upperBand = smaValue + stdMultiplied; midBand = smaValue; lowerBand = smaValue - stdMultiplied; AddValue(point.PointDateTime, key, upperBand, indicators); AddValue(point.PointDateTime, key2, midBand, indicators); AddValue(point.PointDateTime, key3, lowerBand, indicators); } } }