/***************************************************************************** * FUNCTION: CalculateAvgPrice * Description: * Parameters: *****************************************************************************/ private void CalculateAvgPrice() { if (HistoricalPrice.Count() > 0) { avgPrice = Math.Round(HistoricalPrice.Average(), 2); avgDailyPctChange = HistoricalPctChange.Average(); avgDailyVolume = (long)HistoricalVolumes.Average(); } }
/***************************************************************************** * FUNCTION: ComputeTrend * Description: * Parameters: *****************************************************************************/ private void ComputeTrend() { int i, windowSize; double alpha, emAvg, weight, xmin, xmax; List <double> scaled_volumes; //daily percent change less than this value will be considered as zero const double pct_change_zero = 0.00001; //set scaled volume such that -1 < x < 1 scaled_volumes = HistoricalVolumes.Select(x => (double)((double)x / (double)HistoricalVolumes.Max())).ToList(); //Trend will be one of {-1, 0, +1} Trend = HistoricalPctChange.Select(sign => (Math.Abs(sign) < pct_change_zero) ? 0 : (int)(sign / Math.Abs(sign))).ToList(); WeightedTrend.Clear(); windowSize = 5; emAvg = scaled_volumes[0]; for (i = 0; i < Trend.Count; i++) { if (i < windowSize - 1) { alpha = 2.0 / ((double)(i + 1) + 1.0); } else { alpha = 2.0 / ((double)(windowSize) + 1.0); } scaled_volumes[i] += 1; emAvg = (scaled_volumes[i] * alpha) + ((1 - alpha) * emAvg); //weight = (scaled volume / exp. average volume) ^ daily % change // note: 100% = 1.0 weight = Math.Pow((scaled_volumes[i] / emAvg), Math.Abs(HistoricalPctChange[i])); WeightedTrend.Add(Trend[i] * weight); } //Normalize the weighted trend such that -1 < x < 1 // x' = -1 + (x - xmin)(1 - (-1)) / (xmax - xmin) xmin = WeightedTrend.Min(); xmax = WeightedTrend.Max(); for (i = 0; i < WeightedTrend.Count; i++) { WeightedTrend[i] = -1.0 + ((WeightedTrend[i] - xmin) * 2) / (xmax - xmin); } i = 0; }