ta-lib-python icon indicating copy to clipboard operation
ta-lib-python copied to clipboard

call function from talib.func seems not working

Open lambokini opened this issue 11 years ago • 12 comments

import talib, re
import numpy as np

# O, H, L, C = Open, High, Low, Close
#O = [39.00, 40.32, 40.51, 38.09, 35.00, 27.66,  ]
#H = [40.84, 41.69, 40.84, 38.12, 35.50, 31.74,  ]
#L = [35.80, 39.26, 36.73, 33.37, 30.03, 27.03, ]
#C = [40.29, 40.46, 37.08, 33.37, 30.03, 31.46, ]

O = [39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 40.32, 40.51, 38.09, 35.00, 27.66, 30.80]
H = [40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 41.69, 40.84, 38.12, 35.50, 31.74, 32.51]
L = [35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 39.26, 36.73, 33.37, 30.03, 27.03, 28.31]
C = [40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.46, 37.08, 33.37, 30.03, 31.46, 28.31]

result = talib.func.CDL3BLACKCROWS(np.array(O), np.array(H), np.array(L), np.array(C))
print result

result is [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]

The result is not correct since when I use the same data test in a MSVC project, CDL3BLACKCROWS returns a different result.

I think this is due to in func.pyd, TA_Initialize is not called which causes TA_Globals isn't initialized properly.

lambokini avatar Nov 08 '15 15:11 lambokini

We call TA_initialize from__init__.py, but perhaps that is not working?

mrjbq7 avatar Nov 08 '15 15:11 mrjbq7

i think that only works for common since I saw common._ta_initialize() in init.py func.pyd is another module and has its own TA_Globals copy. I validate this to print of the address of TA_Globals in the souce c code. the two TA_Globals have different address.

lambokini avatar Nov 08 '15 15:11 lambokini

TA_RestoreCandleDefaultSettings TA_AllCandleSettings@TA_RestoreCandleDefaultSettings TA_Globals=10009E74,idx=0, settingType=0, TA_RangeType=0, avgPeriod=10, factor=1 .000000 TA_Globals=10009E74,idx=1, settingType=1, TA_RangeType=0, avgPeriod=10, factor=3 .000000 TA_Globals=10009E74,idx=2, settingType=2, TA_RangeType=0, avgPeriod=10, factor=1 .000000 TA_Globals=10009E74,idx=3, settingType=3, TA_RangeType=1, avgPeriod=10, factor=0 .100000 TA_Globals=10009E74,idx=4, settingType=4, TA_RangeType=0, avgPeriod=0, factor=1. 000000 TA_Globals=10009E74,idx=5, settingType=5, TA_RangeType=0, avgPeriod=0, factor=2. 000000 TA_Globals=10009E74,idx=6, settingType=6, TA_RangeType=2, avgPeriod=10, factor=1 .000000 TA_Globals=10009E74,idx=7, settingType=7, TA_RangeType=1, avgPeriod=10, factor=0 .100000 TA_Globals=10009E74,idx=8, settingType=8, TA_RangeType=1, avgPeriod=5, factor=0. 200000 TA_Globals=10009E74,idx=9, settingType=9, TA_RangeType=1, avgPeriod=5, factor=0. 600000 TA_Globals=10009E74,idx=10, settingType=10, TA_RangeType=1, avgPeriod=5, factor= 0.050000 above outputs are from ta_global.c

and below outputs are from TA_CDL3BLACKCROWS.c . I add my own codes to print TA_Globals.

TA_Globals=024AC394,idx=0, settingType=0, TA_RangeType=0, avgPeriod=0, factor=0. 000000 TA_Globals=024AC394,idx=1, settingType=0, TA_RangeType=0, avgPeriod=0, factor=0. 000000 TA_Globals=024AC394,idx=2, settingType=0, TA_RangeType=0, avgPeriod=0, factor=0. 000000 TA_Globals=024AC394,idx=3, settingType=0, TA_RangeType=0, avgPeriod=0, factor=0. 000000 TA_Globals=024AC394,idx=4, settingType=0, TA_RangeType=0, avgPeriod=0, factor=0. 000000 TA_Globals=024AC394,idx=5, settingType=0, TA_RangeType=0, avgPeriod=0, factor=0. 000000 TA_Globals=024AC394,idx=6, settingType=0, TA_RangeType=0, avgPeriod=0, factor=0. 000000 TA_Globals=024AC394,idx=7, settingType=0, TA_RangeType=0, avgPeriod=0, factor=0. 000000 TA_Globals=024AC394,idx=8, settingType=0, TA_RangeType=0, avgPeriod=0, factor=0. 000000 TA_Globals=024AC394,idx=9, settingType=0, TA_RangeType=0, avgPeriod=0, factor=0. 000000 TA_Globals=024AC394,idx=10, settingType=0, TA_RangeType=0, avgPeriod=0, factor=0 .000000

