Correlation Coefficient
Forums › ProRealTime English forum › ProBuilder support › Correlation Coefficient
- This topic has 16 replies, 1 voice, and was last updated 2 years ago by Nicolas.
Tagged: correlation
-
-
08/02/2016 at 9:04 PM #11148
Dear ProRealCode,
I was wondering if it’s possible to code a correlation coefficient between volume and price?
My first thought was to code some kind of loop for getting (x[i]-avereage[n](x)).Unfortunately I keep getting wrong result in relation to my own manual work.
Can somebody please help me with what I think is a relative simple code. 🙂
08/03/2016 at 7:47 PM #1118408/04/2016 at 6:35 PM #11205Hi Nicolas,
It was exactly what I was having in mind. My first thought was to take development in price correlated with development in volume. It could be % increase/ decrease in price correlated with % increase/decrease in volume.
08/04/2016 at 7:25 PM #1120708/08/2016 at 8:58 PM #11382For me there is a difference between those indicators you mention. ROC behave in a similar way but doesn’t take volume into consideration. I will be grateful if you can help me code the Pearson correlation coefficient! 🙂
08/09/2016 at 9:01 AM #11389Well, this is 2 very different time series. Price never reset to zero as Volume do each day. So I assume the best way to do would be to reckon up price as long as volumes, average these 2 values and then doing comparison with a pseudo correlation coefficient formula. IMO, I don’t believe we are going somewhere, but we could give it a try.
08/09/2016 at 9:27 AM #11398I tried it on Tradingview, and my simple code seems to work. Their platform have the function correlation built-in so it was easy relative to PRT which doesn’t have it.
x = close/close[1]-1
y = volume/volume[1]-1The interpretation of the correlation between x and y:
Positive correlation – sign of stregnth
Negative correlation – sign of weaknessIt was my first thought of this indicator.
08/10/2016 at 9:01 AM #11468Hi Lars,
I’ll do my best to make a function for correlation calculation. That way it would be helpful to re-use it for future purposes. It involved a lot of work and I’m actually very busy with my prog services customers. I’ll get back to you ASAP.
08/11/2016 at 12:58 PM #11536Hi,
Maybe something like this would do the trick. It’s far from a direct calculation though..
Please not that you need to change the period of lookback for calculation of the correlation coefficient at the beginning of the code (50 periods by default).
1234567891011121314151617181920period = 50if barindex>period thenX = ABS(close/close[1])Y = ABS(volume/volume[1])meanX = average[period](x)meanY = average[period](y)a = X-meanXb = Y-meanYsqA = summation[period](square(a))sqB = summation[period](square(b))R = summation[period](a*b)/sqrt(sqA*sqB)endifRETURN R as "Correlation", 0 as "0"08/11/2016 at 5:35 PM #11554Actually I have coded something similar but as you correctly stated it is not the correct way of calculation. It gives some lagging signals compared to the indicator in Tradingview.
After your post I got the idea to rewrite the Pearson correlation equation and I might have come up with the correct way of calculation. Can you confirm my assertion?
12345678910111213141516171819period = 20if barindex>period thenX = close/close[1]-1Y = volume/volume[1]-1meanX = average[period](x)meanY = average[period](y)meanXY = average[period](x*y)meanSqX = average[period](square(X))meanSqY = average[period](square(Y))R = ((meanXY-meanX*meanY)/(sqrt(meanSqX-square(meanX))*sqrt(meanSqY-square(meanY))))endifRETURN R as "Correlation", 0 as "0"08/11/2016 at 8:06 PM #11569I don’t think you make it the correct way. It needs standard deviation calculation (a and b variables from my formula). I also don’t understand why you are using average instead of summation of the whole datas?
Well, I’ll try differently later, I’m done for today 🙂
08/11/2016 at 8:11 PM #11570My formula is based on the attached image but I don’t know if I have put it together the right way.
Looking forward to hear from you 🙂
08/12/2016 at 10:33 AM #11603I think I finally got it right now. Seems that TV has a wrong formula explanation in its own documentation.. Well here is the code:
1234567891011121314151617181920period = 12if barindex>period thenX = (close/close[1])Y = (volume/volume[1])sqX = square(X)sqY = square(Y)varianceX = average[period](sqX)-(average[period](X)*average[period](X))varianceY = average[period](sqY)-(average[period](Y)*average[period](Y))covariance = (summation[period]((X-average[period](X))*(Y-average[period](Y))))/periodcorrelation = covariance/SQRT(varianceX*varianceY)endifRETURN correlation as "Correlation", 0 as "0"Do you have any example for comparing with the TV result on the same instrument/timeframe?
1 user thanked author for this post.
09/04/2016 at 3:04 PM #12665Sorry for the late answer which is caused by my vacation. The way you calculate the correlation does not provide my with the correct result compared to me manual calculation. The way I ended up with gives me almost the correct result despite from some decimals.
08/08/2017 at 7:47 AM #42729Hi Nicholas, I’m Domenico. The Pearson R Correlation is a measure of the linear correlation between two variables Xand Y. It has a value between +1 and −1, where 1 is total positive linear correlation, 0 is no linear correlation, and −1 is total negative linear correlation. But I have tryed your code and it has values greater than 1. Can you help me, please?
-
AuthorPosts
Find exclusive trading pro-tools on