Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Implementing talib correlation indicator , need help

Hi ,

Does anyone have an idea how to implement the correlation (correl) study from talib? I'm trying to add a correlation feature to one of my algo, that would prevent any trading if the correlation between two securities is above 0.25. Any help or guidance would be appreciated.

thanks

Lionel

5 responses

I don't think TALib lib provides correlation studies.

You need to get the return of the stocks you want to compare and run something like
numpy.correlate
http://docs.scipy.org/doc/numpy/reference/generated/numpy.correlate.html

It might not be so efficient if you compute it on the handle_data, maybe you can compute it once a week

Hi Lucas,

Thank you for your reply. On the function list page of ta-lib, you can see "CORREL Pearson's Correlation Coefficient (r)" on the list. Just wondering how to implement it. I took a look at your document ( thanks for sharing it). I will try to use it .

Thanks again

Lionel

It is actually quite simple just make sure that you get your data with the history for your loopback period to check the correlation like the following

   hist = history(90, '1d','price',ffill=True)  
   if hist.corr()[symbol('SPY')][symbol('AAPL')] > 0.25:  
    do something....  

and you can tell it which method and min periods

 close_pricez = history(C.quarter*2, '1d', 'price')  
 correlation = close_pricez.corr(method='spearman', min_periods=C.quarter)

Erick, Peter ,

Thank you for your help. I was able to implement your suggestion to my algo and its working. Really appreciate your help.

cheers

Lionel :)