lambokini avatar Nov 08 '15 15:11 lambokini

That's weird so it loads the library several times in the process due to having multiple python modules?

mrjbq7 avatar Nov 08 '15 15:11 mrjbq7

both common.pyd and func.pyd link to library ta_libc_cdr.lib statically. both of them have its own copy code of ta_ib_cdr.lib. I think the reason is this.

lambokini avatar Nov 08 '15 16:11 lambokini

This is a great find, thanks! Especially with a test case. I will make a fix for it in the next couple days after I get back from a business trip.

mrjbq7 avatar Nov 08 '15 17:11 mrjbq7

By the way, what does your MSVC project produce for the CDL3BLACKCROWS output?

mrjbq7 avatar Nov 08 '15 17:11 mrjbq7

My pleasure!

This is my c code. int _tmain(int argc, _TCHAR* argv[]) {

double open[20]=     {39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 40.32, 40.51, 38.09, 35.00, 27.66, 30.80};
double high[20]=     {40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 41.69, 40.84, 38.12, 35.50, 31.74, 32.51};
double low[20]=      {35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 39.26, 36.73, 33.37, 30.03, 27.03, 28.31};
double close[20]=    {40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.46, 37.08, 33.37, 30.03, 31.46, 28.31};

int          outBegIdx = 0;
int          outNBElement = 0;
int          outInteger[22];

TA_RetCode ret;
memset(outInteger, 0xFF, sizeof(outInteger));
TA_Initialize();
ret = TA_CDL3BLACKCROWS(0, 19, open, high, low, close, &outBegIdx, &outNBElement, outInteger);
return 0;

}

after running this code. outBegIdx = 13 outNBElement = 7

outInteger[] = {0, 0, 0 0, -100, 0, 0, -1, -1, ......}.


I think the quick and easiest solution is just to merge common.pyx and func.pyx to a single file. I tried this and got the desired result.

lambokini avatar Nov 09 '15 15:11 lambokini

Hi I have noticed this as well with most of the pattern indicators. I am still a learning coder and not that experienced but was wondering if there was any proposed fix or solution. Is there any update to this?

"I think the quick and easiest solution is just to merge common.pyx and func.pyx to a single file. I tried this and got the desired result."

Not sure if the above is full proof or not, but if so can you explain in a little more detail how to go about doing that?

P.S I tried copying and pasting the code from the CDL3BLACKCROWS sample which returned an array with values other than 0, and my output did not show that.

func.CDLBREAKAWAY(o,h,l,c) array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

The inputs were identical.

sicadinho91 avatar May 04 '16 20:05 sicadinho91

This should have been fixed (although I may not have made a new release to PyPI):

import numpy as np

o = np.array([39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 39.00, 40.32, 40.51, 38.09, 35.00, 27.66, 30.80])
h = np.array([40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 40.84, 41.69, 40.84, 38.12, 35.50, 31.74, 32.51])
l = np.array([35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 35.80, 39.26, 36.73, 33.37, 30.03, 27.03, 28.31])
c = np.array([40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.29, 40.46, 37.08, 33.37, 30.03, 31.46, 28.31])

import talib as ta

print(ta.CDL3BLACKCROWS(o, h, l, c))

produces this output:

[   0    0    0    0    0    0    0    0    0    0    0    0    0    0    0
    0    0 -100    0    0]

mrjbq7 avatar May 05 '16 03:05 mrjbq7

@sicadinho91 maybe you can try latest master branch and see if it works for you?

mrjbq7 avatar May 05 '16 03:05 mrjbq7

@sicadinho91 did you have a chance to try this?

mrjbq7 avatar May 25 '16 00:05 mrjbq7