Correlation
Created at 20 Sep 2018, 23:41
Correlation
20 Sep 2018, 23:41
Good evening,
I am developing a correlation bot but im stuck in the correlation calculation.
Result indicates that its not a number.
Follow the code below and thank you for your cooperation.
private void CoefCorrel() { int index = 0; SubSymbolGetSeries = MarketData.GetSeries(SubSymbol, TimeFrame); var SubSymbolGetSymbol = MarketData.GetSymbol(SubSymbol); int CountOfBars = 24; double Result = 0; double sumX = 0, sumY = 0, sumXY = 0, sumX2 = 0, sumY2 = 0; double x, y, x2, y2; int index2 = GetIndexByDate(SubSymbolGetSeries, SubSymbolGetSeries.OpenTime[index]); if (index2 == -1) return; for (int i = 0; i < CountOfBars; i++) { x = MarketSeries.Close[index - i]; y = SubSymbolGetSeries.Close[index2 - i]; x2 = x * x; y2 = y * y; sumX += x; sumY += y; sumXY += x * y; sumX2 += x2; sumY2 += y2; } Result = (CountOfBars * (sumXY) - sumX * sumY) / Math.Sqrt((CountOfBars * sumX2 - sumX * sumX) * (CountOfBars * sumY2 - sumY * sumY)); ChartObjects.DrawText("LabelCorrel2", "\n\n\n\n\n\n\n\n\nAverageSubSymbol: " + Result, StaticPosition.TopLeft, Colors.GreenYellow); } private int GetIndexByDate(MarketSeries series, DateTime time) { for (int i = SubSymbolGetSeries.Close.Count - 1; i >= 0; i--) { if (time == SubSymbolGetSeries.OpenTime[i]) return i; } return -1; }
Replies
afhacker
21 Sep 2018, 07:21
For double generic collections you can use this method:
public static double Correlation(IEnumerable<double> x, IEnumerable<double> y) { double xSum = x.Sum(); double ySum = y.Sum(); double xSumSquared = Math.Pow(xSum, 2); double ySumSquared = Math.Pow(ySum, 2); double xSquaredSum = x.Select(value => Math.Pow(value, 2)).Sum(); double ySquaredSum = y.Select(value => Math.Pow(value, 2)).Sum(); double xAndyProductSum = x.Zip(y, (value1, value2) => value1 * value2).Sum(); double n = x.Count(); return ((n * xAndyProductSum) - (xSum * ySum)) / Math.Sqrt(((n * xSquaredSum) - xSumSquared) * ((n * ySquaredSum) - ySumSquared)); }
@afhacker
DelFonseca
21 Sep 2018, 20:36
RE:
afhacker said:
For double generic collections you can use this method:
public static double Correlation(IEnumerable<double> x, IEnumerable<double> y) { double xSum = x.Sum(); double ySum = y.Sum(); double xSumSquared = Math.Pow(xSum, 2); double ySumSquared = Math.Pow(ySum, 2); double xSquaredSum = x.Select(value => Math.Pow(value, 2)).Sum(); double ySquaredSum = y.Select(value => Math.Pow(value, 2)).Sum(); double xAndyProductSum = x.Zip(y, (value1, value2) => value1 * value2).Sum(); double n = x.Count(); return ((n * xAndyProductSum) - (xSum * ySum)) / Math.Sqrt(((n * xSquaredSum) - xSumSquared) * ((n * ySquaredSum) - ySumSquared)); }
Thank you very much for your feedback.
Please tell me how I can make correlation value print.
@DelFonseca
afhacker
21 Sep 2018, 07:16
This function returns the correlation between two data series.
@afhacker