0

So for the past three days I've been trying to figure out getting TA-Lib to work with python. This is the source I compiled into a dylib (mac version of a .so) and have been calling it from a python script coded as follows:

from ctypes import *
import numpy

c_float_p = POINTER(c_float)
data = numpy.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20])
data = data.astype(numpy.float32)
data_p = data.ctypes.data_as(c_float_p)


dylib = CDLL('libta_lib.dylib')


value = dylib.TA_S_SMA(c_int(0), c_int(data.size - 1), data_p, 0, 19, data_p)

Printing value returns 2, no matter what the array values are. I cannot change the fourth argument of TA_S_SMA from 0 or 1, or else I get a python 138 error followed by a python crash. Can anyone explain to me the proper way to call this function? My C skills are limited (read 0).

Useful links:

Thanks!

Community
  • 1
  • 1
NoviceCoding
  • 6,145
  • 2
  • 27
  • 33

2 Answers2

0

You should take a look at this TA-Lib Python project.

It uses Cython to wrap TA-Lib and is cross-platform, easy to install, and faster than the SWIG bindings.

mrjbq7
  • 238
  • 3
  • 7
0

I had the same problem a couple of weeks ago and I found these instructions. Now they're not using ctypes but it works better in my opinion. SWIG will do all the wrapping for you. A Couple of things to watch out for. When you get to the Single: Multi: sections, if you don't know which on, start with the Multi and if that doesn't work go to the single. A little further down you'll see he's replacing Python 2.3 to Python 2.6. I was using python 2.7 and just replaced the 2.6 with 2.7 and it worked. I'm not sure if this will work for higher versions of python but worth a shot if that's what you're using. Hope it helps.

Jeff
  • 6,932
  • 7
  • 42
  • 72