TA-Lib / ta-lib-python

Python wrapper for TA-Lib (http://ta-lib.org/).
http://ta-lib.github.io/ta-lib-python
Other
9.46k stars 1.74k forks source link

talib.CORREL not working as intended? #267

Open grantfang opened 5 years ago

grantfang commented 5 years ago

Snippet as demonstration:

In[1]:import talib In[2]:import numpy as np In[3]:a = np.array([1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0]) #9 elements in total In[4]:b = np.array([9.0,8.0,7.0,np.nan,5.0,4.0,3.0,2.0,1.0]) #9 elements as well, nan appears on 4th In[5]:talib.CORREL(a,b,timeperiod=3)

Out[5]: array([nan, nan, -1., nan, nan, nan, nan, nan, nan])

So with missing value in one array, I expect the result to be: array([nan, nan, -1., nan, nan, nan, -1, -1, -1]) That is, in any timeperiod window, if both array are with no nan value, a correlation should present. Current result is that after first nan value, all result afterwards become nan.

Another trivial thing I noticed while making the snippet if a = np.array([1,1,1,1,1,1,1,1,1])


  File "C:\Users\Grant\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2961, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-6-9536a62a5689>", line 1, in <module>
    talib.CORREL(a,b,timeperiod=3)
  File "C:\Users\Grant\Anaconda3\lib\site-packages\talib\__init__.py", line 24, in wrapper
    return func(*args, **kwargs)
  File "_func.pxi", line 6724, in talib._ta_lib.CORREL
Exception: real0 is not double```

talib.__version__
Out[22]: '0.4.17'```
mrjbq7 commented 5 years ago

Your second comment is a problem where inputs have to be doubles and your input (a = np.array([1,1,1,1,1,1,1,1,1])) is a numpy array of integers...

The first, I'm not sure, perhaps nan interrupts the underling TA-Lib algorithm, you can see the code here where it does a rolling algorithm and nan problem pollutes all future values:

https://github.com/TA-Lib/ta-lib/blob/master/src/ta_func/ta_CORREL.c#L245

grantfang commented 5 years ago

yeah, unfortunately I am only familiar with python