 
    
            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
public static double GetCorrelation(DataSeries dataSeries, DataSeries otherDataSeries) { double[] values1 = new double[dataSeries.Count]; double[] values2 = new double[dataSeries.Count]; for (int i = 0; i < dataSeries.Count; i++) { values1[i] = dataSeries.Last(i); values2[i] = otherDataSeries.Last(i); } var avg1 = values1.Average(); var avg2 = values2.Average(); var sum = values1.Zip(values2, (x1, y1) => (x1 - avg1) * (y1 - avg2)).Sum(); var sumSqr1 = values1.Sum(x => Math.Pow((x - avg1), 2.0)); var sumSqr2 = values2.Sum(y => Math.Pow((y - avg2), 2.0)); return Math.Round(sum / Math.Sqrt(sumSqr1 * sumSqr2), 2); }This function returns the correlation between two data series.
@afhacker