Skip to content

Commit aec16e6

Browse files
briancappellomrjbq7
authored andcommitted
add support for pandas input data to the streaming functions
1 parent 116c577 commit aec16e6

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

requirements_test.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
-r requirements.txt
22
pandas==0.24.2
3+
nose==1.3.7

talib/__init__.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ def wrapper(*args, **kwargs):
3434

3535
result = func(*args, **kwargs)
3636

37+
# check to see if we got a streaming result
38+
first_result = result[0] if isinstance(result, tuple) else result
39+
is_streaming_fn_result = not hasattr(first_result, '__len__')
40+
if is_streaming_fn_result:
41+
return result
42+
3743
# Series was passed in, Series gets out; re-apply index
3844
if isinstance(result, tuple):
3945
# Handle multi-array results such as BBANDS
@@ -58,11 +64,18 @@ def wrapper(*args, **kwargs):
5864
setattr(func, func_name, wrapped_func)
5965
globals()[func_name] = wrapped_func
6066

67+
stream_func_names = ['stream_%s' % fname for fname in __TA_FUNCTION_NAMES__]
68+
stream = __import__("stream", globals(), locals(), stream_func_names, level=1)
69+
for func_name, stream_func_name in zip(__TA_FUNCTION_NAMES__, stream_func_names):
70+
wrapped_func = _pandas_wrapper(getattr(stream, func_name))
71+
setattr(stream, func_name, wrapped_func)
72+
globals()[stream_func_name] = wrapped_func
73+
6174
__version__ = '0.4.18'
6275

6376
# In order to use this python library, talib (i.e. this __file__) will be
6477
# imported at some point, either explicitly or indirectly via talib.func
65-
# or talib.abstract. Here, we handle initalizing and shutting down the
78+
# or talib.abstract. Here, we handle initializing and shutting down the
6679
# underlying TA-Lib. Initialization happens on import, before any other TA-Lib
6780
# functions are called. Finally, when the python process exits, we shutdown
6881
# the underlying TA-Lib.

talib/test_stream.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import numpy as np
2+
import pandas as pd
3+
24
from nose.tools import assert_equals, assert_true, assert_raises
35

46
import talib
@@ -21,6 +23,23 @@ def test_streaming():
2123
r = stream.MOM(a, timeperiod=7)
2224
assert_true(np.isnan(r))
2325

26+
def test_streaming_pandas():
27+
a = pd.Series([1,1,2,3,5,8,13])
28+
r = stream.MOM(a, timeperiod=1)
29+
assert_equals(r, 5)
30+
r = stream.MOM(a, timeperiod=2)
31+
assert_equals(r, 8)
32+
r = stream.MOM(a, timeperiod=3)
33+
assert_equals(r, 10)
34+
r = stream.MOM(a, timeperiod=4)
35+
assert_equals(r, 11)
36+
r = stream.MOM(a, timeperiod=5)
37+
assert_equals(r, 12)
38+
r = stream.MOM(a, timeperiod=6)
39+
assert_equals(r, 12)
40+
r = stream.MOM(a, timeperiod=7)
41+
assert_true(np.isnan(r))
42+
2443
def test_CDL3BLACKCROWS():
2544
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])
2645
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])
@@ -29,3 +48,12 @@ def test_CDL3BLACKCROWS():
2948

3049
r = stream.CDL3BLACKCROWS(o, h, l, c)
3150
assert_equals(r, -100)
51+
52+
def test_CDL3BLACKCROWS_pandas():
53+
o = pd.Series([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])
54+
h = pd.Series([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])
55+
l = pd.Series([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])
56+
c = pd.Series([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])
57+
58+
r = stream.CDL3BLACKCROWS(o, h, l, c)
59+
assert_equals(r, -100)

0 commit comments

Comments
 (0)