Correlation Coefficient

Viewing 15 posts - 1 through 15 (of 17 total)
  • Author
    Posts
  • #11148 quote
    Lars Nørgaard Larsen
    Participant
    New

    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. 🙂

    #11184 quote
    Nicolas
    Keymaster
    Master

    Hi Lars,

    Yes it’s possible, but quite difficult to find how if you don’t already have made a formula for it. Since Volume and price don’t share the same “scale”.. Are you talking about Pearson R Correlation?

    #11205 quote
    Lars Nørgaard Larsen
    Participant
    New

    Hi 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.

    #11207 quote
    Nicolas
    Keymaster
    Master

    I think you are looking for things that already exist. You should have a look at indicators already made that use volumes in their own calculations: OBV, volume ROC, etc..

    #11382 quote
    Lars Nørgaard Larsen
    Participant
    New

    For 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! 🙂

    #11389 quote
    Nicolas
    Keymaster
    Master

    Well, 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.

    #11398 quote
    Lars Nørgaard Larsen
    Participant
    New

    I 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]-1

    The interpretation of the correlation between x and y:

    Positive correlation – sign of stregnth
    Negative correlation – sign of weakness

    It was my first thought of this indicator.

    #11468 quote
    Nicolas
    Keymaster
    Master

    Hi 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.

    #11536 quote
    Nicolas
    Keymaster
    Master

    Hi,

    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).

    period = 50
    
    if barindex>period then
    
     X = ABS(close/close[1])
     Y = ABS(volume/volume[1])
    
     meanX = average[period](x)
     meanY = average[period](y)
    
     a = X-meanX
     b = Y-meanY
    
     sqA = summation[period](square(a))
     sqB = summation[period](square(b))
    
     R = summation[period](a*b)/sqrt(sqA*sqB)
    endif
    
    RETURN R as "Correlation", 0 as "0"
    #11554 quote
    Lars Nørgaard Larsen
    Participant
    New

    Actually 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?

    period = 20
    
    if barindex>period then
    
    X = close/close[1]-1
    Y = volume/volume[1]-1
    
    meanX = 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))))
    endif
    
    RETURN R as "Correlation", 0 as "0"
    #11569 quote
    Nicolas
    Keymaster
    Master

    I 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 🙂

    #11570 quote
    Lars Nørgaard Larsen
    Participant
    New

    My 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 🙂

    #11603 quote
    Nicolas
    Keymaster
    Master

    I think I finally got it right now. Seems that TV has a wrong formula explanation in its own documentation.. Well here is the code:

    period = 12
    
    if barindex>period then
    
     X = (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))))/period
    
     correlation = covariance/SQRT(varianceX*varianceY) 
    
    endif
    
    RETURN correlation as "Correlation", 0 as "0"

     

    Do you have any example for comparing with the TV result on the same instrument/timeframe?

    grizzly thanked this post
    #12665 quote
    Lars Nørgaard Larsen
    Participant
    New

    Sorry 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.

    #42729 quote
    Leonida1984
    Participant
    Average

    Hi 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?

Viewing 15 posts - 1 through 15 (of 17 total)
  • You must be logged in to reply to this topic.

Correlation Coefficient


ProBuilder: Indicators & Custom Tools

New Reply
Summary

This topic contains 16 replies,
has 1 voice, and was last updated by Nicolas
3 years, 9 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 08/02/2016
Status: Active
Attachments: 2 files
Logo Logo
Loading...