Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TA-Lib numpy "AssertionError: real is not double"

Tags:

python

numpy

I have AssertionError using TA-Lib wrapper in python. Could you take a look at my code? I really appreciate your help.

import numpy as np
import talib

#This works
test_data = np.random.random(5)
np_out = talib.SMA(test_data,3)
print np_out

#How come this does not work?  I need to fix
real_data = [135.01, 133.0, 134.0, 131.0, 133.0, 131.0]
np_real_data = np.array(real_data,dtype=np.object)
np_out = talib.SMA(np_real_data,3)
print np_out

error message:

  File "func.pyx", line 9200, in talib.func.SMA (talib/func.c:85610)
AssertionError: real is not double

I suspet the solution might be to convert double to real. I want to test that idea. How do I convert the real_data from double to real?

Thanks.

like image 243
vt2424253 Avatar asked Apr 10 '14 15:04

vt2424253


2 Answers

I suspect the solution might be to convert double to real.

No. You have real data. TA-lib doesn't like "real data". You want to convert it to double float data.

re: qcc's unexplained answer:

f8 is a 64 bit "double precision" floating point number. http://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html

TA-lib wants numpy arrays of "double" floats as inputs.

http://en.wikipedia.org/wiki/Double-precision_floating-point_format

There are several methods you can use to get where you're going, probably the best for your purposes is:

    real_data = [135.01, 133.0, 134.0, 131.0, 133.0, 131.0]
    float_data = [float(x) for x in real_data]
    np_float_data = np.array(float_data)
    np_out = talib.SMA(np_float_data,3)

Here are several others:

1)

    real_data = [float(x) for x in [135.01, 133.0, 134.0, 131.0, 133.0, 131.0]]
    np_real_data = np.array(real_data)
    np_out = talib.SMA(np_real_data,3)

2)

    real_data = [135.01, 133.0, 134.0, 131.0, 133.0, 131.0]
    np_real_data = np.array(real_data, dtype='f8')
    np_out = talib.SMA(np_real_data,3)

3)

    real_data = [135.01, 133.0, 134.0, 131.0, 133.0, 131.0]
    np_real_data = np.array(real_data, dtype=float)
    np_out = talib.SMA(np_real_data,3)

4)

    real_data = map(float, [135.01, 133.0, 134.0, 131.0, 133.0, 131.0])
    np_real_data = np.array(real_data)
    np_out = talib.SMA(np_real_data,3)

5)

    real_data = [float(135.01), float(133.0), float(134.0), float(131.0), 
                 float(133.0), float(131.0)]
    np_real_data = np.array(real_data)
    np_out = talib.SMA(np_real_data,3)
like image 148
litepresence Avatar answered Oct 22 '22 03:10

litepresence


try this

np_real_data = np.array(real_data,dtype='f8')
like image 41
qcc Avatar answered Oct 22 '22 03:10

qcc